aboutsummaryrefslogtreecommitdiff
path: root/tests/type/test_univ.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/type/test_univ.py')
-rw-r--r--tests/type/test_univ.py227
1 files changed, 200 insertions, 27 deletions
diff --git a/tests/type/test_univ.py b/tests/type/test_univ.py
index a44f82a..0092588 100644
--- a/tests/type/test_univ.py
+++ b/tests/type/test_univ.py
@@ -22,8 +22,9 @@ 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.compat.octets import str2octs, ints2octs, octs2ints, octs2str
from pyasn1.error import PyAsn1Error
+from pyasn1.error import PyAsn1UnicodeEncodeError, PyAsn1UnicodeDecodeError
class NoValueTestCase(BaseTestCase):
@@ -149,13 +150,18 @@ class NoValueTestCase(BaseTestCase):
try:
if hasattr(sys, 'getsizeof'):
sys.getsizeof(univ.noValue)
- else:
+
+ # TODO: remove when Py2.5 support is gone
+ elif sys.version_info > (2, 6):
raise unittest.SkipTest("no sys.getsizeof() method")
except PyAsn1Error:
assert False, 'sizeof failed for NoValue object'
+
except TypeError:
- raise unittest.SkipTest("sys.getsizeof() raises TypeError")
+ # TODO: remove when Py2.5 support is gone
+ if sys.version_info > (2, 6):
+ raise unittest.SkipTest("sys.getsizeof() raises TypeError")
class IntegerTestCase(BaseTestCase):
@@ -543,6 +549,36 @@ class OctetStringWithAsciiTestCase(OctetStringWithUnicodeMixIn, BaseTestCase):
encoding = 'us-ascii'
+class OctetStringUnicodeErrorTestCase(BaseTestCase):
+ def testEncodeError(self):
+ text = octs2str(ints2octs((0xff, 0xfe)))
+
+ try:
+ univ.OctetString(text, encoding='us-ascii')
+
+ except PyAsn1UnicodeEncodeError:
+ pass
+
+ # TODO: remove when Py2.5 support is gone
+ else:
+ if sys.version_info > (2, 6):
+ assert False, 'Unicode encoding error not caught'
+
+ def testDecodeError(self):
+ serialized = ints2octs((0xff, 0xfe))
+
+ try:
+ str(univ.OctetString(serialized, encoding='us-ascii'))
+
+ except PyAsn1UnicodeDecodeError:
+ pass
+
+ # TODO: remove when Py2.5 support is gone
+ else:
+ if sys.version_info > (2, 6):
+ assert False, 'Unicode decoding error not caught'
+
+
class OctetStringWithUtf8TestCase(OctetStringWithUnicodeMixIn, BaseTestCase):
initializer = (208, 176, 208, 177, 208, 178)
encoding = 'utf-8'
@@ -1027,21 +1063,25 @@ class SequenceOf(BaseTestCase):
}
def testSubtype(self):
- self.s1.clear()
- assert self.s1.subtype(
+ subtype = self.s1.subtype(
implicitTag=tag.Tag(tag.tagClassPrivate, tag.tagFormatSimple, 2),
subtypeSpec=constraint.SingleValueConstraint(1, 3),
sizeSpec=constraint.ValueSizeConstraint(0, 1)
- ) == self.s1.clone(
+ )
+ 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)
)
+ clone.clear()
+ assert clone == subtype
def testClone(self):
self.s1.setComponentByPosition(0, univ.OctetString('abc'))
s = self.s1.clone()
+ s.clear()
assert len(s) == 0
s = self.s1.clone(cloneValueFlag=1)
assert len(s) == 1
@@ -1056,31 +1096,32 @@ class SequenceOf(BaseTestCase):
s.append('xxx')
assert s[0]
- try:
- s[2]
-
- except IndexError:
- pass
-
- else:
- assert False, 'IndexError not raised'
-
# this is a deviation from standard sequence protocol
- assert not s[1]
+ assert not s[2]
+
+ def testGetItemSlice(self):
+ s = self.s1.clone()
+ s.extend(['xxx', 'yyy', 'zzz'])
+ assert s[:1] == [str2octs('xxx')]
+ assert s[-2:] == [str2octs('yyy'), str2octs('zzz')]
+ assert s[1:2] == [str2octs('yyy')]
def testSetItem(self):
s = self.s1.clone()
s.append('xxx')
+ s[2] = 'yyy'
+ assert len(s) == 3
+ assert s[1] == str2octs('')
- try:
-
- s[2] = 'xxx'
-
- except IndexError:
- pass
-
- else:
- assert False, 'IndexError not raised'
+ def testSetItemSlice(self):
+ s = self.s1.clone()
+ s[:1] = ['xxx']
+ assert s == [str2octs('xxx')]
+ s[-2:] = ['yyy', 'zzz']
+ assert s == [str2octs('yyy'), str2octs('zzz')]
+ s[1:2] = ['yyy']
+ assert s == [str2octs('yyy'), str2octs('yyy')]
+ assert len(s) == 2
def testAppend(self):
self.s1.clear()
@@ -1132,6 +1173,15 @@ class SequenceOf(BaseTestCase):
assert len(s) == 1
assert s == [str2octs('abc')]
+ def testUntyped(self):
+ n = univ.SequenceOf()
+
+ assert not n.isValue
+
+ n[0] = univ.OctetString('fox')
+
+ assert n.isValue
+
def testLegacyInitializer(self):
n = univ.SequenceOf(
componentType=univ.OctetString()
@@ -1174,6 +1224,39 @@ class SequenceOf(BaseTestCase):
s.clear()
assert s.getComponentByPosition(0, instantiate=False) is univ.noValue
+ def testClear(self):
+
+ class SequenceOf(univ.SequenceOf):
+ componentType = univ.OctetString()
+
+ s = SequenceOf()
+ s.setComponentByPosition(0, 'test')
+
+ assert s.getComponentByPosition(0) == str2octs('test')
+ assert len(s) == 1
+ assert s.isValue
+
+ s.clear()
+
+ assert len(s) == 0
+ assert s == []
+ assert s.isValue
+
+ def testReset(self):
+
+ class SequenceOf(univ.SequenceOf):
+ componentType = univ.OctetString()
+
+ s = SequenceOf()
+ s.setComponentByPosition(0, 'test')
+
+ assert s.getComponentByPosition(0) == str2octs('test')
+ assert s.isValue
+
+ s.reset()
+
+ assert not s.isValue
+
class SequenceOfPicklingTestCase(unittest.TestCase):
@@ -1441,6 +1524,75 @@ class Sequence(BaseTestCase):
s.clear()
assert s.getComponentByPosition(1, instantiate=False) is univ.noValue
+ def testSchemaWithComponents(self):
+
+ class Sequence(univ.Sequence):
+ componentType = namedtype.NamedTypes(
+ namedtype.NamedType('name', univ.OctetString())
+ )
+
+ s = Sequence()
+
+ assert not s.isValue
+
+ s[0] = 'test'
+
+ assert s.isValue
+
+ s.clear()
+
+ assert not s.isValue
+
+ s.reset()
+
+ assert not s.isValue
+
+ def testSchemaWithOptionalComponents(self):
+
+ class Sequence(univ.Sequence):
+ componentType = namedtype.NamedTypes(
+ namedtype.OptionalNamedType('name', univ.OctetString())
+ )
+
+ s = Sequence()
+
+ assert s.isValue
+
+ s[0] = 'test'
+
+ assert s.isValue
+
+ s.clear()
+
+ assert s.isValue
+
+ s.reset()
+
+ assert not s.isValue
+
+ def testSchemaWithOptionalComponents(self):
+
+ class Sequence(univ.Sequence):
+ componentType = namedtype.NamedTypes(
+ namedtype.DefaultedNamedType('name', univ.OctetString(''))
+ )
+
+ s = Sequence()
+
+ assert s.isValue
+
+ s[0] = 'test'
+
+ assert s.isValue
+
+ s.clear()
+
+ assert s.isValue
+
+ s.reset()
+
+ assert not s.isValue
+
class SequenceWithoutSchema(BaseTestCase):
@@ -1500,7 +1652,7 @@ class SequenceWithoutSchema(BaseTestCase):
assert list(s.items()) == [('field-0', str2octs('abc')), ('field-1', 123)]
def testUpdate(self):
- s = univ.Sequence()
+ s = univ.Sequence().clear()
assert not s
s.setComponentByPosition(0, univ.OctetString('abc'))
s.setComponentByPosition(1, univ.Integer(123))
@@ -1522,6 +1674,27 @@ class SequenceWithoutSchema(BaseTestCase):
s.clear()
assert 'field-0' not in s
+ def testSchema(self):
+
+ class Sequence(univ.Sequence):
+ pass
+
+ s = Sequence()
+
+ assert not s.isValue
+
+ s[0] = univ.OctetString('test')
+
+ assert s.isValue
+
+ s.clear()
+
+ assert s.isValue
+
+ s.reset()
+
+ assert not s.isValue
+
class SequencePicklingTestCase(unittest.TestCase):
@@ -1633,7 +1806,7 @@ class Set(BaseTestCase):
def testGetTagMap(self):
assert self.s1.tagMap.presentTypes == {
- univ.Set.tagSet: univ.Set()
+ univ.Set.tagSet: univ.Set().clear()
}
def testGetComponentTagMap(self):