summaryrefslogtreecommitdiff
path: root/tests/compat/test_octets.py
blob: 5a9bc7c31b6ea3b3458f13b3b11cbf3a6dacfd5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#
# This file is part of pyasn1 software.
#
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pyasn1/license.html
#
import sys

try:
    import unittest2 as unittest
except ImportError:
    import unittest

from tests.base import BaseTestCase

from pyasn1.compat import octets


class OctetsTestCase(BaseTestCase):

    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)