File: test_util.py

package info (click to toggle)
pyscard 2.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,348 kB
  • sloc: python: 8,718; ansic: 1,971; makefile: 51; sh: 7
file content (183 lines) | stat: -rw-r--r-- 4,847 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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# pylint: disable=missing-module-docstring
# pylint: disable=missing-function-docstring

import pytest

from smartcard.util import (
    COMMA,
    HEX,
    PACK,
    UPPERCASE,
    BinStringToHexList,
    HexListToBinString,
    bs2hl,
    hl2bs,
    padd,
    toASCIIBytes,
    toASCIIString,
    toBytes,
    toGSM3_38Bytes,
    toHexString,
)


def test_to_bytes():
    data_in = "3B 65 00 00 9C 11 01 01 03"
    data_out = [59, 101, 0, 0, 156, 17, 1, 1, 3]
    assert toBytes(data_in) == data_out

    data_in = "3B6500009C11010103"
    assert toBytes(data_in) == data_out

    data_in = "3B6500   009C1101  0103"
    assert toBytes(data_in) == data_out

    data_in = """
                3B 65 00
                00 9C 11 01
                01 03
              """
    assert toBytes(data_in) == data_out

    data_in = "zz"
    with pytest.raises(TypeError):
        toBytes(data_in)


def test_padd():
    data_in = toBytes("3B 65 00 00 9C 11 01 01 03")
    old_data_in = list(data_in)
    data_out = [
        0x3B,
        0x65,
        0,
        0,
        0x9C,
        0x11,
        1,
        1,
        3,
        0xFF,
        0xFF,
        0xFF,
        0xFF,
        0xFF,
        0xFF,
        0xFF,
    ]
    assert padd(data_in, 16) == data_out
    assert data_in == old_data_in

    assert padd(data_in, 4) == data_in


def test_to_ascii_bytes():
    data_in = "Number 101"
    data_out = [0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0x20, 0x31, 0x30, 0x31]
    assert toASCIIBytes(data_in) == data_out


def test_to_ascii_string():
    data_in = [0x4E, 0x75, 0x6D, 0x62, 0x65, 0x72, 0x20, 0x31, 0x30, 0x31]
    data_out = "Number 101"
    assert toASCIIString(data_in) == data_out

    data_in = [0x01, 0x20, 0x80, 0x7E, 0xF0]
    data_out = ". .~."
    assert toASCIIString(data_in) == data_out


def test_to_gsm3_38_bytes():
    data_in = "@Pascal"
    data_out = [0x00, 0x50, 0x61, 0x73, 0x63, 0x61, 0x6C]
    assert toGSM3_38Bytes(data_in) == data_out

    data_in = "@ùPascal"
    data_out = [0x00, 0x06, 0x50, 0x61, 0x73, 0x63, 0x61, 0x6C]
    assert toGSM3_38Bytes(data_in) == data_out

    data_in = "@ùPascal".encode("iso8859-1")
    data_out = [0x00, 0x06, 0x50, 0x61, 0x73, 0x63, 0x61, 0x6C]
    assert toGSM3_38Bytes(data_in) == data_out

    data_in = "1234"
    data_out = [0x31, 0x32, 0x33, 0x34]
    assert toGSM3_38Bytes(data_in) == data_out


def test_to_hex_string():
    data_in = []
    data_out = ""
    assert toHexString(data_in) == data_out

    data_in = 42
    with pytest.raises(TypeError):
        toHexString(data_in)

    data_in = [0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03]
    data_out = "3B 65 00 00 9C 11 01 01 03"
    assert toHexString(data_in) == data_out

    data_out = "3B, 65, 00, 00, 9C, 11, 01, 01, 03"
    assert toHexString(data_in, COMMA) == data_out

    data_out = "0x3B 0x65 0x00 0x00 0x9C 0x11 0x01 0x01 0x03"
    assert toHexString(data_in, HEX) == data_out

    data_out = "0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03"
    assert toHexString(data_in, HEX | COMMA) == data_out

    data_out = "0X3B 0X65 0X00 0X00 0X9C 0X11 0X01 0X01 0X03"
    assert toHexString(data_in, HEX | UPPERCASE) == data_out

    data_out = "0X3B, 0X65, 0X00, 0X00, 0X9C, 0X11, 0X01, 0X01, 0X03"
    assert toHexString(data_in, HEX | UPPERCASE | COMMA) == data_out

    data_out = "3B6500009C11010103"
    assert toHexString(data_in, PACK) == data_out

    data_out = "3B,65,00,00,9C,11,01,01,03"
    assert toHexString(data_in, COMMA | PACK) == data_out

    data_out = "0x3B0x650x000x000x9C0x110x010x010x03"
    assert toHexString(data_in, HEX | PACK) == data_out

    data_out = "0x3B,0x65,0x00,0x00,0x9C,0x11,0x01,0x01,0x03"
    assert toHexString(data_in, HEX | COMMA | PACK) == data_out

    data_out = "0X3B0X650X000X000X9C0X110X010X010X03"
    assert toHexString(data_in, HEX | UPPERCASE | PACK) == data_out

    data_out = "0X3B,0X65,0X00,0X00,0X9C,0X11,0X01,0X01,0X03"
    assert toHexString(data_in, HEX | UPPERCASE | COMMA | PACK) == data_out

    with pytest.raises(TypeError):
        toHexString("foo")


def test_hex_list_to_bin_string():
    data_in = [1, 2, 3]
    data_out = "\x01\x02\x03"
    with pytest.warns(DeprecationWarning):
        assert HexListToBinString(data_in) == data_out


def test_bin_string_to_hex_list():
    data_in = "\x01\x02\x03"
    data_out = [1, 2, 3]
    with pytest.warns(DeprecationWarning):
        assert BinStringToHexList(data_in) == data_out


def test_hl2bs():
    data_in = [78, 117, 109, 98, 101, 114, 32, 49, 48, 49]
    data_out = "Number 101"
    with pytest.warns(DeprecationWarning):
        assert hl2bs(data_in) == data_out


def test_bs2hl():
    data_in = "Number 101"
    data_out = [78, 117, 109, 98, 101, 114, 32, 49, 48, 49]
    with pytest.warns(DeprecationWarning):
        assert bs2hl(data_in) == data_out