diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/codec/ber/test_decoder.py | 6 | ||||
-rw-r--r-- | tests/codec/ber/test_encoder.py | 12 | ||||
-rw-r--r-- | tests/type/test_constraint.py | 63 | ||||
-rw-r--r-- | tests/type/test_univ.py | 136 |
4 files changed, 187 insertions, 30 deletions
diff --git a/tests/codec/ber/test_decoder.py b/tests/codec/ber/test_decoder.py index 089f0f3..e3b74df 100644 --- a/tests/codec/ber/test_decoder.py +++ b/tests/codec/ber/test_decoder.py @@ -486,17 +486,17 @@ class RealDecoderTestCase(BaseTestCase): if sys.version_info[0:2] > (2, 5): class UniversalStringDecoderTestCase(BaseTestCase): def testDecoder(self): - assert decoder.decode(ints2octs((28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99))) == (char.UniversalString(sys.version_info[0] == 3 and 'abc' or unicode('abc')), null) + assert decoder.decode(ints2octs((28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99))) == (char.UniversalString(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null) class BMPStringDecoderTestCase(BaseTestCase): def testDecoder(self): - assert decoder.decode(ints2octs((30, 6, 0, 97, 0, 98, 0, 99))) == (char.BMPString(sys.version_info[0] == 3 and 'abc' or unicode('abc')), null) + assert decoder.decode(ints2octs((30, 6, 0, 97, 0, 98, 0, 99))) == (char.BMPString(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null) class UTF8StringDecoderTestCase(BaseTestCase): def testDecoder(self): - assert decoder.decode(ints2octs((12, 3, 97, 98, 99))) == (char.UTF8String(sys.version_info[0] == 3 and 'abc' or unicode('abc')), null) + assert decoder.decode(ints2octs((12, 3, 97, 98, 99))) == (char.UTF8String(sys.version_info[0] >= 3 and 'abc' or unicode('abc')), null) class SequenceOfDecoderTestCase(BaseTestCase): diff --git a/tests/codec/ber/test_encoder.py b/tests/codec/ber/test_encoder.py index 38d75c0..df82e7b 100644 --- a/tests/codec/ber/test_encoder.py +++ b/tests/codec/ber/test_encoder.py @@ -436,40 +436,40 @@ class RealEncoderWithSchemaTestCase(BaseTestCase): if sys.version_info[0:2] > (2, 5): class UniversalStringEncoderTestCase(BaseTestCase): def testEncoding(self): - assert encoder.encode(char.UniversalString(sys.version_info[0] == 3 and 'abc' or unicode('abc'))) == ints2octs( + assert encoder.encode(char.UniversalString(sys.version_info[0] >= 3 and 'abc' or unicode('abc'))) == ints2octs( (28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99)), 'Incorrect encoding' class UniversalStringEncoderWithSchemaTestCase(BaseTestCase): def testEncoding(self): assert encoder.encode( - sys.version_info[0] == 3 and 'abc' or unicode('abc'), asn1Spec=char.UniversalString() + sys.version_info[0] >= 3 and 'abc' or unicode('abc'), asn1Spec=char.UniversalString() ) == ints2octs((28, 12, 0, 0, 0, 97, 0, 0, 0, 98, 0, 0, 0, 99)), 'Incorrect encoding' class BMPStringEncoderTestCase(BaseTestCase): def testEncoding(self): - assert encoder.encode(char.BMPString(sys.version_info[0] == 3 and 'abc' or unicode('abc'))) == ints2octs( + assert encoder.encode(char.BMPString(sys.version_info[0] >= 3 and 'abc' or unicode('abc'))) == ints2octs( (30, 6, 0, 97, 0, 98, 0, 99)), 'Incorrect encoding' class BMPStringEncoderWithSchemaTestCase(BaseTestCase): def testEncoding(self): assert encoder.encode( - sys.version_info[0] == 3 and 'abc' or unicode('abc'), asn1Spec=char.BMPString() + sys.version_info[0] >= 3 and 'abc' or unicode('abc'), asn1Spec=char.BMPString() ) == ints2octs((30, 6, 0, 97, 0, 98, 0, 99)), 'Incorrect encoding' class UTF8StringEncoderTestCase(BaseTestCase): def testEncoding(self): - assert encoder.encode(char.UTF8String(sys.version_info[0] == 3 and 'abc' or unicode('abc'))) == ints2octs( + assert encoder.encode(char.UTF8String(sys.version_info[0] >= 3 and 'abc' or unicode('abc'))) == ints2octs( (12, 3, 97, 98, 99)), 'Incorrect encoding' class UTF8StringEncoderWithSchemaTestCase(BaseTestCase): def testEncoding(self): assert encoder.encode( - sys.version_info[0] == 3 and 'abc' or unicode('abc'), asn1Spec=char.UTF8String() + sys.version_info[0] >= 3 and 'abc' or unicode('abc'), asn1Spec=char.UTF8String() ) == ints2octs((12, 3, 97, 98, 99)), 'Incorrect encoding' 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 0092588..9762959 100644 --- a/tests/type/test_univ.py +++ b/tests/type/test_univ.py @@ -992,11 +992,13 @@ class SequenceOf(BaseTestCase): assert self.s1 == self.s2, '__cmp__() fails' def testSubtypeSpec(self): - s = self.s1.clone(subtypeSpec=constraint.ConstraintsUnion( - constraint.SingleValueConstraint(str2octs('abc')) - )) + s = self.s1.clone( + componentType=univ.OctetString().subtype( + subtypeSpec=constraint.SingleValueConstraint(str2octs('abc')))) try: - s.setComponentByPosition(0, univ.OctetString('abc')) + s.setComponentByPosition( + 0, univ.OctetString().subtype( + 'abc', subtypeSpec=constraint.SingleValueConstraint(str2octs('abc')))) except PyAsn1Error: assert 0, 'constraint fails' try: @@ -1006,7 +1008,7 @@ class SequenceOf(BaseTestCase): s.setComponentByPosition(1, univ.OctetString('Abc'), verifyConstraints=False) except PyAsn1Error: - assert 0, 'constraint failes with verifyConstraints=True' + assert 0, 'constraint fails with verifyConstraints=False' else: assert 0, 'constraint fails' @@ -1040,22 +1042,14 @@ class SequenceOf(BaseTestCase): else: pass - def testSizeSpec(self): - s = self.s1.clone(sizeSpec=constraint.ConstraintsUnion( + def testConsistency(self): + s = self.s1.clone(subtypeSpec=constraint.ConstraintsUnion( constraint.ValueSizeConstraint(1, 1) )) s.setComponentByPosition(0, univ.OctetString('abc')) - try: - s.verifySizeSpec() - except PyAsn1Error: - assert 0, 'size spec fails' + assert not s.isInconsistent, 'size spec fails' s.setComponentByPosition(1, univ.OctetString('abc')) - try: - s.verifySizeSpec() - except PyAsn1Error: - pass - else: - assert 0, 'size spec fails' + assert s.isInconsistent, 'size spec fails' def testGetComponentTagMap(self): assert self.s1.componentType.tagMap.presentTypes == { @@ -1065,15 +1059,13 @@ class SequenceOf(BaseTestCase): def testSubtype(self): subtype = self.s1.subtype( implicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 2), - subtypeSpec=constraint.SingleValueConstraint(1, 3), - sizeSpec=constraint.ValueSizeConstraint(0, 1) + subtypeSpec=constraint.ValueSizeConstraint(0, 1) ) subtype.clear() clone = self.s1.clone( tagSet=tag.TagSet(tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 2)), - subtypeSpec=constraint.ConstraintsIntersection(constraint.SingleValueConstraint(1, 3)), - sizeSpec=constraint.ValueSizeConstraint(0, 1) + subtypeSpec=constraint.ValueSizeConstraint(0, 1) ) clone.clear() assert clone == subtype @@ -1257,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): @@ -1593,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): |