• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#
2# This file is part of pyasn1 software.
3#
4# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
5# License: http://snmplabs.com/pyasn1/license.html
6#
7import sys
8
9try:
10    import unittest2 as unittest
11except ImportError:
12    import unittest
13
14from tests.base import BaseTestCase
15
16from pyasn1.compat import octets
17
18
19class OctetsTestCase(BaseTestCase):
20
21    if sys.version_info[0] > 2:
22
23        def test_ints2octs(self):
24            assert [1, 2, 3] == list(octets.ints2octs([1, 2, 3]))
25
26        def test_ints2octs_empty(self):
27            assert not octets.ints2octs([])
28
29        def test_int2oct(self):
30            assert [12] == list(octets.int2oct(12))
31
32        def test_octs2ints(self):
33            assert [1, 2, 3] == list(octets.octs2ints(bytes([1, 2, 3])))
34
35        def test_octs2ints_empty(self):
36            assert not octets.octs2ints(bytes([]))
37
38        def test_oct2int(self):
39            assert 12 == octets.oct2int(bytes([12]))[0]
40
41        def test_str2octs(self):
42            assert bytes([1, 2, 3]) == octets.str2octs('\x01\x02\x03')
43
44        def test_str2octs_empty(self):
45            assert not octets.str2octs('')
46
47        def test_octs2str(self):
48            assert '\x01\x02\x03' == octets.octs2str(bytes([1, 2, 3]))
49
50        def test_octs2str_empty(self):
51            assert not octets.octs2str(bytes([]))
52
53        def test_isOctetsType(self):
54            assert octets.isOctetsType('abc') == False
55            assert octets.isOctetsType(123) == False
56            assert octets.isOctetsType(bytes()) == True
57
58        def test_isStringType(self):
59            assert octets.isStringType('abc') == True
60            assert octets.isStringType(123) == False
61            assert octets.isStringType(bytes()) == False
62
63        def test_ensureString(self):
64            assert 'abc'.encode() == octets.ensureString('abc'.encode())
65            assert bytes([1, 2, 3]) == octets.ensureString([1, 2, 3])
66
67    else:
68
69        def test_ints2octs(self):
70            assert '\x01\x02\x03' == octets.ints2octs([1, 2, 3])
71
72        def test_ints2octs_empty(self):
73            assert not octets.ints2octs([])
74
75        def test_int2oct(self):
76            assert '\x0c' == octets.int2oct(12)
77
78        def test_octs2ints(self):
79            assert [1, 2, 3] == octets.octs2ints('\x01\x02\x03')
80
81        def test_octs2ints_empty(self):
82            assert not octets.octs2ints('')
83
84        def test_oct2int(self):
85            assert 12 == octets.oct2int('\x0c')
86
87        def test_str2octs(self):
88            assert '\x01\x02\x03' == octets.str2octs('\x01\x02\x03')
89
90        def test_str2octs_empty(self):
91            assert not octets.str2octs('')
92
93        def test_octs2str(self):
94            assert '\x01\x02\x03' == octets.octs2str('\x01\x02\x03')
95
96        def test_octs2str_empty(self):
97            assert not octets.octs2str('')
98
99        def test_isOctetsType(self):
100            assert octets.isOctetsType('abc') == True
101            assert octets.isOctetsType(123) == False
102            assert octets.isOctetsType(unicode('abc')) == False
103
104        def test_isStringType(self):
105            assert octets.isStringType('abc') == True
106            assert octets.isStringType(123) == False
107            assert octets.isStringType(unicode('abc')) == True
108
109        def test_ensureString(self):
110            assert 'abc' == octets.ensureString('abc')
111            assert '123' == octets.ensureString(123)
112
113
114suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
115
116if __name__ == '__main__':
117    unittest.TextTestRunner(verbosity=2).run(suite)
118