diff options
author | Ilya Etingof <etingof@gmail.com> | 2017-07-10 02:04:32 +0200 |
---|---|---|
committer | Ilya Etingof <etingof@gmail.com> | 2017-07-10 02:04:32 +0200 |
commit | 186ed2d39ef3f5d3a0aad1c92bfbb7044c94bc63 (patch) | |
tree | a17c99334d5af4707ea650a4529ebbf5a75a080b /tests/compat | |
parent | f0dbb71d3da3524baa52176df732eb979c76cc17 (diff) | |
download | pyasn1-git-186ed2d39ef3f5d3a0aad1c92bfbb7044c94bc63.tar.gz |
unit tests added for "compat" sub-package
Diffstat (limited to 'tests/compat')
-rw-r--r-- | tests/compat/__init__.py | 1 | ||||
-rw-r--r-- | tests/compat/__main__.py | 21 | ||||
-rw-r--r-- | tests/compat/test_binary.py | 53 | ||||
-rw-r--r-- | tests/compat/test_integer.py | 51 | ||||
-rw-r--r-- | tests/compat/test_octets.py | 114 |
5 files changed, 240 insertions, 0 deletions
diff --git a/tests/compat/__init__.py b/tests/compat/__init__.py new file mode 100644 index 0000000..8c3066b --- /dev/null +++ b/tests/compat/__init__.py @@ -0,0 +1 @@ +# This file is necessary to make this directory a package. diff --git a/tests/compat/__main__.py b/tests/compat/__main__.py new file mode 100644 index 0000000..ce26562 --- /dev/null +++ b/tests/compat/__main__.py @@ -0,0 +1,21 @@ +# +# This file is part of pyasn1 software. +# +# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com> +# License: http://pyasn1.sf.net/license.html +# +try: + import unittest2 as unittest + +except ImportError: + import unittest + +suite = unittest.TestLoader().loadTestsFromNames( + ['tests.compat.test_binary.suite', + 'tests.compat.test_integer.suite', + 'tests.compat.test_octets.suite'] +) + + +if __name__ == '__main__': + unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/tests/compat/test_binary.py b/tests/compat/test_binary.py new file mode 100644 index 0000000..7660206 --- /dev/null +++ b/tests/compat/test_binary.py @@ -0,0 +1,53 @@ +# +# 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.compat import binary + +try: + import unittest2 as unittest +except ImportError: + import unittest + + +class BinaryTestCase(unittest.TestCase): + + def test_bin_zero(self): + assert '0b0' == binary.bin(0) + + + def test_bin_noarg(self): + try: + binary.bin() + + except TypeError: + pass + + except: + assert 0, 'bin() tolerates no arguments' + + + def test_bin_allones(self): + assert '0b1111111111111111111111111111111111111111111111111111111111111111' == binary.bin(0xffffffffffffffff) + + + def test_bin_allzeros(self): + assert '0b0' == binary.bin(0x0000000) + + + + def test_bin_pos(self): + assert '0b1000000010000000100000001' == binary.bin(0x01010101) + + + def test_bin_neg(self): + assert '-0b1000000010000000100000001' == binary.bin(-0x01010101) + + +suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) + +if __name__ == '__main__': + unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/tests/compat/test_integer.py b/tests/compat/test_integer.py new file mode 100644 index 0000000..005abb6 --- /dev/null +++ b/tests/compat/test_integer.py @@ -0,0 +1,51 @@ +# +# 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.compat import integer + +try: + import unittest2 as unittest +except ImportError: + import unittest + + +class IntegerTestCase(unittest.TestCase): + + if sys.version_info[0] > 2: + + def test_from_bytes_zero(self): + assert 0 == integer.from_bytes(bytes([0]), signed=False) + + def test_from_bytes_unsigned(self): + assert -66051 == integer.from_bytes(bytes([254, 253, 253]), signed=True) + + def test_from_bytes_signed(self): + assert 66051 == integer.from_bytes(bytes([0, 1, 2, 3]), signed=False) + + def test_from_bytes_empty(self): + assert 0 == integer.from_bytes(bytes([])) + + else: + + def test_from_bytes_zero(self): + assert 0 == integer.from_bytes('\x00', signed=False) + + def test_from_bytes_unsigned(self): + assert -66051 == integer.from_bytes('\xfe\xfd\xfd', signed=True) + + def test_from_bytes_signed(self): + assert 66051 == integer.from_bytes('\x01\x02\x03', signed=False) + +# TODO: fix from_bytes() +# def test_from_bytes_empty(self): +# assert 0 == integer.from_bytes('') + + +suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) + +if __name__ == '__main__': + unittest.TextTestRunner(verbosity=2).run(suite) diff --git a/tests/compat/test_octets.py b/tests/compat/test_octets.py new file mode 100644 index 0000000..4652b33 --- /dev/null +++ b/tests/compat/test_octets.py @@ -0,0 +1,114 @@ +# +# 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.compat import octets + +try: + import unittest2 as unittest +except ImportError: + import unittest + + +class OctetsTestCase(unittest.TestCase): + + if sys.version_info[0] > 2: + + def test_ints2octs(self): + assert [1, 2, 3] == list(octets.ints2octs([1, 2, 3])) + + def test_ints2octs_empty(self): + assert not octets.ints2octs([]) + + def test_int2oct(self): + assert [12] == list(octets.int2oct(12)) + + def test_octs2ints(self): + assert [1, 2, 3] == list(octets.octs2ints(bytes([1, 2, 3]))) + + def test_octs2ints_empty(self): + assert not octets.octs2ints(bytes([])) + + def test_oct2int(self): + assert 12 == octets.oct2int(bytes([12]))[0] + + def test_str2octs(self): + assert bytes([1, 2, 3]) == octets.str2octs('\x01\x02\x03') + + def test_str2octs_empty(self): + assert not octets.str2octs('') + + def test_octs2str(self): + assert '\x01\x02\x03' == octets.octs2str(bytes([1, 2, 3])) + + def test_octs2str_empty(self): + assert not octets.octs2str(bytes([])) + + def test_isOctetsType(self): + assert octets.isOctetsType('abc') == False + assert octets.isOctetsType(123) == False + assert octets.isOctetsType(bytes()) == True + + def test_isStringType(self): + assert octets.isStringType('abc') == True + assert octets.isStringType(123) == False + assert octets.isStringType(bytes()) == False + + def test_ensureString(self): + assert 'abc'.encode() == octets.ensureString('abc'.encode()) + assert bytes([1, 2, 3]) == octets.ensureString([1, 2, 3]) + + else: + + def test_ints2octs(self): + assert '\x01\x02\x03' == octets.ints2octs([1, 2, 3]) + + def test_ints2octs_empty(self): + assert not octets.ints2octs([]) + + def test_int2oct(self): + assert '\x0c' == octets.int2oct(12) + + def test_octs2ints(self): + assert [1, 2, 3] == octets.octs2ints('\x01\x02\x03') + + def test_octs2ints_empty(self): + assert not octets.octs2ints('') + + def test_oct2int(self): + assert 12 == octets.oct2int('\x0c') + + def test_str2octs(self): + assert '\x01\x02\x03' == octets.str2octs('\x01\x02\x03') + + def test_str2octs_empty(self): + assert not octets.str2octs('') + + def test_octs2str(self): + assert '\x01\x02\x03' == octets.octs2str('\x01\x02\x03') + + def test_octs2str_empty(self): + assert not octets.octs2str('') + + def test_isOctetsType(self): + assert octets.isOctetsType('abc') == True + assert octets.isOctetsType(123) == False + assert octets.isOctetsType(unicode('abc')) == False + + def test_isStringType(self): + assert octets.isStringType('abc') == True + assert octets.isStringType(123) == False + assert octets.isStringType(unicode('abc')) == True + + def test_ensureString(self): + assert 'abc' == octets.ensureString('abc') + assert '123' == octets.ensureString(123) + + +suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) + +if __name__ == '__main__': + unittest.TextTestRunner(verbosity=2).run(suite) |