35c61a3b360ea6f4c5799706ad269fc77ca3cfcd
[stack/cam.git] / cam / tests / test_ca.py
1 import logging
2 import os
3 import tempfile
4 import shutil
5 import unittest
6 from cam import ca
7 from cam import openssl_wrap
8
9
10 logging.basicConfig(level=logging.DEBUG)
11
12
13 class CertStub(object):
14
15     def __init__(self, name, cn, tmpdir):
16         self.name = name
17         self.cn = cn
18         self.alt_names = [cn]
19         self.ou = None
20         self.days = '365'
21         self.public_key_file = os.path.join(tmpdir, '%s.pub' % name)
22         self.private_key_file = os.path.join(tmpdir, '%s.priv' % name)
23
24     def get_expiration_date(self):
25         return 123456789
26
27
28 class CATest(unittest.TestCase):
29
30     def setUp(self):
31         self.tmpdir = tempfile.mkdtemp()
32         self.ca = ca.CA(self.tmpdir,
33                         {'cn': 'test ca', 'org': 'test',
34                          'bits': '1024', 'email': 'test@test.com'},
35                         password='testpw')
36
37     def tearDown(self):
38         self.ca.close()
39         shutil.rmtree(self.tmpdir)
40
41     def test_create(self):
42         self.ca.create()
43         self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'conf/ca.conf')))
44
45     def test_create_cert(self):
46         self.ca.create()
47         cert = CertStub('test', 'www.test.com', self.tmpdir)
48         self.ca.generate(cert)
49         self.assertTrue(os.path.exists(cert.public_key_file))
50         self.assertTrue(os.path.exists(cert.private_key_file))
51
52     def test_revoke(self):
53         self.ca.create()
54         cert = CertStub('test', 'www.test.com', self.tmpdir)
55         self.ca.generate(cert)
56         self.ca.revoke(cert)
57         self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'public/ca.crl')))