test_main.py 1.53 KB
Newer Older
ale's avatar
ale committed
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
import getpass
import os
import shutil
import subprocess
import sys
import tempfile
import unittest
from cam import main


class MainTest(unittest.TestCase):

    def setUp(self):
        self.tmpdir = tempfile.mkdtemp()
        self.cfgfile = os.path.join(self.tmpdir, 'test.conf')
        with open(self.cfgfile, 'w') as fd:
            fd.write("""
[ca]
cn = Test Ca
org = Test
country = IE
email = ca@test.org
bits = 1024

[web]
cn = www.test.org
""")

        def _fake_getpass(prompt):
            return 'testpass'
        getpass.getpass = _fake_getpass

    def tearDown(self):
        shutil.rmtree(self.tmpdir)

    def _run(self, *args):
        sys.argv = ['cam', '--config=%s' % self.cfgfile] + list(args)
        try:
            return main.main()
        except SystemExit, e:
            return e.code

    def test_init_and_sanity_check(self):
        self.assertEquals(None, self._run('init'))
        self.assertEquals(None, self._run('gen', 'web'))

        ca_file = os.path.join(self.tmpdir, 'public/ca.pem')
        crt_file = os.path.join(self.tmpdir, 'public/certs/web.pem')
        self.assertTrue(os.path.exists(ca_file))
        self.assertTrue(os.path.exists(crt_file))

        pipe = subprocess.Popen(
            ['openssl', 'verify', '-CAfile', ca_file, '-verbose', crt_file],
            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        output = pipe.communicate()[0]
        result = pipe.returncode
        self.assertEquals(0, result)

        print output
        self.assertTrue('error ' not in output)