• 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
11except ImportError:
12    import unittest
13
14from tests.base import BaseTestCase
15
16from pyasn1.type import tag
17from pyasn1.type import namedtype
18from pyasn1.type import univ
19from pyasn1.type import useful
20from pyasn1.codec.cer import encoder
21from pyasn1.compat.octets import ints2octs
22from pyasn1.error import PyAsn1Error
23
24
25class BooleanEncoderTestCase(BaseTestCase):
26    def testTrue(self):
27        assert encoder.encode(univ.Boolean(1)) == ints2octs((1, 1, 255))
28
29    def testFalse(self):
30        assert encoder.encode(univ.Boolean(0)) == ints2octs((1, 1, 0))
31
32
33class BitStringEncoderTestCase(BaseTestCase):
34    def testShortMode(self):
35        assert encoder.encode(
36            univ.BitString((1, 0) * 5)
37        ) == ints2octs((3, 3, 6, 170, 128))
38
39    def testLongMode(self):
40        assert encoder.encode(univ.BitString((1, 0) * 501)) == ints2octs((3, 127, 6) + (170,) * 125 + (128,))
41
42
43class OctetStringEncoderTestCase(BaseTestCase):
44    def testShortMode(self):
45        assert encoder.encode(
46            univ.OctetString('Quick brown fox')
47        ) == ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
48
49    def testLongMode(self):
50        assert encoder.encode(
51            univ.OctetString('Q' * 1001)
52        ) == ints2octs((36, 128, 4, 130, 3, 232) + (81,) * 1000 + (4, 1, 81, 0, 0))
53
54
55class GeneralizedTimeEncoderTestCase(BaseTestCase):
56    #    def testExtraZeroInSeconds(self):
57    #        try:
58    #            assert encoder.encode(
59    #                useful.GeneralizedTime('20150501120112.10Z')
60    #            )
61    #        except PyAsn1Error:
62    #            pass
63    #        else:
64    #            assert 0, 'Meaningless trailing zero in fraction part tolerated'
65
66    def testLocalTimezone(self):
67        try:
68            assert encoder.encode(
69                useful.GeneralizedTime('20150501120112.1+0200')
70            )
71        except PyAsn1Error:
72            pass
73        else:
74            assert 0, 'Local timezone tolerated'
75
76    def testMissingTimezone(self):
77        try:
78            assert encoder.encode(
79                useful.GeneralizedTime('20150501120112.1')
80            )
81        except PyAsn1Error:
82            pass
83        else:
84            assert 0, 'Missing timezone tolerated'
85
86
87    def testDecimalCommaPoint(self):
88        try:
89            assert encoder.encode(
90                    useful.GeneralizedTime('20150501120112,1Z')
91             )
92        except PyAsn1Error:
93            pass
94        else:
95            assert 0, 'Decimal comma tolerated'
96
97    def testWithSubseconds(self):
98        assert encoder.encode(
99                    useful.GeneralizedTime('20170801120112.59Z')
100             ) == ints2octs((24, 18, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 46, 53, 57, 90))
101
102    def testWithSeconds(self):
103        assert encoder.encode(
104                    useful.GeneralizedTime('20170801120112Z')
105             ) == ints2octs((24, 15, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 90))
106
107    def testWithMinutes(self):
108        assert encoder.encode(
109                    useful.GeneralizedTime('201708011201Z')
110             ) == ints2octs((24, 13, 50, 48, 49, 55, 48, 56, 48, 49, 49, 50, 48, 49, 90))
111
112
113class UTCTimeEncoderTestCase(BaseTestCase):
114    def testFractionOfSecond(self):
115        try:
116            assert encoder.encode(
117                useful.UTCTime('150501120112.10Z')
118            )
119        except PyAsn1Error:
120            pass
121        else:
122            assert 0, 'Decimal point tolerated'
123
124    def testMissingTimezone(self):
125        try:
126            assert encoder.encode(
127                useful.UTCTime('150501120112')
128            ) == ints2octs((23, 13, 49, 53, 48, 53, 48, 49, 49, 50, 48, 49, 49, 50, 90))
129        except PyAsn1Error:
130            pass
131        else:
132            assert 0, 'Missing timezone tolerated'
133
134    def testLocalTimezone(self):
135        try:
136            assert encoder.encode(
137                useful.UTCTime('150501120112+0200')
138            )
139        except PyAsn1Error:
140            pass
141        else:
142            assert 0, 'Local timezone tolerated'
143
144    def testWithSeconds(self):
145        assert encoder.encode(
146                    useful.UTCTime('990801120112Z')
147             ) == ints2octs((23, 13, 57, 57, 48, 56, 48, 49, 49, 50, 48, 49, 49, 50, 90))
148
149    def testWithMinutes(self):
150        assert encoder.encode(
151                    useful.UTCTime('9908011201Z')
152             ) == ints2octs((23, 11, 57, 57, 48, 56, 48, 49, 49, 50, 48, 49, 90))
153
154
155class SequenceOfEncoderTestCase(BaseTestCase):
156    def testEmpty(self):
157        s = univ.SequenceOf()
158        assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
159
160    def testDefMode1(self):
161        s = univ.SequenceOf()
162        s.append(univ.OctetString('a'))
163        s.append(univ.OctetString('ab'))
164        assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
165
166    def testDefMode2(self):
167        s = univ.SequenceOf()
168        s.append(univ.OctetString('ab'))
169        s.append(univ.OctetString('a'))
170        assert encoder.encode(s) == ints2octs((48, 128, 4, 2, 97, 98, 4, 1, 97, 0, 0))
171
172    def testDefMode3(self):
173        s = univ.SequenceOf()
174        s.append(univ.OctetString('b'))
175        s.append(univ.OctetString('a'))
176        assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 98, 4, 1, 97, 0, 0))
177
178    def testDefMode4(self):
179        s = univ.SequenceOf()
180        s.append(univ.OctetString('a'))
181        s.append(univ.OctetString('b'))
182        assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 97, 4, 1, 98, 0, 0))
183
184
185class SequenceOfEncoderWithSchemaTestCase(BaseTestCase):
186    def setUp(self):
187        BaseTestCase.setUp(self)
188        self.s = univ.SequenceOf(componentType=univ.OctetString())
189
190    def testEmpty(self):
191        self.s.clear()
192        assert encoder.encode(self.s) == ints2octs((48, 128, 0, 0))
193
194    def testIndefMode1(self):
195        self.s.clear()
196        self.s.append('a')
197        self.s.append('ab')
198        assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
199
200    def testIndefMode2(self):
201        self.s.clear()
202        self.s.append('ab')
203        self.s.append('a')
204        assert encoder.encode(self.s) == ints2octs((48, 128, 4, 2, 97, 98, 4, 1, 97, 0, 0))
205
206    def testIndefMode3(self):
207        self.s.clear()
208        self.s.append('b')
209        self.s.append('a')
210        assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 98, 4, 1, 97, 0, 0))
211
212    def testIndefMode4(self):
213        self.s.clear()
214        self.s.append('a')
215        self.s.append('b')
216        assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 97, 4, 1, 98, 0, 0))
217
218
219class SetOfEncoderTestCase(BaseTestCase):
220    def testEmpty(self):
221        s = univ.SetOf()
222        assert encoder.encode(s) == ints2octs((49, 128, 0, 0))
223
224    def testDefMode1(self):
225        s = univ.SetOf()
226        s.append(univ.OctetString('a'))
227        s.append(univ.OctetString('ab'))
228        assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
229
230    def testDefMode2(self):
231        s = univ.SetOf()
232        s.append(univ.OctetString('ab'))
233        s.append(univ.OctetString('a'))
234        assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
235
236    def testDefMode3(self):
237        s = univ.SetOf()
238        s.append(univ.OctetString('b'))
239        s.append(univ.OctetString('a'))
240        assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
241
242    def testDefMode4(self):
243        s = univ.SetOf()
244        s.append(univ.OctetString('a'))
245        s.append(univ.OctetString('b'))
246        assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
247
248
249class SetOfEncoderWithSchemaTestCase(BaseTestCase):
250    def setUp(self):
251        BaseTestCase.setUp(self)
252        self.s = univ.SetOf(componentType=univ.OctetString())
253
254    def testEmpty(self):
255        self.s.clear()
256        assert encoder.encode(self.s) == ints2octs((49, 128, 0, 0))
257
258    def testIndefMode1(self):
259        self.s.clear()
260        self.s.append('a')
261        self.s.append('ab')
262
263        assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
264
265    def testIndefMode2(self):
266        self.s.clear()
267        self.s.append('ab')
268        self.s.append('a')
269
270        assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
271
272    def testIndefMode3(self):
273        self.s.clear()
274        self.s.append('b')
275        self.s.append('a')
276
277        assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
278
279    def testIndefMode4(self):
280        self.s.clear()
281        self.s.append('a')
282        self.s.append('b')
283
284        assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
285
286
287class SetEncoderTestCase(BaseTestCase):
288    def setUp(self):
289        BaseTestCase.setUp(self)
290        self.s = univ.Set()
291        self.s.setComponentByPosition(0, univ.Null(''))
292        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
293        self.s.setComponentByPosition(2, univ.Integer(1))
294
295    def testIndefMode(self):
296        assert encoder.encode(self.s) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
297
298    def testWithOptionalIndefMode(self):
299        assert encoder.encode(
300            self.s
301        ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
302
303    def testWithDefaultedIndefMode(self):
304        assert encoder.encode(
305            self.s
306        ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
307
308    def testWithOptionalAndDefaultedIndefMode(self):
309        assert encoder.encode(
310            self.s
311        ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
312
313
314class SetEncoderWithSchemaTestCase(BaseTestCase):
315    def setUp(self):
316        BaseTestCase.setUp(self)
317        self.s = univ.Set(componentType=namedtype.NamedTypes(
318            namedtype.NamedType('place-holder', univ.Null('')),
319            namedtype.OptionalNamedType('first-name', univ.OctetString()),
320            namedtype.DefaultedNamedType('age', univ.Integer(33))
321        ))
322
323    def __init(self):
324        self.s.clear()
325        self.s.setComponentByPosition(0)
326
327    def __initWithOptional(self):
328        self.s.clear()
329        self.s.setComponentByPosition(0)
330        self.s.setComponentByPosition(1, 'quick brown')
331
332    def __initWithDefaulted(self):
333        self.s.clear()
334        self.s.setComponentByPosition(0)
335        self.s.setComponentByPosition(2, 1)
336
337    def __initWithOptionalAndDefaulted(self):
338        self.s.clear()
339        self.s.setComponentByPosition(0, univ.Null(''))
340        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
341        self.s.setComponentByPosition(2, univ.Integer(1))
342
343    def testIndefMode(self):
344        self.__init()
345        assert encoder.encode(self.s) == ints2octs((49, 128, 5, 0, 0, 0))
346
347    def testWithOptionalIndefMode(self):
348        self.__initWithOptional()
349        assert encoder.encode(
350            self.s
351        ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
352
353    def testWithDefaultedIndefMode(self):
354        self.__initWithDefaulted()
355        assert encoder.encode(
356            self.s
357        ) == ints2octs((49, 128, 2, 1, 1, 5, 0, 0, 0))
358
359    def testWithOptionalAndDefaultedIndefMode(self):
360        self.__initWithOptionalAndDefaulted()
361        assert encoder.encode(
362            self.s
363        ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
364
365
366class SetWithChoiceWithSchemaEncoderTestCase(BaseTestCase):
367    def setUp(self):
368        BaseTestCase.setUp(self)
369        c = univ.Choice(componentType=namedtype.NamedTypes(
370            namedtype.NamedType('actual', univ.Boolean(0))
371        ))
372        self.s = univ.Set(componentType=namedtype.NamedTypes(
373            namedtype.NamedType('place-holder', univ.Null('')),
374            namedtype.NamedType('status', c)
375        ))
376
377    def testIndefMode(self):
378        self.s.setComponentByPosition(0)
379        self.s.setComponentByName('status')
380        self.s.getComponentByName('status').setComponentByPosition(0, 1)
381        assert encoder.encode(self.s) == ints2octs((49, 128, 1, 1, 255, 5, 0, 0, 0))
382
383
384class SetWithTaggedChoiceEncoderTestCase(BaseTestCase):
385
386    def testWithUntaggedChoice(self):
387
388        c = univ.Choice(
389            componentType=namedtype.NamedTypes(
390                namedtype.NamedType('premium', univ.Boolean())
391            )
392        )
393
394        s = univ.Set(
395            componentType=namedtype.NamedTypes(
396                namedtype.NamedType('name', univ.OctetString()),
397                namedtype.NamedType('customer', c)
398            )
399        )
400
401        s.setComponentByName('name', 'A')
402        s.getComponentByName('customer').setComponentByName('premium', True)
403
404        assert encoder.encode(s) == ints2octs((49, 128, 1, 1, 255, 4, 1, 65, 0, 0))
405
406    def testWithTaggedChoice(self):
407
408        c = univ.Choice(
409            componentType=namedtype.NamedTypes(
410                namedtype.NamedType('premium', univ.Boolean())
411            )
412        ).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 7))
413
414        s = univ.Set(
415            componentType=namedtype.NamedTypes(
416                namedtype.NamedType('name', univ.OctetString()),
417                namedtype.NamedType('customer', c)
418            )
419        )
420
421        s.setComponentByName('name', 'A')
422        s.getComponentByName('customer').setComponentByName('premium', True)
423
424        assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 65, 167, 128, 1, 1, 255, 0, 0, 0, 0))
425
426
427class SetEncoderTestCase(BaseTestCase):
428    def setUp(self):
429        BaseTestCase.setUp(self)
430        self.s = univ.Set()
431        self.s.setComponentByPosition(0, univ.Null(''))
432        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
433        self.s.setComponentByPosition(2, univ.Integer(1))
434
435    def testIndefMode(self):
436        assert encoder.encode(self.s) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
437
438    def testWithOptionalIndefMode(self):
439        assert encoder.encode(
440            self.s
441        ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
442
443    def testWithDefaultedIndefMode(self):
444        assert encoder.encode(
445            self.s
446        ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
447
448    def testWithOptionalAndDefaultedIndefMode(self):
449        assert encoder.encode(
450            self.s
451        ) == ints2octs((49, 128, 2, 1, 1, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
452
453
454class SequenceEncoderTestCase(BaseTestCase):
455    def setUp(self):
456        BaseTestCase.setUp(self)
457        self.s = univ.Sequence()
458        self.s.setComponentByPosition(0, univ.Null(''))
459        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
460        self.s.setComponentByPosition(2, univ.Integer(1))
461
462    def testIndefMode(self):
463        assert encoder.encode(self.s) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
464
465    def testWithOptionalIndefMode(self):
466        assert encoder.encode(
467            self.s
468        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
469
470    def testWithDefaultedIndefMode(self):
471        assert encoder.encode(
472            self.s
473        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
474
475    def testWithOptionalAndDefaultedIndefMode(self):
476        assert encoder.encode(
477            self.s
478        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
479
480
481class SequenceEncoderWithSchemaTestCase(BaseTestCase):
482    def setUp(self):
483        BaseTestCase.setUp(self)
484        self.s = univ.Sequence(
485            componentType=namedtype.NamedTypes(
486                namedtype.NamedType('place-holder', univ.Null('')),
487                namedtype.OptionalNamedType('first-name', univ.OctetString()),
488                namedtype.DefaultedNamedType('age', univ.Integer(33))
489            )
490        )
491
492    def __init(self):
493        self.s.clear()
494        self.s.setComponentByPosition(0)
495
496    def __initWithOptional(self):
497        self.s.clear()
498        self.s.setComponentByPosition(0)
499        self.s.setComponentByPosition(1, 'quick brown')
500
501    def __initWithDefaulted(self):
502        self.s.clear()
503        self.s.setComponentByPosition(0)
504        self.s.setComponentByPosition(2, 1)
505
506    def __initWithOptionalAndDefaulted(self):
507        self.s.clear()
508        self.s.setComponentByPosition(0, univ.Null(''))
509        self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
510        self.s.setComponentByPosition(2, univ.Integer(1))
511
512    def testIndefMode(self):
513        self.__init()
514        assert encoder.encode(self.s) == ints2octs((48, 128, 5, 0, 0, 0))
515
516    def testWithOptionalIndefMode(self):
517        self.__initWithOptional()
518        assert encoder.encode(
519            self.s
520        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
521
522    def testWithDefaultedIndefMode(self):
523        self.__initWithDefaulted()
524        assert encoder.encode(
525            self.s
526        ) == ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0))
527
528    def testWithOptionalAndDefaultedIndefMode(self):
529        self.__initWithOptionalAndDefaulted()
530        assert encoder.encode(
531            self.s
532        ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
533
534
535class NestedOptionalSequenceEncoderTestCase(BaseTestCase):
536    def setUp(self):
537        BaseTestCase.setUp(self)
538        inner = univ.Sequence(
539            componentType=namedtype.NamedTypes(
540                namedtype.OptionalNamedType('first-name', univ.OctetString()),
541                namedtype.DefaultedNamedType('age', univ.Integer(33)),
542            )
543        )
544
545        outerWithOptional = univ.Sequence(
546            componentType=namedtype.NamedTypes(
547                namedtype.OptionalNamedType('inner', inner),
548            )
549        )
550
551        outerWithDefault = univ.Sequence(
552            componentType=namedtype.NamedTypes(
553                namedtype.DefaultedNamedType('inner', inner),
554            )
555        )
556
557        self.s1 = outerWithOptional
558        self.s2 = outerWithDefault
559
560    def __initOptionalWithDefaultAndOptional(self):
561        self.s1.clear()
562        self.s1[0][0] = 'test'
563        self.s1[0][1] = 123
564        return self.s1
565
566    def __initOptionalWithDefault(self):
567        self.s1.clear()
568        self.s1[0][1] = 123
569        return self.s1
570
571    def __initOptionalWithOptional(self):
572        self.s1.clear()
573        self.s1[0][0] = 'test'
574        return self.s1
575
576    def __initOptional(self):
577        self.s1.clear()
578        return self.s1
579
580    def __initDefaultWithDefaultAndOptional(self):
581        self.s2.clear()
582        self.s2[0][0] = 'test'
583        self.s2[0][1] = 123
584        return self.s2
585
586    def __initDefaultWithDefault(self):
587        self.s2.clear()
588        self.s2[0][0] = 'test'
589        return self.s2
590
591    def __initDefaultWithOptional(self):
592        self.s2.clear()
593        self.s2[0][1] = 123
594        return self.s2
595
596    def testOptionalWithDefaultAndOptional(self):
597        s = self.__initOptionalWithDefaultAndOptional()
598        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 2, 1, 123, 0, 0, 0, 0))
599
600    def testOptionalWithDefault(self):
601        s = self.__initOptionalWithDefault()
602        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 2, 1, 123, 0, 0, 0, 0))
603
604    def testOptionalWithOptional(self):
605        s = self.__initOptionalWithOptional()
606        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 0, 0, 0, 0))
607
608    def testOptional(self):
609        s = self.__initOptional()
610        assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
611
612    def testDefaultWithDefaultAndOptional(self):
613        s = self.__initDefaultWithDefaultAndOptional()
614        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 2, 1, 123, 0, 0, 0, 0))
615
616    def testDefaultWithDefault(self):
617        s = self.__initDefaultWithDefault()
618        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 0, 0, 0, 0))
619
620    def testDefaultWithOptional(self):
621        s = self.__initDefaultWithOptional()
622        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 2, 1, 123, 0, 0, 0, 0))
623
624
625class NestedOptionalChoiceEncoderTestCase(BaseTestCase):
626    def setUp(self):
627        BaseTestCase.setUp(self)
628        layer3 = univ.Sequence(
629            componentType=namedtype.NamedTypes(
630                namedtype.OptionalNamedType('first-name', univ.OctetString()),
631                namedtype.DefaultedNamedType('age', univ.Integer(33)),
632            )
633        )
634
635        layer2 = univ.Choice(
636            componentType=namedtype.NamedTypes(
637                namedtype.NamedType('inner', layer3),
638                namedtype.NamedType('first-name', univ.OctetString())
639            )
640        )
641
642        layer1 = univ.Sequence(
643            componentType=namedtype.NamedTypes(
644                namedtype.OptionalNamedType('inner', layer2),
645            )
646        )
647
648        self.s = layer1
649
650    def __initOptionalWithDefaultAndOptional(self):
651        self.s.clear()
652        self.s[0][0][0] = 'test'
653        self.s[0][0][1] = 123
654        return self.s
655
656    def __initOptionalWithDefault(self):
657        self.s.clear()
658        self.s[0][0][1] = 123
659        return self.s
660
661    def __initOptionalWithOptional(self):
662        self.s.clear()
663        self.s[0][0][0] = 'test'
664        return self.s
665
666    def __initOptional(self):
667        self.s.clear()
668        return self.s
669
670    def testOptionalWithDefaultAndOptional(self):
671        s = self.__initOptionalWithDefaultAndOptional()
672        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 2, 1, 123, 0, 0, 0, 0))
673
674    def testOptionalWithDefault(self):
675        s = self.__initOptionalWithDefault()
676        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 2, 1, 123, 0, 0, 0, 0))
677
678    def testOptionalWithOptional(self):
679        s = self.__initOptionalWithOptional()
680        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 0, 0, 0, 0))
681
682    def testOptional(self):
683        s = self.__initOptional()
684        assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
685
686
687class NestedOptionalSequenceOfEncoderTestCase(BaseTestCase):
688    def setUp(self):
689        BaseTestCase.setUp(self)
690        layer2 = univ.SequenceOf(
691            componentType=univ.OctetString()
692        )
693
694        layer1 = univ.Sequence(
695            componentType=namedtype.NamedTypes(
696                namedtype.OptionalNamedType('inner', layer2),
697            )
698        )
699
700        self.s = layer1
701
702    def __initOptionalWithValue(self):
703        self.s.clear()
704        self.s[0][0] = 'test'
705        return self.s
706
707    def __initOptional(self):
708        self.s.clear()
709        return self.s
710
711    def testOptionalWithValue(self):
712        s = self.__initOptionalWithValue()
713        assert encoder.encode(s) == ints2octs((48, 128, 48, 128, 4, 4, 116, 101, 115, 116, 0, 0, 0, 0))
714
715    def testOptional(self):
716        s = self.__initOptional()
717        assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
718
719
720suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
721
722if __name__ == '__main__':
723    unittest.TextTestRunner(verbosity=2).run(suite)
724