diff options
author | Ilya Etingof <ietingof@redhat.com> | 2017-02-13 20:08:07 +0100 |
---|---|---|
committer | Ilya Etingof <ietingof@redhat.com> | 2017-02-13 20:08:07 +0100 |
commit | dd91c947771dc3243b6869ead12fdf81f91bd42c (patch) | |
tree | ead830b3200c4d353f695ddb31d5a2738cfa0be1 /tests | |
parent | 6dd2a9840f5ee3e9ee41578e756ccd1328a3718b (diff) | |
download | pyasn1-dd91c947771dc3243b6869ead12fdf81f91bd42c.tar.gz |
character types keep unicode contents, not octet stream
Diffstat (limited to 'tests')
-rw-r--r-- | tests/type/__main__.py | 3 | ||||
-rw-r--r-- | tests/type/test_char.py | 109 |
2 files changed, 111 insertions, 1 deletions
diff --git a/tests/type/__main__.py b/tests/type/__main__.py index bd89f8b..15036ea 100644 --- a/tests/type/__main__.py +++ b/tests/type/__main__.py @@ -15,7 +15,8 @@ suite = unittest.TestLoader().loadTestsFromNames( 'tests.type.test_namedtype.suite', 'tests.type.test_namedval.suite', 'tests.type.test_tag.suite', - 'tests.type.test_univ.suite'] + 'tests.type.test_univ.suite', + 'tests.type.test_char.suite'] ) diff --git a/tests/type/test_char.py b/tests/type/test_char.py new file mode 100644 index 0000000..e48f634 --- /dev/null +++ b/tests/type/test_char.py @@ -0,0 +1,109 @@ +# +# This file is part of pyasn1 software. +# +# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com> +# License: http://pyasn1.sf.net/license.html +# +import sys +from pyasn1.type import char, univ, constraint +from pyasn1.compat.octets import ints2octs +from pyasn1.error import PyAsn1Error + +try: + import unittest2 as unittest +except ImportError: + import unittest + + +class UTF8StringTestCase(unittest.TestCase): + + def setUp(self): + self.asn1String = char.UTF8String(ints2octs([209, 132, 208, 176])) + self.pythonString = ints2octs([209, 132, 208, 176]).decode('utf-8') + + def testUnicode(self): + assert self.asn1String == self.pythonString, 'unicode init fails' + + def testLength(self): + assert len(self.asn1String) == len(self.pythonString), 'unicode len() fails' + + def testSizeConstraint(self): + asn1Spec = char.UTF8String(subtypeSpec=constraint.ValueSizeConstraint(1, 1)) + + try: + asn1Spec.clone(ints2octs([0xd1, 0x84, 0xd1, 0x84])) + except PyAsn1Error: + pass + else: + assert False, 'Size constraint tolerated' + + try: + asn1Spec.clone(ints2octs([0xd1, 0x84])) + except PyAsn1Error: + assert False, 'Size constraint failed' + + if sys.version_info[0] <= 2: + def testStr(self): + assert str(self.asn1String) == self.pythonString.encode('utf-8'), '__str__() fails' + + def testInit(self): + assert char.UTF8String(unicode('abc')) == 'abc' + assert char.UTF8String('abc') == 'abc' + assert char.UTF8String([97, 98, 99]) == 'abc' +# assert char.UTF8String(map(lambda x: x, [97, 98, 99])) == 'abc' + else: + def testStr(self): + assert str(self.asn1String) == self.pythonString, '__str__() fails' + + def testInit(self): + assert char.UTF8String(bytes('abc', 'utf-8')) == 'abc' + assert char.UTF8String('abc') == 'abc' + assert char.UTF8String([97, 98, 99]) == 'abc' +# assert char.UTF8String(map(lambda x: x, [97, 98, 99])) == 'abc' + + def testInitFromAsn1(self): + assert char.UTF8String(char.UTF8String('abc')) == 'abc' + assert char.UTF8String(univ.OctetString('abc')) == 'abc' + + def testAsOctets(self): + assert self.asn1String.asOctets() == self.pythonString.encode('utf-8'), 'testAsOctets() fails' + + def testAsNumbers(self): + assert self.asn1String.asNumbers() == (209, 132, 208, 176), 'testAsNumbers() fails' + + def testSeq(self): + assert self.asn1String[0] == self.pythonString[0], '__getitem__() fails' + + # def testEmpty(self): + # try: + # str(char.UTF8String()) + # except PyAsn1Error: + # pass + # else: + # assert 0, 'Value operation on ASN1 type tolerated' + + def testAdd(self): + assert self.asn1String + 'q' == self.pythonString + 'q', '__add__() fails' + + def testRadd(self): + assert 'q' + self.asn1String == 'q' + self.pythonString, '__radd__() fails' + + def testMul(self): + assert self.asn1String * 2 == self.pythonString * 2, '__mul__() fails' + + def testRmul(self): + assert 2 * self.asn1String == 2 * self.pythonString, '__rmul__() fails' + + def testContains(self): + assert self.pythonString in self.asn1String + assert 'q' + self.pythonString not in self.asn1String + +# if sys.version_info[:2] > (2, 4): +# def testReverse(self): +# assert reversed(self.asn1String) == self.pythonString[1] + self.pythonString[0] + + +suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) + +if __name__ == '__main__': + unittest.TextTestRunner(verbosity=2).run(suite) |