summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIlya Etingof <etingof@gmail.com>2017-10-17 23:26:46 +0200
committerGitHub <noreply@github.com>2017-10-17 23:26:46 +0200
commit251d99bde71b8311d087d13d2564a016fad3290e (patch)
treea409c9f87a5efdcbff882dac1b6a9e94fc649304 /tests
parent475a2fb1f3493a1d7ee8b91161132dc51f3d066e (diff)
downloadpyasn1-git-251d99bde71b8311d087d13d2564a016fad3290e.tar.gz
fixed DER/CER encoders to respect ordering of tagged CHOICE (#93)
Diffstat (limited to 'tests')
-rw-r--r--tests/codec/cer/test_encoder.py45
-rw-r--r--tests/codec/der/test_encoder.py48
2 files changed, 90 insertions, 3 deletions
diff --git a/tests/codec/cer/test_encoder.py b/tests/codec/cer/test_encoder.py
index 32d87b0..a100157 100644
--- a/tests/codec/cer/test_encoder.py
+++ b/tests/codec/cer/test_encoder.py
@@ -13,7 +13,7 @@ except ImportError:
from tests.base import BaseTestCase
-from pyasn1.type import namedtype, univ, useful
+from pyasn1.type import tag, namedtype, univ, useful
from pyasn1.codec.cer import encoder
from pyasn1.compat.octets import ints2octs
from pyasn1.error import PyAsn1Error
@@ -378,6 +378,49 @@ class SetWithChoiceWithSchemaEncoderTestCase(BaseTestCase):
assert encoder.encode(self.s) == ints2octs((49, 128, 1, 1, 255, 5, 0, 0, 0))
+class SetWithTaggedChoiceEncoderTestCase(BaseTestCase):
+
+ def testWithUntaggedChoice(self):
+
+ c = univ.Choice(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('premium', univ.Boolean())
+ )
+ )
+
+ s = univ.Set(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('name', univ.OctetString()),
+ namedtype.NamedType('customer', c)
+ )
+ )
+
+ s.setComponentByName('name', 'A')
+ s.getComponentByName('customer').setComponentByName('premium', True)
+
+ assert encoder.encode(s) == ints2octs((49, 128, 1, 1, 255, 4, 1, 65, 0, 0))
+
+ def testWithTaggedChoice(self):
+
+ c = univ.Choice(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('premium', univ.Boolean())
+ )
+ ).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 7))
+
+ s = univ.Set(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('name', univ.OctetString()),
+ namedtype.NamedType('customer', c)
+ )
+ )
+
+ s.setComponentByName('name', 'A')
+ s.getComponentByName('customer').setComponentByName('premium', True)
+
+ assert encoder.encode(s) == ints2octs((49, 128, 4, 1, 65, 167, 128, 1, 1, 255, 0, 0, 0, 0))
+
+
class SetEncoderTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
diff --git a/tests/codec/der/test_encoder.py b/tests/codec/der/test_encoder.py
index 66a8a91..e06c769 100644
--- a/tests/codec/der/test_encoder.py
+++ b/tests/codec/der/test_encoder.py
@@ -13,7 +13,7 @@ except ImportError:
from tests.base import BaseTestCase
-from pyasn1.type import namedtype, univ
+from pyasn1.type import tag, namedtype, univ
from pyasn1.codec.der import encoder
from pyasn1.compat.octets import ints2octs
@@ -76,7 +76,8 @@ class SetOfEncoderTestCase(BaseTestCase):
assert encoder.encode(self.s) == ints2octs((49, 6, 4, 1, 97, 4, 1, 98))
-class SetWithChoiceEncoderTestCase(BaseTestCase):
+
+class SetWithAlternatingChoiceEncoderTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
@@ -101,6 +102,49 @@ class SetWithChoiceEncoderTestCase(BaseTestCase):
assert encoder.encode(self.s) == ints2octs((49, 6, 1, 1, 255, 2, 1, 5))
+class SetWithTaggedChoiceEncoderTestCase(BaseTestCase):
+
+ def testWithUntaggedChoice(self):
+
+ c = univ.Choice(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('premium', univ.Boolean())
+ )
+ )
+
+ s = univ.Set(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('name', univ.OctetString()),
+ namedtype.NamedType('customer', c)
+ )
+ )
+
+ s.setComponentByName('name', 'A')
+ s.getComponentByName('customer').setComponentByName('premium', True)
+
+ assert encoder.encode(s) == ints2octs((49, 6, 1, 1, 255, 4, 1, 65))
+
+ def testWithTaggedChoice(self):
+
+ c = univ.Choice(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('premium', univ.Boolean())
+ )
+ ).subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 7))
+
+ s = univ.Set(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('name', univ.OctetString()),
+ namedtype.NamedType('customer', c)
+ )
+ )
+
+ s.setComponentByName('name', 'A')
+ s.getComponentByName('customer').setComponentByName('premium', True)
+
+ assert encoder.encode(s) == ints2octs((49, 8, 4, 1, 65, 167, 3, 1, 1, 255))
+
+
class NestedOptionalSequenceEncoderTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)