• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# This file is part of pyasn1 software.
3#
4# Copyright (c) 2005-2018, Ilya Etingof <etingof@gmail.com>
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 encoder
23from pyasn1.compat.octets import ints2octs
24from pyasn1.error import PyAsn1Error
25
26
27class LargeTagEncoderTestCase(BaseTestCase):
28    def setUp(self):
29        BaseTestCase.setUp(self)
30
31        self.o = univ.Integer().subtype(
32            value=1, explicitTag=tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 0xdeadbeaf)
33        )
34
35    def testEncoder(self):
36        assert encoder.encode(self.o) == ints2octs((127, 141, 245, 182, 253, 47, 3, 2, 1, 1))
37
38
39class IntegerEncoderTestCase(BaseTestCase):
40    def testPosInt(self):
41        assert encoder.encode(univ.Integer(12)) == ints2octs((2, 1, 12))
42
43    def testNegInt(self):
44        assert encoder.encode(univ.Integer(-12)) == ints2octs((2, 1, 244))
45
46    def testZero(self):
47        assert encoder.encode(univ.Integer(0)) == ints2octs((2, 1, 0))
48
49    def testCompactZero(self):
50        encoder.IntegerEncoder.supportCompactZero = True
51        substrate = encoder.encode(univ.Integer(0))
52        encoder.IntegerEncoder.supportCompactZero = False
53        assert substrate == ints2octs((2, 0))
54
55    def testMinusOne(self):
56        assert encoder.encode(univ.Integer(-1)) == ints2octs((2, 1, 255))
57
58    def testPosLong(self):
59        assert encoder.encode(
60            univ.Integer(0xffffffffffffffff)
61        ) == ints2octs((2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255))
62
63    def testNegLong(self):
64        assert encoder.encode(
65            univ.Integer(-0xffffffffffffffff)
66        ) == ints2octs((2, 9, 255, 0, 0, 0, 0, 0, 0, 0, 1))
67
68
69class IntegerEncoderWithSchemaTestCase(BaseTestCase):
70    def testPosInt(self):
71        assert encoder.encode(12, asn1Spec=univ.Integer()) == ints2octs((2, 1, 12))
72
73    def testNegInt(self):
74        assert encoder.encode(-12, asn1Spec=univ.Integer()) == ints2octs((2, 1, 244))
75
76    def testZero(self):
77        assert encoder.encode(0, asn1Spec=univ.Integer()) == ints2octs((2, 1, 0))
78
79    def testPosLong(self):
80        assert encoder.encode(
81            0xffffffffffffffff, asn1Spec=univ.Integer()
82        ) == ints2octs((2, 9, 0, 255, 255, 255, 255, 255, 255, 255, 255))
83
84
85class BooleanEncoderTestCase(BaseTestCase):
86    def testTrue(self):
87        assert encoder.encode(univ.Boolean(1)) == ints2octs((1, 1, 1))
88
89    def testFalse(self):
90        assert encoder.encode(univ.Boolean(0)) == ints2octs((1, 1, 0))
91
92
93class BooleanEncoderWithSchemaTestCase(BaseTestCase):
94    def testTrue(self):
95        assert encoder.encode(True, asn1Spec=univ.Boolean()) == ints2octs((1, 1, 1))
96
97    def testFalse(self):
98        assert encoder.encode(False, asn1Spec=univ.Boolean()) == ints2octs((1, 1, 0))
99
100
101class BitStringEncoderTestCase(BaseTestCase):
102    def setUp(self):
103        BaseTestCase.setUp(self)
104        self.b = univ.BitString((1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1))
105
106    def testDefMode(self):
107        assert encoder.encode(self.b) == ints2octs((3, 3, 1, 169, 138))
108
109    def testIndefMode(self):
110        assert encoder.encode(
111            self.b, defMode=False
112        ) == ints2octs((3, 3, 1, 169, 138))
113
114    def testDefModeChunked(self):
115        assert encoder.encode(
116            self.b, maxChunkSize=1
117        ) == ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138))
118
119    def testIndefModeChunked(self):
120        assert encoder.encode(
121            self.b, defMode=False, maxChunkSize=1
122        ) == ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
123
124    def testEmptyValue(self):
125        assert encoder.encode(univ.BitString([])) == ints2octs((3, 1, 0))
126
127
128class BitStringEncoderWithSchemaTestCase(BaseTestCase):
129    def setUp(self):
130        BaseTestCase.setUp(self)
131        self.b = (1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1)
132        self.s = univ.BitString()
133
134    def testDefMode(self):
135        assert encoder.encode(self.b, asn1Spec=self.s) == ints2octs((3, 3, 1, 169, 138))
136
137    def testIndefMode(self):
138        assert encoder.encode(
139            self.b, asn1Spec=self.s, defMode=False
140        ) == ints2octs((3, 3, 1, 169, 138))
141
142    def testDefModeChunked(self):
143        assert encoder.encode(
144            self.b, asn1Spec=self.s, maxChunkSize=1
145        ) == ints2octs((35, 8, 3, 2, 0, 169, 3, 2, 1, 138))
146
147    def testIndefModeChunked(self):
148        assert encoder.encode(
149            self.b, asn1Spec=self.s, defMode=False, maxChunkSize=1
150        ) == ints2octs((35, 128, 3, 2, 0, 169, 3, 2, 1, 138, 0, 0))
151
152    def testEmptyValue(self):
153        assert encoder.encode([],  asn1Spec=self.s) == ints2octs((3, 1, 0))
154
155
156class OctetStringEncoderTestCase(BaseTestCase):
157    def setUp(self):
158        BaseTestCase.setUp(self)
159        self.o = univ.OctetString('Quick brown fox')
160
161    def testDefMode(self):
162        assert encoder.encode(self.o) == ints2octs(
163            (4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
164
165    def testIndefMode(self):
166        assert encoder.encode(
167            self.o, defMode=False
168        ) == ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
169
170    def testDefModeChunked(self):
171        assert encoder.encode(
172            self.o, maxChunkSize=4
173        ) == ints2octs((36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119,
174                        110, 32, 4, 3, 102, 111, 120))
175
176    def testIndefModeChunked(self):
177        assert encoder.encode(
178            self.o, defMode=False, maxChunkSize=4
179        ) == ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110,
180                        32, 4, 3, 102,  111, 120, 0, 0))
181
182
183class OctetStringEncoderWithSchemaTestCase(BaseTestCase):
184    def setUp(self):
185        BaseTestCase.setUp(self)
186        self.s = univ.OctetString()
187        self.o = 'Quick brown fox'
188
189    def testDefMode(self):
190        assert encoder.encode(self.o, asn1Spec=self.s) == ints2octs(
191            (4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
192
193    def testIndefMode(self):
194        assert encoder.encode(
195            self.o, asn1Spec=self.s, defMode=False
196        ) == ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
197
198    def testDefModeChunked(self):
199        assert encoder.encode(
200            self.o, asn1Spec=self.s, maxChunkSize=4
201        ) == ints2octs((36, 23, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119,
202                        110, 32, 4, 3, 102, 111, 120))
203
204    def testIndefModeChunked(self):
205        assert encoder.encode(
206            self.o, asn1Spec=self.s, defMode=False, maxChunkSize=4
207        ) == ints2octs((36, 128, 4, 4, 81, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 4, 111, 119, 110,
208                        32, 4, 3, 102,  111, 120, 0, 0))
209
210
211class ExpTaggedOctetStringEncoderTestCase(BaseTestCase):
212    def setUp(self):
213        BaseTestCase.setUp(self)
214        self.o = univ.OctetString().subtype(
215            value='Quick brown fox',
216            explicitTag=tag.Tag(tag.tagClassApplication, tag.tagFormatSimple, 5)
217        )
218
219    def testDefMode(self):
220        assert encoder.encode(self.o) == ints2octs(
221            (101, 17, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
222
223    def testIndefMode(self):
224        assert encoder.encode(
225            self.o, defMode=False
226        ) == ints2octs((101, 128, 4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 0, 0))
227
228    def testDefModeChunked(self):
229        assert encoder.encode(
230            self.o, defMode=True, maxChunkSize=4
231        ) == 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,
232                        102, 111, 120))
233
234    def testIndefModeChunked(self):
235        assert encoder.encode(
236            self.o, defMode=False, maxChunkSize=4
237        ) == 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))
238
239
240class NullEncoderTestCase(BaseTestCase):
241    def testNull(self):
242        assert encoder.encode(univ.Null('')) == ints2octs((5, 0))
243
244
245class NullEncoderWithSchemaTestCase(BaseTestCase):
246    def testNull(self):
247        assert encoder.encode(None, univ.Null()) == ints2octs((5, 0))
248
249
250class ObjectIdentifierEncoderTestCase(BaseTestCase):
251    def testOne(self):
252        assert encoder.encode(
253            univ.ObjectIdentifier((1, 3, 6, 0, 0xffffe))
254        ) == ints2octs((6, 6, 43, 6, 0, 191, 255, 126))
255
256    def testEdge1(self):
257        assert encoder.encode(
258            univ.ObjectIdentifier((0, 39))
259        ) == ints2octs((6, 1, 39))
260
261    def testEdge2(self):
262        assert encoder.encode(
263            univ.ObjectIdentifier((1, 39))
264        ) == ints2octs((6, 1, 79))
265
266    def testEdge3(self):
267        # 01111111
268        assert encoder.encode(
269            univ.ObjectIdentifier((2, 40))
270        ) == ints2octs((6, 1, 120))
271
272    def testEdge4(self):
273        # 10010000|10000000|10000000|10000000|01001111
274        assert encoder.encode(
275            univ.ObjectIdentifier((2, 0xffffffff))
276        ) == ints2octs((6, 5, 0x90, 0x80, 0x80, 0x80, 0x4F))
277
278    def testEdge5(self):
279        # 01111111
280        assert encoder.encode(
281            univ.ObjectIdentifier((2, 47))
282        ) == ints2octs((6, 1, 0x7F))
283
284    def testEdge6(self):
285        # 10000001|00000000
286        assert encoder.encode(
287            univ.ObjectIdentifier((2, 48))
288        ) == ints2octs((6, 2, 0x81, 0x00))
289
290    def testEdge7(self):
291        # 10000001|00110100|00000003
292        assert encoder.encode(
293            univ.ObjectIdentifier((2, 100, 3))
294        ) == ints2octs((6, 3, 0x81, 0x34, 0x03))
295
296    def testEdge8(self):
297        # 10000101|00000000
298        assert encoder.encode(
299            univ.ObjectIdentifier((2, 560))
300        ) == ints2octs((6, 2, 133, 0))
301
302    def testEdge9(self):
303        # 10001000|10000100|10000111|0000010
304        assert encoder.encode(
305            univ.ObjectIdentifier((2, 16843570))
306        ) == ints2octs((6, 4, 0x88, 0x84, 0x87, 0x02))
307
308    def testEdgeA(self):
309        assert encoder.encode(
310            univ.ObjectIdentifier((2, 5))
311        ) == ints2octs((6, 1, 85))
312
313    def testImpossible1(self):
314        try:
315            encoder.encode(univ.ObjectIdentifier((3, 1, 2)))
316        except PyAsn1Error:
317            pass
318        else:
319            assert 0, 'impossible leading arc tolerated'
320
321    def testImpossible2(self):
322        try:
323            encoder.encode(univ.ObjectIdentifier((0,)))
324        except PyAsn1Error:
325            pass
326        else:
327            assert 0, 'single arc OID tolerated'
328
329    def testImpossible3(self):
330        try:
331            encoder.encode(univ.ObjectIdentifier((0, 40)))
332        except PyAsn1Error:
333            pass
334        else:
335            assert 0, 'second arc overflow tolerated'
336
337    def testImpossible4(self):
338        try:
339            encoder.encode(univ.ObjectIdentifier((1, 40)))
340        except PyAsn1Error:
341            pass
342        else:
343            assert 0, 'second arc overflow tolerated'
344
345    def testLarge1(self):
346        assert encoder.encode(
347            univ.ObjectIdentifier((2, 18446744073709551535184467440737095))
348        ) == ints2octs((0x06, 0x11, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6, 0xB8, 0xCB,
349                        0xE2, 0xB7, 0x17))
350
351    def testLarge2(self):
352        assert encoder.encode(
353            univ.ObjectIdentifier((2, 999, 18446744073709551535184467440737095))
354        ) == ints2octs((0x06, 0x13, 0x88, 0x37, 0x83, 0xC6, 0xDF, 0xD4, 0xCC, 0xB3, 0xFF, 0xFF, 0xFE, 0xF0, 0xB8, 0xD6,
355                        0xB8, 0xCB, 0xE2, 0xB6, 0x47))
356
357
358class ObjectIdentifierWithSchemaEncoderTestCase(BaseTestCase):
359    def testOne(self):
360        assert encoder.encode(
361            (1, 3, 6, 0, 0xffffe), asn1Spec=univ.ObjectIdentifier()
362        ) == ints2octs((6, 6, 43, 6, 0, 191, 255, 126))
363
364
365class RealEncoderTestCase(BaseTestCase):
366    def testChar(self):
367        assert encoder.encode(
368            univ.Real((123, 10, 11))
369        ) == ints2octs((9, 7, 3, 49, 50, 51, 69, 49, 49))
370
371    def testBin1(self):
372        assert encoder.encode(  # default binEncBase = 2
373            univ.Real((0.5, 2, 0))  # check encbase = 2 and exponent = -1
374        ) == ints2octs((9, 3, 128, 255, 1))
375
376    def testBin2(self):
377        r = univ.Real((3.25, 2, 0))
378        r.binEncBase = 8  # change binEncBase only for this instance of Real
379        assert encoder.encode(
380            r  # check encbase = 8
381        ) == ints2octs((9, 3, 148, 255, 13))
382
383    def testBin3(self):
384        # change binEncBase in the RealEncoder instance => for all further Real
385        binEncBase, encoder.typeMap[univ.Real.typeId].binEncBase = encoder.typeMap[univ.Real.typeId].binEncBase, 16
386        assert encoder.encode(
387            univ.Real((0.00390625, 2, 0))  # check encbase = 16
388        ) == ints2octs((9, 3, 160, 254, 1))
389        encoder.typeMap[univ.Real.typeId].binEncBase = binEncBase
390
391    def testBin4(self):
392        # choose binEncBase automatically for all further Real (testBin[4-7])
393        binEncBase, encoder.typeMap[univ.Real.typeId].binEncBase = encoder.typeMap[univ.Real.typeId].binEncBase, None
394        assert encoder.encode(
395            univ.Real((1, 2, 0))  # check exponenta = 0
396        ) == ints2octs((9, 3, 128, 0, 1))
397        encoder.typeMap[univ.Real.typeId].binEncBase = binEncBase
398
399    def testBin5(self):
400        assert encoder.encode(
401            univ.Real((3, 2, -1020))  # case of 2 octs for exponent and
402            # negative exponenta and abs(exponent) is
403            # all 1's and fills the whole octet(s)
404        ) == ints2octs((9, 4, 129, 252, 4, 3))
405
406    def testBin6(self):
407        assert encoder.encode(
408            univ.Real((1, 2, 262140))  # case of 3 octs for exponent and
409            # check that first 9 bits for exponent
410            # are not all 1's
411        ) == ints2octs((9, 5, 130, 3, 255, 252, 1))
412
413    def testBin7(self):
414        assert encoder.encode(
415            univ.Real((-1, 2, 76354972))  # case of >3 octs for exponent and
416            # mantissa < 0
417        ) == ints2octs((9, 7, 195, 4, 4, 141, 21, 156, 1))
418
419    def testPlusInf(self):
420        assert encoder.encode(univ.Real('inf')) == ints2octs((9, 1, 64))
421
422    def testMinusInf(self):
423        assert encoder.encode(univ.Real('-inf')) == ints2octs((9, 1, 65))
424
425    def testZero(self):
426        assert encoder.encode(univ.Real(0)) == ints2octs((9, 0))
427
428
429class RealEncoderWithSchemaTestCase(BaseTestCase):
430    def testChar(self):
431        assert encoder.encode(
432            (123, 10, 11), asn1Spec=univ.Real()
433        ) == ints2octs((9, 7, 3, 49, 50, 51, 69, 49, 49))
434
435
436if sys.version_info[0:2] > (2, 5):
437    class UniversalStringEncoderTestCase(BaseTestCase):
438        def testEncoding(self):
439            assert encoder.encode(char.UniversalString(sys.version_info[0] == 3 and 'abc' or unicode('abc'))) == ints2octs(
440                (28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99)), 'Incorrect encoding'
441
442
443    class UniversalStringEncoderWithSchemaTestCase(BaseTestCase):
444        def testEncoding(self):
445            assert encoder.encode(
446                sys.version_info[0] == 3 and 'abc' or unicode('abc'), asn1Spec=char.UniversalString()
447            ) == ints2octs((28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99)), 'Incorrect encoding'
448
449
450class BMPStringEncoderTestCase(BaseTestCase):
451    def testEncoding(self):
452        assert encoder.encode(char.BMPString(sys.version_info[0] == 3 and 'abc' or unicode('abc'))) == ints2octs(
453            (30, 6, 0, 97, 0, 98, 0, 99)), 'Incorrect encoding'
454
455
456class BMPStringEncoderWithSchemaTestCase(BaseTestCase):
457    def testEncoding(self):
458        assert encoder.encode(
459            sys.version_info[0] == 3 and 'abc' or unicode('abc'), asn1Spec=char.BMPString()
460        ) == ints2octs((30, 6, 0, 97, 0, 98, 0, 99)), 'Incorrect encoding'
461
462
463class UTF8StringEncoderTestCase(BaseTestCase):
464    def testEncoding(self):
465        assert encoder.encode(char.UTF8String(sys.version_info[0] == 3 and 'abc' or unicode('abc'))) == ints2octs(
466            (12, 3, 97, 98, 99)), 'Incorrect encoding'
467
468
469class UTF8StringEncoderWithSchemaTestCase(BaseTestCase):
470    def testEncoding(self):
471        assert encoder.encode(
472            sys.version_info[0] == 3 and 'abc' or unicode('abc'), asn1Spec=char.UTF8String()
473        ) == ints2octs((12, 3, 97, 98, 99)), 'Incorrect encoding'
474
475
476class SequenceOfEncoderTestCase(BaseTestCase):
477    def testEmpty(self):
478        s = univ.SequenceOf()
479        assert encoder.encode(s) == ints2octs((48, 0))
480
481    def testDefMode(self):
482        s = univ.SequenceOf()
483        s.setComponentByPosition(0, univ.OctetString('quick brown'))
484        assert encoder.encode(s) == ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
485
486    def testIndefMode(self):
487        s = univ.SequenceOf()
488        s.setComponentByPosition(0, univ.OctetString('quick brown'))
489        assert encoder.encode(
490            s, defMode=False
491        ) == ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
492
493    def testDefModeChunked(self):
494        s = univ.SequenceOf()
495        s.setComponentByPosition(0, univ.OctetString('quick brown'))
496        assert encoder.encode(
497            s, defMode=True, maxChunkSize=4
498        ) == ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
499
500    def testIndefModeChunked(self):
501        s = univ.SequenceOf()
502        s.setComponentByPosition(0, univ.OctetString('quick brown'))
503        assert encoder.encode(
504            s, defMode=False, maxChunkSize=4
505        ) == 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))
506
507
508class SequenceOfEncoderWithSchemaTestCase(BaseTestCase):
509    def setUp(self):
510        BaseTestCase.setUp(self)
511        self.s = univ.SequenceOf(componentType=univ.OctetString())
512        self.v = ['quick brown']
513
514    def testEmpty(self):
515        assert encoder.encode([], asn1Spec=self.s) == ints2octs((48, 0))
516
517    def testDefMode(self):
518        assert encoder.encode(
519            self.v, asn1Spec=self.s
520        ) == ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
521
522    def testIndefMode(self):
523        assert encoder.encode(
524            self.v, asn1Spec=self.s, defMode=False
525        ) == ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
526
527    def testDefModeChunked(self):
528        assert encoder.encode(
529            self.v, asn1Spec=self.s, defMode=True, maxChunkSize=4
530        ) == ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
531
532    def testIndefModeChunked(self):
533        assert encoder.encode(
534            self.v, asn1Spec=self.s, defMode=False, maxChunkSize=4
535        ) == 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))
536
537
538class SequenceOfEncoderWithComponentsSchemaTestCase(BaseTestCase):
539    def setUp(self):
540        BaseTestCase.setUp(self)
541        self.s = univ.SequenceOf(componentType=univ.OctetString())
542
543    def __init(self):
544        self.s.clear()
545        self.s.setComponentByPosition(0, 'quick brown')
546
547    def testDefMode(self):
548        self.__init()
549        assert encoder.encode(self.s) == ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
550
551    def testIndefMode(self):
552        self.__init()
553        assert encoder.encode(
554            self.s, defMode=False
555        ) == ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
556
557    def testDefModeChunked(self):
558        self.__init()
559        assert encoder.encode(
560            self.s, defMode=True, maxChunkSize=4
561        ) == ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
562
563    def testIndefModeChunked(self):
564        self.__init()
565        assert encoder.encode(
566            self.s, defMode=False, maxChunkSize=4
567        ) == 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))
568
569
570class SetOfEncoderTestCase(BaseTestCase):
571    def testEmpty(self):
572        s = univ.SetOf()
573        assert encoder.encode(s) == ints2octs((49, 0))
574
575    def testDefMode(self):
576        s = univ.SetOf()
577        s.setComponentByPosition(0, univ.OctetString('quick brown'))
578        assert encoder.encode(s) == ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
579
580    def testIndefMode(self):
581        s = univ.SetOf()
582        s.setComponentByPosition(0, univ.OctetString('quick brown'))
583        assert encoder.encode(
584            s, defMode=False
585        ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
586
587    def testDefModeChunked(self):
588        s = univ.SetOf()
589        s.setComponentByPosition(0, univ.OctetString('quick brown'))
590        assert encoder.encode(
591            s, defMode=True, maxChunkSize=4
592        ) == ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
593
594    def testIndefModeChunked(self):
595        s = univ.SetOf()
596        s.setComponentByPosition(0, univ.OctetString('quick brown'))
597        assert encoder.encode(
598            s, defMode=False, maxChunkSize=4
599        ) == 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))
600
601
602class SetOfEncoderWithSchemaTestCase(BaseTestCase):
603    def setUp(self):
604        BaseTestCase.setUp(self)
605        self.s = univ.SetOf(componentType=univ.OctetString())
606        self.v = ['quick brown']
607
608    def testEmpty(self):
609        s = univ.SetOf()
610        assert encoder.encode([], asn1Spec=self.s) == ints2octs((49, 0))
611
612    def testDefMode(self):
613        assert encoder.encode(
614            self.v, asn1Spec=self.s
615        ) == ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
616
617    def testIndefMode(self):
618        assert encoder.encode(
619            self.v, asn1Spec=self.s, defMode=False
620        ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
621
622    def testDefModeChunked(self):
623        assert encoder.encode(
624            self.v, asn1Spec=self.s, defMode=True, maxChunkSize=4
625        ) == ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
626
627    def testIndefModeChunked(self):
628        assert encoder.encode(
629            self.v, asn1Spec=self.s, defMode=False, maxChunkSize=4
630        ) == ints2octs(
631            (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))
632
633
634class SetOfEncoderWithComponentsSchemaTestCase(BaseTestCase):
635    def setUp(self):
636        BaseTestCase.setUp(self)
637        self.s = univ.SetOf(componentType=univ.OctetString())
638
639    def __init(self):
640        self.s.clear()
641        self.s.setComponentByPosition(0, 'quick brown')
642
643    def testDefMode(self):
644        self.__init()
645        assert encoder.encode(self.s) == ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
646
647    def testIndefMode(self):
648        self.__init()
649        assert encoder.encode(
650            self.s, defMode=False
651        ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
652
653    def testDefModeChunked(self):
654        self.__init()
655        assert encoder.encode(
656            self.s, defMode=True, maxChunkSize=4
657        ) == ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
658
659    def testIndefModeChunked(self):
660        self.__init()
661        assert encoder.encode(
662            self.s, defMode=False, maxChunkSize=4
663        ) == 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))
664
665
666class SequenceEncoderTestCase(BaseTestCase):
667    def setUp(self):
668        BaseTestCase.setUp(self)
669        self.s = univ.Sequence()
670        self.s.setComponentByPosition(0, univ.Null(''))
671        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
672        self.s.setComponentByPosition(2, univ.Integer(1))
673
674    def testDefMode(self):
675        assert encoder.encode(self.s) == ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
676
677    def testIndefMode(self):
678        assert encoder.encode(
679            self.s, defMode=False
680        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
681
682    def testDefModeChunked(self):
683        assert encoder.encode(
684            self.s, defMode=True, maxChunkSize=4
685        ) == ints2octs((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))
686
687    def testIndefModeChunked(self):
688        assert encoder.encode(
689            self.s, defMode=False, maxChunkSize=4
690        ) == 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))
691
692
693class SequenceEncoderWithSchemaTestCase(BaseTestCase):
694    def setUp(self):
695        BaseTestCase.setUp(self)
696        self.s = univ.Sequence(
697            componentType=namedtype.NamedTypes(
698                namedtype.NamedType('place-holder', univ.Null()),
699                namedtype.OptionalNamedType('first-name', univ.OctetString()),
700                namedtype.DefaultedNamedType('age', univ.Integer(33)),
701            )
702        )
703        self.v = {
704            'place-holder': None,
705            'first-name': 'quick brown',
706            'age': 1
707        }
708
709    def testEmpty(self):
710        try:
711            assert encoder.encode({}, asn1Spec=self.s)
712
713        except PyAsn1Error:
714            pass
715
716        else:
717            assert False, 'empty bare sequence tolerated'
718
719    def testDefMode(self):
720        assert encoder.encode(
721            self.v, asn1Spec=self.s
722        ) == ints2octs((48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
723
724    def testIndefMode(self):
725        assert encoder.encode(
726            self.v, asn1Spec=self.s, defMode=False
727        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
728
729    def testDefModeChunked(self):
730        assert encoder.encode(
731            self.v, asn1Spec=self.s, defMode=True, maxChunkSize=4
732        ) == ints2octs((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))
733
734    def testIndefModeChunked(self):
735        assert encoder.encode(
736            self.v, asn1Spec=self.s, defMode=False, maxChunkSize=4
737        ) == 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))
738
739
740class SequenceEncoderWithUntaggedOpenTypesTestCase(BaseTestCase):
741    def setUp(self):
742        BaseTestCase.setUp(self)
743
744        openType = opentype.OpenType(
745            'id',
746            {1: univ.Integer(),
747             2: univ.OctetString()}
748        )
749        self.s = univ.Sequence(
750            componentType=namedtype.NamedTypes(
751                namedtype.NamedType('id', univ.Integer()),
752                namedtype.NamedType('blob', univ.Any(), openType=openType)
753            )
754        )
755
756    def testEncodeOpenTypeChoiceOne(self):
757        self.s.clear()
758
759        self.s[0] = 1
760        self.s[1] = univ.Integer(12)
761
762        assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
763            (48, 6, 2, 1, 1, 2, 1, 12)
764        )
765
766    def testEncodeOpenTypeChoiceTwo(self):
767        self.s.clear()
768
769        self.s[0] = 2
770        self.s[1] = univ.OctetString('quick brown')
771
772        assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
773            (48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)
774        )
775
776    def testEncodeOpenTypeUnknownId(self):
777        self.s.clear()
778
779        self.s[0] = 2
780        self.s[1] = univ.ObjectIdentifier('1.3.6')
781
782        try:
783            encoder.encode(self.s, asn1Spec=self.s)
784
785        except PyAsn1Error:
786            assert False, 'incompatible open type tolerated'
787
788    def testEncodeOpenTypeIncompatibleType(self):
789        self.s.clear()
790
791        self.s[0] = 2
792        self.s[1] = univ.ObjectIdentifier('1.3.6')
793
794        try:
795            encoder.encode(self.s, asn1Spec=self.s)
796
797        except PyAsn1Error:
798            assert False, 'incompatible open type tolerated'
799
800
801class SequenceEncoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
802    def setUp(self):
803        BaseTestCase.setUp(self)
804
805        openType = opentype.OpenType(
806            'id',
807            {1: univ.Integer(),
808             2: univ.OctetString()}
809        )
810        self.s = univ.Sequence(
811            componentType=namedtype.NamedTypes(
812                namedtype.NamedType('id', univ.Integer()),
813                namedtype.NamedType('blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType)
814            )
815        )
816
817    def testEncodeOpenTypeChoiceOne(self):
818        self.s.clear()
819
820        self.s[0] = 1
821        self.s[1] = univ.Integer(12)
822
823        assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
824            (48, 8, 2, 1, 1, 131, 3, 2, 1, 12)
825        )
826
827
828class SequenceEncoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
829    def setUp(self):
830        BaseTestCase.setUp(self)
831
832        openType = opentype.OpenType(
833            'id',
834            {1: univ.Integer(),
835             2: univ.OctetString()}
836        )
837        self.s = univ.Sequence(
838            componentType=namedtype.NamedTypes(
839                namedtype.NamedType('id', univ.Integer()),
840                namedtype.NamedType('blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType)
841            )
842        )
843
844    def testEncodeOpenTypeChoiceOne(self):
845        self.s.clear()
846
847        self.s[0] = 1
848        self.s[1] = univ.Integer(12)
849
850        assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
851            (48, 8, 2, 1, 1, 163, 3, 2, 1, 12)
852        )
853
854
855class SequenceEncoderWithComponentsSchemaTestCase(BaseTestCase):
856    def setUp(self):
857        BaseTestCase.setUp(self)
858        self.s = univ.Sequence(
859            componentType=namedtype.NamedTypes(
860                namedtype.NamedType('place-holder', univ.Null()),
861                namedtype.OptionalNamedType('first-name', univ.OctetString()),
862                namedtype.DefaultedNamedType('age', univ.Integer(33)),
863            )
864        )
865
866    def __init(self):
867        self.s.clear()
868        self.s.setComponentByPosition(0, '')
869
870    def __initWithOptional(self):
871        self.s.clear()
872        self.s.setComponentByPosition(0, '')
873        self.s.setComponentByPosition(1, 'quick brown')
874
875    def __initWithDefaulted(self):
876        self.s.clear()
877        self.s.setComponentByPosition(0, '')
878        self.s.setComponentByPosition(2, 1)
879
880    def __initWithOptionalAndDefaulted(self):
881        self.s.clear()
882        self.s.setComponentByPosition(0, univ.Null(''))
883        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
884        self.s.setComponentByPosition(2, univ.Integer(1))
885
886    def testDefMode(self):
887        self.__init()
888        assert encoder.encode(self.s) == ints2octs((48, 2, 5, 0))
889
890    def testIndefMode(self):
891        self.__init()
892        assert encoder.encode(
893            self.s, defMode=False
894        ) == ints2octs((48, 128, 5, 0, 0, 0))
895
896    def testDefModeChunked(self):
897        self.__init()
898        assert encoder.encode(
899            self.s, defMode=True, maxChunkSize=4
900        ) == ints2octs((48, 2, 5, 0))
901
902    def testIndefModeChunked(self):
903        self.__init()
904        assert encoder.encode(
905            self.s, defMode=False, maxChunkSize=4
906        ) == ints2octs((48, 128, 5, 0, 0, 0))
907
908    def testWithOptionalDefMode(self):
909        self.__initWithOptional()
910        assert encoder.encode(self.s) == ints2octs(
911            (48, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
912
913    def testWithOptionalIndefMode(self):
914        self.__initWithOptional()
915        assert encoder.encode(
916            self.s, defMode=False
917        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
918
919    def testWithOptionalDefModeChunked(self):
920        self.__initWithOptional()
921        assert encoder.encode(
922            self.s, defMode=True, maxChunkSize=4
923        ) == ints2octs((48, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
924
925    def testWithOptionalIndefModeChunked(self):
926        self.__initWithOptional()
927        assert encoder.encode(
928            self.s, defMode=False, maxChunkSize=4
929        ) == ints2octs(
930            (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, 0, 0))
931
932    def testWithDefaultedDefMode(self):
933        self.__initWithDefaulted()
934        assert encoder.encode(self.s) == ints2octs((48, 5, 5, 0, 2, 1, 1))
935
936    def testWithDefaultedIndefMode(self):
937        self.__initWithDefaulted()
938        assert encoder.encode(
939            self.s, defMode=False
940        ) == ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0))
941
942    def testWithDefaultedDefModeChunked(self):
943        self.__initWithDefaulted()
944        assert encoder.encode(
945            self.s, defMode=True, maxChunkSize=4
946        ) == ints2octs((48, 5, 5, 0, 2, 1, 1))
947
948    def testWithDefaultedIndefModeChunked(self):
949        self.__initWithDefaulted()
950        assert encoder.encode(
951            self.s, defMode=False, maxChunkSize=4
952        ) == ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0))
953
954    def testWithOptionalAndDefaultedDefMode(self):
955        self.__initWithOptionalAndDefaulted()
956        assert encoder.encode(self.s) == ints2octs(
957            (48, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
958
959    def testWithOptionalAndDefaultedIndefMode(self):
960        self.__initWithOptionalAndDefaulted()
961        assert encoder.encode(
962            self.s, defMode=False
963        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
964
965    def testWithOptionalAndDefaultedDefModeChunked(self):
966        self.__initWithOptionalAndDefaulted()
967        assert encoder.encode(
968            self.s, defMode=True, maxChunkSize=4
969        ) == ints2octs(
970            (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))
971
972    def testWithOptionalAndDefaultedIndefModeChunked(self):
973        self.__initWithOptionalAndDefaulted()
974        assert encoder.encode(
975            self.s, defMode=False, maxChunkSize=4
976        ) == 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,
977                        0, 2, 1, 1, 0, 0))
978
979
980class ExpTaggedSequenceEncoderTestCase(BaseTestCase):
981    def setUp(self):
982        BaseTestCase.setUp(self)
983        s = univ.Sequence(
984            componentType=namedtype.NamedTypes(
985                namedtype.NamedType('number', univ.Integer()),
986            )
987        )
988
989        s = s.subtype(
990            explicitTag=tag.Tag(tag.tagClassApplication, tag.tagFormatConstructed, 5)
991        )
992
993        s[0] = 12
994
995        self.s = s
996
997    def testDefMode(self):
998        assert encoder.encode(self.s) == ints2octs((101, 5, 48, 3, 2, 1, 12))
999
1000    def testIndefMode(self):
1001        assert encoder.encode(
1002            self.s, defMode=False
1003        ) == ints2octs((101, 128, 48, 128, 2, 1, 12, 0, 0, 0, 0))
1004
1005
1006class ExpTaggedSequenceComponentEncoderTestCase(BaseTestCase):
1007    def setUp(self):
1008        BaseTestCase.setUp(self)
1009        self.s = univ.Sequence(
1010            componentType=namedtype.NamedTypes(
1011                namedtype.NamedType('number', univ.Boolean().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
1012            )
1013        )
1014
1015        self.s[0] = True
1016
1017    def testDefMode(self):
1018        assert encoder.encode(self.s) == ints2octs((48, 5, 160, 3, 1, 1, 1))
1019
1020    def testIndefMode(self):
1021        assert encoder.encode(
1022            self.s, defMode=False
1023        ) == ints2octs((48, 128, 160, 3, 1, 1, 1, 0, 0, 0, 0))
1024
1025
1026class SetEncoderTestCase(BaseTestCase):
1027    def setUp(self):
1028        BaseTestCase.setUp(self)
1029        self.s = univ.Set()
1030        self.s.setComponentByPosition(0, univ.Null(''))
1031        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
1032        self.s.setComponentByPosition(2, univ.Integer(1))
1033
1034    def testDefMode(self):
1035        assert encoder.encode(self.s) == ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
1036
1037    def testIndefMode(self):
1038        assert encoder.encode(
1039            self.s, defMode=False
1040        ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
1041
1042    def testDefModeChunked(self):
1043        assert encoder.encode(
1044            self.s, defMode=True, maxChunkSize=4
1045        ) == 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))
1046
1047    def testIndefModeChunked(self):
1048        assert encoder.encode(
1049            self.s, defMode=False, maxChunkSize=4
1050        ) == 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))
1051
1052
1053class SetEncoderWithSchemaTestCase(BaseTestCase):
1054    def setUp(self):
1055        BaseTestCase.setUp(self)
1056        self.s = univ.Set(
1057            componentType=namedtype.NamedTypes(
1058                namedtype.NamedType('place-holder', univ.Null()),
1059                namedtype.OptionalNamedType('first-name', univ.OctetString()),
1060                namedtype.DefaultedNamedType('age', univ.Integer(33)),
1061            )
1062        )
1063        self.v = {
1064            'place-holder': None,
1065            'first-name': 'quick brown',
1066            'age': 1
1067        }
1068
1069    def testEmpty(self):
1070        try:
1071            assert encoder.encode({}, asn1Spec=self.s)
1072
1073        except PyAsn1Error:
1074            pass
1075
1076        else:
1077            assert False, 'empty bare SET tolerated'
1078
1079    def testDefMode(self):
1080        assert encoder.encode(
1081            self.v, asn1Spec=self.s
1082        ) == ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
1083
1084    def testIndefMode(self):
1085        assert encoder.encode(
1086            self.v, asn1Spec=self.s, defMode=False
1087        ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
1088
1089    def testDefModeChunked(self):
1090        assert encoder.encode(
1091            self.v, asn1Spec=self.s, defMode=True, maxChunkSize=4
1092        ) == 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))
1093
1094    def testIndefModeChunked(self):
1095        assert encoder.encode(
1096            self.v, asn1Spec=self.s, defMode=False, maxChunkSize=4
1097        ) == 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))
1098
1099
1100class SetEncoderWithComponentsSchemaTestCase(BaseTestCase):
1101    def setUp(self):
1102        BaseTestCase.setUp(self)
1103        self.s = univ.Set(
1104            componentType=namedtype.NamedTypes(
1105                namedtype.NamedType('place-holder', univ.Null()),
1106                namedtype.OptionalNamedType('first-name', univ.OctetString()),
1107                namedtype.DefaultedNamedType('age', univ.Integer(33)),
1108            )
1109        )
1110
1111    def __init(self):
1112        self.s.clear()
1113        self.s.setComponentByPosition(0, '')
1114
1115    def __initWithOptional(self):
1116        self.s.clear()
1117        self.s.setComponentByPosition(0, '')
1118        self.s.setComponentByPosition(1, 'quick brown')
1119
1120    def __initWithDefaulted(self):
1121        self.s.clear()
1122        self.s.setComponentByPosition(0, '')
1123        self.s.setComponentByPosition(2, 1)
1124
1125    def __initWithOptionalAndDefaulted(self):
1126        self.s.clear()
1127        self.s.setComponentByPosition(0, univ.Null(''))
1128        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
1129        self.s.setComponentByPosition(2, univ.Integer(1))
1130
1131    def testDefMode(self):
1132        self.__init()
1133        assert encoder.encode(self.s) == ints2octs((49, 2, 5, 0))
1134
1135    def testIndefMode(self):
1136        self.__init()
1137        assert encoder.encode(
1138            self.s, defMode=False
1139        ) == ints2octs((49, 128, 5, 0, 0, 0))
1140
1141    def testDefModeChunked(self):
1142        self.__init()
1143        assert encoder.encode(
1144            self.s, defMode=True, maxChunkSize=4
1145        ) == ints2octs((49, 2, 5, 0))
1146
1147    def testIndefModeChunked(self):
1148        self.__init()
1149        assert encoder.encode(
1150            self.s, defMode=False, maxChunkSize=4
1151        ) == ints2octs((49, 128, 5, 0, 0, 0))
1152
1153    def testWithOptionalDefMode(self):
1154        self.__initWithOptional()
1155        assert encoder.encode(self.s) == ints2octs(
1156            (49, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
1157
1158    def testWithOptionalIndefMode(self):
1159        self.__initWithOptional()
1160        assert encoder.encode(
1161            self.s, defMode=False
1162        ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
1163
1164    def testWithOptionalDefModeChunked(self):
1165        self.__initWithOptional()
1166        assert encoder.encode(
1167            self.s, defMode=True, maxChunkSize=4
1168        ) == ints2octs((49, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
1169
1170    def testWithOptionalIndefModeChunked(self):
1171        self.__initWithOptional()
1172        assert encoder.encode(
1173            self.s, defMode=False, maxChunkSize=4
1174        ) == ints2octs(
1175            (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))
1176
1177    def testWithDefaultedDefMode(self):
1178        self.__initWithDefaulted()
1179        assert encoder.encode(self.s) == ints2octs((49, 5, 5, 0, 2, 1, 1))
1180
1181    def testWithDefaultedIndefMode(self):
1182        self.__initWithDefaulted()
1183        assert encoder.encode(
1184            self.s, defMode=False
1185        ) == ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0))
1186
1187    def testWithDefaultedDefModeChunked(self):
1188        self.__initWithDefaulted()
1189        assert encoder.encode(
1190            self.s, defMode=True, maxChunkSize=4
1191        ) == ints2octs((49, 5, 5, 0, 2, 1, 1))
1192
1193    def testWithDefaultedIndefModeChunked(self):
1194        self.__initWithDefaulted()
1195        assert encoder.encode(
1196            self.s, defMode=False, maxChunkSize=4
1197        ) == ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0))
1198
1199    def testWithOptionalAndDefaultedDefMode(self):
1200        self.__initWithOptionalAndDefaulted()
1201        assert encoder.encode(self.s) == ints2octs(
1202            (49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1))
1203
1204    def testWithOptionalAndDefaultedIndefMode(self):
1205        self.__initWithOptionalAndDefaulted()
1206        assert encoder.encode(
1207            self.s, defMode=False
1208        ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
1209
1210    def testWithOptionalAndDefaultedDefModeChunked(self):
1211        self.__initWithOptionalAndDefaulted()
1212        assert encoder.encode(
1213            self.s, defMode=True, maxChunkSize=4
1214        ) == ints2octs(
1215            (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))
1216
1217    def testWithOptionalAndDefaultedIndefModeChunked(self):
1218        self.__initWithOptionalAndDefaulted()
1219        assert encoder.encode(
1220            self.s, defMode=False, maxChunkSize=4
1221        ) == 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))
1222
1223
1224class ChoiceEncoderTestCase(BaseTestCase):
1225
1226    def testEmpty(self):
1227        s = univ.Choice()
1228        try:
1229            encoder.encode(s)
1230        except PyAsn1Error:
1231            pass
1232        else:
1233            assert 0, 'encoded unset choice'
1234
1235    def testDefModeOptionOne(self):
1236        s = univ.Choice()
1237        s.setComponentByPosition(0, univ.Null(''))
1238        assert encoder.encode(s) == ints2octs((5, 0))
1239
1240    def testDefModeOptionTwo(self):
1241        s = univ.Choice()
1242        s.setComponentByPosition(0, univ.OctetString('quick brown'))
1243        assert encoder.encode(s) == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
1244
1245    def testIndefMode(self):
1246        s = univ.Choice()
1247        s.setComponentByPosition(0, univ.OctetString('quick brown'))
1248        assert encoder.encode(
1249            s, defMode=False
1250        ) == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
1251
1252    def testDefModeChunked(self):
1253        s = univ.Choice()
1254        s.setComponentByPosition(0, univ.OctetString('quick brown'))
1255        assert encoder.encode(
1256            s, defMode=True, maxChunkSize=4
1257        ) == ints2octs((36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
1258
1259    def testIndefModeChunked(self):
1260        s = univ.Choice()
1261        s.setComponentByPosition(0, univ.OctetString('quick brown'))
1262        assert encoder.encode(
1263            s, defMode=False, maxChunkSize=4
1264        ) == ints2octs((36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0))
1265
1266
1267class ChoiceEncoderWithSchemaTestCase(BaseTestCase):
1268    def setUp(self):
1269        BaseTestCase.setUp(self)
1270        self.s = univ.Choice(
1271            componentType=namedtype.NamedTypes(
1272                namedtype.NamedType('place-holder', univ.Null('')),
1273                namedtype.NamedType('number', univ.Integer(0)),
1274                namedtype.NamedType('string', univ.OctetString())
1275            )
1276        )
1277        self.v = {
1278            'place-holder': None
1279        }
1280
1281    def testFilled(self):
1282        assert encoder.encode(
1283            self.v, asn1Spec=self.s
1284        ) == ints2octs((5, 0))
1285
1286
1287class ChoiceEncoderWithComponentsSchemaTestCase(BaseTestCase):
1288    def setUp(self):
1289        BaseTestCase.setUp(self)
1290        self.s = univ.Choice(
1291            componentType=namedtype.NamedTypes(
1292                namedtype.NamedType('place-holder', univ.Null('')),
1293                namedtype.NamedType('number', univ.Integer(0)),
1294                namedtype.NamedType('string', univ.OctetString())
1295            )
1296        )
1297
1298    def testEmpty(self):
1299        try:
1300            encoder.encode(self.s)
1301        except PyAsn1Error:
1302            pass
1303        else:
1304            assert 0, 'encoded unset choice'
1305
1306    def testFilled(self):
1307        self.s.setComponentByPosition(0, univ.Null(''))
1308        assert encoder.encode(self.s) == ints2octs((5, 0))
1309
1310    def testTagged(self):
1311        s = self.s.subtype(
1312            explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 4)
1313        )
1314        s.setComponentByPosition(0, univ.Null(''))
1315        assert encoder.encode(s) == ints2octs((164, 2, 5, 0))
1316
1317    def testUndefLength(self):
1318        self.s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
1319        assert encoder.encode(self.s, defMode=False, maxChunkSize=3) == ints2octs(
1320            (36, 128, 4, 3, 97, 98, 99, 4, 3, 100, 101, 102, 4, 2, 103, 104, 0, 0))
1321
1322    def testTaggedUndefLength(self):
1323        s = self.s.subtype(
1324            explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 4)
1325        )
1326        s.setComponentByPosition(2, univ.OctetString('abcdefgh'))
1327        assert encoder.encode(s, defMode=False, maxChunkSize=3) == ints2octs(
1328            (164, 128, 36, 128, 4, 3, 97, 98, 99, 4, 3, 100, 101, 102, 4, 2, 103, 104, 0, 0, 0, 0))
1329
1330
1331class AnyEncoderTestCase(BaseTestCase):
1332    def setUp(self):
1333        BaseTestCase.setUp(self)
1334        self.s = univ.Any(encoder.encode(univ.OctetString('fox')))
1335
1336    def testUntagged(self):
1337        assert encoder.encode(self.s) == ints2octs((4, 3, 102, 111, 120))
1338
1339    def testTaggedEx(self):
1340        s = self.s.subtype(
1341            explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)
1342        )
1343        assert encoder.encode(s) == ints2octs((164, 5, 4, 3, 102, 111, 120))
1344
1345    def testTaggedIm(self):
1346        s = self.s.subtype(
1347            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)
1348        )
1349        assert encoder.encode(s) == ints2octs((132, 5, 4, 3, 102, 111, 120))
1350
1351
1352class AnyEncoderWithSchemaTestCase(BaseTestCase):
1353    def setUp(self):
1354        BaseTestCase.setUp(self)
1355        self.s = univ.Any()
1356        self.v = encoder.encode(univ.OctetString('fox'))
1357
1358    def testUntagged(self):
1359        assert encoder.encode(self.v, asn1Spec=self.s) == ints2octs((4, 3, 102, 111, 120))
1360
1361    def testTaggedEx(self):
1362        s = self.s.subtype(
1363            explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)
1364        )
1365        assert encoder.encode(self.v, asn1Spec=s) == ints2octs((164, 5, 4, 3, 102, 111, 120))
1366
1367    def testTaggedIm(self):
1368        s = self.s.subtype(
1369            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 4)
1370        )
1371        assert encoder.encode(self.v, asn1Spec=s) == ints2octs((132, 5, 4, 3, 102, 111, 120))
1372
1373
1374suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
1375
1376if __name__ == '__main__':
1377    unittest.TextTestRunner(verbosity=2).run(suite)
1378