diff options
author | elie <elie> | 2013-12-12 19:55:44 +0000 |
---|---|---|
committer | elie <elie> | 2013-12-12 19:55:44 +0000 |
commit | e121ac2cd0d548aeac9d74dc96452a6593c9ac13 (patch) | |
tree | 77d8189a6c5cfa7756b1cf1a154f78286693153e | |
parent | d901df205427ce7ef72b074e05e6d7bb1f8b4ee9 (diff) | |
download | pyasn1-e121ac2cd0d548aeac9d74dc96452a6593c9ac13.tar.gz |
Constructed types now verify their consistency by invoking
isSameTypeWith(matchTags=True, matchConstraints=False) and
isSuperTypeOf(matchTags=False, matchConstraints=True) for each of their
components rather than isSuperTypeOf() as it used to be. Constriants check
could be enforced to isSameTypeWith() with the strictConstraints=True
constructed classes attribute.
-rw-r--r-- | CHANGES | 8 | ||||
-rw-r--r-- | pyasn1/type/univ.py | 20 | ||||
-rw-r--r-- | test/type/test_univ.py | 16 |
3 files changed, 37 insertions, 7 deletions
@@ -18,8 +18,12 @@ Revision 0.1.8 - When comparing ASN.1 types, by-tag and/or by-constraints matching can now be performed with the isSuperTypeOf()/isSameTypeWith() optional flags. -- Constructed types now verify their consistency by invoking isSameTypeWith() - for their components rather than isSuperTypeOf() as it used to be. +- Constructed types now verify their consistency by invoking + isSameTypeWith(matchTags=True, matchConstraints=False) and + isSuperTypeOf(matchTags=False, matchConstraints=True) for each of their + components rather than isSuperTypeOf() as it used to be. Constriants check + could be enforced to isSameTypeWith() with the strictConstraints=True + constructed classes attribute. - Constructed types can now be initialized with new .setComponents() method which accepts both var-args and keyword-args. Default repr() modified to reflect this change. diff --git a/pyasn1/type/univ.py b/pyasn1/type/univ.py index 1963c9a..4b88fda 100644 --- a/pyasn1/type/univ.py +++ b/pyasn1/type/univ.py @@ -677,6 +677,7 @@ class SetOf(base.AbstractConstructedAsn1Item): tag.Tag(tag.tagClassUniversal, tag.tagFormatConstructed, 0x11) ) typeId = 1 + strictConstraints = False def _cloneComponentValues(self, myClone, cloneValueFlag): idx = 0; l = len(self._componentValues) @@ -692,9 +693,14 @@ class SetOf(base.AbstractConstructedAsn1Item): idx = idx + 1 def _verifyComponent(self, idx, value): - if self._componentType is not None and \ - not self._componentType.isSameTypeWith(value): - raise error.PyAsn1Error('Component type error %r' % (value,)) + t = self._componentType + if t is None: + return + if not t.isSameTypeWith(value,matchConstraints=self.strictConstraints): + raise error.PyAsn1Error('Component value is tag-incompatible: %r vs %r' % (value, t)) + if self.strictConstraints and \ + not t.isSuperTypeOf(value, matchTags=False): + raise error.PyAsn1Error('Component value is constraints-incompatible: %r' % (value, t)) def getComponentByPosition(self, idx): return self._componentValues[idx] def setComponentByPosition(self, idx, value=None, verifyConstraints=True): @@ -747,6 +753,7 @@ class SequenceOf(SetOf): class SequenceAndSetBase(base.AbstractConstructedAsn1Item): componentType = namedtype.NamedTypes() + strictConstraints = False def __init__(self, componentType=None, tagSet=None, subtypeSpec=None, sizeSpec=None): if componentType is None: @@ -787,8 +794,11 @@ class SequenceAndSetBase(base.AbstractConstructedAsn1Item): 'Component type error out of range' ) t = self._componentType[idx].getType() - if not t.isSameTypeWith(value): - raise error.PyAsn1Error('Component type error %r vs %r' % (t, value)) + if not t.isSameTypeWith(value,matchConstraints=self.strictConstraints): + raise error.PyAsn1Error('Component value is tag-incompatible: %r vs %r' % (value, t)) + if self.strictConstraints and \ + not t.isSuperTypeOf(value, matchTags=False): + raise error.PyAsn1Error('Component value is constraints-incompatible: %r' % (value, t)) def getComponentByName(self, name): return self.getComponentByPosition( diff --git a/test/type/test_univ.py b/test/type/test_univ.py index 561b454..d2280b9 100644 --- a/test/type/test_univ.py +++ b/test/type/test_univ.py @@ -319,12 +319,20 @@ class SequenceOf(unittest.TestCase): def testComponentConstraintsMatching(self): s = self.s1.clone() o = univ.OctetString().subtype(subtypeSpec=constraint.ConstraintsUnion(constraint.SingleValueConstraint(str2octs('cba')))) + s.strictConstraints = True try: s.setComponentByPosition(0, o.clone('cba')) except: pass else: assert 0, 'inner supertype constraint allowed' + s.strictConstraints = False + try: + s.setComponentByPosition(0, o.clone('cba')) + except: + assert 0, 'inner supertype constraint disallowed' + else: + pass def testSizeSpec(self): s = self.s1.clone(sizeSpec=constraint.ConstraintsUnion( constraint.ValueSizeConstraint(1,1) @@ -431,12 +439,20 @@ class Sequence(unittest.TestCase): def testComponentConstraintsMatching(self): s = self.s1.clone() o = univ.OctetString().subtype(subtypeSpec=constraint.ConstraintsUnion(constraint.SingleValueConstraint(str2octs('cba')))) + s.strictConstraints = True try: s.setComponentByName('name', o.clone('cba')) except: pass else: assert 0, 'inner supertype constraint allowed' + s.strictConstraints = False + try: + s.setComponentByName('name', o.clone('cba')) + except: + assert 0, 'inner supertype constraint disallowed' + else: + pass def testSetComponents(self): assert self.s1.clone().setComponents(name='a', nick='b', age=1) == \ self.s1.setComponentByPosition(0, 'a').setComponentByPosition(1, 'b').setComponentByPosition(2, 1) |