-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
148 lines (128 loc) · 5.46 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import string
import random
import unittest
from time import time, sleep
from test_sshkeys import gen_test_sshkeys, remove_test_sshkeys
from rsa_certificates import make_rsa_certificate, decode_rsa_certificate
from dss_certificates import make_dss_certificate, decode_dss_certificate
from ecdsa_certificates import make_ecdsa_certificate, decode_ecdsa_certificate
from ed25519_certificates import make_ed25519_certificate, decode_ed25519_certificate
RAND_LETTERS = string.ascii_letters + string.digits
RAND_CRITICAL_OPTIONS = [
'verify-required'
]
RAND_EXTENSIONS = [
'no-touch-required',
'permit-X11-forwarding',
'permit-agent-forwarding',
'permit-port-forwarding',
'permit-pty',
'permit-user-rc'
]
class TestCertificate(unittest.TestCase):
def setUp(self):
self.config = {
'ca': '',
'user': '',
'pass': ''.join(random.choice(RAND_LETTERS) for _ in range(10)),
'attributes': {
"serial": random.randint(0, 2**64),
"type": 1,
"key_id": ''.join(random.choice(RAND_LETTERS) for _ in range(random.randint(10, 100))),
"valid_principals": [
"root",
"user_one",
"user two",
''.join(random.choice(RAND_LETTERS) for _ in range(10))
],
"valid_after": int(time()),
"valid_before": int(time() + random.randint(1, 3600*48)),
"critical_options": [ random.choice(RAND_CRITICAL_OPTIONS) for _ in range(random.randint(0, len(RAND_CRITICAL_OPTIONS))) ],
"extensions": [ random.choice(RAND_EXTENSIONS) for _ in range(random.randint(0, len(RAND_EXTENSIONS))) ],
"reserved": ""
}
}
def test_certificates(self):
self.gen_certificate(
user_pubkey_path=f"{self.config['user']}.pub",
ca_pubkey_path=f"{self.config['ca']}.pub",
ca_privkey_path=f"{self.config['ca']}",
ca_privkey_pass=self.config['pass'],
attributes=self.config['attributes'],
auto_verify=False
)
self.assertEqual(0, os.system(f"ssh-keygen -Lf {self.config['user']}-cert.pub > /dev/null 2>&1"))
decoded = self.decode_certificate(f"{self.config['user']}-cert.pub")
self.assertEqual(self.config['attributes']['serial'], decoded['serial'])
self.assertEqual(self.config['attributes']['type'], decoded['ctype'])
self.assertEqual(self.config['attributes']['key_id'], decoded['key_id'])
self.assertEqual(self.config['attributes']['valid_principals'], decoded['valid_principals'])
self.assertEqual(self.config['attributes']['valid_after'], decoded['valid_after'])
self.assertEqual(self.config['attributes']['valid_before'], decoded['valid_before'])
self.assertEqual(self.config['attributes']['critical_options'], decoded['critical_options'])
self.assertEqual(self.config['attributes']['extensions'], decoded['extensions'])
self.assertEqual(self.config['attributes']['reserved'], decoded['reserved'])
def tearDown(self):
remove_test_sshkeys(True)
class RSATest(TestCertificate):
def setUp(self):
super().setUp()
self.gen_certificate = make_rsa_certificate
self.decode_certificate = decode_rsa_certificate
self.config['ca'] = 'test_rsa_ca'
self.config['user'] = 'test_rsa_user'
gen_test_sshkeys(
password=self.config['pass'],
dss=False,
ecdsa=False,
ed25519=False,
)
class DSSTest(TestCertificate):
def setUp(self):
super().setUp()
self.gen_certificate = make_dss_certificate
self.decode_certificate = decode_dss_certificate
self.config['ca'] = 'test_dss_ca'
self.config['user'] = 'test_dss_user'
gen_test_sshkeys(
password=self.config['pass'],
rsa=False,
ecdsa=False,
ed25519=False
)
class ECDSATest(TestCertificate):
def setUp(self):
super().setUp()
self.gen_certificate = make_ecdsa_certificate
self.decode_certificate = decode_ecdsa_certificate
self.config['ca'] = 'test_ecdsa_ca'
self.config['user'] = 'test_ecdsa_user'
gen_test_sshkeys(
password=self.config['pass'],
rsa=False,
dss=False,
ed25519=False
)
class ED25519Test(TestCertificate):
def setUp(self):
super().setUp()
self.gen_certificate = make_ed25519_certificate
self.decode_certificate = decode_ed25519_certificate
self.config['ca'] = 'test_ed25519_ca'
self.config['user'] = 'test_ed25519_user'
gen_test_sshkeys(
password=self.config['pass'],
rsa=False,
dss=False,
ecdsa=False
)
if __name__ == '__main__':
for _ in range(10):
test_suite = unittest.TestSuite()
test_suite.addTests(unittest.TestLoader().loadTestsFromTestCase(RSATest))
test_suite.addTests(unittest.TestLoader().loadTestsFromTestCase(DSSTest))
test_suite.addTests(unittest.TestLoader().loadTestsFromTestCase(ECDSATest))
test_suite.addTests(unittest.TestLoader().loadTestsFromTestCase(ED25519Test))
test_runner = unittest.TextTestRunner()
test_runner.run(test_suite)