1 """COMPLI ASN.1:2008 compliance test suite
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
11 from unittest import skip
12 from unittest import TestCase
14 from pyderasn import BitString
15 from pyderasn import Boolean
16 from pyderasn import DecodeError
17 from pyderasn import Integer
18 from pyderasn import InvalidLength
19 from pyderasn import len_decode
20 from pyderasn import LenIndefForm
21 from pyderasn import NotEnoughData
22 from pyderasn import Null
23 from pyderasn import ObjectIdentifier
24 from pyderasn import OctetString
25 from pyderasn import tag_decode
26 from pyderasn import tag_strip
29 test_suite_path = path.join(
30 path.dirname(path.abspath(__file__)),
37 with open(path.join(test_suite_path, ("tc%d.ber" % num)), "rb") as fd:
41 class TestTestSuite(TestCase):
49 with self.assertRaisesRegex(DecodeError, "unfinished tag"):
54 t, _, _ = tag_strip(data)
55 with self.assertRaises(NotEnoughData):
56 OctetString(impl=t).decode(data)
60 t, _, lv = tag_strip(data)
61 with self.assertRaises(NotEnoughData):
66 t, _, lv = tag_strip(data)
67 with self.assertRaisesRegex(DecodeError, "long form instead of"):
70 @skip("PyDERASN does not support REAL")
74 @skip("PyDERASN does not support REAL")
78 @skip("PyDERASN does not support REAL")
82 @skip("PyDERASN does not support REAL")
86 @skip("PyDERASN does not support REAL")
90 @skip("PyDERASN does not support REAL")
94 @skip("PyDERASN does not support REAL")
98 @skip("PyDERASN does not support REAL")
102 @skip("PyDERASN does not support REAL")
106 @skip("PyDERASN does not support REAL")
110 @skip("PyDERASN does not support REAL")
114 @skip("PyDERASN does not support REAL")
120 with self.assertRaisesRegex(DecodeError, "non normalized"):
121 Integer().decode(data)
125 with self.assertRaises(NotEnoughData):
126 Integer().decode(data)
130 Integer().decode(data)
134 with self.assertRaisesRegex(DecodeError, "non normalized"):
135 ObjectIdentifier().decode(data)
136 ObjectIdentifier().decode(data, ctx={"bered": True})
140 with self.assertRaisesRegex(DecodeError, "too huge value"):
141 ObjectIdentifier().decode(data)
145 with self.assertRaises(NotEnoughData):
146 ObjectIdentifier().decode(data)
150 ObjectIdentifier().decode(data)
153 # X.690-201508 8.2.1: The encoding of a boolean value shall be
154 # primitive. The contents octets shall consist of a single octet.
156 with self.assertRaises(InvalidLength):
157 Boolean().decode(data)
158 with self.assertRaises(InvalidLength):
159 Boolean().decode(data, ctx={"bered": True})
162 # X.690-201508 8.2.1: The encoding of a boolean value shall be
163 # primitive. The contents octets shall consist of a single octet.
165 with self.assertRaises(InvalidLength):
166 Boolean().decode(data)
167 with self.assertRaises(InvalidLength):
168 Boolean().decode(data, ctx={"bered": True})
172 with self.assertRaises(InvalidLength):
173 Boolean().decode(data)
177 self.assertTrue(bool(Boolean().decode(data)[0]))
181 self.assertFalse(bool(Boolean().decode(data)[0]))
185 with self.assertRaises(InvalidLength):
190 with self.assertRaises(InvalidLength):
199 with self.assertRaisesRegex(DecodeError, "too big pad"):
200 BitString().decode(data)
201 with self.assertRaisesRegex(DecodeError, "too big pad"):
202 BitString().decode(data, ctx={"bered": True})
206 with self.assertRaises(NotEnoughData):
207 BitString().decode(data)
211 with self.assertRaisesRegex(DecodeError, "expected BitString encoded chunk"):
212 BitString().decode(data, ctx={"bered": True})
216 with self.assertRaisesRegex(DecodeError, "invalid pad"):
217 BitString().decode(data, ctx={"bered": True})
220 # X.690-201508 8.6.4: To encode a bitstring value in this way,
221 # it is segmented. Each segment shall consist of a series of
222 # consecutive bits of the value, and with the possible exception
223 # of the last, shall contain a number of bits which is a
226 with self.assertRaisesRegex(DecodeError, "invalid pad"):
227 BitString().decode(data, ctx={"bered": True})
231 BitString().decode(data, ctx={"bered": True})
234 # X.690-201508 8.6.2: The contents octets for the primitive
235 # encoding shall contain an initial octet followed by zero, one
236 # or more subsequent octets.
237 # X.690-201508 8.6.2.3: If the bitstring is empty, there shall
238 # be no subsequent octets, and the initial octet shall be zero.
240 with self.assertRaises(NotEnoughData):
241 BitString().decode(data, ctx={"bered": True})
244 # X.690-201508 8.6.2: The contents octets for the primitive
245 # encoding shall contain an initial octet followed by zero, one
246 # or more subsequent octets.
247 # X.690-201508 8.6.2.3: If the bitstring is empty, there shall
248 # be no subsequent octets, and the initial octet shall be zero.
250 with self.assertRaises(NotEnoughData):
251 BitString().decode(data, ctx={"bered": True})
255 with self.assertRaisesRegex(DecodeError, "expected OctetString encoded chunk"):
256 OctetString().decode(data, ctx={"bered": True})
260 with self.assertRaises(NotEnoughData):
261 OctetString().decode(data, ctx={"bered": True})
265 with self.assertRaises(NotEnoughData):
266 OctetString().decode(data, ctx={"bered": True})
270 OctetString().decode(data)
274 OctetString().decode(data, ctx={"bered": True})
278 with self.assertRaises(LenIndefForm):
279 BitString().decode(data, ctx={"bered": True})
283 with self.assertRaisesRegex(DecodeError, "expected BitString encoded chunk"):
284 BitString().decode(data, ctx={"bered": True})
288 with self.assertRaisesRegex(DecodeError, "too big pad"):
289 BitString().decode(data, ctx={"bered": True})