1#
2# This file is part of pyasn1 software.
3#
4# Copyright (c) 2005-2019, Ilya Etingof <[email protected]>
5# License: http://snmplabs.com/pyasn1/license.html
6#
7import sys
8
9try:
10    import unittest2 as unittest
11
12except ImportError:
13    import unittest
14
15from tests.base import BaseTestCase
16
17from pyasn1.type import tag
18from pyasn1.type import namedtype
19from pyasn1.type import opentype
20from pyasn1.type import univ
21from pyasn1.type import char
22from pyasn1.codec.ber import decoder
23from pyasn1.codec.ber import eoo
24from pyasn1.compat.octets import ints2octs, str2octs, null
25from pyasn1.error import PyAsn1Error
26
27
28class LargeTagDecoderTestCase(BaseTestCase):
29    def testLargeTag(self):
30        assert decoder.decode(ints2octs((127, 141, 245, 182, 253, 47, 3, 2, 1, 1))) == (1, null)
31
32    def testLongTag(self):
33        assert decoder.decode(ints2octs((0x1f, 2, 1, 0)))[0].tagSet == univ.Integer.tagSet
34
35    def testTagsEquivalence(self):
36        integer = univ.Integer(2).subtype(implicitTag=tag.Tag(tag.tagClassContext, 0, 0))
37        assert decoder.decode(ints2octs((0x9f, 0x80, 0x00, 0x02, 0x01, 0x02)), asn1Spec=integer) == decoder.decode(
38            ints2octs((0x9f, 0x00, 0x02, 0x01, 0x02)), asn1Spec=integer)
39
40
41class DecoderCacheTestCase(BaseTestCase):
42    def testCache(self):
43        assert decoder.decode(ints2octs((0x1f, 2, 1, 0))) == decoder.decode(ints2octs((0x1f, 2, 1, 0)))
44
45
46class IntegerDecoderTestCase(BaseTestCase):
47    def testPosInt(self):
48        assert decoder.decode(ints2octs((2, 1, 12))) == (12, null)
49
50    def testNegInt(self):
51        assert decoder.decode(ints2octs((2, 1, 244))) == (-12, null)
52
53    def testZero(self):
54        assert decoder.decode(ints2octs((2, 0))) == (0, null)
55
56    def testZeroLong(self):
57        assert decoder.decode(ints2octs((2, 1, 0))) == (0, null)
58
59    def testMinusOne(self):
60        assert decoder.decode(ints2octs((2, 1, 255))) == (-1, null)
61
62    def testPosLong(self):
63        assert decoder.decode(
64            ints2octs((2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255))
65        ) == (0xffffffffffffffff, null)
66
67    def testNegLong(self):
68        assert decoder.decode(
69            ints2octs((2, 9, 255, 0, 0, 0, 0, 0, 0, 0, 1))
70        ) == (-0xffffffffffffffff, null)
71
72    def testSpec(self):
73        try:
74            decoder.decode(
75                ints2octs((2, 1, 12)), asn1Spec=univ.Null()
76            ) == (12, null)
77        except PyAsn1Error:
78            pass
79        else:
80            assert 0, 'wrong asn1Spec worked out'
81        assert decoder.decode(
82            ints2octs((2, 1, 12)), asn1Spec=univ.Integer()
83        ) == (12, null)
84
85    def testTagFormat(self):
86        try:
87            decoder.decode(ints2octs((34, 1, 12)))
88        except PyAsn1Error:
89            pass
90        else:
91            assert 0, 'wrong tagFormat worked out'
92
93
94class BooleanDecoderTestCase(BaseTestCase):
95    def testTrue(self):
96        assert decoder.decode(ints2octs((1, 1, 1))) == (1, null)
97
98    def testTrueNeg(self):
99        assert decoder.decode(ints2octs((1, 1, 255))) == (1, null)
100
101    def testExtraTrue(self):
102        assert decoder.decode(ints2octs((1, 1, 1, 0, 120, 50, 50))) == (1, ints2octs((0, 120, 50, 50)))
103
104    def testFalse(self):
105        assert decoder.decode(ints2octs((1, 1, 0))) == (0, null)
106
107    def testTagFormat(self):
108        try:
109            decoder.decode(ints2octs((33, 1, 1)))
110        except PyAsn1Error:
111            pass
112        else:
113            assert 0, 'wrong tagFormat worked out'
114
115
116class BitStringDecoderTestCase(BaseTestCase):
117    def testDefMode(self):
118        assert decoder.decode(
119            ints2octs((3, 3, 1, 169, 138))
120        ) == ((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), null)
121
122    def testIndefMode(self):
123        assert decoder.decode(
124            ints2octs((3, 3, 1, 169, 138))
125        ) == ((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), null)
126
127    def testDefModeChunked(self):
128        assert decoder.decode(
129            ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138))
130        ) == ((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), null)
131
132    def testIndefModeChunked(self):
133        assert decoder.decode(
134            ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
135        ) == ((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1), null)
136
137    def testDefModeChunkedSubst(self):
138        assert decoder.decode(
139            ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138)),
140            substrateFun=lambda a, b, c: (b, b[c:])
141        ) == (ints2octs((3, 2, 0, 169, 3, 2, 1, 138)), str2octs(''))
142
143    def testIndefModeChunkedSubst(self):
144        assert decoder.decode(
145            ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0)),
146            substrateFun=lambda a, b, c: (b, str2octs(''))
147        ) == (ints2octs((3, 2, 0, 169, 3, 2, 1, 138, 0, 0)), str2octs(''))
148
149    def testTypeChecking(self):
150        try:
151            decoder.decode(ints2octs((35, 4, 2, 2, 42, 42)))
152        except PyAsn1Error:
153            pass
154        else:
155            assert 0, 'accepted mis-encoded bit-string constructed out of an integer'
156
157
158class OctetStringDecoderTestCase(BaseTestCase):
159    def testDefMode(self):
160        assert decoder.decode(
161            ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
162        ) == (str2octs('Quick brown fox'), null)
163
164    def testIndefMode(self):
165        assert decoder.decode(
166            ints2octs((36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0))
167        ) == (str2octs('Quick brown fox'), null)
168
169    def testDefModeChunked(self):
170        assert decoder.decode(
171            ints2octs(
172                (36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120))
173        ) == (str2octs('Quick brown fox'), null)
174
175    def testIndefModeChunked(self):
176        assert decoder.decode(
177            ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0))
178        ) == (str2octs('Quick brown fox'), null)
179
180    def testDefModeChunkedSubst(self):
181        assert decoder.decode(
182            ints2octs(
183                (36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120)),
184            substrateFun=lambda a, b, c: (b, b[c:])
185        ) == (ints2octs((4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120)), str2octs(''))
186
187    def testIndefModeChunkedSubst(self):
188        assert decoder.decode(
189            ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111,
190                       120, 0, 0)),
191            substrateFun=lambda a, b, c: (b, str2octs(''))
192        ) == (ints2octs(
193            (4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0)), str2octs(''))
194
195
196class ExpTaggedOctetStringDecoderTestCase(BaseTestCase):
197    def setUp(self):
198        BaseTestCase.setUp(self)
199        self.o = univ.OctetString(
200            'Quick brown fox',
201            tagSet=univ.OctetString.tagSet.tagExplicitly(
202                tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 5)
203            ))
204
205    def testDefMode(self):
206        o, r = decoder.decode(
207            ints2octs((101, 17, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
208        )
209        assert not r
210        assert self.o == o
211        assert self.o.tagSet == o.tagSet
212        assert self.o.isSameTypeWith(o)
213
214    def testIndefMode(self):
215        o, r = decoder.decode(
216            ints2octs((101, 128, 36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0, 0, 0))
217        )
218        assert not r
219        assert self.o == o
220        assert self.o.tagSet == o.tagSet
221        assert self.o.isSameTypeWith(o)
222
223    def testDefModeChunked(self):
224        o, r = decoder.decode(
225            ints2octs((101, 25, 36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120))
226        )
227        assert not r
228        assert self.o == o
229        assert self.o.tagSet == o.tagSet
230        assert self.o.isSameTypeWith(o)
231
232    def testIndefModeChunked(self):
233        o, r = decoder.decode(
234            ints2octs((101, 128, 36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110, 32, 4, 3, 102, 111, 120, 0, 0, 0, 0))
235        )
236        assert not r
237        assert self.o == o
238        assert self.o.tagSet == o.tagSet
239        assert self.o.isSameTypeWith(o)
240
241    def testDefModeSubst(self):
242        assert decoder.decode(
243            ints2octs((101, 17, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)),
244            substrateFun=lambda a, b, c: (b, b[c:])
245        ) == (ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120)), str2octs(''))
246
247    def testIndefModeSubst(self):
248        assert decoder.decode(
249            ints2octs((
250                      101, 128, 36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0,
251                      0, 0, 0)),
252            substrateFun=lambda a, b, c: (b, str2octs(''))
253        ) == (ints2octs(
254            (36, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0, 0, 0)), str2octs(''))
255
256
257class NullDecoderTestCase(BaseTestCase):
258    def testNull(self):
259        assert decoder.decode(ints2octs((5, 0))) == (null, null)
260
261    def testTagFormat(self):
262        try:
263            decoder.decode(ints2octs((37, 0)))
264        except PyAsn1Error:
265            pass
266        else:
267            assert 0, 'wrong tagFormat worked out'
268
269
270# Useful analysis of OID encoding issues could be found here:
271# https://misc.daniel-marschall.de/asn.1/oid_facts.html
272class ObjectIdentifierDecoderTestCase(BaseTestCase):
273    def testOne(self):
274        assert decoder.decode(
275            ints2octs((6, 6, 43, 6, 0, 191, 255, 126))
276        ) == ((1, 3, 6, 0, 0xffffe), null)
277
278    def testEdge1(self):
279        assert decoder.decode(
280            ints2octs((6, 1, 39))
281        ) == ((0, 39), null)
282
283    def testEdge2(self):
284        assert decoder.decode(
285            ints2octs((6, 1, 79))
286        ) == ((1, 39), null)
287
288    def testEdge3(self):
289        assert decoder.decode(
290            ints2octs((6, 1, 120))
291        ) == ((2, 40), null)
292
293    def testEdge4(self):
294        assert decoder.decode(
295            ints2octs((6, 5, 0x90, 0x80, 0x80, 0x80, 0x4F))
296        ) == ((2, 0xffffffff), null)
297
298    def testEdge5(self):
299        assert decoder.decode(
300            ints2octs((6, 1, 0x7F))
301        ) == ((2, 47), null)
302
303    def testEdge6(self):
304        assert decoder.decode(
305            ints2octs((6, 2, 0x81, 0x00))
306        ) == ((2, 48), null)
307
308    def testEdge7(self):
309        assert decoder.decode(
310            ints2octs((6, 3, 0x81, 0x34, 0x03))
311        ) == ((2, 100, 3), null)
312
313    def testEdge8(self):
314        assert decoder.decode(
315            ints2octs((6, 2, 133, 0))
316        ) == ((2, 560), null)
317
318    def testEdge9(self):
319        assert decoder.decode(
320            ints2octs((6, 4, 0x88, 0x84, 0x87, 0x02))
321        ) == ((2, 16843570), null)
322
323    def testNonLeading0x80(self):
324        assert decoder.decode(
325            ints2octs((6, 5, 85, 4, 129, 128, 0)),
326        ) == ((2, 5, 4, 16384), null)
327
328    def testLeading0x80Case1(self):
329        try:
330            decoder.decode(
331                ints2octs((6, 5, 85, 4, 128, 129, 0))
332            )
333        except PyAsn1Error:
334            pass
335        else:
336            assert 0, 'Leading 0x80 tolerated'
337
338    def testLeading0x80Case2(self):
339        try:
340            decoder.decode(
341                ints2octs((6, 7, 1, 0x80, 0x80, 0x80, 0x80, 0x80, 0x7F))
342            )
343        except PyAsn1Error:
344            pass
345        else:
346            assert 0, 'Leading 0x80 tolerated'
347
348    def testLeading0x80Case3(self):
349        try:
350            decoder.decode(
351                ints2octs((6, 2, 0x80, 1))
352            )
353        except PyAsn1Error:
354            pass
355        else:
356            assert 0, 'Leading 0x80 tolerated'
357
358    def testLeading0x80Case4(self):
359        try:
360            decoder.decode(
361                ints2octs((6, 2, 0x80, 0x7F))
362            )
363        except PyAsn1Error:
364            pass
365        else:
366            assert 0, 'Leading 0x80 tolerated'
367
368    def testTagFormat(self):
369        try:
370            decoder.decode(ints2octs((38, 1, 239)))
371        except PyAsn1Error:
372            pass
373        else:
374            assert 0, 'wrong tagFormat worked out'
375
376    def testZeroLength(self):
377        try:
378            decoder.decode(ints2octs((6, 0, 0)))
379        except PyAsn1Error:
380            pass
381        else:
382            assert 0, 'zero length tolerated'
383
384    def testIndefiniteLength(self):
385        try:
386            decoder.decode(ints2octs((6, 128, 0)))
387        except PyAsn1Error:
388            pass
389        else:
390            assert 0, 'indefinite length tolerated'
391
392    def testReservedLength(self):
393        try:
394            decoder.decode(ints2octs((6, 255, 0)))
395        except PyAsn1Error:
396            pass
397        else:
398            assert 0, 'reserved length tolerated'
399
400    def testLarge1(self):
401        assert decoder.decode(
402            ints2octs((0x06, 0x11, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB, 0xE2, 0xB7, 0x17))
403        ) == ((2, 18446744073709551535184467440737095), null)
404
405    def testLarge2(self):
406        assert decoder.decode(
407            ints2octs((0x06, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB, 0xE2, 0xB6, 0x47))
408        ) == ((2, 999, 18446744073709551535184467440737095), null)
409
410
411class RealDecoderTestCase(BaseTestCase):
412    def testChar(self):
413        assert decoder.decode(
414            ints2octs((9, 7, 3, 49, 50, 51, 69, 49, 49))
415        ) == (univ.Real((123, 10, 11)), null)
416
417    def testBin1(self):  # check base = 2
418        assert decoder.decode(  # (0.5, 2, 0) encoded with base = 2
419            ints2octs((9, 3, 128, 255, 1))
420        ) == (univ.Real((1, 2, -1)), null)
421
422    def testBin2(self):  # check base = 2 and scale factor
423        assert decoder.decode(  # (3.25, 2, 0) encoded with base = 8
424            ints2octs((9, 3, 148, 255, 13))
425        ) == (univ.Real((26, 2, -3)), null)
426
427    def testBin3(self):  # check base = 16
428        assert decoder.decode(  # (0.00390625, 2, 0) encoded with base = 16
429            ints2octs((9, 3, 160, 254, 1))
430        ) == (univ.Real((1, 2, -8)), null)
431
432    def testBin4(self):  # check exponent = 0
433        assert decoder.decode(  # (1, 2, 0) encoded with base = 2
434            ints2octs((9, 3, 128, 0, 1))
435        ) == (univ.Real((1, 2, 0)), null)
436
437    def testBin5(self):  # case of 2 octs for exponent and negative exponent
438        assert decoder.decode(  # (3, 2, -1020) encoded with base = 16
439            ints2octs((9, 4, 161, 255, 1, 3))
440        ) == (univ.Real((3, 2, -1020)), null)
441
442# TODO: this requires Real type comparison fix
443
444#    def testBin6(self):
445#        assert decoder.decode(
446#            ints2octs((9, 5, 162, 0, 255, 255, 1))
447#        ) == (univ.Real((1, 2, 262140)), null)
448
449#    def testBin7(self):
450#        assert decoder.decode(
451#            ints2octs((9, 7, 227, 4, 1, 35, 69, 103, 1))
452#        ) == (univ.Real((-1, 2, 76354972)), null)
453
454    def testPlusInf(self):
455        assert decoder.decode(
456            ints2octs((9, 1, 64))
457        ) == (univ.Real('inf'), null)
458
459    def testMinusInf(self):
460        assert decoder.decode(
461            ints2octs((9, 1, 65))
462        ) == (univ.Real('-inf'), null)
463
464    def testEmpty(self):
465        assert decoder.decode(
466            ints2octs((9, 0))
467        ) == (univ.Real(0.0), null)
468
469    def testTagFormat(self):
470        try:
471            decoder.decode(ints2octs((41, 0)))
472        except PyAsn1Error:
473            pass
474        else:
475            assert 0, 'wrong tagFormat worked out'
476
477    def testShortEncoding(self):
478        try:
479            decoder.decode(ints2octs((9, 1, 131)))
480        except PyAsn1Error:
481            pass
482        else:
483            assert 0, 'accepted too-short real'
484
485
486if sys.version_info[0:2] > (2, 5):
487    class UniversalStringDecoderTestCase(BaseTestCase):
488        def testDecoder(self):
489            assert decoder.decode(ints2octs((28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99))) == (char.UniversalString(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null)
490
491
492class BMPStringDecoderTestCase(BaseTestCase):
493    def testDecoder(self):
494        assert decoder.decode(ints2octs((30, 6, 0, 97, 0, 98, 0, 99))) == (char.BMPString(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null)
495
496
497class UTF8StringDecoderTestCase(BaseTestCase):
498    def testDecoder(self):
499        assert decoder.decode(ints2octs((12, 3, 97, 98, 99))) == (char.UTF8String(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null)
500
501
502class SequenceOfDecoderTestCase(BaseTestCase):
503    def setUp(self):
504        BaseTestCase.setUp(self)
505
506        self.s = univ.SequenceOf(componentType=univ.OctetString())
507        self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
508
509    def testDefMode(self):
510        assert decoder.decode(
511            ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
512        ) == (self.s, null)
513
514    def testIndefMode(self):
515        assert decoder.decode(
516            ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
517        ) == (self.s, null)
518
519    def testDefModeChunked(self):
520        assert decoder.decode(
521            ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
522        ) == (self.s, null)
523
524    def testIndefModeChunked(self):
525        assert decoder.decode(
526            ints2octs((48, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0))
527        ) == (self.s, null)
528
529    def testSchemalessDecoder(self):
530        assert decoder.decode(
531            ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=univ.SequenceOf()
532        ) == (self.s, null)
533
534
535class ExpTaggedSequenceOfDecoderTestCase(BaseTestCase):
536
537    def testWithSchema(self):
538        s = univ.SequenceOf().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3))
539        s2, r = decoder.decode(
540            ints2octs((163, 15, 48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=s
541        )
542        assert not r
543        assert s2 == [str2octs('quick brown')]
544        assert s.tagSet == s2.tagSet
545
546    def testWithoutSchema(self):
547        s = univ.SequenceOf().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 3))
548        s2, r = decoder.decode(
549            ints2octs((163, 15, 48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
550        )
551        assert not r
552        assert s2 == [str2octs('quick brown')]
553        assert s.tagSet == s2.tagSet
554
555
556class SequenceOfDecoderWithSchemaTestCase(BaseTestCase):
557    def setUp(self):
558        BaseTestCase.setUp(self)
559        self.s = univ.SequenceOf(componentType=univ.OctetString())
560        self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
561
562    def testDefMode(self):
563        assert decoder.decode(
564            ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
565        ) == (self.s, null)
566
567    def testIndefMode(self):
568        assert decoder.decode(
569            ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)), asn1Spec=self.s
570        ) == (self.s, null)
571
572    def testDefModeChunked(self):
573        assert decoder.decode(
574            ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s
575        ) == (self.s, null)
576
577    def testIndefModeChunked(self):
578        assert decoder.decode(
579            ints2octs((48, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
580        ) == (self.s, null)
581
582
583class SetOfDecoderTestCase(BaseTestCase):
584    def setUp(self):
585        BaseTestCase.setUp(self)
586        self.s = univ.SetOf(componentType=univ.OctetString())
587        self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
588
589    def testDefMode(self):
590        assert decoder.decode(
591            ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
592        ) == (self.s, null)
593
594    def testIndefMode(self):
595        assert decoder.decode(
596            ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
597        ) == (self.s, null)
598
599    def testDefModeChunked(self):
600        assert decoder.decode(
601            ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
602        ) == (self.s, null)
603
604    def testIndefModeChunked(self):
605        assert decoder.decode(
606            ints2octs((49, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0))
607        ) == (self.s, null)
608
609    def testSchemalessDecoder(self):
610        assert decoder.decode(
611            ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=univ.SetOf()
612        ) == (self.s, null)
613
614
615class SetOfDecoderWithSchemaTestCase(BaseTestCase):
616    def setUp(self):
617        BaseTestCase.setUp(self)
618        self.s = univ.SetOf(componentType=univ.OctetString())
619        self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
620
621    def testDefMode(self):
622        assert decoder.decode(
623            ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
624        ) == (self.s, null)
625
626    def testIndefMode(self):
627        assert decoder.decode(
628            ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)), asn1Spec=self.s
629        ) == (self.s, null)
630
631    def testDefModeChunked(self):
632        assert decoder.decode(
633            ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s
634        ) == (self.s, null)
635
636    def testIndefModeChunked(self):
637        assert decoder.decode(
638            ints2octs((49, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
639        ) == (self.s, null)
640
641
642class SequenceDecoderTestCase(BaseTestCase):
643    def setUp(self):
644        BaseTestCase.setUp(self)
645        self.s = univ.Sequence(
646            componentType=namedtype.NamedTypes(
647                namedtype.NamedType('place-holder', univ.Null(null)),
648                namedtype.NamedType('first-name', univ.OctetString(null)),
649                namedtype.NamedType('age', univ.Integer(33))
650            )
651        )
652        self.s.setComponentByPosition(0, univ.Null(null))
653        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
654        self.s.setComponentByPosition(2, univ.Integer(1))
655
656    def testWithOptionalAndDefaultedDefMode(self):
657        assert decoder.decode(
658            ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
659        ) == (self.s, null)
660
661    def testWithOptionalAndDefaultedIndefMode(self):
662        assert decoder.decode(
663            ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))
664        ) == (self.s, null)
665
666    def testWithOptionalAndDefaultedDefModeChunked(self):
667        assert decoder.decode(
668            ints2octs(
669                (48, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1))
670        ) == (self.s, null)
671
672    def testWithOptionalAndDefaultedIndefModeChunked(self):
673        assert decoder.decode(
674            ints2octs((48, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))
675        ) == (self.s, null)
676
677    def testWithOptionalAndDefaultedDefModeSubst(self):
678        assert decoder.decode(
679            ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
680            substrateFun=lambda a, b, c: (b, b[c:])
681        ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
682
683    def testWithOptionalAndDefaultedIndefModeSubst(self):
684        assert decoder.decode(
685            ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)),
686            substrateFun=lambda a, b, c: (b, str2octs(''))
687        ) == (ints2octs(
688            (5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), str2octs(''))
689
690    def testTagFormat(self):
691        try:
692            decoder.decode(
693                ints2octs((16, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
694            )
695        except PyAsn1Error:
696            pass
697        else:
698            assert 0, 'wrong tagFormat worked out'
699
700
701class SequenceDecoderWithSchemaTestCase(BaseTestCase):
702    def setUp(self):
703        BaseTestCase.setUp(self)
704        self.s = univ.Sequence(
705            componentType=namedtype.NamedTypes(
706                namedtype.NamedType('place-holder', univ.Null(null)),
707                namedtype.OptionalNamedType('first-name', univ.OctetString()),
708                namedtype.DefaultedNamedType('age', univ.Integer(33)),
709            )
710        )
711
712    def __init(self):
713        self.s.clear()
714        self.s.setComponentByPosition(0, univ.Null(null))
715
716    def __initWithOptional(self):
717        self.s.clear()
718        self.s.setComponentByPosition(0, univ.Null(null))
719        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
720
721    def __initWithDefaulted(self):
722        self.s.clear()
723        self.s.setComponentByPosition(0, univ.Null(null))
724        self.s.setComponentByPosition(2, univ.Integer(1))
725
726    def __initWithOptionalAndDefaulted(self):
727        self.s.clear()
728        self.s.setComponentByPosition(0, univ.Null(null))
729        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
730        self.s.setComponentByPosition(2, univ.Integer(1))
731
732    def testDefMode(self):
733        self.__init()
734        assert decoder.decode(
735            ints2octs((48, 2, 5, 0)), asn1Spec=self.s
736        ) == (self.s, null)
737
738    def testIndefMode(self):
739        self.__init()
740        assert decoder.decode(
741            ints2octs((48, 128, 5, 0, 0, 0)), asn1Spec=self.s
742        ) == (self.s, null)
743
744    def testDefModeChunked(self):
745        self.__init()
746        assert decoder.decode(
747            ints2octs((48, 2, 5, 0)), asn1Spec=self.s
748        ) == (self.s, null)
749
750    def testIndefModeChunked(self):
751        self.__init()
752        assert decoder.decode(
753            ints2octs((48, 128, 5, 0, 0, 0)), asn1Spec=self.s
754        ) == (self.s, null)
755
756    def testWithOptionalDefMode(self):
757        self.__initWithOptional()
758        assert decoder.decode(
759            ints2octs((48, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
760        ) == (self.s, null)
761
762    def testWithOptionaIndefMode(self):
763        self.__initWithOptional()
764        assert decoder.decode(
765            ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 0, 0)),
766            asn1Spec=self.s
767        ) == (self.s, null)
768
769    def testWithOptionalDefModeChunked(self):
770        self.__initWithOptional()
771        assert decoder.decode(
772            ints2octs((48, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)),
773            asn1Spec=self.s
774        ) == (self.s, null)
775
776    def testWithOptionalIndefModeChunked(self):
777        self.__initWithOptional()
778        assert decoder.decode(
779            ints2octs((48, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0,
780                       0, 0, 0)),
781            asn1Spec=self.s
782        ) == (self.s, null)
783
784    def testWithDefaultedDefMode(self):
785        self.__initWithDefaulted()
786        assert decoder.decode(
787            ints2octs((48, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
788        ) == (self.s, null)
789
790    def testWithDefaultedIndefMode(self):
791        self.__initWithDefaulted()
792        assert decoder.decode(
793            ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
794        ) == (self.s, null)
795
796    def testWithDefaultedDefModeChunked(self):
797        self.__initWithDefaulted()
798        assert decoder.decode(
799            ints2octs((48, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
800        ) == (self.s, null)
801
802    def testWithDefaultedIndefModeChunked(self):
803        self.__initWithDefaulted()
804        assert decoder.decode(
805            ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
806        ) == (self.s, null)
807
808    def testWithOptionalAndDefaultedDefMode(self):
809        self.__initWithOptionalAndDefaulted()
810        assert decoder.decode(
811            ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
812            asn1Spec=self.s
813        ) == (self.s, null)
814
815    def testWithOptionalAndDefaultedIndefMode(self):
816        self.__initWithOptionalAndDefaulted()
817        assert decoder.decode(
818            ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1,
819                       0, 0)), asn1Spec=self.s
820        ) == (self.s, null)
821
822    def testWithOptionalAndDefaultedDefModeChunked(self):
823        self.__initWithOptionalAndDefaulted()
824        assert decoder.decode(
825            ints2octs(
826                (48, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1)),
827            asn1Spec=self.s
828        ) == (self.s, null)
829
830    def testWithOptionalAndDefaultedIndefModeChunked(self):
831        self.__initWithOptionalAndDefaulted()
832        assert decoder.decode(
833            ints2octs((48, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0,
834                       0, 2, 1, 1, 0, 0)), asn1Spec=self.s
835        ) == (self.s, null)
836
837
838class SequenceDecoderWithUntaggedOpenTypesTestCase(BaseTestCase):
839    def setUp(self):
840        openType = opentype.OpenType(
841            'id',
842            {1: univ.Integer(),
843             2: univ.OctetString()}
844        )
845        self.s = univ.Sequence(
846            componentType=namedtype.NamedTypes(
847                namedtype.NamedType('id', univ.Integer()),
848                namedtype.NamedType('blob', univ.Any(), openType=openType)
849            )
850        )
851
852    def testDecodeOpenTypesChoiceOne(self):
853        s, r = decoder.decode(
854            ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s,
855            decodeOpenTypes=True
856        )
857        assert not r
858        assert s[0] == 1
859        assert s[1] == 12
860
861    def testDecodeOpenTypesChoiceTwo(self):
862        s, r = decoder.decode(
863            ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
864            decodeOpenTypes=True
865        )
866        assert not r
867        assert s[0] == 2
868        assert s[1] == univ.OctetString('quick brown')
869
870    def testDecodeOpenTypesUnknownType(self):
871        try:
872            s, r = decoder.decode(
873                ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
874                decodeOpenTypes=True
875            )
876
877        except PyAsn1Error:
878            pass
879
880        else:
881            assert False, 'unknown open type tolerated'
882
883    def testDecodeOpenTypesUnknownId(self):
884        s, r = decoder.decode(
885            ints2octs((48, 6, 2, 1, 3, 6, 1, 39)), asn1Spec=self.s,
886            decodeOpenTypes=True
887        )
888        assert not r
889        assert s[0] == 3
890        assert s[1] == univ.OctetString(hexValue='060127')
891
892    def testDontDecodeOpenTypesChoiceOne(self):
893        s, r = decoder.decode(
894            ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s
895        )
896        assert not r
897        assert s[0] == 1
898        assert s[1] == ints2octs((2, 1, 12))
899
900    def testDontDecodeOpenTypesChoiceTwo(self):
901        s, r = decoder.decode(
902            ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
903        )
904        assert not r
905        assert s[0] == 2
906        assert s[1] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
907
908
909class SequenceDecoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
910    def setUp(self):
911        openType = opentype.OpenType(
912            'id',
913            {1: univ.Integer(),
914             2: univ.OctetString()}
915        )
916        self.s = univ.Sequence(
917            componentType=namedtype.NamedTypes(
918                namedtype.NamedType('id', univ.Integer()),
919                namedtype.NamedType(
920                    'blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
921                )
922            )
923        )
924
925    def testDecodeOpenTypesChoiceOne(self):
926        s, r = decoder.decode(
927            ints2octs((48, 8, 2, 1, 1, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
928        )
929        assert not r
930        assert s[0] == 1
931        assert s[1] == 12
932
933    def testDecodeOpenTypesUnknownId(self):
934        s, r = decoder.decode(
935            ints2octs((48, 8, 2, 1, 3, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
936        )
937        assert not r
938        assert s[0] == 3
939        assert s[1] == univ.OctetString(hexValue='02010C')
940
941
942class SequenceDecoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
943    def setUp(self):
944        openType = opentype.OpenType(
945            'id',
946            {1: univ.Integer(),
947             2: univ.OctetString()}
948        )
949        self.s = univ.Sequence(
950            componentType=namedtype.NamedTypes(
951                namedtype.NamedType('id', univ.Integer()),
952                namedtype.NamedType(
953                    'blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
954                )
955            )
956        )
957
958    def testDecodeOpenTypesChoiceOne(self):
959        s, r = decoder.decode(
960            ints2octs((48, 8, 2, 1, 1, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
961        )
962        assert not r
963        assert s[0] == 1
964        assert s[1] == 12
965
966    def testDecodeOpenTypesUnknownId(self):
967        s, r = decoder.decode(
968            ints2octs((48, 8, 2, 1, 3, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
969        )
970        assert not r
971        assert s[0] == 3
972        assert s[1] == univ.OctetString(hexValue='02010C')
973
974
975class SequenceDecoderWithUnaggedSetOfOpenTypesTestCase(BaseTestCase):
976    def setUp(self):
977        openType = opentype.OpenType(
978            'id',
979            {1: univ.Integer(),
980             2: univ.OctetString()}
981        )
982        self.s = univ.Sequence(
983            componentType=namedtype.NamedTypes(
984                namedtype.NamedType('id', univ.Integer()),
985                namedtype.NamedType('blob', univ.SetOf(componentType=univ.Any()),
986                                    openType=openType)
987            )
988        )
989
990    def testDecodeOpenTypesChoiceOne(self):
991        s, r = decoder.decode(
992            ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s,
993            decodeOpenTypes=True
994        )
995        assert not r
996        assert s[0] == 1
997        assert s[1][0] == 12
998
999    def testDecodeOpenTypesChoiceTwo(self):
1000        s, r = decoder.decode(
1001            ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99,
1002                       107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
1003            decodeOpenTypes=True
1004        )
1005        assert not r
1006        assert s[0] == 2
1007        assert s[1][0] == univ.OctetString('quick brown')
1008
1009    def testDecodeOpenTypesUnknownType(self):
1010        try:
1011            s, r = decoder.decode(
1012                ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
1013                decodeOpenTypes=True
1014            )
1015
1016        except PyAsn1Error:
1017            pass
1018
1019        else:
1020            assert False, 'unknown open type tolerated'
1021
1022    def testDecodeOpenTypesUnknownId(self):
1023        s, r = decoder.decode(
1024            ints2octs((48, 8, 2, 1, 3, 49, 3, 2, 1, 12)), asn1Spec=self.s,
1025            decodeOpenTypes=True
1026        )
1027        assert not r
1028        assert s[0] == 3
1029        assert s[1][0] == univ.OctetString(hexValue='02010c')
1030
1031    def testDontDecodeOpenTypesChoiceOne(self):
1032        s, r = decoder.decode(
1033            ints2octs((48, 8, 2, 1, 1, 49, 3, 2, 1, 12)), asn1Spec=self.s
1034        )
1035        assert not r
1036        assert s[0] == 1
1037        assert s[1][0] == ints2octs((2, 1, 12))
1038
1039    def testDontDecodeOpenTypesChoiceTwo(self):
1040        s, r = decoder.decode(
1041            ints2octs((48, 18, 2, 1, 2, 49, 13, 4, 11, 113, 117, 105, 99,
1042                       107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
1043        )
1044        assert not r
1045        assert s[0] == 2
1046        assert s[1][0] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114,
1047                                     111, 119, 110))
1048
1049
1050class SequenceDecoderWithImplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
1051    def setUp(self):
1052        openType = opentype.OpenType(
1053            'id',
1054            {1: univ.Integer(),
1055             2: univ.OctetString()}
1056        )
1057        self.s = univ.Sequence(
1058            componentType=namedtype.NamedTypes(
1059                namedtype.NamedType('id', univ.Integer()),
1060                namedtype.NamedType(
1061                    'blob', univ.SetOf(
1062                        componentType=univ.Any().subtype(
1063                            implicitTag=tag.Tag(
1064                                tag.tagClassContext, tag.tagFormatSimple, 3))),
1065                    openType=openType
1066                )
1067            )
1068        )
1069
1070    def testDecodeOpenTypesChoiceOne(self):
1071        s, r = decoder.decode(
1072            ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)),
1073            asn1Spec=self.s, decodeOpenTypes=True
1074        )
1075        assert not r
1076        assert s[0] == 1
1077        assert s[1][0] == 12
1078
1079    def testDecodeOpenTypesUnknownId(self):
1080        s, r = decoder.decode(
1081            ints2octs((48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)),
1082            asn1Spec=self.s, decodeOpenTypes=True
1083        )
1084        assert not r
1085        assert s[0] == 3
1086        assert s[1][0] == univ.OctetString(hexValue='02010C')
1087
1088
1089class SequenceDecoderWithExplicitlyTaggedSetOfOpenTypesTestCase(BaseTestCase):
1090    def setUp(self):
1091        openType = opentype.OpenType(
1092            'id',
1093            {1: univ.Integer(),
1094             2: univ.OctetString()}
1095        )
1096        self.s = univ.Sequence(
1097            componentType=namedtype.NamedTypes(
1098                namedtype.NamedType('id', univ.Integer()),
1099                namedtype.NamedType(
1100                    'blob', univ.SetOf(
1101                        componentType=univ.Any().subtype(
1102                            explicitTag=tag.Tag(
1103                                tag.tagClassContext, tag.tagFormatSimple, 3))),
1104                    openType=openType
1105                )
1106            )
1107        )
1108
1109    def testDecodeOpenTypesChoiceOne(self):
1110        s, r = decoder.decode(
1111            ints2octs((48, 10, 2, 1, 1, 49, 5, 131, 3, 2, 1, 12)),
1112            asn1Spec=self.s, decodeOpenTypes=True
1113        )
1114        assert not r
1115        assert s[0] == 1
1116        assert s[1][0] == 12
1117
1118    def testDecodeOpenTypesUnknownId(self):
1119        s, r = decoder.decode(
1120            ints2octs( (48, 10, 2, 1, 3, 49, 5, 131, 3, 2, 1, 12)),
1121            asn1Spec=self.s, decodeOpenTypes=True
1122        )
1123        assert not r
1124        assert s[0] == 3
1125        assert s[1][0] == univ.OctetString(hexValue='02010C')
1126
1127
1128class SetDecoderTestCase(BaseTestCase):
1129    def setUp(self):
1130        BaseTestCase.setUp(self)
1131        self.s = univ.Set(
1132            componentType=namedtype.NamedTypes(
1133                namedtype.NamedType('place-holder', univ.Null(null)),
1134                namedtype.NamedType('first-name', univ.OctetString(null)),
1135                namedtype.NamedType('age', univ.Integer(33))
1136            )
1137        )
1138        self.s.setComponentByPosition(0, univ.Null(null))
1139        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
1140        self.s.setComponentByPosition(2, univ.Integer(1))
1141
1142    def testWithOptionalAndDefaultedDefMode(self):
1143        assert decoder.decode(
1144            ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
1145        ) == (self.s, null)
1146
1147    def testWithOptionalAndDefaultedIndefMode(self):
1148        assert decoder.decode(
1149            ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))
1150        ) == (self.s, null)
1151
1152    def testWithOptionalAndDefaultedDefModeChunked(self):
1153        assert decoder.decode(
1154            ints2octs(
1155                (49, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1))
1156        ) == (self.s, null)
1157
1158    def testWithOptionalAndDefaultedIndefModeChunked(self):
1159        assert decoder.decode(
1160            ints2octs((49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0))
1161        ) == (self.s, null)
1162
1163    def testWithOptionalAndDefaultedDefModeSubst(self):
1164        assert decoder.decode(
1165            ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)),
1166            substrateFun=lambda a, b, c: (b, b[c:])
1167        ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), str2octs(''))
1168
1169    def testWithOptionalAndDefaultedIndefModeSubst(self):
1170        assert decoder.decode(
1171            ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)),
1172            substrateFun=lambda a, b, c: (b, str2octs(''))
1173        ) == (ints2octs(
1174            (5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), str2octs(''))
1175
1176    def testTagFormat(self):
1177        try:
1178            decoder.decode(
1179                ints2octs((16, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
1180            )
1181        except PyAsn1Error:
1182            pass
1183        else:
1184            assert 0, 'wrong tagFormat worked out'
1185
1186
1187class SetDecoderWithSchemaTestCase(BaseTestCase):
1188    def setUp(self):
1189        BaseTestCase.setUp(self)
1190        self.s = univ.Set(
1191            componentType=namedtype.NamedTypes(
1192                namedtype.NamedType('place-holder', univ.Null(null)),
1193                namedtype.OptionalNamedType('first-name', univ.OctetString()),
1194                namedtype.DefaultedNamedType('age', univ.Integer(33)),
1195            )
1196        )
1197
1198    def __init(self):
1199        self.s.clear()
1200        self.s.setComponentByPosition(0, univ.Null(null))
1201
1202    def __initWithOptional(self):
1203        self.s.clear()
1204        self.s.setComponentByPosition(0, univ.Null(null))
1205        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
1206
1207    def __initWithDefaulted(self):
1208        self.s.clear()
1209        self.s.setComponentByPosition(0, univ.Null(null))
1210        self.s.setComponentByPosition(2, univ.Integer(1))
1211
1212    def __initWithOptionalAndDefaulted(self):
1213        self.s.clear()
1214        self.s.setComponentByPosition(0, univ.Null(null))
1215        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
1216        self.s.setComponentByPosition(2, univ.Integer(1))
1217
1218    def testDefMode(self):
1219        self.__init()
1220        assert decoder.decode(
1221            ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
1222        ) == (self.s, null)
1223
1224    def testIndefMode(self):
1225        self.__init()
1226        assert decoder.decode(
1227            ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
1228        ) == (self.s, null)
1229
1230    def testDefModeChunked(self):
1231        self.__init()
1232        assert decoder.decode(
1233            ints2octs((49, 2, 5, 0)), asn1Spec=self.s
1234        ) == (self.s, null)
1235
1236    def testIndefModeChunked(self):
1237        self.__init()
1238        assert decoder.decode(
1239            ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s
1240        ) == (self.s, null)
1241
1242    def testWithOptionalDefMode(self):
1243        self.__initWithOptional()
1244        assert decoder.decode(
1245            ints2octs((49, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s
1246        ) == (self.s, null)
1247
1248    def testWithOptionalIndefMode(self):
1249        self.__initWithOptional()
1250        assert decoder.decode(
1251            ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
1252        ) == (self.s, null)
1253
1254    def testWithOptionalDefModeChunked(self):
1255        self.__initWithOptional()
1256        assert decoder.decode(
1257            ints2octs((49, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s
1258        ) == (self.s, null)
1259
1260    def testWithOptionalIndefModeChunked(self):
1261        self.__initWithOptional()
1262        assert decoder.decode(
1263            ints2octs((49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s
1264        ) == (self.s, null)
1265
1266    def testWithDefaultedDefMode(self):
1267        self.__initWithDefaulted()
1268        assert decoder.decode(
1269            ints2octs((49, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
1270        ) == (self.s, null)
1271
1272    def testWithDefaultedIndefMode(self):
1273        self.__initWithDefaulted()
1274        assert decoder.decode(
1275            ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
1276        ) == (self.s, null)
1277
1278    def testWithDefaultedDefModeChunked(self):
1279        self.__initWithDefaulted()
1280        assert decoder.decode(
1281            ints2octs((49, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s
1282        ) == (self.s, null)
1283
1284    def testWithDefaultedIndefModeChunked(self):
1285        self.__initWithDefaulted()
1286        assert decoder.decode(
1287            ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
1288        ) == (self.s, null)
1289
1290    def testWithOptionalAndDefaultedDefMode(self):
1291        self.__initWithOptionalAndDefaulted()
1292        assert decoder.decode(
1293            ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), asn1Spec=self.s
1294        ) == (self.s, null)
1295
1296    def testWithOptionalAndDefaultedDefModeReordered(self):
1297        self.__initWithOptionalAndDefaulted()
1298        assert decoder.decode(
1299            ints2octs((49, 18, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0)), asn1Spec=self.s
1300        ) == (self.s, null)
1301
1302    def testWithOptionalAndDefaultedIndefMode(self):
1303        self.__initWithOptionalAndDefaulted()
1304        assert decoder.decode(
1305            ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
1306        ) == (self.s, null)
1307
1308    def testWithOptionalAndDefaultedIndefModeReordered(self):
1309        self.__initWithOptionalAndDefaulted()
1310        assert decoder.decode(
1311            ints2octs((49, 128, 2, 1, 1, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0,  0, 0)), asn1Spec=self.s
1312        ) == (self.s, null)
1313
1314    def testWithOptionalAndDefaultedDefModeChunked(self):
1315        self.__initWithOptionalAndDefaulted()
1316        assert decoder.decode(
1317            ints2octs((49, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1)), asn1Spec=self.s
1318        ) == (self.s, null)
1319
1320    def testWithOptionalAndDefaultedIndefModeChunked(self):
1321        self.__initWithOptionalAndDefaulted()
1322        assert decoder.decode(
1323            ints2octs((49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s
1324        ) == (self.s, null)
1325
1326
1327class SequenceOfWithExpTaggedOctetStringDecoder(BaseTestCase):
1328    def setUp(self):
1329        BaseTestCase.setUp(self)
1330        self.s = univ.SequenceOf(
1331            componentType=univ.OctetString().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))
1332        )
1333        self.s.setComponentByPosition(0, 'q')
1334        self.s2 = univ.SequenceOf()
1335
1336    def testDefModeSchema(self):
1337        s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s)
1338        assert not r
1339        assert s == self.s
1340        assert s.tagSet == self.s.tagSet
1341
1342    def testIndefModeSchema(self):
1343        s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s)
1344        assert not r
1345        assert s == self.s
1346        assert s.tagSet == self.s.tagSet
1347
1348    def testDefModeNoComponent(self):
1349        s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s2)
1350        assert not r
1351        assert s == self.s
1352        assert s.tagSet == self.s.tagSet
1353
1354    def testIndefModeNoComponent(self):
1355        s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s2)
1356        assert not r
1357        assert s == self.s
1358        assert s.tagSet == self.s.tagSet
1359
1360    def testDefModeSchemaless(self):
1361        s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)))
1362        assert not r
1363        assert s == self.s
1364        assert s.tagSet == self.s.tagSet
1365
1366    def testIndefModeSchemaless(self):
1367        s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)))
1368        assert not r
1369        assert s == self.s
1370        assert s.tagSet == self.s.tagSet
1371
1372
1373class SequenceWithExpTaggedOctetStringDecoder(BaseTestCase):
1374    def setUp(self):
1375        BaseTestCase.setUp(self)
1376        self.s = univ.Sequence(
1377            componentType=namedtype.NamedTypes(
1378                namedtype.NamedType(
1379                    'x', univ.OctetString().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3))
1380                )
1381            )
1382        )
1383        self.s.setComponentByPosition(0, 'q')
1384        self.s2 = univ.Sequence()
1385
1386    def testDefModeSchema(self):
1387        s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s)
1388        assert not r
1389        assert s == self.s
1390        assert s.tagSet == self.s.tagSet
1391
1392    def testIndefModeSchema(self):
1393        s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s)
1394        assert not r
1395        assert s == self.s
1396        assert s.tagSet == self.s.tagSet
1397
1398    def testDefModeNoComponent(self):
1399        s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)), asn1Spec=self.s2)
1400        assert not r
1401        assert s == self.s
1402        assert s.tagSet == self.s.tagSet
1403
1404    def testIndefModeNoComponent(self):
1405        s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)), asn1Spec=self.s2)
1406        assert not r
1407        assert s == self.s
1408        assert s.tagSet == self.s.tagSet
1409
1410    def testDefModeSchemaless(self):
1411        s, r = decoder.decode(ints2octs((48, 5, 163, 3, 4, 1, 113)))
1412        assert not r
1413        assert s == self.s
1414        assert s.tagSet == self.s.tagSet
1415
1416    def testIndefModeSchemaless(self):
1417        s, r = decoder.decode(ints2octs((48, 128, 163, 128, 4, 1, 113, 0, 0, 0, 0)))
1418        assert not r
1419        assert s == self.s
1420        assert s.tagSet == self.s.tagSet
1421
1422
1423class ChoiceDecoderTestCase(BaseTestCase):
1424    def setUp(self):
1425        BaseTestCase.setUp(self)
1426        self.s = univ.Choice(
1427            componentType=namedtype.NamedTypes(
1428                namedtype.NamedType('place-holder', univ.Null(null)),
1429                namedtype.NamedType('number', univ.Integer(0)),
1430                namedtype.NamedType('string', univ.OctetString())
1431            )
1432        )
1433
1434    def testBySpec(self):
1435        self.s.setComponentByPosition(0, univ.Null(null))
1436        assert decoder.decode(
1437            ints2octs((5, 0)), asn1Spec=self.s
1438        ) == (self.s, null)
1439
1440    def testWithoutSpec(self):
1441        self.s.setComponentByPosition(0, univ.Null(null))
1442        assert decoder.decode(ints2octs((5, 0))) == (self.s, null)
1443        assert decoder.decode(ints2octs((5, 0))) == (univ.Null(null), null)
1444
1445    def testUndefLength(self):
1446        self.s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
1447        assert decoder.decode(ints2octs((36, 128, 4, 3, 97, 98, 99, 4, 3, 100, 101, 102, 4, 2, 103, 104, 0, 0)),
1448                              asn1Spec=self.s) == (self.s, null)
1449
1450    def testExplicitTag(self):
1451        s = self.s.subtype(explicitTag=tag.Tag(tag.tagClassContext,
1452                                               tag.tagFormatConstructed, 4))
1453        s.setComponentByPosition(0, univ.Null(null))
1454        assert decoder.decode(ints2octs((164, 2, 5, 0)), asn1Spec=s) == (s, null)
1455
1456    def testExplicitTagUndefLength(self):
1457        s = self.s.subtype(explicitTag=tag.Tag(tag.tagClassContext,
1458                                               tag.tagFormatConstructed, 4))
1459        s.setComponentByPosition(0, univ.Null(null))
1460        assert decoder.decode(ints2octs((164, 128, 5, 0, 0, 0)), asn1Spec=s) == (s, null)
1461
1462
1463class AnyDecoderTestCase(BaseTestCase):
1464    def setUp(self):
1465        BaseTestCase.setUp(self)
1466        self.s = univ.Any()
1467
1468    def testByUntagged(self):
1469        assert decoder.decode(
1470            ints2octs((4, 3, 102, 111, 120)), asn1Spec=self.s
1471        ) == (univ.Any('\004\003fox'), null)
1472
1473    def testTaggedEx(self):
1474        s = univ.Any('\004\003fox').subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
1475        assert decoder.decode(ints2octs((164, 5, 4, 3, 102, 111, 120)), asn1Spec=s) == (s, null)
1476
1477    def testTaggedIm(self):
1478        s = univ.Any('\004\003fox').subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
1479        assert decoder.decode(ints2octs((132, 5, 4, 3, 102, 111, 120)), asn1Spec=s) == (s, null)
1480
1481    def testByUntaggedIndefMode(self):
1482        assert decoder.decode(
1483            ints2octs((4, 3, 102, 111, 120)), asn1Spec=self.s
1484        ) == (univ.Any('\004\003fox'), null)
1485
1486    def testTaggedExIndefMode(self):
1487        s = univ.Any('\004\003fox').subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
1488        assert decoder.decode(ints2octs((164, 128, 4, 3, 102, 111, 120, 0, 0)), asn1Spec=s) == (s, null)
1489
1490    def testTaggedImIndefMode(self):
1491        s = univ.Any('\004\003fox').subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4))
1492        assert decoder.decode(ints2octs((164, 128, 4, 3, 102, 111, 120, 0, 0)), asn1Spec=s) == (s, null)
1493
1494    def testByUntaggedSubst(self):
1495        assert decoder.decode(
1496            ints2octs((4, 3, 102, 111, 120)),
1497            asn1Spec=self.s,
1498            substrateFun=lambda a, b, c: (b, b[c:])
1499        ) == (ints2octs((4, 3, 102, 111, 120)), str2octs(''))
1500
1501    def testTaggedExSubst(self):
1502        assert decoder.decode(
1503            ints2octs((164, 5, 4, 3, 102, 111, 120)),
1504            asn1Spec=self.s,
1505            substrateFun=lambda a, b, c: (b, b[c:])
1506        ) == (ints2octs((164, 5, 4, 3, 102, 111, 120)), str2octs(''))
1507
1508
1509class EndOfOctetsTestCase(BaseTestCase):
1510    def testUnexpectedEoo(self):
1511        try:
1512            decoder.decode(ints2octs((0, 0)))
1513        except PyAsn1Error:
1514            pass
1515        else:
1516            assert 0, 'end-of-contents octets accepted at top level'
1517
1518    def testExpectedEoo(self):
1519        result, remainder = decoder.decode(ints2octs((0, 0)), allowEoo=True)
1520        assert eoo.endOfOctets.isSameTypeWith(result) and result == eoo.endOfOctets and result is eoo.endOfOctets
1521        assert remainder == null
1522
1523    def testDefiniteNoEoo(self):
1524        try:
1525            decoder.decode(ints2octs((0x23, 0x02, 0x00, 0x00)))
1526        except PyAsn1Error:
1527            pass
1528        else:
1529            assert 0, 'end-of-contents octets accepted inside definite-length encoding'
1530
1531    def testIndefiniteEoo(self):
1532        result, remainder = decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x00)))
1533        assert result == () and remainder == null, 'incorrect decoding of indefinite length end-of-octets'
1534
1535    def testNoLongFormEoo(self):
1536        try:
1537            decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x81, 0x00)))
1538        except PyAsn1Error:
1539            pass
1540        else:
1541            assert 0, 'end-of-contents octets accepted with invalid long-form length'
1542
1543    def testNoConstructedEoo(self):
1544        try:
1545            decoder.decode(ints2octs((0x23, 0x80, 0x20, 0x00)))
1546        except PyAsn1Error:
1547            pass
1548        else:
1549            assert 0, 'end-of-contents octets accepted with invalid constructed encoding'
1550
1551    def testNoEooData(self):
1552        try:
1553            decoder.decode(ints2octs((0x23, 0x80, 0x00, 0x01, 0x00)))
1554        except PyAsn1Error:
1555            pass
1556        else:
1557            assert 0, 'end-of-contents octets accepted with unexpected data'
1558
1559
1560class NonStringDecoderTestCase(BaseTestCase):
1561    def setUp(self):
1562        BaseTestCase.setUp(self)
1563        self.s = univ.Sequence(
1564            componentType=namedtype.NamedTypes(
1565                namedtype.NamedType('place-holder', univ.Null(null)),
1566                namedtype.NamedType('first-name', univ.OctetString(null)),
1567                namedtype.NamedType('age', univ.Integer(33))
1568            )
1569        )
1570        self.s.setComponentByPosition(0, univ.Null(null))
1571        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
1572        self.s.setComponentByPosition(2, univ.Integer(1))
1573
1574        self.substrate = ints2octs([48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1])
1575
1576    def testOctetString(self):
1577        s, _ = decoder.decode(univ.OctetString(self.substrate), asn1Spec=self.s)
1578        assert self.s == s
1579
1580    def testAny(self):
1581        s, _ = decoder.decode(univ.Any(self.substrate), asn1Spec=self.s)
1582        assert self.s == s
1583
1584
1585class ErrorOnDecodingTestCase(BaseTestCase):
1586
1587    def testErrorCondition(self):
1588        decode = decoder.Decoder(decoder.tagMap, decoder.typeMap)
1589
1590        try:
1591            asn1Object, rest = decode(str2octs('abc'))
1592
1593        except PyAsn1Error:
1594            exc = sys.exc_info()[1]
1595            assert isinstance(exc, PyAsn1Error), (
1596                'Unexpected exception raised %r' % (exc,))
1597
1598        else:
1599            assert False, 'Unexpected decoder result %r' % (asn1Object,)
1600
1601    def testRawDump(self):
1602        decode = decoder.Decoder(decoder.tagMap, decoder.typeMap)
1603
1604        decode.defaultErrorState = decoder.stDumpRawValue
1605
1606        asn1Object, rest = decode(ints2octs(
1607            (31, 8, 2, 1, 1, 131, 3, 2, 1, 12)))
1608
1609        assert isinstance(asn1Object, univ.Any), (
1610            'Unexpected raw dump type %r' % (asn1Object,))
1611        assert asn1Object.asNumbers() == (31, 8, 2, 1, 1), (
1612            'Unexpected raw dump value %r' % (asn1Object,))
1613        assert rest == ints2octs((131, 3, 2, 1, 12)), (
1614            'Unexpected rest of substrate after raw dump %r' % rest)
1615
1616
1617suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
1618
1619if __name__ == '__main__':
1620    unittest.TextTestRunner(verbosity=2).run(suite)
1621