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 univ
20from pyasn1.codec.der import encoder
21from pyasn1.compat.octets import ints2octs
22
23
24class OctetStringEncoderTestCase(BaseTestCase):
25    def testDefModeShort(self):
26        assert encoder.encode(
27            univ.OctetString('Quick brown fox')
28        ) == ints2octs((4, 15, 81, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120))
29
30    def testDefModeLong(self):
31        assert encoder.encode(
32            univ.OctetString('Q' * 10000)
33        ) == ints2octs((4, 130, 39, 16) + (81,) * 10000)
34
35
36class BitStringEncoderTestCase(BaseTestCase):
37    def testDefModeShort(self):
38        assert encoder.encode(
39            univ.BitString((1,))
40        ) == ints2octs((3, 2, 7, 128))
41
42    def testDefModeLong(self):
43        assert encoder.encode(
44            univ.BitString((1,) * 80000)
45        ) == ints2octs((3, 130, 39, 17, 0) + (255,) * 10000)
46
47
48class SetOfEncoderTestCase(BaseTestCase):
49    def setUp(self):
50        BaseTestCase.setUp(self)
51
52        self.s = univ.SetOf(componentType=univ.OctetString())
53
54    def testDefMode1(self):
55        self.s.clear()
56        self.s.append('a')
57        self.s.append('ab')
58
59        assert encoder.encode(self.s) == ints2octs((49, 7, 4, 1, 97, 4, 2, 97, 98))
60
61    def testDefMode2(self):
62        self.s.clear()
63        self.s.append('ab')
64        self.s.append('a')
65
66        assert encoder.encode(self.s) == ints2octs((49, 7, 4, 1, 97, 4, 2, 97, 98))
67
68    def testDefMode3(self):
69        self.s.clear()
70        self.s.append('b')
71        self.s.append('a')
72
73        assert encoder.encode(self.s) == ints2octs((49, 6, 4, 1, 97, 4, 1, 98))
74
75    def testDefMode4(self):
76        self.s.clear()
77        self.s.append('a')
78        self.s.append('b')
79
80        assert encoder.encode(self.s) == ints2octs((49, 6, 4, 1, 97, 4, 1, 98))
81
82
83class SetWithAlternatingChoiceEncoderTestCase(BaseTestCase):
84    def setUp(self):
85        BaseTestCase.setUp(self)
86
87        c = univ.Choice(componentType=namedtype.NamedTypes(
88            namedtype.NamedType('name', univ.OctetString()),
89            namedtype.NamedType('amount', univ.Boolean()))
90        )
91
92        self.s = univ.Set(componentType=namedtype.NamedTypes(
93            namedtype.NamedType('value', univ.Integer(5)),
94            namedtype.NamedType('status', c))
95        )
96
97    def testComponentsOrdering1(self):
98        self.s.setComponentByName('status')
99        self.s.getComponentByName('status').setComponentByPosition(0, 'A')
100        assert encoder.encode(self.s) == ints2octs((49, 6, 2, 1, 5, 4, 1, 65))
101
102    def testComponentsOrdering2(self):
103        self.s.setComponentByName('status')
104        self.s.getComponentByName('status').setComponentByPosition(1, True)
105        assert encoder.encode(self.s) == ints2octs((49, 6, 1, 1, 255, 2, 1, 5))
106
107
108class SetWithTaggedChoiceEncoderTestCase(BaseTestCase):
109
110    def testWithUntaggedChoice(self):
111
112        c = univ.Choice(
113            componentType=namedtype.NamedTypes(
114                namedtype.NamedType('premium', univ.Boolean())
115            )
116        )
117
118        s = univ.Set(
119            componentType=namedtype.NamedTypes(
120                namedtype.NamedType('name', univ.OctetString()),
121                namedtype.NamedType('customer', c)
122            )
123        )
124
125        s.setComponentByName('name', 'A')
126        s.getComponentByName('customer').setComponentByName('premium', True)
127
128        assert encoder.encode(s) == ints2octs((49, 6, 1, 1, 255, 4, 1, 65))
129
130    def testWithTaggedChoice(self):
131
132        c = univ.Choice(
133            componentType=namedtype.NamedTypes(
134                namedtype.NamedType('premium', univ.Boolean())
135            )
136        ).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 7))
137
138        s = univ.Set(
139            componentType=namedtype.NamedTypes(
140                namedtype.NamedType('name', univ.OctetString()),
141                namedtype.NamedType('customer', c)
142            )
143        )
144
145        s.setComponentByName('name', 'A')
146        s.getComponentByName('customer').setComponentByName('premium', True)
147
148        assert encoder.encode(s) == ints2octs((49, 8, 4, 1, 65, 167, 3, 1, 1, 255))
149
150
151class NestedOptionalSequenceEncoderTestCase(BaseTestCase):
152    def setUp(self):
153        BaseTestCase.setUp(self)
154
155        inner = univ.Sequence(
156            componentType=namedtype.NamedTypes(
157                namedtype.OptionalNamedType('first-name', univ.OctetString()),
158                namedtype.DefaultedNamedType('age', univ.Integer(33)),
159            )
160        )
161
162        outerWithOptional = univ.Sequence(
163            componentType=namedtype.NamedTypes(
164                namedtype.OptionalNamedType('inner', inner),
165            )
166        )
167
168        outerWithDefault = univ.Sequence(
169            componentType=namedtype.NamedTypes(
170                namedtype.DefaultedNamedType('inner', inner),
171            )
172        )
173
174        self.s1 = outerWithOptional
175        self.s2 = outerWithDefault
176
177    def __initOptionalWithDefaultAndOptional(self):
178        self.s1.clear()
179        self.s1[0][0] = 'test'
180        self.s1[0][1] = 123
181        return self.s1
182
183    def __initOptionalWithDefault(self):
184        self.s1.clear()
185        self.s1[0][1] = 123
186        return self.s1
187
188    def __initOptionalWithOptional(self):
189        self.s1.clear()
190        self.s1[0][0] = 'test'
191        return self.s1
192
193    def __initOptional(self):
194        self.s1.clear()
195        return self.s1
196
197    def __initDefaultWithDefaultAndOptional(self):
198        self.s2.clear()
199        self.s2[0][0] = 'test'
200        self.s2[0][1] = 123
201        return self.s2
202
203    def __initDefaultWithDefault(self):
204        self.s2.clear()
205        self.s2[0][0] = 'test'
206        return self.s2
207
208    def __initDefaultWithOptional(self):
209        self.s2.clear()
210        self.s2[0][1] = 123
211        return self.s2
212
213    def testDefModeOptionalWithDefaultAndOptional(self):
214        s = self.__initOptionalWithDefaultAndOptional()
215        assert encoder.encode(s) == ints2octs((48, 11, 48, 9, 4, 4, 116, 101, 115, 116, 2, 1, 123))
216
217    def testDefModeOptionalWithDefault(self):
218        s = self.__initOptionalWithDefault()
219        assert encoder.encode(s) == ints2octs((48, 5, 48, 3, 2, 1, 123))
220
221    def testDefModeOptionalWithOptional(self):
222        s = self.__initOptionalWithOptional()
223        assert encoder.encode(s) == ints2octs((48, 8, 48, 6, 4, 4, 116, 101, 115, 116))
224
225    def testDefModeOptional(self):
226        s = self.__initOptional()
227        assert encoder.encode(s) == ints2octs((48, 0))
228
229    def testDefModeDefaultWithDefaultAndOptional(self):
230        s = self.__initDefaultWithDefaultAndOptional()
231        assert encoder.encode(s) == ints2octs((48, 11, 48, 9, 4, 4, 116, 101, 115, 116, 2, 1, 123))
232
233    def testDefModeDefaultWithDefault(self):
234        s = self.__initDefaultWithDefault()
235        assert encoder.encode(s) == ints2octs((48, 8, 48, 6, 4, 4, 116, 101, 115, 116))
236
237    def testDefModeDefaultWithOptional(self):
238        s = self.__initDefaultWithOptional()
239        assert encoder.encode(s) == ints2octs((48, 5, 48, 3, 2, 1, 123))
240
241
242class NestedOptionalChoiceEncoderTestCase(BaseTestCase):
243    def setUp(self):
244        BaseTestCase.setUp(self)
245
246        layer3 = univ.Sequence(
247            componentType=namedtype.NamedTypes(
248                namedtype.OptionalNamedType('first-name', univ.OctetString()),
249                namedtype.DefaultedNamedType('age', univ.Integer(33)),
250            )
251        )
252
253        layer2 = univ.Choice(
254            componentType=namedtype.NamedTypes(
255                namedtype.NamedType('inner', layer3),
256                namedtype.NamedType('first-name', univ.OctetString())
257            )
258        )
259
260        layer1 = univ.Sequence(
261            componentType=namedtype.NamedTypes(
262                namedtype.OptionalNamedType('inner', layer2),
263            )
264        )
265
266        self.s = layer1
267
268    def __initOptionalWithDefaultAndOptional(self):
269        self.s.clear()
270        self.s[0][0][0] = 'test'
271        self.s[0][0][1] = 123
272        return self.s
273
274    def __initOptionalWithDefault(self):
275        self.s.clear()
276        self.s[0][0][1] = 123
277        return self.s
278
279    def __initOptionalWithOptional(self):
280        self.s.clear()
281        self.s[0][0][0] = 'test'
282        return self.s
283
284    def __initOptional(self):
285        self.s.clear()
286        return self.s
287
288    def testDefModeOptionalWithDefaultAndOptional(self):
289        s = self.__initOptionalWithDefaultAndOptional()
290        assert encoder.encode(s) == ints2octs((48, 11, 48, 9, 4, 4, 116, 101, 115, 116, 2, 1, 123))
291
292    def testDefModeOptionalWithDefault(self):
293        s = self.__initOptionalWithDefault()
294        assert encoder.encode(s) == ints2octs((48, 5, 48, 3, 2, 1, 123))
295
296    def testDefModeOptionalWithOptional(self):
297        s = self.__initOptionalWithOptional()
298        assert encoder.encode(s) == ints2octs((48, 8, 48, 6, 4, 4, 116, 101, 115, 116))
299
300    def testDefModeOptional(self):
301        s = self.__initOptional()
302        assert encoder.encode(s) == ints2octs((48, 0))
303
304
305class NestedOptionalSequenceOfEncoderTestCase(BaseTestCase):
306    def setUp(self):
307        BaseTestCase.setUp(self)
308
309        layer2 = univ.SequenceOf(
310            componentType=univ.OctetString()
311        )
312
313        layer1 = univ.Sequence(
314            componentType=namedtype.NamedTypes(
315                namedtype.OptionalNamedType('inner', layer2),
316            )
317        )
318
319        self.s = layer1
320
321    def __initOptionalWithValue(self):
322        self.s.clear()
323        self.s[0][0] = 'test'
324        return self.s
325
326    def __initOptional(self):
327        self.s.clear()
328        return self.s
329
330    def testDefModeOptionalWithValue(self):
331        s = self.__initOptionalWithValue()
332        assert encoder.encode(s) == ints2octs((48, 8, 48, 6, 4, 4, 116, 101, 115, 116))
333
334    def testDefModeOptional(self):
335        s = self.__initOptional()
336        assert encoder.encode(s) == ints2octs((48, 0))
337
338
339class EmptyInnerFieldOfSequenceEncoderTestCase(BaseTestCase):
340
341    def testInitializedOptionalNullIsEncoded(self):
342        self.s = univ.Sequence(
343            componentType=namedtype.NamedTypes(
344                namedtype.OptionalNamedType('null', univ.Null())
345            )
346        )
347
348        self.s.clear()
349        self.s[0] = ''
350        assert encoder.encode(self.s) == ints2octs((48, 2, 5, 0))
351
352    def testUninitializedOptionalNullIsNotEncoded(self):
353        self.s = univ.Sequence(
354            componentType=namedtype.NamedTypes(
355                namedtype.OptionalNamedType('null', univ.Null())
356            )
357        )
358
359        self.s.clear()
360        assert encoder.encode(self.s) == ints2octs((48, 0))
361
362    def testInitializedDefaultNullIsNotEncoded(self):
363        self.s = univ.Sequence(
364            componentType=namedtype.NamedTypes(
365                namedtype.DefaultedNamedType('null', univ.Null(''))
366            )
367        )
368
369        self.s.clear()
370        self.s[0] = ''
371        assert encoder.encode(self.s) == ints2octs((48, 0))
372
373    def testInitializedOptionalOctetStringIsEncoded(self):
374        self.s = univ.Sequence(
375            componentType=namedtype.NamedTypes(
376                namedtype.OptionalNamedType('str', univ.OctetString())
377            )
378        )
379
380        self.s.clear()
381        self.s[0] = ''
382        assert encoder.encode(self.s) == ints2octs((48, 2, 4, 0))
383
384    def testUninitializedOptionalOctetStringIsNotEncoded(self):
385        self.s = univ.Sequence(
386            componentType=namedtype.NamedTypes(
387                namedtype.OptionalNamedType('str', univ.OctetString())
388            )
389        )
390
391        self.s.clear()
392        assert encoder.encode(self.s) == ints2octs((48, 0))
393
394    def testInitializedDefaultOctetStringIsNotEncoded(self):
395        self.s = univ.Sequence(
396            componentType=namedtype.NamedTypes(
397                namedtype.DefaultedNamedType('str', univ.OctetString(''))
398            )
399        )
400
401        self.s.clear()
402        self.s[0] = ''
403        assert encoder.encode(self.s) == ints2octs((48, 0))
404
405
406suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
407
408if __name__ == '__main__':
409    unittest.TextTestRunner(verbosity=2).run(suite)
410