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 six import assertRaisesRegex
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
31 test_suite_path = path.join(
32 path.dirname(path.abspath(__file__)),
39 with open(path.join(test_suite_path, ("tc%d.ber" % num)), "rb") as fd:
43 class TestTestSuite(TestCase):
51 with assertRaisesRegex(self, DecodeError, "unfinished tag"):
56 t, _, _ = tag_strip(data)
57 with self.assertRaises(NotEnoughData):
58 OctetString(impl=t).decode(data)
62 t, _, lv = tag_strip(data)
63 with self.assertRaises(NotEnoughData):
68 t, _, lv = tag_strip(data)
69 with assertRaisesRegex(self, DecodeError, "long form instead of"):
72 @skip("PyDERASN does not support REAL")
76 @skip("PyDERASN does not support REAL")
80 @skip("PyDERASN does not support REAL")
84 @skip("PyDERASN does not support REAL")
88 @skip("PyDERASN does not support REAL")
92 @skip("PyDERASN does not support REAL")
96 @skip("PyDERASN does not support REAL")
100 @skip("PyDERASN does not support REAL")
104 @skip("PyDERASN does not support REAL")
108 @skip("PyDERASN does not support REAL")
112 @skip("PyDERASN does not support REAL")
116 @skip("PyDERASN does not support REAL")
122 with assertRaisesRegex(self, DecodeError, "non normalized"):
123 Integer().decode(data)
127 with self.assertRaises(NotEnoughData):
128 Integer().decode(data)
132 Integer().decode(data)
136 with assertRaisesRegex(self, DecodeError, "non normalized"):
137 ObjectIdentifier().decode(data)
138 ObjectIdentifier().decode(data, ctx={"bered": True})
142 ObjectIdentifier().decode(data)
146 with self.assertRaises(NotEnoughData):
147 ObjectIdentifier().decode(data)
151 ObjectIdentifier().decode(data)
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.
157 with self.assertRaises(InvalidLength):
158 Boolean().decode(data)
159 with self.assertRaises(InvalidLength):
160 Boolean().decode(data, ctx={"bered": True})
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.
166 with self.assertRaises(InvalidLength):
167 Boolean().decode(data)
168 with self.assertRaises(InvalidLength):
169 Boolean().decode(data, ctx={"bered": True})
173 with self.assertRaises(InvalidLength):
174 Boolean().decode(data)
178 self.assertTrue(bool(Boolean().decode(data)[0]))
182 self.assertFalse(bool(Boolean().decode(data)[0]))
186 with self.assertRaises(InvalidLength):
191 with self.assertRaises(InvalidLength):
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})
207 with self.assertRaises(NotEnoughData):
208 BitString().decode(data)
212 with assertRaisesRegex(self, DecodeError, "expected BitString encoded chunk"):
213 BitString().decode(data, ctx={"bered": True})
217 with assertRaisesRegex(self, DecodeError, "invalid pad"):
218 BitString().decode(data, ctx={"bered": True})
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
227 with assertRaisesRegex(self, DecodeError, "invalid pad"):
228 BitString().decode(data, ctx={"bered": True})
232 BitString().decode(data, ctx={"bered": True})
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.
241 with self.assertRaises(NotEnoughData):
242 BitString().decode(data, ctx={"bered": True})
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.
251 with self.assertRaises(NotEnoughData):
252 BitString().decode(data, ctx={"bered": True})
256 with assertRaisesRegex(self, DecodeError, "expected OctetString encoded chunk"):
257 OctetString().decode(data, ctx={"bered": True})
261 with self.assertRaises(NotEnoughData):
262 OctetString().decode(data, ctx={"bered": True})
266 with self.assertRaises(NotEnoughData):
267 OctetString().decode(data, ctx={"bered": True})
271 OctetString().decode(data)
275 OctetString().decode(data, ctx={"bered": True})
279 with self.assertRaises(LenIndefForm):
280 BitString().decode(data, ctx={"bered": True})
284 with assertRaisesRegex(self, DecodeError, "expected BitString encoded chunk"):
285 BitString().decode(data, ctx={"bered": True})
289 with assertRaisesRegex(self, DecodeError, "too big pad"):
290 BitString().decode(data, ctx={"bered": True})