8 from cam import openssl_wrap
11 logging.basicConfig(level=logging.DEBUG)
14 class CertStub(object):
16 def __init__(self, name, cn, tmpdir):
22 self.public_key_file = os.path.join(tmpdir, '%s.pub' % name)
23 self.private_key_file = os.path.join(tmpdir, '%s.priv' % name)
25 def get_expiration_date(self):
29 return os.path.exists(self.public_key_file)
33 def __init__(self, *args, **kwargs):
34 self.tmpdir = tempfile.mkdtemp()
35 self.ca = ca.CA(self.tmpdir, *args, **kwargs)
39 shutil.rmtree(self.tmpdir)
42 class CATest(unittest.TestCase):
45 self.ca_stub = CAStub({'cn': 'test ca', 'org': 'test',
46 'bits': '1024', 'email': 'test@test.com'},
48 self.ca = self.ca_stub.ca
49 self.tmpdir = self.ca_stub.tmpdir
52 self.ca_stub.destroy()
54 def test_create(self):
56 self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'conf/ca.conf')))
58 def test_create_cert(self):
60 cert = CertStub('test', 'www.test.com', self.tmpdir)
61 self.ca.generate(cert)
62 self.assertTrue(os.path.exists(cert.public_key_file))
63 self.assertTrue(os.path.exists(cert.private_key_file))
65 def test_create_cert_with_digest_override(self):
66 self.ca.config['signature_algorithm'] = 'md5'
68 cert = CertStub('test', 'www.test.com', self.tmpdir)
69 self.ca.generate(cert)
70 self.assertTrue(os.path.exists(cert.public_key_file))
71 self.assertTrue(os.path.exists(cert.private_key_file))
73 'Signature Algorithm: md5WithRSAEncryption' in subprocess.check_output(
74 ['openssl', 'x509', '-text', '-noout', '-in', cert.public_key_file]))
76 def test_revoke(self):
78 cert = CertStub('test', 'www.test.com', self.tmpdir)
79 self.ca.generate(cert)
81 self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'public/ca.crl')))
83 def test_verify(self):
84 orig_cert = CertStub('test', 'www.test.com', self.tmpdir)
86 self.ca.generate(orig_cert)
88 alt_ca = CAStub({'cn': 'test ca', 'org': 'test',
89 'bits': '1024', 'email': 'test@test.com'},
92 alt_cert = CertStub('test', 'www.test.com', alt_ca.tmpdir)
93 alt_ca.ca.generate(alt_cert)
95 self.assertTrue(self.ca.verify(orig_cert.public_key_file))
96 self.assertTrue(alt_ca.ca.verify(alt_cert.public_key_file))
97 self.assertFalse(self.ca.verify(alt_cert.public_key_file))
98 self.assertFalse(alt_ca.ca.verify(orig_cert.public_key_file))