]> Cypherpunks.ru repositories - pyderasn.git/blob - tests/test_compli.py
8e0ff827480ca6e68404eb0f1f903f03f8b442e8
[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         ObjectIdentifier().decode(data)
143
144     def test_tc23(self):
145         data = load_tc(23)
146         with self.assertRaises(NotEnoughData):
147             ObjectIdentifier().decode(data)
148
149     def test_tc24(self):
150         data = load_tc(24)
151         ObjectIdentifier().decode(data)
152
153     def test_tc25(self):
154         # X.690-201508 8.2.1: The encoding of a boolean value shall be
155         # primitive. The contents octets shall consist of a single octet.
156         data = load_tc(25)
157         with self.assertRaises(InvalidLength):
158             Boolean().decode(data)
159         with self.assertRaises(InvalidLength):
160             Boolean().decode(data, ctx={"bered": True})
161
162     def test_tc26(self):
163         # X.690-201508 8.2.1: The encoding of a boolean value shall be
164         # primitive. The contents octets shall consist of a single octet.
165         data = load_tc(26)
166         with self.assertRaises(InvalidLength):
167             Boolean().decode(data)
168         with self.assertRaises(InvalidLength):
169             Boolean().decode(data, ctx={"bered": True})
170
171     def test_tc27(self):
172         data = load_tc(27)
173         with self.assertRaises(InvalidLength):
174             Boolean().decode(data)
175
176     def test_tc28(self):
177         data = load_tc(28)
178         self.assertTrue(bool(Boolean().decode(data)[0]))
179
180     def test_tc29(self):
181         data = load_tc(29)
182         self.assertFalse(bool(Boolean().decode(data)[0]))
183
184     def test_tc30(self):
185         data = load_tc(30)
186         with self.assertRaises(InvalidLength):
187             Null().decode(data)
188
189     def test_tc31(self):
190         data = load_tc(31)
191         with self.assertRaises(InvalidLength):
192             Null().decode(data)
193
194     def test_tc32(self):
195         data = load_tc(32)
196         Null().decode(data)
197
198     def test_tc33(self):
199         data = load_tc(33)
200         with assertRaisesRegex(self, DecodeError, "too big pad"):
201             BitString().decode(data)
202         with assertRaisesRegex(self, DecodeError, "too big pad"):
203             BitString().decode(data, ctx={"bered": True})
204
205     def test_tc34(self):
206         data = load_tc(34)
207         with self.assertRaises(NotEnoughData):
208             BitString().decode(data)
209
210     def test_tc35(self):
211         data = load_tc(35)
212         with assertRaisesRegex(self, DecodeError, "expected BitString encoded chunk"):
213             BitString().decode(data, ctx={"bered": True})
214
215     def test_tc36(self):
216         data = load_tc(36)
217         with assertRaisesRegex(self, DecodeError, "invalid pad"):
218             BitString().decode(data, ctx={"bered": True})
219
220     def test_tc37(self):
221         # X.690-201508 8.6.4: To encode a bitstring value in this way,
222         # it is segmented. Each segment shall consist of a series of
223         # consecutive bits of the value, and with the possible exception
224         # of the last, shall contain a number of bits which is a
225         # multiple of eight.
226         data = load_tc(37)
227         with assertRaisesRegex(self, DecodeError, "invalid pad"):
228             BitString().decode(data, ctx={"bered": True})
229
230     def test_tc38(self):
231         data = load_tc(38)
232         BitString().decode(data, ctx={"bered": True})
233
234     def test_tc39(self):
235         # X.690-201508 8.6.2: The contents octets for the primitive
236         # encoding shall contain an initial octet followed by zero, one
237         # or more subsequent octets.
238         # X.690-201508 8.6.2.3: If the bitstring is empty, there shall
239         # be no subsequent octets, and the initial octet shall be zero.
240         data = load_tc(39)
241         with self.assertRaises(NotEnoughData):
242             BitString().decode(data, ctx={"bered": True})
243
244     def test_tc40(self):
245         # X.690-201508 8.6.2: The contents octets for the primitive
246         # encoding shall contain an initial octet followed by zero, one
247         # or more subsequent octets.
248         # X.690-201508 8.6.2.3: If the bitstring is empty, there shall
249         # be no subsequent octets, and the initial octet shall be zero.
250         data = load_tc(40)
251         with self.assertRaises(NotEnoughData):
252             BitString().decode(data, ctx={"bered": True})
253
254     def test_tc41(self):
255         data = load_tc(41)
256         with assertRaisesRegex(self, DecodeError, "expected OctetString encoded chunk"):
257             OctetString().decode(data, ctx={"bered": True})
258
259     def test_tc42(self):
260         data = load_tc(42)
261         with self.assertRaises(NotEnoughData):
262             OctetString().decode(data, ctx={"bered": True})
263
264     def test_tc43(self):
265         data = load_tc(43)
266         with self.assertRaises(NotEnoughData):
267             OctetString().decode(data, ctx={"bered": True})
268
269     def test_tc44(self):
270         data = load_tc(44)
271         OctetString().decode(data)
272
273     def test_tc45(self):
274         data = load_tc(45)
275         OctetString().decode(data, ctx={"bered": True})
276
277     def test_tc46(self):
278         data = load_tc(46)
279         with self.assertRaises(LenIndefForm):
280             BitString().decode(data, ctx={"bered": True})
281
282     def test_tc47(self):
283         data = load_tc(47)
284         with assertRaisesRegex(self, DecodeError, "expected BitString encoded chunk"):
285             BitString().decode(data, ctx={"bered": True})
286
287     def test_tc48(self):
288         data = load_tc(48)
289         with assertRaisesRegex(self, DecodeError, "too big pad"):
290             BitString().decode(data, ctx={"bered": True})