forked from DavidWittman/csrgenerator.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
174 lines (149 loc) · 5.42 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
#!/usr/bin/env python
import unittest
from nose.tools import raises, assert_equal
from csr import CsrGenerator
class GenerationTests(unittest.TestCase):
def setUp(self):
self.csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'OU': 'Marketing',
'CN': 'example.com'
}
def test_keypair_type(self):
import OpenSSL.crypto
csr = CsrGenerator(self.csr_info)
self.assertTrue(isinstance(csr.keypair, OpenSSL.crypto.PKey))
def test_keypair_bits_default(self):
csr = CsrGenerator(self.csr_info)
assert_equal(csr.keypair.bits(), 2048)
def test_keypair_1024_bits(self):
self.csr_info['keySize'] = 1024
csr = CsrGenerator(self.csr_info)
assert_equal(csr.keypair.bits(), 1024)
def test_keypair_4096_bits(self):
self.csr_info['keySize'] = 4096
csr = CsrGenerator(self.csr_info)
assert_equal(csr.keypair.bits(), 4096)
def test_csr_length(self):
csr = CsrGenerator(self.csr_info)
assert_equal(len(csr.csr), 1029)
def test_csr_starts_with(self):
csr = CsrGenerator(self.csr_info)
self.assertTrue(csr.csr.startswith(b'-----BEGIN CERTIFICATE REQUEST-----'))
def test_csr_ends_with(self):
csr = CsrGenerator(self.csr_info)
self.assertTrue(csr.csr.endswith(b'-----END CERTIFICATE REQUEST-----\n'))
def test_private_key_starts_with(self):
csr = CsrGenerator(self.csr_info)
# The result here can differ based on OpenSSL versions
self.assertTrue(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 = CsrGenerator(self.csr_info)
# The result here can differ based on OpenSSL versions
self.assertTrue(csr.private_key.endswith(b'-----END RSA PRIVATE KEY-----\n') or
csr.private_key.endswith(b'-----END PRIVATE KEY-----\n'))
class ExceptionTests(unittest.TestCase):
@raises(KeyError)
def test_missing_country(self):
csr_info = {
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
@raises(KeyError)
def test_empty_country(self):
csr_info = {
'C': '',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
@raises(KeyError)
def test_missing_state(self):
csr_info = {
'C': 'US',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
@raises(KeyError)
def test_missing_locality(self):
csr_info = {
'C': 'US',
'ST': 'Texas',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(csr_info)
@raises(KeyError)
def test_missing_organization(self):
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'CN': 'example.com'
}
CsrGenerator(csr_info)
@raises(KeyError)
def test_missing_common_name(self):
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)
@raises(KeyError)
def test_zero_key_size(self):
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'OU': 'Marketing',
'CN': 'example.com',
'keySize': 0
}
CsrGenerator(csr_info)
@raises(ValueError)
def test_invalid_key_size(self):
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'O': 'Big Bob\'s Beepers',
'OU': 'Marketing',
'CN': 'example.com',
'keySize': 'penguins'
}
CsrGenerator(csr_info)