-
Notifications
You must be signed in to change notification settings - Fork 68
/
tests.py
181 lines (157 loc) · 5.33 KB
/
tests.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import pytest
import OpenSSL.crypto
from csr import CsrGenerator
class TestGeneration:
@pytest.fixture
def csr_info(self):
return {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'OU': 'Marketing',
'CN': 'example.com'
}
def test_keypair_type(self, csr_info):
csr = CsrGenerator(csr_info)
assert isinstance(csr.keypair, OpenSSL.crypto.PKey)
def test_keypair_bits_default(self, csr_info):
csr = CsrGenerator(csr_info)
assert csr.keypair.bits() == 2048
def test_keypair_1024_bits(self, csr_info):
csr_info['keySize'] = 1024
csr = CsrGenerator(csr_info)
assert csr.keypair.bits() == 1024
def test_keypair_4096_bits(self, csr_info):
csr_info['keySize'] = 4096
csr = CsrGenerator(csr_info)
assert csr.keypair.bits() == 4096
def test_csr_length(self, csr_info):
csr = CsrGenerator(csr_info)
assert len(csr.csr) == 1106
def test_csr_starts_with(self, csr_info):
csr = CsrGenerator(csr_info)
assert csr.csr.startswith(b'-----BEGIN CERTIFICATE REQUEST-----')
def test_csr_ends_with(self, csr_info):
csr = CsrGenerator(csr_info)
assert csr.csr.endswith(b'-----END CERTIFICATE REQUEST-----\n')
def test_private_key_starts_with(self, csr_info):
csr = CsrGenerator(csr_info)
assert (
csr.private_key.startswith(b'-----BEGIN RSA PRIVATE KEY-----') or
csr.private_key.startswith(b'-----BEGIN PRIVATE KEY-----')
)
def test_private_key_ends_with(self, csr_info):
csr = CsrGenerator(csr_info)
assert (
csr.private_key.endswith(b'-----END RSA PRIVATE KEY-----\n') or
csr.private_key.endswith(b'-----END PRIVATE KEY-----\n')
)
def test_subject_alt_names(self, csr_info):
csr_info['subjectAltNames'] = "www.example.com,*.example.com"
csr = CsrGenerator(csr_info)
assert sorted(csr.subjectAltNames) == sorted(["DNS:example.com", "DNS:www.example.com", "DNS:*.example.com"])
def test_default_subject_alt_name(self, csr_info):
csr = CsrGenerator(csr_info)
assert csr.subjectAltNames == ["DNS:example.com", "DNS:www.example.com"]
class TestException:
def test_missing_country(self):
"This should _not_ raise any exceptions"
csr_info = {
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
def test_empty_country(self):
"This should _not_ raise any exceptions"
csr_info = {
'C': '',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
def test_missing_state(self):
"This should _not_ raise any exceptions"
csr_info = {
'C': 'US',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
def test_missing_locality(self):
"This should _not_ raise any exceptions"
csr_info = {
'C': 'US',
'ST': 'Texas',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
def test_missing_organization(self):
"This should _not_ raise any exceptions"
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'CN': 'example.com'
}
CsrGenerator(csr_info)
def test_missing_common_name(self):
with pytest.raises(KeyError):
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers'
}
CsrGenerator(csr_info)
def test_missing_ou(self):
"This should _not_ raise any exceptions"
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
def test_empty_ou(self):
"This should _not_ raise any exceptions"
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'OU': '',
'CN': 'example.com'
}
CsrGenerator(csr_info)
def test_zero_key_size(self):
with pytest.raises(KeyError):
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'OU': 'Marketing',
'CN': 'example.com',
'keySize': 0
}
CsrGenerator(csr_info)
def test_invalid_key_size(self):
with pytest.raises(ValueError):
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'OU': 'Marketing',
'CN': 'example.com',
'keySize': 'penguins'
}
CsrGenerator(csr_info)