summaryrefslogtreecommitdiff
path: root/tests/test_ssl_offline.py
blob: 9c10401d533eb64f46302aa8eb3d0f442f0876f8 (plain)
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
62
63
64
65
"""Unit tests for M2Crypto.SSL offline parts

Copyright (C) 2006 Open Source Applications Foundation. All Rights Reserved.

Copyright (C) 2009-2010 Heikki Toivonen. All Rights Reserved.
"""

import doctest
try:
    import unittest2 as unittest
except ImportError:
    import unittest

from M2Crypto.SSL import Checker
from M2Crypto import X509
from M2Crypto import SSL
from test_ssl import srv_host


class CheckerTestCase(unittest.TestCase):
    def test_checker(self):

        check = Checker.Checker(host=srv_host,
                                peerCertHash='7B754EFA41A264AAD370D43460BC8229F9354ECE')
        x509 = X509.load_cert('tests/server.pem')
        assert check(x509, srv_host)
        self.assertRaises(Checker.WrongHost, check, x509, 'example.com')
        
        doctest.testmod(Checker)

    
class ContextTestCase(unittest.TestCase):
    def test_ctx_load_verify_locations(self):
        ctx = SSL.Context()
        self.assertRaises(ValueError, ctx.load_verify_locations, None, None)
        
    def test_map(self):
        from M2Crypto.SSL.Context import map, _ctxmap
        assert isinstance(map(), _ctxmap)
        ctx = SSL.Context()
        assert map()
        ctx.close()
        assert map() is _ctxmap.singleton

    def test_certstore(self):
        ctx = SSL.Context()
        ctx.set_verify(SSL.verify_peer | SSL.verify_fail_if_no_peer_cert, 9)
        ctx.load_verify_locations('tests/ca.pem')
        ctx.load_cert('tests/x509.pem')

        store = ctx.get_cert_store()
        assert isinstance(store, X509.X509_Store)


def suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(CheckerTestCase))
    suite.addTest(unittest.makeSuite(ContextTestCase))
    return suite    


if __name__ == '__main__':
    Rand.load_file('randpool.dat', -1)
    unittest.TextTestRunner().run(suite())
    Rand.save_file('randpool.dat')