50 #define USE_AUTOALLOCATE
52 #define BLUE "\33[34m"
54 #define BRIGHT_RED "\33[01;31m"
55 #define GREEN "\33[32m"
56 #define NORMAL "\33[0m"
57 #define MAGENTA "\33[35m"
59 static void test_rv(LONG rv,
SCARDCONTEXT hContext,
int dont_panic)
76 int 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: ");
123 test_rv(rv, hContext, PANIC);
125 printf(
"Testing SCardIsValidContext\t: ");
127 test_rv(rv, hContext, DONT_PANIC);
129 printf(
"Testing SCardListReaderGroups\t: ");
130 #ifdef USE_AUTOALLOCATE
135 test_rv(rv, hContext, PANIC);
137 printf(
"Testing SCardListReaderGroups\t: ");
138 mszGroups = calloc(dwGroups,
sizeof(
char));
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: ");
157 test_rv(rv, hContext, PANIC);
164 printf(
"Testing SCardListReaders\t: ");
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";
176 test_rv(rv, hContext, PANIC);
179 printf(
"Testing SCardListReaders\t: ");
180 #ifdef USE_AUTOALLOCATE
185 test_rv(rv, hContext, PANIC);
187 printf(
"Testing SCardListReaders\t: ");
188 mszReaders = calloc(dwReaders,
sizeof(
char));
191 test_rv(rv, hContext, DONT_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) ;
211 printf(
"Enter the reader number\t\t: ");
212 r = fgets(input,
sizeof(input), stdin);
216 iReader = atoi(input);
218 if (iReader > p || iReader <= 0)
219 printf(
"Invalid Value - try again\n");
221 while (iReader > p || iReader <= 0);
225 rgReaderStates[0].szReader = &mszReaders[iList[iReader]];
228 printf(
"Waiting for card insertion\t: ");
229 (void)fflush(stdout);
231 test_rv(rv, hContext, PANIC);
234 printf(
"\nA reader has been connected/disconnected\n");
235 goto wait_for_card_again;
238 printf(
"Testing SCardConnect\t\t: ");
239 rv =
SCardConnect(hContext, &mszReaders[iList[iReader]],
242 test_rv(rv, hContext, PANIC);
256 printf(
"Unknown protocol\n");
261 printf(
"Select file:");
263 memcpy(bSendBuffer,
"\x00\xA4\x00\x00\x02\x3F\x00", send_length);
264 for (i=0; i<send_length; i++)
265 printf(
" %02X", bSendBuffer[i]);
267 length =
sizeof(bRecvBuffer);
269 printf(
"Testing SCardTransmit\t\t: ");
270 rv =
SCardTransmit(hCard, pioSendPci, bSendBuffer, send_length,
271 &ioRecvPci, bRecvBuffer, &length);
272 test_rv(rv, hContext, PANIC);
273 printf(
" card response:" GREEN);
274 for (i=0; i<length; i++)
275 printf(
" %02X", bRecvBuffer[i]);
278 printf(
"Testing SCardControl\t\t: ");
281 char buffer[1024] =
"Foobar";
282 DWORD cbRecvLength =
sizeof(buffer);
284 rv =
SCardControl(hCard, buffer, 7, buffer, &cbRecvLength);
288 char buffer[1024] = { 0x02 };
289 DWORD cbRecvLength =
sizeof(buffer);
292 sizeof(buffer), &cbRecvLength);
295 for (i=0; i<cbRecvLength; i++)
296 printf(
"%c", buffer[i]);
301 test_rv(rv, hContext, DONT_PANIC);
303 printf(
"Testing SCardGetAttrib\t\t: ");
304 #ifdef USE_AUTOALLOCATE
310 test_rv(rv, hContext, DONT_PANIC);
313 printf(
"SCARD_ATTR_DEVICE_FRIENDLY_NAME length: " GREEN
"%ld\n" NORMAL, pcbAttrLen);
314 pbAttr = malloc(pcbAttrLen);
317 printf(
"Testing SCardGetAttrib\t\t: ");
320 test_rv(rv, hContext, DONT_PANIC);
322 printf(
"SCARD_ATTR_DEVICE_FRIENDLY_NAME: " GREEN
"%s\n" NORMAL, pbAttr);
324 #ifdef USE_AUTOALLOCATE
325 printf(
"Testing SCardFreeMemory\t\t: ");
327 test_rv(rv, hContext, PANIC);
333 printf(
"Testing SCardGetAttrib\t\t: ");
334 #ifdef USE_AUTOALLOCATE
340 test_rv(rv, hContext, DONT_PANIC);
343 printf(
"SCARD_ATTR_ATR_STRING length: " GREEN
"%ld\n" NORMAL, pcbAttrLen);
344 pbAttr = malloc(pcbAttrLen);
347 printf(
"Testing SCardGetAttrib\t\t: ");
350 test_rv(rv, hContext, DONT_PANIC);
353 printf(
"SCARD_ATTR_ATR_STRING length: " GREEN
"%ld\n" NORMAL, pcbAttrLen);
354 printf(
"SCARD_ATTR_ATR_STRING: " GREEN);
355 for (i = 0; i < pcbAttrLen; i++)
356 printf(
"%02X ", pbAttr[i]);
360 #ifdef USE_AUTOALLOCATE
361 printf(
"Testing SCardFreeMemory\t\t: ");
363 test_rv(rv, hContext, PANIC);
369 printf(
"Testing SCardGetAttrib\t\t: ");
370 dwBufLen =
sizeof(buf);
372 test_rv(rv, hContext, DONT_PANIC);
377 printf(
"Vendor IFD version\t\t: ");
378 if (dwBufLen ==
sizeof(DWORD))
379 value = buf.as_DWORD;
382 if (dwBufLen ==
sizeof(uint32_t))
383 value = buf.as_uint32_t;
386 printf(RED
"Unsupported size\n" NORMAL);
393 int M = (value & 0xFF000000) >> 24;
394 int m = (value & 0x00FF0000) >> 16;
395 int b = (value & 0x0000FFFF);
396 printf(GREEN
"%d.%d.%d\n" NORMAL, M, m, b);
400 printf(
"Testing SCardGetAttrib\t\t: ");
401 dwBufLen =
sizeof(buf);
403 test_rv(rv, hContext, DONT_PANIC);
406 if (dwBufLen ==
sizeof(uint32_t))
407 printf(
"Max message length\t\t: " GREEN
"%d\n" NORMAL,
410 printf(RED
"Wrong size" NORMAL);
413 printf(
"Testing SCardGetAttrib\t\t: ");
414 dwBufLen =
sizeof(buf);
416 test_rv(rv, hContext, DONT_PANIC);
418 printf(
"Vendor name\t\t\t: " GREEN
"%s\n" NORMAL, buf.as_char);
420 printf(
"Testing SCardSetAttrib\t\t: ");
422 test_rv(rv, hContext, DONT_PANIC);
424 printf(
"Testing SCardStatus\t\t: ");
426 #ifdef USE_AUTOALLOCATE
429 rv =
SCardStatus(hCard, (LPSTR)&pcReader, &dwReaderLen, &dwState, &dwProt,
430 (LPBYTE)&pbAtr, &dwAtrLen);
433 pcReader = malloc(
sizeof(
char) * 100);
436 rv =
SCardStatus(hCard, pcReader, &dwReaderLen, &dwState, &dwProt,
439 test_rv(rv, hContext, PANIC);
441 printf(
"Current Reader Name\t\t: " GREEN
"%s\n" NORMAL, pcReader);
442 printf(
"Current Reader State\t\t: " GREEN
"0x%.4lx\n" NORMAL, dwState);
443 printf(
"Current Reader Protocol\t\t: T=" GREEN
"%ld\n" NORMAL, dwProt - 1);
444 printf(
"Current Reader ATR Size\t\t: " GREEN
"%ld" NORMAL
" bytes\n",
446 printf(
"Current Reader ATR Value\t: " GREEN);
448 for (i = 0; i < dwAtrLen; i++)
450 printf(
"%02X ", pbAtr[i]);
454 #ifdef USE_AUTOALLOCATE
455 printf(
"Testing SCardFreeMemory\t\t: ");
457 test_rv(rv, hContext, PANIC);
458 printf(
"Testing SCardFreeMemory\t\t: ");
460 test_rv(rv, hContext, PANIC);
466 printf(
"Press enter: ");
468 printf(
"Testing SCardReconnect\t\t: ");
471 test_rv(rv, hContext, PANIC);
473 printf(
"Testing SCardDisconnect\t\t: ");
475 test_rv(rv, hContext, PANIC);
477 #ifdef USE_AUTOALLOCATE
478 printf(
"Testing SCardFreeMemory\t\t: ");
480 test_rv(rv, hContext, PANIC);
485 printf(
"Testing SCardReleaseContext\t: ");
487 test_rv(rv, hContext, PANIC);
490 printf(
"PC/SC Test Completed Successfully !\n");