upgrade to CAM v2.0
[stack/cam.git] / cam / tests / test_ca.py
diff --git a/cam/tests/test_ca.py b/cam/tests/test_ca.py
new file mode 100644 (file)
index 0000000..c443fe4
--- /dev/null
@@ -0,0 +1,57 @@
+import logging
+import os
+import tempfile
+import shutil
+import unittest
+from cam import ca
+from cam import openssl_wrap
+
+
+logging.basicConfig(level=logging.DEBUG)
+
+
+class CertStub(object):
+
+    def __init__(self, name, cn, tmpdir):
+        self.name = name
+        self.cn = cn
+        self.alt_names = [cn]
+        self.ou = None
+        self.days = '365'
+        self.public_key_file = os.path.join(tmpdir, '%s.pub' % name)
+        self.private_key_file = os.path.join(tmpdir, '%s.priv' % name)
+
+    def get_expiration_date(self):
+        return 123456789
+
+
+class CATest(unittest.TestCase):
+
+    def setUp(self):
+        self.tmpdir = tempfile.mkdtemp()
+        self.ca = ca.CA(self.tmpdir,
+                        {'cn': 'test ca', 'org': 'test',
+                         'bits': '1024', 'email': 'test@test.com'},
+                        password='testpw')
+
+    def tearDown(self):
+        self.ca.close()
+        shutil.rmtree(self.tmpdir)
+
+    def test_create(self):
+        self.ca.create()
+        self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'conf/ca.conf')))
+
+    def test_create_cert(self):
+        self.ca.create()
+        cert = CertStub('test', 'www.test.com', self.tmpdir)
+        self.ca.generate(cert)
+        self.assertTrue(os.path.exists(cert.public_key_file))
+        self.assertTrue(os.path.exists(cert.private_key_file))
+
+    def test_revoke(self):
+        self.ca.create()
+        cert = CertStub('test', 'www.test.com', self.tmpdir)
+        self.ca.generate(cert)
+        self.ca.revoke(cert)
+        self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'public/crl.pem')))