]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - tests/testoids.py
b3856225f7b51eb39d2c7b774829fa2e1854fea6
[oss/ctypescrypto.git] / tests / testoids.py
1 from ctypescrypto.oid import Oid,create,cleanup
2 import unittest
3
4 class TestStandard(unittest.TestCase):
5     def test_cn(self):
6         o=Oid("2.5.4.3")
7         self.assertEqual(repr(o),"Oid('2.5.4.3')")
8         self.assertEqual(o.dotted(),"2.5.4.3")
9         self.assertEqual(str(o),"2.5.4.3")
10         self.assertEqual(o.shortname(),"CN")
11         self.assertEqual(o.longname(),"commonName")
12     def test_getnid(self):
13         o=Oid("2.5.4.3")
14         x=Oid("CN")
15         self.assertEqual(o.nid,x.nid)
16         self.assertEqual(o,x)
17         self.assertEqual(hash(o),hash(x))
18
19     def test_cons2(self):
20         o=Oid("2.5.4.3")
21         x=Oid("commonName")
22         self.assertEqual(o.nid,x.nid)
23     def test_bynid(self):
24         o=Oid("2.5.4.3")
25         x=Oid(o.nid)
26         self.assertEqual(o.nid,x.nid)
27     def test_clone(self):
28         o1=Oid('2.5.4.3')
29         o2=Oid(o1)
30         self.assertEqual(o1.nid,o2.nid)
31     def test_fromunicode(self):
32         o=Oid(u'commonName')
33         self.assertEqual(o.shortname(),'CN')
34     def test_wrongoid(self):
35         with self.assertRaises(ValueError):
36             o=Oid("1.2.3.4.5.6.7.8.10.111.1111")
37     def test_wrongname(self):
38         with self.assertRaises(ValueError):
39             o=Oid("No such oid in the database")
40     def test_wrongnid(self):
41         with self.assertRaises(ValueError):
42             o=Oid(9999999)
43     def test_wrongtype(self):
44         with self.assertRaises(TypeError):
45             o=Oid([2,5,3,4])
46
47 class TestCustom(unittest.TestCase):
48     def testCreate(self):
49         d='1.2.643.100.3'
50         sn="SNILS"
51         long_name="Russian Pension security number"
52         o=create(d,sn,long_name)
53         self.assertEqual(str(o),d)
54         self.assertEqual(o.shortname(),sn)
55         self.assertEqual(o.longname(),long_name)
56     def testLookup(self):
57         d='1.2.643.100.3'
58         sn="SNILS"
59         long_name="Russian Pension security number"
60         o=create(d,sn,long_name)
61         x=Oid(sn)
62         self.assertEqual(o,x)
63     def testCleanup(self):
64         d='1.2.643.100.9'
65         sn="SNILX"
66         long_name="Russian Pension security number"
67         o=create(d,sn,long_name)
68         self.assertEqual(str(o),'1.2.643.100.9')
69
70         cleanup()
71         with self.assertRaises(ValueError):
72             x=Oid(sn)
73     def testFromObj(self):
74         from ctypescrypto import libcrypto
75         from ctypes import c_int, c_char_p, c_void_p
76         libcrypto.OBJ_txt2obj.argtypes = (c_char_p, c_int)
77         libcrypto.OBJ_txt2obj.restype = c_void_p
78         obj= libcrypto.OBJ_txt2obj("1.2.643.100.9",1)
79         oid=Oid.fromobj(obj)
80         self.assertEqual(str(oid),'1.2.643.100.9')
81     def tearDown(self):
82         # Always call cleanup before next test
83         cleanup()
84     
85
86
87
88 if __name__ == '__main__':
89     unittest.main()