forked from DavidWittman/csrgenerator.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
139 lines (119 loc) · 4.47 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
#!/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(2048, self.csr_info)
self.assertTrue(isinstance(csr.keypair, OpenSSL.crypto.PKey))
def test_keypair_bits(self):
csr = CsrGenerator(2048, self.csr_info)
assert_equal(csr.keypair.bits(), 2048)
csr = CsrGenerator(1024, self.csr_info)
assert_equal(csr.keypair.bits(), 1024)
def test_csr_length(self):
csr = CsrGenerator(2048, self.csr_info)
assert_equal(len(csr.csr), 1025)
def test_csr_starts_with(self):
csr = CsrGenerator(2048, self.csr_info)
self.assertTrue(csr.csr.startswith('-----BEGIN CERTIFICATE REQUEST-----'))
def test_csr_ends_with(self):
csr = CsrGenerator(2048, self.csr_info)
self.assertTrue(csr.csr.endswith('-----END CERTIFICATE REQUEST-----\n'))
def test_private_key_starts_with(self):
csr = CsrGenerator(2048, self.csr_info)
# The result here can differ based on OpenSSL versions
self.assertTrue(csr.private_key.startswith('-----BEGIN RSA PRIVATE KEY-----') or
csr.private_key.startswith('-----BEGIN PRIVATE KEY-----'))
def test_private_key_ends_with(self):
csr = CsrGenerator(2048, self.csr_info)
# The result here can differ based on OpenSSL versions
self.assertTrue(csr.private_key.endswith('-----END RSA PRIVATE KEY-----\n') or
csr.private_key.endswith('-----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(2048, 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(2048, 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(2048, csr_info)
@raises(KeyError)
def test_missing_locality(self):
csr_info = {
'C': 'US',
'ST': 'Texas',
'O': 'Big Bob\'s Beepers',
'CN': 'example.com'
}
CsrGenerator(2048, csr_info)
@raises(KeyError)
def test_missing_organization(self):
csr_info = {
'C': 'US',
'ST': 'Texas',
'L': 'San Antonio',
'CN': 'example.com'
}
CsrGenerator(2048, 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(2048, 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(2048, 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(2048, csr_info)