]> www.wagner.pp.ru Git - oss/ctypescrypto.git/blob - tests/testbio.py
Added explicit check for CMS functions in libcrypto
[oss/ctypescrypto.git] / tests / testbio.py
1 from ctypescrypto.bio import Membio
2 import unittest
3 import sys
4 if sys.version[0]>'2':
5     def unicode(b):
6         return str(b)
7
8
9 class TestRead(unittest.TestCase):
10     def test_readshortstr(self):
11         s=b"A quick brown fox jumps over a lazy dog"
12         bio=Membio(s)
13         data=bio.read()
14         self.assertEqual(data,s)
15         data2=bio.read()
16         self.assertEqual(data2,b"")
17         del bio
18     def test_readwithlen(self):
19         s=b"A quick brown fox jumps over a lazy dog"
20         bio=Membio(s)
21         data=bio.read(len(s))
22         self.assertEqual(data,s)
23         data2=bio.read(5)
24         self.assertEqual(data2,b"")
25     def test_readwrongtype(self):
26         s=b"A quick brown fox jumps over a lazy dog"
27         bio=Membio(s)
28         with self.assertRaises(TypeError):
29             data=bio.read("5")
30     def test_reset(self):
31         s=b"A quick brown fox jumps over a lazy dog"
32         bio=Membio(s)
33         data=bio.read()
34         bio.reset()
35         data2=bio.read()
36         del bio
37         self.assertEqual(data,data2)
38         self.assertEqual(data,s)
39     def test_readlongstr(self):
40         poem=b'''Eyes of grey--a sodden quay,
41 Driving rain and falling tears,
42 As the steamer wears to sea
43 In a parting storm of cheers.
44
45 Sing, for Faith and Hope are high--
46 None so true as you and I--
47 Sing the Lovers' Litany:
48 "Love like ours can never die!"
49
50 Eyes of black--a throbbing keel,
51 Milky foam to left and right;
52 Whispered converse near the wheel
53 In the brilliant tropic night.
54
55 Cross that rules the Southern Sky!
56 Stars that sweep and wheel and fly,
57 Hear the Lovers' Litany:
58 Love like ours can never die!"
59
60 Eyes of brown--a dusty plain
61 Split and parched with heat of June,
62 Flying hoof and tightened rein,
63 Hearts that beat the old, old tune.
64
65 Side by side the horses fly,
66 Frame we now the old reply
67 Of the Lovers' Litany:
68 "Love like ours can never die!"
69
70 Eyes of blue--the Simla Hills
71 Silvered with the moonlight hoar;
72 Pleading of the waltz that thrills,
73 Dies and echoes round Benmore.
74
75 "Mabel," "Officers," "Goodbye,"
76 Glamour, wine, and witchery--
77 On my soul's sincerity,
78 "Love like ours can never die!"
79
80 Maidens of your charity,
81 Pity my most luckless state.
82 Four times Cupid's debtor I--
83 Bankrupt in quadruplicate.
84
85 Yet, despite this evil case,
86 And a maiden showed me grace,
87 Four-and-forty times would I
88 Sing the Lovers' Litany:
89 "Love like ours can never die!"'''
90         bio=Membio(poem)
91         data=bio.read()
92         self.assertEqual(data,poem)
93         del bio
94     def test_readparts(self):
95         s=b"A quick brown fox jumps over the lazy dog"
96         bio=Membio(s)
97         a=bio.read(10)
98         self.assertEqual(a,s[0:10])
99         b=bio.read(9)
100         self.assertEqual(b,s[10:19])
101         c=bio.read()
102         self.assertEqual(c,s[19:])
103         d=bio.read()
104         self.assertEqual(d,b"")
105
106 class TestWrite(unittest.TestCase):
107     def test_write(self):
108         b=Membio()
109         b.write(b"A quick brown ")
110         b.write(b"fox jumps over ")
111         b.write(b"the lazy dog.")
112         self.assertEqual(str(b),"A quick brown fox jumps over the lazy dog.")
113
114     def test_unicode(self):
115         b=Membio()
116         s=b'\xd0\xba\xd0\xb0\xd0\xba \xd1\x8d\xd1\x82\xd0\xbe \xd0\xbf\xd0\xbe-\xd1\x80\xd1\x83\xd1\x81\xd1\x81\xd0\xba\xd0\xb8'
117         b.write(s)
118         self.assertEqual(unicode(b),u'\u043a\u0430\u043a \u044d\u0442\u043e \u043f\u043e-\u0440\u0443\u0441\u0441\u043a\u0438')
119     def test_unicode2(self):
120         b=Membio()
121         u=u'\u043a\u0430\u043a \u044d\u0442\u043e \u043f\u043e-\u0440\u0443\u0441\u0441\u043a\u0438'
122         b.write(u)
123         self.assertEqual(unicode(b),u)
124 if __name__ == '__main__':
125     unittest.main()