summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorIlya Etingof <etingof@gmail.com>2019-08-27 10:17:42 +0200
committerGitHub <noreply@github.com>2019-08-27 10:17:42 +0200
commitd0b7f2ec8677eec8f9aa31103a66f5cab18e9308 (patch)
tree52c0e965e87f064d88a10bc43f9cfd9c86fd052a /tests
parent7214dcae11f58b0c480bd30170da3eb4734ca707 (diff)
downloadpyasn1-git-d0b7f2ec8677eec8f9aa31103a66f5cab18e9308.tar.gz
Add `SET ... WITH COMPONENTS ...` ASN.1 construct support (#171)
Added `WithComponentsConstraint` along with related `ComponentPresentConstraint` and `ComponentAbsentConstraint` classes to be used with `Sequence`/`Set` types representing `SET ... WITH COMPONENTS ...` like ASN.1 constructs.
Diffstat (limited to 'tests')
-rw-r--r--tests/type/test_constraint.py63
-rw-r--r--tests/type/test_univ.py102
2 files changed, 165 insertions, 0 deletions
diff --git a/tests/type/test_constraint.py b/tests/type/test_constraint.py
index b5276cd..0f49c78 100644
--- a/tests/type/test_constraint.py
+++ b/tests/type/test_constraint.py
@@ -128,6 +128,69 @@ class PermittedAlphabetConstraintTestCase(SingleValueConstraintTestCase):
assert 0, 'constraint check fails'
+class WithComponentsConstraintTestCase(BaseTestCase):
+
+ def testGoodVal(self):
+ c = constraint.WithComponentsConstraint(
+ ('A', constraint.ComponentPresentConstraint()),
+ ('B', constraint.ComponentAbsentConstraint()))
+
+ try:
+ c({'A': 1})
+
+ except error.ValueConstraintError:
+ assert 0, 'constraint check fails'
+
+ def testGoodValWithExtraFields(self):
+ c = constraint.WithComponentsConstraint(
+ ('A', constraint.ComponentPresentConstraint()),
+ ('B', constraint.ComponentAbsentConstraint())
+ )
+
+ try:
+ c({'A': 1, 'C': 2})
+
+ except error.ValueConstraintError:
+ assert 0, 'constraint check fails'
+
+ def testEmptyConstraint(self):
+ c = constraint.WithComponentsConstraint()
+
+ try:
+ c({'A': 1})
+
+ except error.ValueConstraintError:
+ assert 0, 'constraint check fails'
+
+ def testBadVal(self):
+ c = constraint.WithComponentsConstraint(
+ ('A', constraint.ComponentPresentConstraint())
+ )
+
+ try:
+ c({'B': 2})
+
+ except error.ValueConstraintError:
+ pass
+
+ else:
+ assert 0, 'constraint check fails'
+
+ def testBadValExtraFields(self):
+ c = constraint.WithComponentsConstraint(
+ ('A', constraint.ComponentPresentConstraint())
+ )
+
+ try:
+ c({'B': 2, 'C': 3})
+
+ except error.ValueConstraintError:
+ pass
+
+ else:
+ assert 0, 'constraint check fails'
+
+
class ConstraintsIntersectionTestCase(BaseTestCase):
def setUp(self):
BaseTestCase.setUp(self)
diff --git a/tests/type/test_univ.py b/tests/type/test_univ.py
index d9f921b..9762959 100644
--- a/tests/type/test_univ.py
+++ b/tests/type/test_univ.py
@@ -1249,6 +1249,37 @@ class SequenceOf(BaseTestCase):
assert not s.isValue
+ def testIsInconsistentSizeConstraint(self):
+
+ class SequenceOf(univ.SequenceOf):
+ componentType = univ.OctetString()
+ subtypeSpec = constraint.ValueSizeConstraint(0, 1)
+
+ s = SequenceOf()
+
+ assert s.isInconsistent
+
+ s[0] = 'test'
+
+ assert not s.isInconsistent
+
+ s[0] = 'test'
+ s[1] = 'test'
+
+ assert s.isInconsistent
+
+ s.clear()
+
+ assert not s.isInconsistent
+
+ s.reset()
+
+ assert s.isInconsistent
+
+ s[1] = 'test'
+
+ assert not s.isInconsistent
+
class SequenceOfPicklingTestCase(unittest.TestCase):
@@ -1585,6 +1616,77 @@ class Sequence(BaseTestCase):
assert not s.isValue
+ def testIsInconsistentWithComponentsConstraint(self):
+
+ class Sequence(univ.Sequence):
+ componentType = namedtype.NamedTypes(
+ namedtype.OptionalNamedType('name', univ.OctetString()),
+ namedtype.DefaultedNamedType('age', univ.Integer(65))
+ )
+ subtypeSpec = constraint.WithComponentsConstraint(
+ ('name', constraint.ComponentPresentConstraint()),
+ ('age', constraint.ComponentAbsentConstraint())
+ )
+
+ s = Sequence()
+
+ assert s.isInconsistent
+
+ s[0] = 'test'
+
+ assert not s.isInconsistent
+
+ s[0] = 'test'
+ s[1] = 23
+
+ assert s.isInconsistent
+
+ s.clear()
+
+ assert s.isInconsistent
+
+ s.reset()
+
+ assert s.isInconsistent
+
+ s[1] = 23
+
+ assert s.isInconsistent
+
+ def testIsInconsistentSizeConstraint(self):
+
+ class Sequence(univ.Sequence):
+ componentType = namedtype.NamedTypes(
+ namedtype.OptionalNamedType('name', univ.OctetString()),
+ namedtype.DefaultedNamedType('age', univ.Integer(65))
+ )
+ subtypeSpec = constraint.ValueSizeConstraint(0, 1)
+
+ s = Sequence()
+
+ assert not s.isInconsistent
+
+ s[0] = 'test'
+
+ assert not s.isInconsistent
+
+ s[0] = 'test'
+ s[1] = 23
+
+ assert s.isInconsistent
+
+ s.clear()
+
+ assert not s.isInconsistent
+
+ s.reset()
+
+ assert s.isInconsistent
+
+ s[1] = 23
+
+ assert not s.isInconsistent
+
class SequenceWithoutSchema(BaseTestCase):