File: SCardBlockingBehaviourTest.py

package info (click to toggle)
pcsc-lite 2.4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,700 kB
  • sloc: ansic: 13,894; python: 3,231; lex: 609; makefile: 246; sh: 54; xml: 22
file content (292 lines) | stat: -rwxr-xr-x 9,825 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3

"""
# Copyright (c) 2010 Jean-Luc Giraud (jlgiraud@mac.com)
# All rights reserved.
"""

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import sys
import threading
import time

from smartcard.Exceptions import NoReadersException
from smartcard.pcsc.PCSCExceptions import (  # ReleaseContextException,
    BaseSCardException,
    EstablishContextException,
    ListReadersException,
)
from smartcard.scard import (  # SCardReleaseContext,
    SCARD_ATTR_DEVICE_FRIENDLY_NAME_A,
    SCARD_LEAVE_CARD,
    SCARD_PCI_T0,
    SCARD_PROTOCOL_ANY,
    SCARD_PROTOCOL_T0,
    SCARD_S_SUCCESS,
    SCARD_SCOPE_USER,
    SCARD_SHARE_SHARED,
    SCardBeginTransaction,
    SCardConnect,
    SCardEndTransaction,
    SCardEstablishContext,
    SCardGetAttrib,
    SCardGetErrorMessage,
    SCardListReaders,
    SCardReconnect,
    SCardStatus,
    SCardTransmit,
)

# Global variable used by test functions to acknowledge
# that they do not get blocked
unblocked = False


def check(testFunctionName, hresult, duration):
    """check"""
    if hresult != SCARD_S_SUCCESS:
        print(f"{testFunctionName} failed: {SCardGetErrorMessage(hresult)}")
        print('Failure for "Sharing violation" are OK for non blocking calls')
    print(f"{testFunctionName} finished after {duration}s")


def SCardReconnectTest(_hcontextTest, hcardTest, _readerName):
    """SCardReconnectTest"""
    global unblocked
    testFunctionName = sys._getframe().f_code.co_name
    print(f"Test thread for {testFunctionName}")
    before = time.time()
    hresult, _dwDisposition = SCardReconnect(
        hcardTest,
        SCARD_SHARE_SHARED,
        SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T0,
        SCARD_LEAVE_CARD,
    )
    check(testFunctionName, hresult, time.time() - before)
    unblocked = True


def SCardGetAttribTest(_hcontextTest, hcardTest, _readerName):
    """SCardGetAttribTest"""
    global unblocked
    testFunctionName = sys._getframe().f_code.co_name
    print(f"Test thread for {testFunctionName}")
    before = time.time()
    hresult, _attrib = SCardGetAttrib(hcardTest, SCARD_ATTR_DEVICE_FRIENDLY_NAME_A)
    check(testFunctionName, hresult, time.time() - before)
    unblocked = True


def SCardTransmitTest(_hcontextTest, hcardTest, _readerName):
    """SCardTransmitTest"""
    global unblocked
    testFunctionName = sys._getframe().f_code.co_name
    print(f"Test thread for {testFunctionName}")
    SELECT = [0xA0, 0xA4, 0x00, 0x00, 0x02]
    DF_TELECOM = [0x7F, 0x10]
    before = time.time()
    hresult, _response = SCardTransmit(hcardTest, SCARD_PCI_T0, SELECT + DF_TELECOM)
    check(testFunctionName, hresult, time.time() - before)
    unblocked = True


def SCardStatusTest(_hcontextTest, hcardTest, _readerName):
    """SCardStatusTest"""
    global unblocked
    testFunctionName = sys._getframe().f_code.co_name
    print(f"Test thread for {testFunctionName}")
    before = time.time()
    hresult, _reader, _state, _protocol, _atr = SCardStatus(hcardTest)
    check(testFunctionName, hresult, time.time() - before)
    unblocked = True


def SCardConnectTest(_hcontextTest, _hcardTest, readerName):
    """SCardConnectTest"""
    global unblocked
    testFunctionName = sys._getframe().f_code.co_name
    print(f"Test thread for {testFunctionName}")
    hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
    if hresult != SCARD_S_SUCCESS:
        raise EstablishContextException(hresult)

    before = time.time()
    # Connect in SCARD_SHARE_SHARED mode
    hresult, _hcard, _dwActiveProtocol = SCardConnect(
        hcontext, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_ANY
    )
    check(testFunctionName, hresult, time.time() - before)
    unblocked = True


def SCardBeginTransactionTest(_hcontextTest, hcardTest, _readerName):
    """SCardBeginTransactionTest"""
    global unblocked
    testFunctionName = sys._getframe().f_code.co_name
    print(f"Test thread for {testFunctionName}")
    before = time.time()
    hresult = SCardBeginTransaction(hcardTest)
    if hresult != SCARD_S_SUCCESS:
        print(f"{testFunctionName} failed: {SCardGetErrorMessage(hresult)}")
        print('Failure for "Sharing violation" are OK for non blocking calls')
    hresult = SCardEndTransaction(hcardTest, SCARD_LEAVE_CARD)
    check(testFunctionName, hresult, time.time() - before)
    unblocked = True


# def TemplateTest(hcontextTest, hcardTest, readerName):
#    global unblocked
#    testFunctionName = sys._getframe().f_code.co_name
#    print("Test thread for %s" % testFunctionName)
#    before = time.time()
#    hresult, attrib = Template(hcardTest, SCARD_ATTR_DEVICE_FRIENDLY_NAME_A)
#    check(testFunctionName, hresult, time.time() - before)
#    unblocked = True


# Format:
# 'functioname' : [TestFunction, ShouldBlock?]

tests_SnowLeopard = {
    "SCardReconnect": [SCardReconnectTest, False],
    "SCardGetAttrib": [SCardGetAttribTest, False],
    "SCardTransmit": [SCardTransmitTest, False],
    "SCardStatus": [SCardStatusTest, False],
    "SCardConnect": [SCardConnectTest, True],
    "SCardBeginTransaction": [SCardBeginTransactionTest, True],
}

tests_Win7 = {
    "SCardReconnect": [SCardReconnectTest, True],
    "SCardGetAttrib": [SCardGetAttribTest, False],
    "SCardTransmit": [SCardTransmitTest, True],
    "SCardStatus": [SCardStatusTest, True],
    "SCardConnect": [SCardConnectTest, True],
    "SCardBeginTransaction": [SCardBeginTransactionTest, True],
}

tests_Linux = tests_Win7

# Windows should be the reference implementation
tests = tests_Linux


def Connect(index=0):
    """docstring for Connect"""
    hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
    if hresult != SCARD_S_SUCCESS:
        raise EstablishContextException(hresult)

    hresult, readers = SCardListReaders(hcontext, [])
    if hresult != SCARD_S_SUCCESS:
        raise ListReadersException(hresult)
    print("PC/SC Readers:", readers)
    if len(readers) <= 0:
        raise NoReadersException()
    reader = readers[index]
    print("Using reader:", reader)

    # Connect in SCARD_SHARE_SHARED mode
    hresult, hcard, _dwActiveProtocol = SCardConnect(
        hcontext, reader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_ANY
    )
    if hresult != SCARD_S_SUCCESS:
        print("SCardConnect failed")
        raise BaseSCardException(hresult)
    return hcontext, hcard, reader


def ConnectWithReader(readerName):
    """docstring for Connect"""
    hresult, hcontext = SCardEstablishContext(SCARD_SCOPE_USER)
    if hresult != SCARD_S_SUCCESS:
        raise EstablishContextException(hresult)

    # Connect in SCARD_SHARE_SHARED mode
    hresult, hcard, _dwActiveProtocol = SCardConnect(
        hcontext, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_ANY
    )
    if hresult != SCARD_S_SUCCESS:
        print("SCardConnect failed")
        raise BaseSCardException(hresult)
    return hcontext, hcard


def main():
    """docstring for main"""
    global unblocked

    RED = "\033[0;31m"
    BLUE = "\033[0;34m"
    NORMAL = "\033[00m"

    # if len(sys.argv) < 4:
    #    print(usage)
    #    sys.exit(1)
    # Allow to specify test name
    # Options:
    #   -m : run manually (independent processes)

    index = 0
    if len(sys.argv) > 1:
        index = int(sys.argv[1])
    _hcontext, hcard, readerName = Connect(index)
    # Creating the test handles here:
    # the test thread can't create them as
    # doing it may block on another call
    hcontextTest, hcardTest = ConnectWithReader(readerName)

    for testTarget, testResults in tests.items():
        # Start test function in a new thread
        SCardBeginTransaction(hcard)
        unblocked = False
        print()
        print(f"Testing {testTarget}, expecting", end=" ")
        methodName, shouldBlock = testResults
        if shouldBlock:
            print("blocking")
        else:
            print("non blocking")
        thread = threading.Thread(
            target=methodName, args=(hcontextTest, hcardTest, readerName)
        )
        thread.start()
        # print("Thread started")
        time.sleep(1)
        failed = (unblocked and shouldBlock) or ((not unblocked) and (not shouldBlock))
        SCardEndTransaction(hcard, SCARD_LEAVE_CARD)

        # wait for the thread to finish
        thread.join()

        if failed:
            print(RED + "Test for " + testTarget + " FAILED!" + NORMAL)
        else:
            print(BLUE + "Test for " + testTarget + " succeeded" + NORMAL)


if __name__ == "__main__":
    main()