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
|
# pylint: disable=missing-module-docstring
# pylint: disable=invalid-name
# pylint: disable=missing-function-docstring
import platform
from smartcard.pcsc.PCSCExceptions import (
AddReaderToGroupException,
BaseSCardException,
EstablishContextException,
IntroduceReaderException,
ListReadersException,
ReleaseContextException,
RemoveReaderFromGroupException,
)
from smartcard.scard import (
SCARD_E_DUPLICATE_READER,
SCARD_E_INVALID_HANDLE,
SCARD_E_NO_SERVICE,
SCARD_E_NOT_TRANSACTED,
SCARD_E_UNKNOWN_READER,
SCARD_S_SUCCESS,
)
def test_list_readers_exception():
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: "
"The network resource type is not correct. (0x00000042)"
)
else:
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
def test_establish_context_exception():
exc = EstablishContextException(SCARD_E_NO_SERVICE)
assert exc.hresult == SCARD_E_NO_SERVICE
text = str(exc)
if platform.system() == "Windows":
expected = (
"Failed to establish context: "
"The Smart Card Resource Manager is not running. (0x8010001D)"
)
else:
expected = "Failed to establish context: Service not available. (0x8010001D)"
assert text == expected
def test_introduce_reader_exception():
exc = IntroduceReaderException(SCARD_E_DUPLICATE_READER, "foobar")
assert exc.hresult == SCARD_E_DUPLICATE_READER
text = str(exc)
if platform.system() == "Windows":
expected = (
"Failed to introduce a new reader: foobar: "
"The reader driver did not produce a unique reader name. (0x8010001B)"
)
else:
expected = (
"Failed to introduce a new reader: foobar: "
"Reader already exists. (0x8010001B)"
)
assert text == expected
def test_remove_reader_from_group_exception():
exc = RemoveReaderFromGroupException(
SCARD_E_INVALID_HANDLE, "readername", "readergroup"
)
assert exc.hresult == SCARD_E_INVALID_HANDLE
text = str(exc)
if platform.system() == "Windows":
expected = (
"Failed to remove reader: readername from group: readergroup: "
"The supplied handle was invalid. (0x80100003)"
)
else:
expected = (
"Failed to remove reader: readername from group: readergroup: "
"Invalid handle. (0x80100003)"
)
assert text == expected
def test_add_reader_to_group_exception():
exc = AddReaderToGroupException(SCARD_E_INVALID_HANDLE, "reader", "group")
assert exc.hresult == SCARD_E_INVALID_HANDLE
text = str(exc)
if platform.system() == "Windows":
expected = (
"Failed to add reader: reader to group: group: "
"The supplied handle was invalid. (0x80100003)"
)
else:
expected = (
"Failed to add reader: reader to group: group: "
"Invalid handle. (0x80100003)"
)
assert text == expected
def test_release_context_exception():
exc = ReleaseContextException(SCARD_E_INVALID_HANDLE)
assert exc.hresult == SCARD_E_INVALID_HANDLE
text = str(exc)
if platform.system() == "Windows":
expected = (
"Failed to release context: The supplied handle was invalid. (0x80100003)"
)
else:
expected = "Failed to release context: Invalid handle. (0x80100003)"
assert text == expected
def test_base_scard_exception():
exc = BaseSCardException(SCARD_E_UNKNOWN_READER)
assert exc.hresult == SCARD_E_UNKNOWN_READER
text = str(exc)
if platform.system() == "Windows":
expected = (
"scard exception: "
"The specified reader name is not recognized. (0x80100009)"
)
else:
expected = "scard exception: Unknown reader specified. (0x80100009)"
assert text == expected
exc = BaseSCardException(hresult=-1)
assert exc.hresult == -1
text = str(exc)
expected = "scard exception"
assert text == expected
exc = BaseSCardException(hresult=-1, message="foo bar")
assert exc.hresult == -1
text = str(exc)
expected = "foo bar"
assert text == expected
exc = BaseSCardException(message="foo", hresult=SCARD_E_NOT_TRANSACTED)
assert exc.hresult == SCARD_E_NOT_TRANSACTED
text = str(exc)
if platform.system() == "Windows":
expected = "An attempt was made to end a non-existent transaction. "
else:
expected = "Transaction failed."
expected = "foo: " + expected + " (0x80100016)"
assert text == expected
|