File: test_octets.py

package info (click to toggle)
python-pyasn1-lextudio 1.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,556 kB
  • sloc: python: 17,511; makefile: 171
file content (68 lines) | stat: -rw-r--r-- 1,849 bytes parent folder | download
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
#
# 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):
    def test_bytes(self):
        assert [1, 2, 3] == list(bytes([1, 2, 3]))

    def test_bytes_empty(self):
        assert not bytes([])

    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_bytes(self):
        assert "abc".encode() == bytes("abc".encode())
        assert bytes([1, 2, 3]) == bytes([1, 2, 3])


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == "__main__":
    unittest.TextTestRunner(verbosity=2).run(suite)