summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIlya Etingof <etingof@gmail.com>2017-11-23 01:26:53 +0100
committerGitHub <noreply@github.com>2017-11-23 01:26:53 +0100
commit3d7f65f52b1a4b38f7ca561d460a78125dd31df1 (patch)
tree75dc1f31a5eb88702223cfa9ef0ca85f1cb493e6 /tests
parentfa6dda62e11698a2b0f6482c41e7d1d83d6c6404 (diff)
downloadpyasn1-git-3d7f65f52b1a4b38f7ca561d460a78125dd31df1.tar.gz
relax open type field type check on assignment (#105)
Diffstat (limited to 'tests')
-rw-r--r--tests/codec/ber/test_decoder.py92
-rw-r--r--tests/codec/ber/test_encoder.py117
-rw-r--r--tests/type/__main__.py1
-rw-r--r--tests/type/test_opentype.py111
-rw-r--r--tests/type/test_univ.py7
5 files changed, 324 insertions, 4 deletions
diff --git a/tests/codec/ber/test_decoder.py b/tests/codec/ber/test_decoder.py
index fdbb205..8374fd0 100644
--- a/tests/codec/ber/test_decoder.py
+++ b/tests/codec/ber/test_decoder.py
@@ -828,7 +828,7 @@ class SequenceDecoderWithSchemaTestCase(BaseTestCase):
) == (self.s, null)
-class SequenceDecoderWithIntegerOpenTypesTestCase(BaseTestCase):
+class SequenceDecoderWithUnaggedOpenTypesTestCase(BaseTestCase):
def setUp(self):
openType = opentype.OpenType(
'id',
@@ -854,12 +854,34 @@ class SequenceDecoderWithIntegerOpenTypesTestCase(BaseTestCase):
def testDecodeOpenTypesChoiceTwo(self):
s, r = decoder.decode(
ints2octs((48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)), asn1Spec=self.s,
- decodeOpenTypes = True
+ decodeOpenTypes=True
)
assert not r
assert s[0] == 2
assert s[1] == univ.OctetString('quick brown')
+ def testDecodeOpenTypesUnknownType(self):
+ try:
+ s, r = decoder.decode(
+ ints2octs((48, 6, 2, 1, 2, 6, 1, 39)), asn1Spec=self.s,
+ decodeOpenTypes=True
+ )
+
+ except PyAsn1Error:
+ pass
+
+ else:
+ assert False, 'unknown open type tolerated'
+
+ def testDecodeOpenTypesUnknownId(self):
+ s, r = decoder.decode(
+ ints2octs((48, 6, 2, 1, 3, 6, 1, 39)), asn1Spec=self.s,
+ decodeOpenTypes=True
+ )
+ assert not r
+ assert s[0] == 3
+ assert s[1] == univ.OctetString(hexValue='060127')
+
def testDontDecodeOpenTypesChoiceOne(self):
s, r = decoder.decode(
ints2octs((48, 6, 2, 1, 1, 2, 1, 12)), asn1Spec=self.s
@@ -877,6 +899,72 @@ class SequenceDecoderWithIntegerOpenTypesTestCase(BaseTestCase):
assert s[1] == ints2octs((4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110))
+class SequenceDecoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
+ def setUp(self):
+ openType = opentype.OpenType(
+ 'id',
+ {1: univ.Integer(),
+ 2: univ.OctetString()}
+ )
+ self.s = univ.Sequence(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('id', univ.Integer()),
+ namedtype.NamedType(
+ 'blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
+ )
+ )
+ )
+
+ def testDecodeOpenTypesChoiceOne(self):
+ s, r = decoder.decode(
+ ints2octs((48, 8, 2, 1, 1, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
+ )
+ assert not r
+ assert s[0] == 1
+ assert s[1] == 12
+
+ def testDecodeOpenTypesUnknownId(self):
+ s, r = decoder.decode(
+ ints2octs((48, 8, 2, 1, 3, 131, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
+ )
+ assert not r
+ assert s[0] == 3
+ assert s[1] == univ.OctetString(hexValue='02010C')
+
+
+class SequenceDecoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
+ def setUp(self):
+ openType = opentype.OpenType(
+ 'id',
+ {1: univ.Integer(),
+ 2: univ.OctetString()}
+ )
+ self.s = univ.Sequence(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('id', univ.Integer()),
+ namedtype.NamedType(
+ 'blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType
+ )
+ )
+ )
+
+ def testDecodeOpenTypesChoiceOne(self):
+ s, r = decoder.decode(
+ ints2octs((48, 8, 2, 1, 1, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
+ )
+ assert not r
+ assert s[0] == 1
+ assert s[1] == 12
+
+ def testDecodeOpenTypesUnknownId(self):
+ s, r = decoder.decode(
+ ints2octs((48, 8, 2, 1, 3, 163, 3, 2, 1, 12)), asn1Spec=self.s, decodeOpenTypes=True
+ )
+ assert not r
+ assert s[0] == 3
+ assert s[1] == univ.OctetString(hexValue='02010C')
+
+
class SetDecoderTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
diff --git a/tests/codec/ber/test_encoder.py b/tests/codec/ber/test_encoder.py
index 1806002..34d2ba0 100644
--- a/tests/codec/ber/test_encoder.py
+++ b/tests/codec/ber/test_encoder.py
@@ -14,7 +14,7 @@ except ImportError:
from tests.base import BaseTestCase
-from pyasn1.type import tag, namedtype, univ, char
+from pyasn1.type import tag, namedtype, opentype, univ, char
from pyasn1.codec.ber import encoder
from pyasn1.compat.octets import ints2octs
from pyasn1.error import PyAsn1Error
@@ -733,6 +733,121 @@ class SequenceEncoderWithSchemaTestCase(BaseTestCase):
) == 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 SequenceEncoderWithUntaggedOpenTypesTestCase(BaseTestCase):
+ def setUp(self):
+ BaseTestCase.setUp(self)
+
+ openType = opentype.OpenType(
+ 'id',
+ {1: univ.Integer(),
+ 2: univ.OctetString()}
+ )
+ self.s = univ.Sequence(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('id', univ.Integer()),
+ namedtype.NamedType('blob', univ.Any(), openType=openType)
+ )
+ )
+
+ def testEncodeOpenTypeChoiceOne(self):
+ self.s.clear()
+
+ self.s[0] = 1
+ self.s[1] = univ.Integer(12)
+
+ assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
+ (48, 6, 2, 1, 1, 2, 1, 12)
+ )
+
+ def testEncodeOpenTypeChoiceTwo(self):
+ self.s.clear()
+
+ self.s[0] = 2
+ self.s[1] = univ.OctetString('quick brown')
+
+ assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
+ (48, 16, 2, 1, 2, 4, 11, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110)
+ )
+
+ def testEncodeOpenTypeUnknownId(self):
+ self.s.clear()
+
+ self.s[0] = 2
+ self.s[1] = univ.ObjectIdentifier('1.3.6')
+
+ try:
+ encoder.encode(self.s, asn1Spec=self.s)
+
+ except PyAsn1Error:
+ assert False, 'incompatible open type tolerated'
+
+ def testEncodeOpenTypeIncompatibleType(self):
+ self.s.clear()
+
+ self.s[0] = 2
+ self.s[1] = univ.ObjectIdentifier('1.3.6')
+
+ try:
+ encoder.encode(self.s, asn1Spec=self.s)
+
+ except PyAsn1Error:
+ assert False, 'incompatible open type tolerated'
+
+
+class SequenceEncoderWithImplicitlyTaggedOpenTypesTestCase(BaseTestCase):
+ def setUp(self):
+ BaseTestCase.setUp(self)
+
+ openType = opentype.OpenType(
+ 'id',
+ {1: univ.Integer(),
+ 2: univ.OctetString()}
+ )
+ self.s = univ.Sequence(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('id', univ.Integer()),
+ namedtype.NamedType('blob', univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType)
+ )
+ )
+
+ def testEncodeOpenTypeChoiceOne(self):
+ self.s.clear()
+
+ self.s[0] = 1
+ self.s[1] = univ.Integer(12)
+
+ assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
+ (48, 8, 2, 1, 1, 131, 3, 2, 1, 12)
+ )
+
+
+class SequenceEncoderWithExplicitlyTaggedOpenTypesTestCase(BaseTestCase):
+ def setUp(self):
+ BaseTestCase.setUp(self)
+
+ openType = opentype.OpenType(
+ 'id',
+ {1: univ.Integer(),
+ 2: univ.OctetString()}
+ )
+ self.s = univ.Sequence(
+ componentType=namedtype.NamedTypes(
+ namedtype.NamedType('id', univ.Integer()),
+ namedtype.NamedType('blob', univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 3)), openType=openType)
+ )
+ )
+
+ def testEncodeOpenTypeChoiceOne(self):
+ self.s.clear()
+
+ self.s[0] = 1
+ self.s[1] = univ.Integer(12)
+
+ assert encoder.encode(self.s, asn1Spec=self.s) == ints2octs(
+ (48, 8, 2, 1, 1, 163, 3, 2, 1, 12)
+ )
+
+
class SequenceEncoderWithComponentsSchemaTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
diff --git a/tests/type/__main__.py b/tests/type/__main__.py
index 984cb84..12ff7c3 100644
--- a/tests/type/__main__.py
+++ b/tests/type/__main__.py
@@ -12,6 +12,7 @@ except ImportError:
suite = unittest.TestLoader().loadTestsFromNames(
['tests.type.test_constraint.suite',
+ 'tests.type.test_opentype.suite',
'tests.type.test_namedtype.suite',
'tests.type.test_namedval.suite',
'tests.type.test_tag.suite',
diff --git a/tests/type/test_opentype.py b/tests/type/test_opentype.py
new file mode 100644
index 0000000..f6cbcb9
--- /dev/null
+++ b/tests/type/test_opentype.py
@@ -0,0 +1,111 @@
+#
+# This file is part of pyasn1 software.
+#
+# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
+# License: http://snmplabs.com/pyasn1/license.html
+#
+import sys
+
+try:
+ import unittest2 as unittest
+
+except ImportError:
+ import unittest
+
+from tests.base import BaseTestCase
+
+from pyasn1.type import univ
+from pyasn1.type import tag
+from pyasn1.type import constraint
+from pyasn1.type import namedtype
+from pyasn1.type import namedval
+from pyasn1.type import opentype
+from pyasn1.type import error
+from pyasn1.compat.octets import str2octs
+from pyasn1.compat.octets import ints2octs
+from pyasn1.compat.octets import octs2ints
+from pyasn1.error import PyAsn1Error
+
+
+class UntaggedAnyTestCase(BaseTestCase):
+
+ def setUp(self):
+ BaseTestCase.setUp(self)
+
+ class Sequence(univ.Sequence):
+ componentType = namedtype.NamedTypes(
+ namedtype.NamedType('id', univ.Integer()),
+ namedtype.NamedType('blob', univ.Any())
+ )
+
+ self.s = Sequence()
+
+ def testTypeCheckOnAssignment(self):
+
+ self.s.clear()
+
+ self.s['blob'] = univ.Any(str2octs('xxx'))
+
+ # this should succeed because Any is untagged and unconstrained
+ self.s['blob'] = univ.Integer(123)
+
+
+class TaggedAnyTestCase(BaseTestCase):
+
+ def setUp(self):
+ BaseTestCase.setUp(self)
+
+ self.taggedAny = univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 20))
+
+ class Sequence(univ.Sequence):
+ componentType = namedtype.NamedTypes(
+ namedtype.NamedType('id', univ.Integer()),
+ namedtype.NamedType('blob', self.taggedAny)
+ )
+
+ self.s = Sequence()
+
+ def testTypeCheckOnAssignment(self):
+
+ self.s.clear()
+
+ self.s['blob'] = self.taggedAny.clone('xxx')
+
+ try:
+ self.s.setComponentByName('blob', univ.Integer(123))
+
+ except PyAsn1Error:
+ pass
+
+ else:
+ assert False, 'non-open type assignment tolerated'
+
+
+class TaggedAnyOpenTypeTestCase(BaseTestCase):
+
+ def setUp(self):
+ BaseTestCase.setUp(self)
+
+ self.taggedAny = univ.Any().subtype(implicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 20))
+
+ class Sequence(univ.Sequence):
+ componentType = namedtype.NamedTypes(
+ namedtype.NamedType('id', univ.Integer()),
+ namedtype.NamedType('blob', self.taggedAny, openType=opentype.OpenType(name='id'))
+ )
+
+ self.s = Sequence()
+
+ def testTypeCheckOnAssignment(self):
+
+ self.s.clear()
+
+ self.s['blob'] = univ.Any(str2octs('xxx'))
+ self.s['blob'] = univ.Integer(123)
+
+
+suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
+
+
+if __name__ == '__main__':
+ unittest.TextTestRunner(verbosity=2).run(suite)
diff --git a/tests/type/test_univ.py b/tests/type/test_univ.py
index 6819251..ea6c4d0 100644
--- a/tests/type/test_univ.py
+++ b/tests/type/test_univ.py
@@ -16,7 +16,12 @@ except ImportError:
from tests.base import BaseTestCase
-from pyasn1.type import univ, tag, constraint, namedtype, namedval, error
+from pyasn1.type import univ
+from pyasn1.type import tag
+from pyasn1.type import constraint
+from pyasn1.type import namedtype
+from pyasn1.type import namedval
+from pyasn1.type import error
from pyasn1.compat.octets import str2octs, ints2octs, octs2ints
from pyasn1.error import PyAsn1Error