always regenerate the CA config file if necessary
[stack/cam.git] / cam / tests / test_cert.py
1 import os
2 import tempfile
3 import time
4 import shutil
5 import unittest
6 from cam import cert
7
8
9 TEST_PEM = '''-----BEGIN CERTIFICATE-----
10 MIICNDCCAaECEAKtZn5ORf5eV288mBle3cAwDQYJKoZIhvcNAQECBQAwXzELMAkG
11 A1UEBhMCVVMxIDAeBgNVBAoTF1JTQSBEYXRhIFNlY3VyaXR5LCBJbmMuMS4wLAYD
12 VQQLEyVTZWN1cmUgU2VydmVyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MB4XDTk0
13 MTEwOTAwMDAwMFoXDTEwMDEwNzIzNTk1OVowXzELMAkGA1UEBhMCVVMxIDAeBgNV
14 BAoTF1JTQSBEYXRhIFNlY3VyaXR5LCBJbmMuMS4wLAYDVQQLEyVTZWN1cmUgU2Vy
15 dmVyIENlcnRpZmljYXRpb24gQXV0aG9yaXR5MIGbMA0GCSqGSIb3DQEBAQUAA4GJ
16 ADCBhQJ+AJLOesGugz5aqomDV6wlAXYMra6OLDfO6zV4ZFQD5YRAUcm/jwjiioII
17 0haGN1XpsSECrXZogZoFokvJSyVmIlZsiAeP94FZbYQHZXATcXY+m3dM41CJVphI
18 uR2nKRoTLkoRWZweFdVJVCxzOmmCsZc5nG1wZ0jl3S3WyB57AgMBAAEwDQYJKoZI
19 hvcNAQECBQADfgBl3X7hsuyw4jrg7HFGmhkRuNPHoLQDQCYCPgmc4RKz0Vr2N6W3
20 YQO2WxZpO8ZECAyIUwxrl0nHPjXcbLm7qt9cuzovk2C2qUtN8iD3zV9/ZHuO3ABc
21 1/p3yjkWWW8O6tO1g39NTUJWdrTJXwT4OPjr0l91X817/OWOgHz8UA==
22 -----END CERTIFICATE-----
23 '''
24
25 TEST_SHA1 = '44:63:C5:31:D7:CC:C1:00:67:94:61:2B:B6:56:D3:BF:82:57:84:6F'
26 TEST_MD5 = '74:7B:82:03:43:F0:00:9E:6B:B3:EC:47:BF:85:A5:93'
27 TEST_EXPIRY = 1262908799.0
28
29
30 class CAStub(object):
31
32     def __init__(self, basedir):
33         self.basedir = basedir
34
35
36 class CertTest(unittest.TestCase):
37
38     def setUp(self):
39         self.tmpdir = tempfile.mkdtemp()
40         os.makedirs(os.path.join(self.tmpdir, 'public', 'certs'))
41         self.ca = CAStub(self.tmpdir)
42         self.crt_file = os.path.join(self.tmpdir, 'public', 'certs', 'test.pem')
43         fd = open(self.crt_file, 'w')
44         fd.write(TEST_PEM)
45         fd.close()
46
47     def tearDown(self):
48         shutil.rmtree(self.tmpdir)
49
50     def test_get_fingerprint(self):
51         crt = cert.Cert(self.ca, 'test', {'cn': 'test.com'})
52         md5 = crt.get_fingerprint('md5')
53         self.assertEquals(TEST_MD5, md5)
54         sha1 = crt.get_fingerprint('sha1')
55         self.assertEquals(TEST_SHA1, sha1)
56
57     def test_get_fingerprint_nonexist(self):
58         crt = cert.Cert(self.ca, 'test-nonexist', {'cn': 'test.com'})
59         result = crt.get_fingerprint('md5')
60         self.assertEquals(None, result)
61
62     def test_cn_in_alt_names(self):
63         crt = cert.Cert(self.ca, 'test', {'cn': 'test.com',
64                                           'alt_names': 'test2.com'})
65         self.assert_('test.com' in crt.alt_names)
66
67     def test_get_expiration_date(self):
68         crt = cert.Cert(self.ca, 'test', {'cn': 'test.com'})
69         exp = crt.get_expiration_date()
70         self.assertEquals(TEST_EXPIRY, exp)
71
72     def test_get_expiration_date_nonexist(self):
73         crt = cert.Cert(self.ca, 'test-nonexist', {'cn': 'test.com'})
74         exp = crt.get_expiration_date()
75         self.assertEquals(None, exp)
76
77     def test_expired(self):
78         crt = cert.Cert(self.ca, 'test', {'cn': 'test.com'})
79         exp = crt.get_expiration_date()
80         now = time.time()
81         is_expired = (exp > now)
82         self.assertEquals(is_expired, crt.expired())
83
84
85 if __name__ == '__main__':
86     unittest.main()