50#define USE_AUTOALLOCATE
54#define BRIGHT_RED "\33[01;31m"
55#define GREEN "\33[32m"
56#define NORMAL "\33[0m"
57#define MAGENTA "\33[35m"
59static void test_rv(LONG rv,
SCARDCONTEXT hContext,
int dont_panic)
68 (void)SCardReleaseContext(hContext);
76int main(
int argc,
char **argv)
81 DWORD dwReaderLen, dwState, dwProt, dwAtrLen;
82 DWORD dwPref, dwReaders = 0;
83 char *pcReader = NULL, *mszReaders;
84#ifdef USE_AUTOALLOCATE
85 unsigned char *pbAtr = NULL;
90 unsigned char as_char[100];
95 unsigned char *pbAttr = NULL;
107 DWORD send_length, length;
112 printf(
"\nMUSCLE PC/SC Lite unitary test Program\n\n");
114 printf(MAGENTA
"THIS PROGRAM IS NOT DESIGNED AS A TESTING TOOL FOR END USERS!\n");
115 printf(
"Do NOT use it unless you really know what you do.\n\n" NORMAL);
117 printf(
"Testing SCardEstablishContext\t: ");
119 test_rv(rv, hContext, PANIC);
121 printf(
"Testing SCardIsValidContext\t: ");
122 rv = SCardIsValidContext(hContext);
123 test_rv(rv, hContext, PANIC);
125 printf(
"Testing SCardIsValidContext\t: ");
126 rv = SCardIsValidContext(hContext+1);
127 test_rv(rv, hContext, DONT_PANIC);
129 printf(
"Testing SCardListReaderGroups\t: ");
130#ifdef USE_AUTOALLOCATE
132 rv = SCardListReaderGroups(hContext, (LPSTR)&mszGroups, &dwGroups);
134 rv = SCardListReaderGroups(hContext, NULL, &dwGroups);
135 test_rv(rv, hContext, PANIC);
137 printf(
"Testing SCardListReaderGroups\t: ");
138 mszGroups = calloc(dwGroups,
sizeof(
char));
139 rv = SCardListReaderGroups(hContext, mszGroups, &dwGroups);
141 test_rv(rv, hContext, PANIC);
147 for (i = 0; i+1 < dwGroups; i++)
150 printf(GREEN
"Group %02d: %s\n" NORMAL, p, &mszGroups[i]);
151 while (mszGroups[++i] != 0) ;
154#ifdef USE_AUTOALLOCATE
155 printf(
"Testing SCardFreeMemory\t\t: ");
156 rv = SCardFreeMemory(hContext, mszGroups);
157 test_rv(rv, hContext, PANIC);
164 printf(
"Testing SCardListReaders\t: ");
165 rv = SCardListReaders(hContext, mszGroups, NULL, &dwReaders);
166 test_rv(rv, hContext, DONT_PANIC);
169 printf(
"Testing SCardGetStatusChange \n");
170 printf(
"Please insert a working reader\t: ");
171 (void)fflush(stdout);
172 rgReaderStates[0].szReader =
"\\\\?PnP?\\Notification";
175 rv = SCardGetStatusChange(hContext,
INFINITE, rgReaderStates, 1);
176 test_rv(rv, hContext, PANIC);
179 printf(
"Testing SCardListReaders\t: ");
180#ifdef USE_AUTOALLOCATE
182 rv = SCardListReaders(hContext, mszGroups, (LPSTR)&mszReaders, &dwReaders);
184 rv = SCardListReaders(hContext, mszGroups, NULL, &dwReaders);
185 test_rv(rv, hContext, PANIC);
187 printf(
"Testing SCardListReaders\t: ");
188 mszReaders = calloc(dwReaders,
sizeof(
char));
189 rv = SCardListReaders(hContext, mszGroups, mszReaders, &dwReaders);
191 test_rv(rv, hContext, PANIC);
197 for (i = 0; i+1 < dwReaders; i++)
200 printf(GREEN
"Reader %02d: %s\n" NORMAL, p, &mszReaders[i]);
202 while (mszReaders[++i] != 0) ;
214 printf(
"Enter the reader number\t\t: ");
215 r = fgets(input,
sizeof(input), stdin);
219 iReader = atoi(input);
221 if (iReader > p || iReader <= 0)
223 printf(
"Invalid Value - try again\n");
232 rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
235 printf(
"Waiting for card insertion\t: ");
236 (void)fflush(stdout);
237 rv = SCardGetStatusChange(hContext,
INFINITE, rgReaderStates, 1);
238 test_rv(rv, hContext, PANIC);
241 printf(
"\nA reader has been connected/disconnected\n");
242 goto wait_for_card_again;
245 printf(
"Testing SCardConnect\t\t: ");
246 rv = SCardConnect(hContext, &mszReaders[iList[iReader]],
249 test_rv(rv, hContext, PANIC);
263 printf(
"Unknown protocol\n");
268 printf(
"Select file:");
270 memcpy(bSendBuffer,
"\x00\xA4\x00\x00\x02\x3F\x00", send_length);
271 for (i=0; i<send_length; i++)
272 printf(
" %02X", bSendBuffer[i]);
274 length =
sizeof(bRecvBuffer);
276 printf(
"Testing SCardTransmit\t\t: ");
277 rv = SCardTransmit(hCard, pioSendPci, bSendBuffer, send_length,
278 &ioRecvPci, bRecvBuffer, &length);
279 test_rv(rv, hContext, PANIC);
280 printf(
" card response:" GREEN);
281 for (i=0; i<length; i++)
282 printf(
" %02X", bRecvBuffer[i]);
285 printf(
"Testing SCardControl\t\t: ");
287 char buffer[1024] = { 0x02 };
288 DWORD cbRecvLength =
sizeof(buffer);
291 sizeof(buffer), &cbRecvLength);
294 for (i=0; i<cbRecvLength; i++)
295 printf(
"%c", buffer[i]);
299 test_rv(rv, hContext, DONT_PANIC);
301 printf(
"Testing SCardGetAttrib\t\t: ");
302#ifdef USE_AUTOALLOCATE
308 test_rv(rv, hContext, DONT_PANIC);
311 printf(
"SCARD_ATTR_DEVICE_FRIENDLY_NAME length: " GREEN
"%ld\n" NORMAL, pcbAttrLen);
312 pbAttr = malloc(pcbAttrLen);
315 printf(
"Testing SCardGetAttrib\t\t: ");
318 test_rv(rv, hContext, DONT_PANIC);
320 printf(
"SCARD_ATTR_DEVICE_FRIENDLY_NAME: " GREEN
"%s\n" NORMAL, pbAttr);
322#ifdef USE_AUTOALLOCATE
323 printf(
"Testing SCardFreeMemory\t\t: ");
324 rv = SCardFreeMemory(hContext, pbAttr);
325 test_rv(rv, hContext, PANIC);
331 printf(
"Testing SCardGetAttrib\t\t: ");
332#ifdef USE_AUTOALLOCATE
338 test_rv(rv, hContext, DONT_PANIC);
341 printf(
"SCARD_ATTR_ATR_STRING length: " GREEN
"%ld\n" NORMAL, pcbAttrLen);
342 pbAttr = malloc(pcbAttrLen);
345 printf(
"Testing SCardGetAttrib\t\t: ");
348 test_rv(rv, hContext, DONT_PANIC);
351 printf(
"SCARD_ATTR_ATR_STRING length: " GREEN
"%ld\n" NORMAL, pcbAttrLen);
352 printf(
"SCARD_ATTR_ATR_STRING: " GREEN);
353 for (i = 0; i < pcbAttrLen; i++)
354 printf(
"%02X ", pbAttr[i]);
358#ifdef USE_AUTOALLOCATE
359 printf(
"Testing SCardFreeMemory\t\t: ");
360 rv = SCardFreeMemory(hContext, pbAttr);
361 test_rv(rv, hContext, PANIC);
367 printf(
"Testing SCardGetAttrib\t\t: ");
368 dwBufLen =
sizeof(buf);
370 test_rv(rv, hContext, DONT_PANIC);
375 printf(
"Vendor IFD version\t\t: ");
376 if (dwBufLen ==
sizeof(DWORD))
377 value = buf.as_DWORD;
380 if (dwBufLen ==
sizeof(uint32_t))
381 value = buf.as_uint32_t;
384 printf(RED
"Unsupported size\n" NORMAL);
391 int M = (value & 0xFF000000) >> 24;
392 int m = (value & 0x00FF0000) >> 16;
393 int b = (value & 0x0000FFFF);
394 printf(GREEN
"%d.%d.%d\n" NORMAL, M, m, b);
398 printf(
"Testing SCardGetAttrib\t\t: ");
399 dwBufLen =
sizeof(buf);
401 test_rv(rv, hContext, DONT_PANIC);
404 if (dwBufLen ==
sizeof(uint32_t))
405 printf(
"Max message length\t\t: " GREEN
"%d\n" NORMAL,
408 printf(RED
"Wrong size" NORMAL);
411 printf(
"Testing SCardGetAttrib\t\t: ");
412 dwBufLen =
sizeof(buf);
414 test_rv(rv, hContext, DONT_PANIC);
416 printf(
"Vendor name\t\t\t: " GREEN
"%s\n" NORMAL, buf.as_char);
418 printf(
"Testing SCardSetAttrib\t\t: ");
420 test_rv(rv, hContext, DONT_PANIC);
422 printf(
"Testing SCardStatus\t\t: ");
424#ifdef USE_AUTOALLOCATE
427 rv = SCardStatus(hCard, (LPSTR)&pcReader, &dwReaderLen, &dwState, &dwProt,
428 (LPBYTE)&pbAtr, &dwAtrLen);
431 pcReader = malloc(
sizeof(
char) * 100);
434 rv = SCardStatus(hCard, pcReader, &dwReaderLen, &dwState, &dwProt,
437 test_rv(rv, hContext, PANIC);
439 printf(
"Current Reader Name\t\t: " GREEN
"%s\n" NORMAL, pcReader);
440 printf(
"Current Reader State\t\t: " GREEN
"0x%.4lx\n" NORMAL, dwState);
441 printf(
"Current Reader Protocol\t\t: T=" GREEN
"%ld\n" NORMAL, dwProt - 1);
442 printf(
"Current Reader ATR Size\t\t: " GREEN
"%ld" NORMAL
" bytes\n",
444 printf(
"Current Reader ATR Value\t: " GREEN);
446 for (i = 0; i < dwAtrLen; i++)
448 printf(
"%02X ", pbAtr[i]);
452#ifdef USE_AUTOALLOCATE
453 printf(
"Testing SCardFreeMemory\t\t: ");
454 rv = SCardFreeMemory(hContext, pcReader);
455 test_rv(rv, hContext, PANIC);
456 printf(
"Testing SCardFreeMemory\t\t: ");
457 rv = SCardFreeMemory(hContext, pbAtr);
458 test_rv(rv, hContext, PANIC);
464 printf(
"Press enter: ");
466 printf(
"Testing SCardReconnect\t\t: ");
469 test_rv(rv, hContext, PANIC);
471 printf(
"Testing SCardDisconnect\t\t: ");
473 test_rv(rv, hContext, PANIC);
475#ifdef USE_AUTOALLOCATE
476 printf(
"Testing SCardFreeMemory\t\t: ");
477 rv = SCardFreeMemory(hContext, mszReaders);
478 test_rv(rv, hContext, PANIC);
483 printf(
"Testing SCardReleaseContext\t: ");
484 rv = SCardReleaseContext(hContext);
485 test_rv(rv, hContext, PANIC);
488 printf(
"PC/SC Test Completed Successfully !\n");
PCSC_API const char * pcsc_stringify_error(const LONG pcscError)
Returns a human readable text for the given PC/SC error code.
#define SCARD_S_SUCCESS
No error was encountered.
#define SCARD_E_NO_READERS_AVAILABLE
Cannot find a smart card reader.
This keeps a list of defines for pcsc-lite.
#define SCARD_SCOPE_SYSTEM
Scope in system.
LONG SCARDCONTEXT
hContext returned by SCardEstablishContext()
#define SCARD_PROTOCOL_T1
T=1 active protocol.
#define SCARD_PROTOCOL_T0
T=0 active protocol.
#define SCARD_AUTOALLOCATE
see SCardFreeMemory()
#define SCARD_UNPOWER_CARD
Power down on close.
#define SCARD_SHARE_SHARED
Shared mode only.
#define MAX_ATR_SIZE
Maximum ATR size.
#define SCARD_PCI_RAW
protocol control information (PCI) for RAW protocol
#define SCARD_STATE_UNKNOWN
Reader unknown.
#define INFINITE
Infinite timeout.
#define SCARD_STATE_EMPTY
Card removed.
#define SCARD_PROTOCOL_RAW
Raw active protocol.
#define MAX_BUFFER_SIZE
Maximum Tx/Rx Buffer for short APDU.
#define SCARD_PCI_T1
protocol control information (PCI) for T=1
LONG SCARDHANDLE
hCard returned by SCardConnect()
#define SCARD_PCI_T0
protocol control information (PCI) for T=0
This keeps a list of defines shared between the driver and the application.
#define SCARD_ATTR_VENDOR_IFD_VERSION
Vendor-supplied interface device version (DWORD in the form 0xMMmmbbbb where MM = major version,...
#define SCARD_ATTR_DEVICE_FRIENDLY_NAME
Reader's display name.
#define SCARD_ATTR_MAXINPUT
FIXME.
#define SCARD_CTL_CODE(code)
Provide source compatibility on different platforms.
#define SCARD_ATTR_VENDOR_NAME
Vendor name.
#define SCARD_ATTR_ATR_STRING
Answer to reset (ATR) string.
Protocol Control Information (PCI)
This handles smart card reader communications.