diff options
-rw-r--r-- | pyasn1/codec/ber/decoder.py | 44 | ||||
-rw-r--r-- | tests/codec/ber/test_decoder.py | 279 | ||||
-rw-r--r-- | tests/codec/ber/test_encoder.py | 154 |
3 files changed, 450 insertions, 27 deletions
diff --git a/pyasn1/codec/ber/decoder.py b/pyasn1/codec/ber/decoder.py index 7c4b53e..8b16740 100644 --- a/pyasn1/codec/ber/decoder.py +++ b/pyasn1/codec/ber/decoder.py @@ -359,21 +359,17 @@ class RealDecoder(AbstractSimpleDecoder): class AbstractConstructedDecoder(AbstractDecoder): protoComponent = None - orderedComponents = False - recordType = None - sequenceType = None # noinspection PyUnusedLocal def _createComponent(self, asn1Spec, tagSet, value=noValue): if asn1Spec is None: - if self.protoComponent is not None: - return self.protoComponent.clone(tagSet=tagSet) + return self.protoComponent.clone(tagSet=tagSet) else: return asn1Spec.clone() class UniversalConstructedTypeDecoder(AbstractConstructedDecoder): - protoComponent = None - orderedComponents = False + protoRecordComponent = None + protoSequenceComponent = None def _getComponentTagMap(self, asn1Object, idx): raise NotImplementedError() @@ -397,9 +393,9 @@ class UniversalConstructedTypeDecoder(AbstractConstructedDecoder): # * 1+ components of the same type -> likely SEQUENCE OF/SET OF # * otherwise -> likely SEQUENCE/SET if len(components) > 1 or len(componentTypes) > 1: - asn1Object = self.recordType.clone() + asn1Object = self.protoRecordComponent.clone() else: - asn1Object = self.sequenceType.clone() + asn1Object = self.protoSequenceComponent.clone() for idx, component in enumerate(components): asn1Object.setComponentByPosition( @@ -418,13 +414,16 @@ class UniversalConstructedTypeDecoder(AbstractConstructedDecoder): head, tail = substrate[:length], substrate[length:] if substrateFun is not None: - asn1Object = self._createComponent(asn1Spec, tagSet) + if asn1Spec is not None or self.protoComponent is not None: + asn1Object = self._createComponent(asn1Spec, tagSet) + else: + asn1Object = self.protoRecordComponent, self.protoSequenceComponent return substrateFun(asn1Object, substrate, length) if asn1Spec is None: asn1Object, trailing = self._decodeComponents(head, decodeFun) if trailing: - raise error.PyAsn1Error('Unused trailing %d octets found' % len(trailing)) + raise error.PyAsn1Error('Unused trailing %d octets encountered' % len(trailing)) return asn1Object, tail asn1Object = self._createComponent(asn1Spec, tagSet) @@ -434,7 +433,8 @@ class UniversalConstructedTypeDecoder(AbstractConstructedDecoder): namedTypes = asn1Object.componentType if (namedTypes.hasOptionalOrDefault or - not self.orderedComponents or not namedTypes): + asn1Object.typeId == univ.Set.typeId or + not namedTypes): seenIndices = set() idx = 0 while head: @@ -488,14 +488,14 @@ class UniversalConstructedTypeDecoder(AbstractConstructedDecoder): raise error.PyAsn1Error('Constructed tag format expected') if substrateFun is not None: - asn1Object = self._createComponent(asn1Spec, tagSet) + if asn1Spec is not None or self.protoComponent is not None: + asn1Object = self._createComponent(asn1Spec, tagSet) + else: + asn1Object = self.protoRecordComponent, self.protoSequenceComponent return substrateFun(asn1Object, substrate, length) if asn1Spec is None: - asn1Object, trailing = self._decodeComponents(substrate, decodeFun, allowEoo=True) - if trailing: - raise error.PyAsn1Error('Unused trailing %d octets found' % len(trailing)) - return asn1Object, trailing + return self._decodeComponents(substrate, decodeFun, allowEoo=True) asn1Object = self._createComponent(asn1Spec, tagSet) @@ -571,13 +571,12 @@ class UniversalConstructedTypeDecoder(AbstractConstructedDecoder): return asn1Object, substrate class SequenceOrSequenceOfDecoder(UniversalConstructedTypeDecoder): - recordType = univ.Sequence() - sequenceType = univ.SequenceOf() + protoRecordComponent = univ.Sequence() + protoSequenceComponent = univ.SequenceOf() class SequenceDecoder(SequenceOrSequenceOfDecoder): protoComponent = univ.Sequence() - orderedComponents = True def _getComponentTagMap(self, asn1Object, idx): try: @@ -594,13 +593,12 @@ class SequenceOfDecoder(SequenceOrSequenceOfDecoder): class SetOrSetOfDecoder(UniversalConstructedTypeDecoder): - recordType = univ.Set() - sequenceType = univ.SetOf() + protoRecordComponent = univ.Set() + protoSequenceComponent = univ.SetOf() class SetDecoder(SetOrSetOfDecoder): protoComponent = univ.Set() - orderedComponents = False def _getComponentTagMap(self, asn1Object, idx): return asn1Object.componentType.tagMapUnique diff --git a/tests/codec/ber/test_decoder.py b/tests/codec/ber/test_decoder.py index 0a53bcf..2f61acd 100644 --- a/tests/codec/ber/test_decoder.py +++ b/tests/codec/ber/test_decoder.py @@ -507,6 +507,94 @@ class SequenceOfDecoderTestCase(unittest.TestCase): 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)) ) == (self.s, null) + def testUnguidedDecoder(self): + assert decoder.decode( + ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=univ.SequenceOf() + ) == (self.s, null) + + +class GuidedSequenceOfDecoderTestCase(unittest.TestCase): + def setUp(self): + self.s = univ.SequenceOf(componentType=univ.OctetString()) + self.s.setComponentByPosition(0, univ.OctetString('quick brown')) + + def testDefMode(self): + assert decoder.decode( + ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s + ) == (self.s, null) + + def testIndefMode(self): + assert decoder.decode( + ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testDefModeChunked(self): + assert decoder.decode( + ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s + ) == (self.s, null) + + def testIndefModeChunked(self): + assert decoder.decode( + ints2octs((48, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + +class SetOfDecoderTestCase(unittest.TestCase): + def setUp(self): + self.s = univ.SetOf(componentType=univ.OctetString()) + self.s.setComponentByPosition(0, univ.OctetString('quick brown')) + + def testDefMode(self): + assert decoder.decode( + ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)) + ) == (self.s, null) + + def testIndefMode(self): + assert decoder.decode( + ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)) + ) == (self.s, null) + + def testDefModeChunked(self): + assert decoder.decode( + ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)) + ) == (self.s, null) + + def testIndefModeChunked(self): + assert decoder.decode( + 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)) + ) == (self.s, null) + + def testUnguidedDecoder(self): + assert decoder.decode( + ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=univ.SetOf() + ) == (self.s, null) + + +class GuidedSetOfDecoderTestCase(unittest.TestCase): + def setUp(self): + self.s = univ.SetOf(componentType=univ.OctetString()) + self.s.setComponentByPosition(0, univ.OctetString('quick brown')) + + def testDefMode(self): + assert decoder.decode( + ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s + ) == (self.s, null) + + def testIndefMode(self): + assert decoder.decode( + ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testDefModeChunked(self): + assert decoder.decode( + ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s + ) == (self.s, null) + + def testIndefModeChunked(self): + assert decoder.decode( + ints2octs((49, 128, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + class SequenceDecoderTestCase(unittest.TestCase): def setUp(self): @@ -528,8 +616,7 @@ class SequenceDecoderTestCase(unittest.TestCase): def testWithOptionalAndDefaultedIndefMode(self): assert decoder.decode( - ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, - 0, 0)) + ints2octs((48, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)) ) == (self.s, null) def testWithOptionalAndDefaultedDefModeChunked(self): @@ -540,8 +627,7 @@ class SequenceDecoderTestCase(unittest.TestCase): def testWithOptionalAndDefaultedIndefModeChunked(self): assert decoder.decode( - 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)) + 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)) ) == (self.s, null) def testWithOptionalAndDefaultedDefModeSubst(self): @@ -705,6 +791,191 @@ class GuidedSequenceDecoderTestCase(unittest.TestCase): ) == (self.s, null) +class SetDecoderTestCase(unittest.TestCase): + def setUp(self): + self.s = univ.Set( + componentType=namedtype.NamedTypes( + namedtype.NamedType('place-holder', univ.Null(null)), + namedtype.NamedType('first-name', univ.OctetString(null)), + namedtype.NamedType('age', univ.Integer(33)) + ) + ) + self.s.setComponentByPosition(0, univ.Null(null)) + self.s.setComponentByPosition(1, univ.OctetString('quick brown')) + self.s.setComponentByPosition(2, univ.Integer(1)) + + def testWithOptionalAndDefaultedDefMode(self): + assert decoder.decode( + ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)) + ) == (self.s, null) + + def testWithOptionalAndDefaultedIndefMode(self): + assert decoder.decode( + ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)) + ) == (self.s, null) + + def testWithOptionalAndDefaultedDefModeChunked(self): + assert decoder.decode( + 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)) + ) == (self.s, null) + + def testWithOptionalAndDefaultedIndefModeChunked(self): + assert decoder.decode( + 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)) + ) == (self.s, null) + + def testWithOptionalAndDefaultedDefModeSubst(self): + assert decoder.decode( + ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), + substrateFun=lambda a, b, c: (b, c) + ) == (ints2octs((5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), 18) + + def testWithOptionalAndDefaultedIndefModeSubst(self): + assert decoder.decode( + ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), + substrateFun=lambda a, b, c: (b, c) + ) == (ints2octs( + (5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), -1) + + def testTagFormat(self): + try: + decoder.decode( + ints2octs((16, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)) + ) + except PyAsn1Error: + pass + else: + assert 0, 'wrong tagFormat worked out' + + +class GuidedSetDecoderTestCase(unittest.TestCase): + def setUp(self): + self.s = univ.Set( + componentType=namedtype.NamedTypes( + namedtype.NamedType('place-holder', univ.Null(null)), + namedtype.OptionalNamedType('first-name', univ.OctetString()), + namedtype.DefaultedNamedType('age', univ.Integer(33)), + ) + ) + + def __init(self): + self.s.clear() + self.s.setComponentByPosition(0, univ.Null(null)) + + def __initWithOptional(self): + self.s.clear() + self.s.setComponentByPosition(0, univ.Null(null)) + self.s.setComponentByPosition(1, univ.OctetString('quick brown')) + + def __initWithDefaulted(self): + self.s.clear() + self.s.setComponentByPosition(0, univ.Null(null)) + self.s.setComponentByPosition(2, univ.Integer(1)) + + def __initWithOptionalAndDefaulted(self): + self.s.clear() + self.s.setComponentByPosition(0, univ.Null(null)) + self.s.setComponentByPosition(1, univ.OctetString('quick brown')) + self.s.setComponentByPosition(2, univ.Integer(1)) + + def testDefMode(self): + self.__init() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testIndefMode(self): + self.__init() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testDefModeChunked(self): + self.__init() + assert decoder.decode( + ints2octs((49, 2, 5, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testIndefModeChunked(self): + self.__init() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testWithOptionalDefMode(self): + self.__initWithOptional() + assert decoder.decode( + ints2octs((49, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s + ) == (self.s, null) + + def testWithOptionaIndefMode(self): + self.__initWithOptional() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testWithOptionalDefModeChunked(self): + self.__initWithOptional() + assert decoder.decode( + ints2octs((49, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)), asn1Spec=self.s + ) == (self.s, null) + + def testWithOptionalIndefModeChunked(self): + self.__initWithOptional() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testWithDefaultedDefMode(self): + self.__initWithDefaulted() + assert decoder.decode( + ints2octs((49, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s + ) == (self.s, null) + + def testWithDefaultedIndefMode(self): + self.__initWithDefaulted() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testWithDefaultedDefModeChunked(self): + self.__initWithDefaulted() + assert decoder.decode( + ints2octs((49, 5, 5, 0, 2, 1, 1)), asn1Spec=self.s + ) == (self.s, null) + + def testWithDefaultedIndefModeChunked(self): + self.__initWithDefaulted() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testWithOptionalAndDefaultedDefMode(self): + self.__initWithOptionalAndDefaulted() + assert decoder.decode( + ints2octs((49, 18, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1)), asn1Spec=self.s + ) == (self.s, null) + + def testWithOptionalAndDefaultedIndefMode(self): + self.__initWithOptionalAndDefaulted() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 36, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + def testWithOptionalAndDefaultedDefModeChunked(self): + self.__initWithOptionalAndDefaulted() + assert decoder.decode( + ints2octs((49, 24, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 2, 1, 1)), asn1Spec=self.s + ) == (self.s, null) + + def testWithOptionalAndDefaultedIndefModeChunked(self): + self.__initWithOptionalAndDefaulted() + assert decoder.decode( + ints2octs((49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 2, 1, 1, 0, 0)), asn1Spec=self.s + ) == (self.s, null) + + class ChoiceDecoderTestCase(unittest.TestCase): def setUp(self): self.s = univ.Choice( diff --git a/tests/codec/ber/test_encoder.py b/tests/codec/ber/test_encoder.py index dda2596..8bcdb10 100644 --- a/tests/codec/ber/test_encoder.py +++ b/tests/codec/ber/test_encoder.py @@ -373,6 +373,37 @@ class SequenceOfEncoderTestCase(unittest.TestCase): ) == 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)) +class SetOfEncoderTestCase(unittest.TestCase): + def setUp(self): + self.s = univ.SequenceOf(componentType=univ.OctetString()) + + def __init(self): + self.s.clear() + self.s.setComponentByPosition(0, 'quick brown') + + def testDefMode(self): + self.__init() + assert encoder.encode(self.s) == ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)) + + def testIndefMode(self): + self.__init() + assert encoder.encode( + self.s, defMode=False + ) == ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)) + + def testDefModeChunked(self): + self.__init() + assert encoder.encode( + self.s, defMode=True, maxChunkSize=4 + ) == ints2octs((48, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)) + + def testIndefModeChunked(self): + self.__init() + assert encoder.encode( + self.s, defMode=False, maxChunkSize=4 + ) == 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)) + + class SequenceEncoderTestCase(unittest.TestCase): def setUp(self): self.s = univ.Sequence( @@ -497,6 +528,129 @@ class SequenceEncoderTestCase(unittest.TestCase): 0, 2, 1, 1, 0, 0)) +class SetEncoderTestCase(unittest.TestCase): + def setUp(self): + self.s = univ.Set( + componentType=namedtype.NamedTypes( + namedtype.NamedType('place-holder', univ.Null()), + namedtype.OptionalNamedType('first-name', univ.OctetString()), + namedtype.DefaultedNamedType('age', univ.Integer(33)), + ) + ) + + def __init(self): + self.s.clear() + self.s.setComponentByPosition(0) + + def __initWithOptional(self): + self.s.clear() + self.s.setComponentByPosition(0) + self.s.setComponentByPosition(1, 'quick brown') + + def __initWithDefaulted(self): + self.s.clear() + self.s.setComponentByPosition(0, '') + self.s.setComponentByPosition(2, 1) + + def __initWithOptionalAndDefaulted(self): + self.s.clear() + self.s.setComponentByPosition(0, univ.Null('')) + self.s.setComponentByPosition(1, univ.OctetString('quick brown')) + self.s.setComponentByPosition(2, univ.Integer(1)) + + def testDefMode(self): + self.__init() + assert encoder.encode(self.s) == ints2octs((49, 2, 5, 0)) + + def testIndefMode(self): + self.__init() + assert encoder.encode( + self.s, defMode=False + ) == ints2octs((49, 128, 5, 0, 0, 0)) + + def testDefModeChunked(self): + self.__init() + assert encoder.encode( + self.s, defMode=True, maxChunkSize=4 + ) == ints2octs((49, 2, 5, 0)) + + def testIndefModeChunked(self): + self.__init() + assert encoder.encode( + self.s, defMode=False, maxChunkSize=4 + ) == ints2octs((49, 128, 5, 0, 0, 0)) + + def testWithOptionalDefMode(self): + self.__initWithOptional() + assert encoder.encode(self.s) == ints2octs( + (49, 15, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)) + + def testWithOptionalIndefMode(self): + self.__initWithOptional() + assert encoder.encode( + self.s, defMode=False + ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0)) + + def testWithOptionalDefModeChunked(self): + self.__initWithOptional() + assert encoder.encode( + self.s, defMode=True, maxChunkSize=4 + ) == ints2octs((49, 21, 5, 0, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110)) + + def testWithOptionalIndefModeChunked(self): + self.__initWithOptional() + assert encoder.encode( + self.s, defMode=False, maxChunkSize=4 + ) == ints2octs( + (49, 128, 5, 0, 36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0, 0, 0)) + + def testWithDefaultedDefMode(self): + self.__initWithDefaulted() + assert encoder.encode(self.s) == ints2octs((49, 5, 5, 0, 2, 1, 1)) + + def testWithDefaultedIndefMode(self): + self.__initWithDefaulted() + assert encoder.encode( + self.s, defMode=False + ) == ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)) + + def testWithDefaultedDefModeChunked(self): + self.__initWithDefaulted() + assert encoder.encode( + self.s, defMode=True, maxChunkSize=4 + ) == ints2octs((49, 5, 5, 0, 2, 1, 1)) + + def testWithDefaultedIndefModeChunked(self): + self.__initWithDefaulted() + assert encoder.encode( + self.s, defMode=False, maxChunkSize=4 + ) == ints2octs((49, 128, 5, 0, 2, 1, 1, 0, 0)) + + def testWithOptionalAndDefaultedDefMode(self): + self.__initWithOptionalAndDefaulted() + 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)) + + def testWithOptionalAndDefaultedIndefMode(self): + self.__initWithOptionalAndDefaulted() + assert encoder.encode( + self.s, defMode=False + ) == ints2octs((49, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0)) + + def testWithOptionalAndDefaultedDefModeChunked(self): + self.__initWithOptionalAndDefaulted() + assert encoder.encode( + self.s, defMode=True, maxChunkSize=4 + ) == 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)) + + def testWithOptionalAndDefaultedIndefModeChunked(self): + self.__initWithOptionalAndDefaulted() + assert encoder.encode( + self.s, defMode=False, maxChunkSize=4 + ) == 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)) + + class ChoiceEncoderTestCase(unittest.TestCase): def setUp(self): self.s = univ.Choice( |