aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.rst10
-rw-r--r--pyasn1/codec/cer/encoder.py5
-rw-r--r--pyasn1/type/univ.py2
-rw-r--r--tests/codec/ber/test_decoder.py12
-rw-r--r--tests/codec/ber/test_encoder.py169
-rw-r--r--tests/codec/cer/test_encoder.py364
6 files changed, 477 insertions, 85 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 92c7a67..c0fae44 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -2,6 +2,8 @@
Revision 0.3.4, released XX-08-2017
-----------------------------------
+- Fixed crash at SEQUENCE and SEQUENCE OF CER encoder when running
+ in schemaless mode
- Fixed Character types instantiation from OctetString type -- double
unicode decoding may have scrambled the data
@@ -14,7 +16,7 @@ Revision 0.3.3, released 27-08-2017
- Fixed exponential index size growth bug when building ambiguous
NamedTypes tree
- Fixed constructed types decoding failure at BER codec if running
- in unguided mode
+ in schema-less mode
- Fixed crash on prettyPrint'ing a SEQUENCE with no defined components
- Fixed SetOf ordering at CER/DER encoder
- Fixed crash on conditional binascii module import
@@ -385,7 +387,8 @@ Revision 0.0.13b
- Objects of Constructed types now support __setitem__()
- Set/Sequence objects can now be addressed by their field names (string index)
and position (integer index).
-- Typo fix to ber.SetDecoder code that prevented guided decoding operation.
+- Typo fix to ber.SetDecoder code that prevented with schema decoding
+ operation.
- Fix to explicitly tagged items decoding support.
- Fix to OctetString.prettyPrint() to better handle non-printable content.
- Fix to repr() workings of Choice objects.
@@ -409,7 +412,8 @@ Revision 0.0.13a
+ tag and tagset caches introduced to decoder
+ decoder code improved to prevent unnecessary pyasn1 objects creation
+ allow disabling components verification when setting components to
- structured types, this is used by decoder whilst running in guided mode.
+ structured types, this is used by decoder whilst running with schema
+ mode.
+ BER decoder for integer values now looks up a small set of pre-computed
substrate values to save on decoding.
+ a few pre-computed values configured to ObjectIdentifier BER encoder.
diff --git a/pyasn1/codec/cer/encoder.py b/pyasn1/codec/cer/encoder.py
index 0d4f566..1ad013e 100644
--- a/pyasn1/codec/cer/encoder.py
+++ b/pyasn1/codec/cer/encoder.py
@@ -115,7 +115,7 @@ class SetOfEncoder(encoder.SequenceOfEncoder):
continue
comps.append(value[idx])
- compsMap[id(value[idx])] = namedTypes[idx].isOptional
+ compsMap[id(value[idx])] = namedTypes and namedTypes[idx].isOptional
for comp in self._sortComponents(comps):
substrate += encodeFun(comp, defMode, maxChunkSize, ifNotEmpty=compsMap[id(comp)])
@@ -153,7 +153,8 @@ class SequenceEncoder(encoder.SequenceEncoder):
if namedTypes[idx].isDefaulted and value[idx] == namedTypes[idx].asn1Object:
continue
- substrate = encodeFun(value[idx], defMode, maxChunkSize, namedTypes[idx].isOptional) + substrate
+ substrate = encodeFun(value[idx], defMode, maxChunkSize,
+ namedTypes and namedTypes[idx].isOptional) + substrate
return substrate, True, True
diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py
index b9b8fa0..b83c3ec 100644
--- a/pyasn1/type/univ.py
+++ b/pyasn1/type/univ.py
@@ -1849,7 +1849,7 @@ class SequenceOfAndSetOfBase(base.AbstractConstructedAsn1Item):
elif currentValue is not noValue and isinstance(currentValue, base.AbstractSimpleAsn1Item):
value = currentValue.clone(value=value)
else:
- raise error.PyAsn1Error('%s undefined component type' % componentType.__class__.__name__)
+ raise error.PyAsn1Error('Non-ASN.1 value %r and undefined component type at %r' % (value, self))
elif componentType is not None:
if self.strictConstraints:
if not componentType.isSameTypeWith(value, matchTags, matchConstraints):
diff --git a/tests/codec/ber/test_decoder.py b/tests/codec/ber/test_decoder.py
index a836250..575cd52 100644
--- a/tests/codec/ber/test_decoder.py
+++ b/tests/codec/ber/test_decoder.py
@@ -507,13 +507,13 @@ 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):
+ def testSchemalessDecoder(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):
+class SequenceOfDecoderWithSchemaTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.SequenceOf(componentType=univ.OctetString())
self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
@@ -564,13 +564,13 @@ class SetOfDecoderTestCase(unittest.TestCase):
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):
+ def testSchemalessDecoder(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):
+class SetOfDecoderWithSchemaTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.SetOf(componentType=univ.OctetString())
self.s.setComponentByPosition(0, univ.OctetString('quick brown'))
@@ -655,7 +655,7 @@ class SequenceDecoderTestCase(unittest.TestCase):
assert 0, 'wrong tagFormat worked out'
-class GuidedSequenceDecoderTestCase(unittest.TestCase):
+class SequenceDecoderWithSchemaTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.Sequence(
componentType=namedtype.NamedTypes(
@@ -849,7 +849,7 @@ class SetDecoderTestCase(unittest.TestCase):
assert 0, 'wrong tagFormat worked out'
-class GuidedSetDecoderTestCase(unittest.TestCase):
+class SetDecoderWithSchemaTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.Set(
componentType=namedtype.NamedTypes(
diff --git a/tests/codec/ber/test_encoder.py b/tests/codec/ber/test_encoder.py
index 8bcdb10..332b071 100644
--- a/tests/codec/ber/test_encoder.py
+++ b/tests/codec/ber/test_encoder.py
@@ -343,6 +343,38 @@ class UTF8StringEncoderTestCase(unittest.TestCase):
class SequenceOfEncoderTestCase(unittest.TestCase):
+ def testEmpty(self):
+ s = univ.SequenceOf()
+ assert encoder.encode(s) == ints2octs((48, 0))
+
+ def testDefMode(self):
+ s = univ.SequenceOf()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(s) == ints2octs((48, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
+
+ def testIndefMode(self):
+ s = univ.SequenceOf()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ s, defMode=False
+ ) == ints2octs((48, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
+
+ def testDefModeChunked(self):
+ s = univ.SequenceOf()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ 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):
+ s = univ.SequenceOf()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ 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 SequenceOfEncoderWithSchemaTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.SequenceOf(componentType=univ.OctetString())
@@ -374,8 +406,40 @@ class SequenceOfEncoderTestCase(unittest.TestCase):
class SetOfEncoderTestCase(unittest.TestCase):
+ def testEmpty(self):
+ s = univ.SetOf()
+ assert encoder.encode(s) == ints2octs((49, 0))
+
+ def testDefMode(self):
+ s = univ.SetOf()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(s) == ints2octs((49, 13, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
+
+ def testIndefMode(self):
+ s = univ.SetOf()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ s, defMode=False
+ ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
+
+ def testDefModeChunked(self):
+ s = univ.SetOf()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ s, defMode=True, maxChunkSize=4
+ ) == ints2octs((49, 19, 36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
+
+ def testIndefModeChunked(self):
+ s = univ.SetOf()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ s, defMode=False, maxChunkSize=4
+ ) == 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))
+
+
+class SetOfEncoderWithSchemaTestCase(unittest.TestCase):
def setUp(self):
- self.s = univ.SequenceOf(componentType=univ.OctetString())
+ self.s = univ.SetOf(componentType=univ.OctetString())
def __init(self):
self.s.clear()
@@ -383,29 +447,55 @@ class SetOfEncoderTestCase(unittest.TestCase):
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))
+ assert encoder.encode(self.s) == ints2octs((49, 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))
+ ) == ints2octs((49, 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))
+ ) == ints2octs((49, 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))
+ ) == 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))
class SequenceEncoderTestCase(unittest.TestCase):
def setUp(self):
+ self.s = univ.Sequence()
+ self.s.setComponentByPosition(0, univ.Null(''))
+ self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
+ self.s.setComponentByPosition(2, univ.Integer(1))
+
+ def testDefMode(self):
+ 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))
+
+ def testIndefMode(self):
+ assert encoder.encode(
+ self.s, defMode=False
+ ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 2, 1, 1, 0, 0))
+
+ def testDefModeChunked(self):
+ assert encoder.encode(
+ self.s, defMode=True, maxChunkSize=4
+ ) == 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))
+
+ def testIndefModeChunked(self):
+ assert encoder.encode(
+ self.s, defMode=False, maxChunkSize=4
+ ) == 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))
+
+
+class SequenceEncoderWithSchemaTestCase(unittest.TestCase):
+ def setUp(self):
self.s = univ.Sequence(
componentType=namedtype.NamedTypes(
namedtype.NamedType('place-holder', univ.Null()),
@@ -530,6 +620,32 @@ class SequenceEncoderTestCase(unittest.TestCase):
class SetEncoderTestCase(unittest.TestCase):
def setUp(self):
+ self.s = univ.Set()
+ self.s.setComponentByPosition(0, univ.Null(''))
+ self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
+ self.s.setComponentByPosition(2, univ.Integer(1))
+
+ def testDefMode(self):
+ 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 testIndefMode(self):
+ 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 testDefModeChunked(self):
+ 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 testIndefModeChunked(self):
+ 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 SetEncoderWithSchemaTestCase(unittest.TestCase):
+ def setUp(self):
self.s = univ.Set(
componentType=namedtype.NamedTypes(
namedtype.NamedType('place-holder', univ.Null()),
@@ -652,6 +768,49 @@ class SetEncoderTestCase(unittest.TestCase):
class ChoiceEncoderTestCase(unittest.TestCase):
+
+ def testEmpty(self):
+ s = univ.Choice()
+ try:
+ encoder.encode(s)
+ except PyAsn1Error:
+ pass
+ else:
+ assert 0, 'encoded unset choice'
+
+ def testDefModeOptionOne(self):
+ s = univ.Choice()
+ s.setComponentByPosition(0, univ.Null(''))
+ assert encoder.encode(s) == ints2octs((5, 0))
+
+ def testDefModeOptionTwo(self):
+ s = univ.Choice()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(s) == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
+
+ def testIndefMode(self):
+ s = univ.Choice()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ s, defMode=False
+ ) == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
+
+ def testDefModeChunked(self):
+ s = univ.Choice()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ s, defMode=True, maxChunkSize=4
+ ) == ints2octs((36, 17, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110))
+
+ def testIndefModeChunked(self):
+ s = univ.Choice()
+ s.setComponentByPosition(0, univ.OctetString('quick brown'))
+ assert encoder.encode(
+ s, defMode=False, maxChunkSize=4
+ ) == ints2octs((36, 128, 4, 4, 113, 117, 105, 99, 4, 4, 107, 32, 98, 114, 4, 3, 111, 119, 110, 0, 0))
+
+
+class ChoiceEncoderWithSchemaTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.Choice(
componentType=namedtype.NamedTypes(
diff --git a/tests/codec/cer/test_encoder.py b/tests/codec/cer/test_encoder.py
index a1419fd..7b0cc8c 100644
--- a/tests/codec/cer/test_encoder.py
+++ b/tests/codec/cer/test_encoder.py
@@ -46,74 +46,6 @@ class OctetStringEncoderTestCase(unittest.TestCase):
) == ints2octs((36, 128, 4, 130, 3, 232) + (81,) * 1000 + (4, 1, 81, 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 testIndefMode(self):
- self.__init()
- assert encoder.encode(self.s) == ints2octs((49, 128, 5, 0, 0, 0))
-
- def testWithOptionalIndefMode(self):
- self.__initWithOptional()
- assert encoder.encode(
- self.s
- ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
-
- def testWithDefaultedIndefMode(self):
- self.__initWithDefaulted()
- assert encoder.encode(
- self.s
- ) == ints2octs((49, 128, 2, 1, 1, 5, 0, 0, 0))
-
- def testWithOptionalAndDefaultedIndefMode(self):
- self.__initWithOptionalAndDefaulted()
- 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))
-
-
-class SetWithChoiceEncoderTestCase(unittest.TestCase):
- def setUp(self):
- c = univ.Choice(componentType=namedtype.NamedTypes(
- namedtype.NamedType('actual', univ.Boolean(0))
- ))
- self.s = univ.Set(componentType=namedtype.NamedTypes(
- namedtype.NamedType('place-holder', univ.Null('')),
- namedtype.NamedType('status', c)
- ))
-
- def testIndefMode(self):
- self.s.setComponentByPosition(0)
- self.s.setComponentByName('status')
- self.s.getComponentByName('status').setComponentByPosition(0, 1)
- assert encoder.encode(self.s) == ints2octs((49, 128, 1, 1, 255, 5, 0, 0, 0))
-
-
class GeneralizedTimeEncoderTestCase(unittest.TestCase):
# def testExtraZeroInSeconds(self):
# try:
@@ -214,10 +146,107 @@ class UTCTimeEncoderTestCase(unittest.TestCase):
) == ints2octs((23, 11, 57, 57, 48, 56, 48, 49, 49, 50, 48, 49, 90))
+class SequenceOfEncoderTestCase(unittest.TestCase):
+ def testEmpty(self):
+ s = univ.SequenceOf()
+ assert encoder.encode(s) == ints2octs((48, 128, 0, 0))
+
+ def testDefMode1(self):
+ s = univ.SequenceOf()
+ s.append(univ.OctetString('a'))
+ s.append(univ.OctetString('ab'))
+ assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
+
+ def testDefMode2(self):
+ s = univ.SequenceOf()
+ s.append(univ.OctetString('ab'))
+ s.append(univ.OctetString('a'))
+ assert encoder.encode(s) == ints2octs((48, 128, 4, 2, 97, 98, 4, 1, 97, 0, 0))
+
+ def testDefMode3(self):
+ s = univ.SequenceOf()
+ s.append(univ.OctetString('b'))
+ s.append(univ.OctetString('a'))
+ assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 98, 4, 1, 97, 0, 0))
+
+ def testDefMode4(self):
+ s = univ.SequenceOf()
+ s.append(univ.OctetString('a'))
+ s.append(univ.OctetString('b'))
+ assert encoder.encode(s) == ints2octs((48, 128, 4, 1, 97, 4, 1, 98, 0, 0))
+
+
+class SequenceOfEncoderWithSchemaTestCase(unittest.TestCase):
+ def setUp(self):
+ self.s = univ.SequenceOf(componentType=univ.OctetString())
+
+ def testEmpty(self):
+ self.s.clear()
+ assert encoder.encode(self.s) == ints2octs((48, 128, 0, 0))
+
+ def testIndefMode1(self):
+ self.s.clear()
+ self.s.append('a')
+ self.s.append('ab')
+ assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
+
+ def testIndefMode2(self):
+ self.s.clear()
+ self.s.append('ab')
+ self.s.append('a')
+ assert encoder.encode(self.s) == ints2octs((48, 128, 4, 2, 97, 98, 4, 1, 97, 0, 0))
+
+ def testIndefMode3(self):
+ self.s.clear()
+ self.s.append('b')
+ self.s.append('a')
+ assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 98, 4, 1, 97, 0, 0))
+
+ def testIndefMode4(self):
+ self.s.clear()
+ self.s.append('a')
+ self.s.append('b')
+ assert encoder.encode(self.s) == ints2octs((48, 128, 4, 1, 97, 4, 1, 98, 0, 0))
+
+
class SetOfEncoderTestCase(unittest.TestCase):
+ def testEmpty(self):
+ s = univ.SetOf()
+ assert encoder.encode(s) == ints2octs((49, 128, 0, 0))
+
+ def testDefMode1(self):
+ s = univ.SetOf()
+ s.append(univ.OctetString('a'))
+ s.append(univ.OctetString('ab'))
+ assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
+
+ def testDefMode2(self):
+ s = univ.SetOf()
+ s.append(univ.OctetString('ab'))
+ s.append(univ.OctetString('a'))
+ assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 2, 97, 98, 0, 0))
+
+ def testDefMode3(self):
+ s = univ.SetOf()
+ s.append(univ.OctetString('b'))
+ s.append(univ.OctetString('a'))
+ assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
+
+ def testDefMode4(self):
+ s = univ.SetOf()
+ s.append(univ.OctetString('a'))
+ s.append(univ.OctetString('b'))
+ assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
+
+
+class SetOfEncoderWithSchemaTestCase(unittest.TestCase):
def setUp(self):
self.s = univ.SetOf(componentType=univ.OctetString())
+ def testEmpty(self):
+ self.s.clear()
+ assert encoder.encode(self.s) == ints2octs((49, 128, 0, 0))
+
def testIndefMode1(self):
self.s.clear()
self.s.append('a')
@@ -247,6 +276,205 @@ class SetOfEncoderTestCase(unittest.TestCase):
assert encoder.encode(self.s) == ints2octs((49, 128, 4, 1, 97, 4, 1, 98, 0, 0))
+class SetEncoderTestCase(unittest.TestCase):
+ def setUp(self):
+ self.s = univ.Set()
+ self.s.setComponentByPosition(0, univ.Null(''))
+ self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
+ self.s.setComponentByPosition(2, univ.Integer(1))
+
+ def testIndefMode(self):
+ 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))
+
+ def testWithOptionalIndefMode(self):
+ 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))
+
+ def testWithDefaultedIndefMode(self):
+ 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))
+
+ def testWithOptionalAndDefaultedIndefMode(self):
+ 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))
+
+
+class SetEncoderWithSchemaTestCase(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 testIndefMode(self):
+ self.__init()
+ assert encoder.encode(self.s) == ints2octs((49, 128, 5, 0, 0, 0))
+
+ def testWithOptionalIndefMode(self):
+ self.__initWithOptional()
+ assert encoder.encode(
+ self.s
+ ) == ints2octs((49, 128, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 5, 0, 0, 0))
+
+ def testWithDefaultedIndefMode(self):
+ self.__initWithDefaulted()
+ assert encoder.encode(
+ self.s
+ ) == ints2octs((49, 128, 2, 1, 1, 5, 0, 0, 0))
+
+ def testWithOptionalAndDefaultedIndefMode(self):
+ self.__initWithOptionalAndDefaulted()
+ 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))
+
+
+class SetWithChoiceWithSchemaEncoderTestCase(unittest.TestCase):
+ def setUp(self):
+ c = univ.Choice(componentType=namedtype.NamedTypes(
+ namedtype.NamedType('actual', univ.Boolean(0))
+ ))
+ self.s = univ.Set(componentType=namedtype.NamedTypes(
+ namedtype.NamedType('place-holder', univ.Null('')),
+ namedtype.NamedType('status', c)
+ ))
+
+ def testIndefMode(self):
+ self.s.setComponentByPosition(0)
+ self.s.setComponentByName('status')
+ self.s.getComponentByName('status').setComponentByPosition(0, 1)
+ assert encoder.encode(self.s) == ints2octs((49, 128, 1, 1, 255, 5, 0, 0, 0))
+
+
+class SetEncoderTestCase(unittest.TestCase):
+ def setUp(self):
+ self.s = univ.Set()
+ self.s.setComponentByPosition(0, univ.Null(''))
+ self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
+ self.s.setComponentByPosition(2, univ.Integer(1))
+
+ def testIndefMode(self):
+ 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))
+
+ def testWithOptionalIndefMode(self):
+ 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))
+
+ def testWithDefaultedIndefMode(self):
+ 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))
+
+ def testWithOptionalAndDefaultedIndefMode(self):
+ 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))
+
+
+class SequenceEncoderTestCase(unittest.TestCase):
+ def setUp(self):
+ self.s = univ.Sequence()
+ self.s.setComponentByPosition(0, univ.Null(''))
+ self.s.setComponentByPosition(1, univ.OctetString('quick brown'))
+ self.s.setComponentByPosition(2, univ.Integer(1))
+
+ def testIndefMode(self):
+ 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))
+
+ def testWithOptionalIndefMode(self):
+ 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))
+
+ def testWithDefaultedIndefMode(self):
+ 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))
+
+ def testWithOptionalAndDefaultedIndefMode(self):
+ 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))
+
+
+class SequenceEncoderWithSchemaTestCase(unittest.TestCase):
+ def setUp(self):
+ self.s = univ.Sequence(
+ 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 testIndefMode(self):
+ self.__init()
+ assert encoder.encode(self.s) == ints2octs((48, 128, 5, 0, 0, 0))
+
+ def testWithOptionalIndefMode(self):
+ self.__initWithOptional()
+ assert encoder.encode(
+ self.s
+ ) == ints2octs((48, 128, 5, 0, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 0, 0))
+
+ def testWithDefaultedIndefMode(self):
+ self.__initWithDefaulted()
+ assert encoder.encode(
+ self.s
+ ) == ints2octs((48, 128, 5, 0, 2, 1, 1, 0, 0))
+
+ def testWithOptionalAndDefaultedIndefMode(self):
+ self.__initWithOptionalAndDefaulted()
+ 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))
+
+
class NestedOptionalSequenceEncoderTestCase(unittest.TestCase):
def setUp(self):
inner = univ.Sequence(