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 with assertRaisesRegex(self, DecodeError, "too huge value"):
143 ObjectIdentifier().decode(data)
147 with self.assertRaises(NotEnoughData):
148 ObjectIdentifier().decode(data)
152 ObjectIdentifier().decode(data)
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.
158 with self.assertRaises(InvalidLength):
159 Boolean().decode(data)
160 with self.assertRaises(InvalidLength):
161 Boolean().decode(data, ctx={"bered": True})
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.
167 with self.assertRaises(InvalidLength):
168 Boolean().decode(data)
169 with self.assertRaises(InvalidLength):
170 Boolean().decode(data, ctx={"bered": True})
174 with self.assertRaises(InvalidLength):
175 Boolean().decode(data)
179 self.assertTrue(bool(Boolean().decode(data)[0]))
183 self.assertFalse(bool(Boolean().decode(data)[0]))
187 with self.assertRaises(InvalidLength):
192 with self.assertRaises(InvalidLength):
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})
208 with self.assertRaises(NotEnoughData):
209 BitString().decode(data)
213 with assertRaisesRegex(self, DecodeError, "expected BitString encoded chunk"):
214 BitString().decode(data, ctx={"bered": True})
218 with assertRaisesRegex(self, DecodeError, "invalid pad"):
219 BitString().decode(data, ctx={"bered": True})
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
228 with assertRaisesRegex(self, DecodeError, "invalid pad"):
229 BitString().decode(data, ctx={"bered": True})
233 BitString().decode(data, ctx={"bered": True})
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.
242 with self.assertRaises(NotEnoughData):
243 BitString().decode(data, ctx={"bered": True})
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.
252 with self.assertRaises(NotEnoughData):
253 BitString().decode(data, ctx={"bered": True})
257 with assertRaisesRegex(self, DecodeError, "expected OctetString encoded chunk"):
258 OctetString().decode(data, ctx={"bered": True})
262 with self.assertRaises(NotEnoughData):
263 OctetString().decode(data, ctx={"bered": True})
267 with self.assertRaises(NotEnoughData):
268 OctetString().decode(data, ctx={"bered": True})
272 OctetString().decode(data)
276 OctetString().decode(data, ctx={"bered": True})
280 with self.assertRaises(LenIndefForm):
281 BitString().decode(data, ctx={"bered": True})
285 with assertRaisesRegex(self, DecodeError, "expected BitString encoded chunk"):
286 BitString().decode(data, ctx={"bered": True})
290 with assertRaisesRegex(self, DecodeError, "too big pad"):
291 BitString().decode(data, ctx={"bered": True})