File: test_Exceptions.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 (193 lines) | stat: -rw-r--r-- 5,736 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
184
185
186
187
188
189
190
191
192
193
# pylint: disable=missing-module-docstring
# pylint: disable=invalid-name
# pylint: disable=missing-function-docstring

import platform

import pytest

from smartcard.Exceptions import (
    CardConnectionException,
    CardRequestTimeoutException,
    InvalidATRMaskLengthException,
    InvalidReaderException,
    ListReadersException,
    NoCardException,
    NoReadersException,
    SmartcardException,
)
from smartcard.scard import (
    SCARD_E_NO_SERVICE,
    SCARD_E_NOT_TRANSACTED,
    SCARD_E_UNKNOWN_READER,
    SCARD_PROTOCOL_ANY,
    SCARD_S_SUCCESS,
    SCARD_SCOPE_USER,
    SCARD_SHARE_SHARED,
    SCARD_W_REMOVED_CARD,
    SCardConnect,
    SCardEstablishContext,
)


def test_hresult_value():
    exc = SmartcardException()
    assert exc.hresult == -1


def test_list_readers_exception():
    # pylint: disable=duplicate-code
    exc = ListReadersException(-1)
    assert str(exc) == "Failed to list readers"

    exc = ListReadersException(0)
    assert exc.hresult == 0
    text = str(exc)
    if platform.system() == "Windows":
        expected = (
            "Failed to list readers: "
            "The operation completed successfully.  (0x00000000)"
        )
    else:
        expected = "Failed to list readers: Command successful. (0x00000000)"
    assert text == expected

    exc = ListReadersException(0x42)
    assert exc.hresult == 0x42
    text = str(exc)
    if platform.system() != "Windows":
        expected = "Failed to list readers: Unknown error: 0x00000042 (0x00000042)"
        macos_bug_expected = expected.replace("Unknown", "Unkown")
        assert text in (expected, macos_bug_expected)

    exc = ListReadersException(SCARD_S_SUCCESS)
    assert exc.hresult == 0

    exc = ListReadersException(SCARD_E_NO_SERVICE)
    assert exc.hresult == SCARD_E_NO_SERVICE
    text = str(exc)
    if platform.system() == "Windows":
        expected = (
            "Failed to list readers: "
            "The Smart Card Resource Manager is not running.  (0x8010001D)"
        )
    else:
        expected = "Failed to list readers: Service not available. (0x8010001D)"

    assert text == expected

    exc = ListReadersException(SCARD_E_NOT_TRANSACTED)
    if platform.system() == "Windows":
        expected = (
            "Failed to list readers: "
            "An attempt was made to end a non-existent transaction.  (0x80100016)"
        )
    else:
        expected = "Failed to list readers: Transaction failed. (0x80100016)"
    assert str(exc) == expected


def test_no_readers_exception():
    exc = NoReadersException()
    assert exc.hresult == -1
    text = str(exc)
    assert text == "No reader found"


def test_invalid_reader_exception():
    exc = InvalidReaderException("foobar")
    assert exc.hresult == -1
    text = str(exc)
    assert text == "Invalid reader: foobar"


def test_card_connection_exception():
    exc = CardConnectionException()
    assert exc.hresult == -1
    text = str(exc)
    assert text == ""

    exc = CardConnectionException(hresult=SCARD_W_REMOVED_CARD)
    assert exc.hresult == SCARD_W_REMOVED_CARD
    text = str(exc)
    if platform.system() == "Windows":
        expected = (
            "The smart card has been removed, "
            "so that further communication is not possible.  (0x80100069)"
        )
    else:
        expected = "Card was removed. (0x80100069)"

    assert text == expected

    exc = CardConnectionException("", SCARD_W_REMOVED_CARD)
    assert exc.hresult == SCARD_W_REMOVED_CARD
    text = str(exc)
    assert text == expected

    # now add "foo" in the message
    expected = "foo: " + expected

    exc = CardConnectionException("foo", SCARD_W_REMOVED_CARD)
    assert exc.hresult == SCARD_W_REMOVED_CARD
    text = str(exc)
    assert text == expected

    exc = CardConnectionException(hresult=SCARD_W_REMOVED_CARD, message="foo")
    assert exc.hresult == SCARD_W_REMOVED_CARD
    text = str(exc)
    assert text == expected


def test_hresult():
    hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
    if hresult == SCARD_S_SUCCESS:
        hresult, _, _ = SCardConnect(
            hcontext, "INVALID READER NAME", SCARD_SHARE_SHARED, SCARD_PROTOCOL_ANY
        )
        assert hresult == SCARD_E_UNKNOWN_READER
    else:
        assert hresult == SCARD_E_NO_SERVICE


@pytest.mark.parametrize("arg", ([0], "foo"))
def test_wrong_type(arg):
    # SCardEstablishContext() argument should be int
    with pytest.raises(TypeError):
        SCardEstablishContext(arg)


def test_card_request_timeout_exception():
    exc = CardRequestTimeoutException()
    assert str(exc) == "Time-out during card request"
    exc = CardRequestTimeoutException(SCARD_E_NOT_TRANSACTED)
    if platform.system() == "Windows":
        expected = (
            "Time-out during card request: "
            "An attempt was made to end a non-existent transaction.  (0x80100016)"
        )
    else:
        expected = "Time-out during card request: Transaction failed. (0x80100016)"
    assert str(exc) == expected
    exc = CardRequestTimeoutException(hresult=SCARD_E_NOT_TRANSACTED)
    assert str(exc) == expected


def test_invalid_atr_mask_length_exception():
    exc = InvalidATRMaskLengthException("3B 00")
    assert str(exc) == "Invalid ATR mask length: 3B 00"


def test_no_card_exception():
    exc = NoCardException("foo bar", -1)
    assert str(exc) == "foo bar"

    exc = NoCardException("foo bar", SCARD_E_NOT_TRANSACTED)
    if platform.system() == "Windows":
        expected = (
            "foo bar: "
            "An attempt was made to end a non-existent transaction.  (0x80100016)"
        )
    else:
        expected = "foo bar: Transaction failed. (0x80100016)"
    assert str(exc) == expected