]> Cypherpunks.ru repositories - pyderasn.git/blob - tests/test_compli.py
9996d84a1472137c1ab9e15e331a3f0dfc32535b
[pyderasn.git] / tests / test_compli.py
1 """COMPLI ASN.1:2008 compliance test suite
2
3 COMPLI is a collection of BER-encoded examples that must behave in
4 strictly defined way. All of them are to be valid ASN.1 encodings,
5 however some of them do not comply with X.690-201508, so PyDERASN
6 behaves differently in some tests. PyDERASN does not support REAL
7 values too.
8 """
9
10 from os import path
11 from unittest import skip
12 from unittest import TestCase
13
14 from six import assertRaisesRegex
15
16 from pyderasn import BitString
17 from pyderasn import Boolean
18 from pyderasn import DecodeError
19 from pyderasn import Integer
20 from pyderasn import InvalidLength
21 from pyderasn import len_decode
22 from pyderasn import LenIndefForm
23 from pyderasn import NotEnoughData
24 from pyderasn import Null
25 from pyderasn import ObjectIdentifier
26 from pyderasn import OctetString
27 from pyderasn import tag_decode
28 from pyderasn import tag_strip
29
30
31 test_suite_path = path.join(
32     path.dirname(path.abspath(__file__)),
33     "compli_test_suite",
34     "suite",
35 )
36
37
38 def load_tc(num):
39     with open(path.join(test_suite_path, ("tc%d.ber" % num)), "rb") as fd:
40         return fd.read()
41
42
43 class TestTestSuite(TestCase):
44     def test_tc1(self):
45         data = load_tc(1)
46         tag_strip(data)
47         tag_decode(data)
48
49     def test_tc2(self):
50         data = load_tc(2)
51         with assertRaisesRegex(self, DecodeError, "unfinished tag"):
52             tag_strip(data)
53
54     def test_tc3(self):
55         data = load_tc(3)
56         t, _, _ = tag_strip(data)
57         with self.assertRaises(NotEnoughData):
58             OctetString(impl=t).decode(data)
59
60     def test_tc4(self):
61         data = load_tc(4)
62         t, _, lv = tag_strip(data)
63         with self.assertRaises(NotEnoughData):
64             len_decode(lv)
65
66     def test_tc5(self):
67         data = load_tc(5)
68         t, _, lv = tag_strip(data)
69         with assertRaisesRegex(self, DecodeError, "long form instead of"):
70             len_decode(lv)
71
72     @skip("PyDERASN does not support REAL")
73     def test_tc6(self):
74         pass
75
76     @skip("PyDERASN does not support REAL")
77     def test_tc7(self):
78         pass
79
80     @skip("PyDERASN does not support REAL")
81     def test_tc8(self):
82         pass
83
84     @skip("PyDERASN does not support REAL")
85     def test_tc9(self):
86         pass
87
88     @skip("PyDERASN does not support REAL")
89     def test_tc10(self):
90         pass
91
92     @skip("PyDERASN does not support REAL")
93     def test_tc11(self):
94         pass
95
96     @skip("PyDERASN does not support REAL")
97     def test_tc12(self):
98         pass
99
100     @skip("PyDERASN does not support REAL")
101     def test_tc13(self):
102         pass
103
104     @skip("PyDERASN does not support REAL")
105     def test_tc14(self):
106         pass
107
108     @skip("PyDERASN does not support REAL")
109     def test_tc15(self):
110         pass
111
112     @skip("PyDERASN does not support REAL")
113     def test_tc16(self):
114         pass
115
116     @skip("PyDERASN does not support REAL")
117     def test_tc17(self):
118         pass
119
120     def test_tc18(self):
121         data = load_tc(18)
122         with assertRaisesRegex(self, DecodeError, "non normalized"):
123             Integer().decode(data)
124
125     def test_tc19(self):
126         data = load_tc(19)
127         with self.assertRaises(NotEnoughData):
128             Integer().decode(data)
129
130     def test_tc20(self):
131         data = load_tc(20)
132         Integer().decode(data)
133
134     def test_tc21(self):
135         data = load_tc(21)
136         with assertRaisesRegex(self, DecodeError, "non normalized"):
137             ObjectIdentifier().decode(data)
138         ObjectIdentifier().decode(data, ctx={"bered": True})
139
140     def test_tc22(self):
141         data = load_tc(22)
142         with assertRaisesRegex(self, DecodeError, "too huge value"):
143             ObjectIdentifier().decode(data)
144
145     def test_tc23(self):
146         data = load_tc(23)
147         with self.assertRaises(NotEnoughData):
148             ObjectIdentifier().decode(data)
149
150     def test_tc24(self):
151         data = load_tc(24)
152         ObjectIdentifier().decode(data)
153
154     def test_tc25(self):
155         # X.690-201508 8.2.1: The encoding of a boolean value shall be
156         # primitive. The contents octets shall consist of a single octet.
157         data = load_tc(25)
158         with self.assertRaises(InvalidLength):
159             Boolean().decode(data)
160         with self.assertRaises(InvalidLength):
161             Boolean().decode(data, ctx={"bered": True})
162
163     def test_tc26(self):
164         # X.690-201508 8.2.1: The encoding of a boolean value shall be
165         # primitive. The contents octets shall consist of a single octet.
166         data = load_tc(26)
167         with self.assertRaises(InvalidLength):
168             Boolean().decode(data)
169         with self.assertRaises(InvalidLength):
170             Boolean().decode(data, ctx={"bered": True})
171
172     def test_tc27(self):
173         data = load_tc(27)
174         with self.assertRaises(InvalidLength):
175             Boolean().decode(data)
176
177     def test_tc28(self):
178         data = load_tc(28)
179         self.assertTrue(bool(Boolean().decode(data)[0]))
180
181     def test_tc29(self):
182         data = load_tc(29)
183         self.assertFalse(bool(Boolean().decode(data)[0]))
184
185     def test_tc30(self):
186         data = load_tc(30)
187         with self.assertRaises(InvalidLength):
188             Null().decode(data)
189
190     def test_tc31(self):
191         data = load_tc(31)
192         with self.assertRaises(InvalidLength):
193             Null().decode(data)
194
195     def test_tc32(self):
196         data = load_tc(32)
197         Null().decode(data)
198
199     def test_tc33(self):
200         data = load_tc(33)
201         with assertRaisesRegex(self, DecodeError, "too big pad"):
202             BitString().decode(data)
203         with assertRaisesRegex(self, DecodeError, "too big pad"):
204             BitString().decode(data, ctx={"bered": True})
205
206     def test_tc34(self):
207         data = load_tc(34)
208         with self.assertRaises(NotEnoughData):
209             BitString().decode(data)
210
211     def test_tc35(self):
212         data = load_tc(35)
213         with assertRaisesRegex(self, DecodeError, "expected BitString encoded chunk"):
214             BitString().decode(data, ctx={"bered": True})
215
216     def test_tc36(self):
217         data = load_tc(36)
218         with assertRaisesRegex(self, DecodeError, "invalid pad"):
219             BitString().decode(data, ctx={"bered": True})
220
221     def test_tc37(self):
222         # X.690-201508 8.6.4: To encode a bitstring value in this way,
223         # it is segmented. Each segment shall consist of a series of
224         # consecutive bits of the value, and with the possible exception
225         # of the last, shall contain a number of bits which is a
226         # multiple of eight.
227         data = load_tc(37)
228         with assertRaisesRegex(self, DecodeError, "invalid pad"):
229             BitString().decode(data, ctx={"bered": True})
230
231     def test_tc38(self):
232         data = load_tc(38)
233         BitString().decode(data, ctx={"bered": True})
234
235     def test_tc39(self):
236         # X.690-201508 8.6.2: The contents octets for the primitive
237         # encoding shall contain an initial octet followed by zero, one
238         # or more subsequent octets.
239         # X.690-201508 8.6.2.3: If the bitstring is empty, there shall
240         # be no subsequent octets, and the initial octet shall be zero.
241         data = load_tc(39)
242         with self.assertRaises(NotEnoughData):
243             BitString().decode(data, ctx={"bered": True})
244
245     def test_tc40(self):
246         # X.690-201508 8.6.2: The contents octets for the primitive
247         # encoding shall contain an initial octet followed by zero, one
248         # or more subsequent octets.
249         # X.690-201508 8.6.2.3: If the bitstring is empty, there shall
250         # be no subsequent octets, and the initial octet shall be zero.
251         data = load_tc(40)
252         with self.assertRaises(NotEnoughData):
253             BitString().decode(data, ctx={"bered": True})
254
255     def test_tc41(self):
256         data = load_tc(41)
257         with assertRaisesRegex(self, DecodeError, "expected OctetString encoded chunk"):
258             OctetString().decode(data, ctx={"bered": True})
259
260     def test_tc42(self):
261         data = load_tc(42)
262         with self.assertRaises(NotEnoughData):
263             OctetString().decode(data, ctx={"bered": True})
264
265     def test_tc43(self):
266         data = load_tc(43)
267         with self.assertRaises(NotEnoughData):
268             OctetString().decode(data, ctx={"bered": True})
269
270     def test_tc44(self):
271         data = load_tc(44)
272         OctetString().decode(data)
273
274     def test_tc45(self):
275         data = load_tc(45)
276         OctetString().decode(data, ctx={"bered": True})
277
278     def test_tc46(self):
279         data = load_tc(46)
280         with self.assertRaises(LenIndefForm):
281             BitString().decode(data, ctx={"bered": True})
282
283     def test_tc47(self):
284         data = load_tc(47)
285         with assertRaisesRegex(self, DecodeError, "expected BitString encoded chunk"):
286             BitString().decode(data, ctx={"bered": True})
287
288     def test_tc48(self):
289         data = load_tc(48)
290         with assertRaisesRegex(self, DecodeError, "too big pad"):
291             BitString().decode(data, ctx={"bered": True})