Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
5.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: ifdmsg.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. #include <stdarg.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <conio.h>
  14. #include <afx.h>
  15. #include <afxtempl.h>
  16. #include <winioctl.h>
  17. #include <winsmcrd.h>
  18. #include "ifdtest.h"
  19. // Our log file
  20. static FILE *g_pLogFile;
  21. // These are used by the Testxxx functions
  22. static BOOL g_bTestFailed;
  23. static BOOL g_bReaderFailed;
  24. static BOOL g_bTestStart;
  25. void
  26. LogOpen(
  27. PCHAR in_pchFileName
  28. )
  29. {
  30. CHAR l_rgchFileName[128];
  31. sprintf(l_rgchFileName, "%s.log", in_pchFileName);
  32. g_pLogFile = fopen(l_rgchFileName, "w");
  33. }
  34. void
  35. LogMessage(
  36. PCHAR in_pchFormat,
  37. ...
  38. )
  39. {
  40. CHAR l_rgchBuffer[128];
  41. va_list l_pArg;
  42. va_start(
  43. l_pArg,
  44. in_pchFormat
  45. );
  46. vsprintf(
  47. l_rgchBuffer,
  48. in_pchFormat,
  49. l_pArg
  50. );
  51. printf("%s\n", l_rgchBuffer);
  52. if (g_pLogFile) {
  53. fprintf(g_pLogFile, "%s\n", l_rgchBuffer);
  54. }
  55. }
  56. void
  57. TestStart(
  58. PCHAR in_pchFormat,
  59. ...
  60. )
  61. {
  62. CHAR l_rgchBuffer[128];
  63. va_list l_pArg;
  64. if (g_bTestStart) {
  65. printf("\n*** WARNING: Missing TestResult() call\n");
  66. }
  67. g_bTestStart = TRUE;
  68. g_bTestFailed = FALSE;
  69. va_start(l_pArg, in_pchFormat);
  70. vsprintf(l_rgchBuffer, in_pchFormat, l_pArg);
  71. printf(" %-50s", l_rgchBuffer);
  72. if (g_pLogFile) {
  73. fprintf(g_pLogFile, " %-50s", l_rgchBuffer);
  74. }
  75. }
  76. static
  77. void
  78. TestMsg(
  79. BOOL in_bTestEnd,
  80. BOOL in_bTestResult,
  81. PCHAR in_pchFormat,
  82. va_list in_pArg
  83. )
  84. {
  85. CHAR l_rgchBuffer[2048];
  86. if (in_bTestEnd && g_bTestStart == FALSE) {
  87. printf("\n*** WARNING: Missing TestStart() call\n");
  88. }
  89. vsprintf(l_rgchBuffer, in_pchFormat, in_pArg);
  90. if (in_bTestResult == FALSE && g_bTestFailed == FALSE) {
  91. g_bTestFailed = TRUE;
  92. g_bReaderFailed = TRUE;
  93. printf("* FAILED\n- %s\n", l_rgchBuffer);
  94. if (g_pLogFile) {
  95. fprintf(g_pLogFile, "* FAILED\n- %s\n", l_rgchBuffer);
  96. }
  97. }
  98. if (in_bTestEnd) {
  99. if (g_bTestFailed != TRUE) {
  100. printf(" Passed\n");
  101. if (g_pLogFile) {
  102. fprintf(g_pLogFile, " Passed\n");
  103. }
  104. }
  105. }
  106. }
  107. void
  108. TestCheck(
  109. BOOL in_bResult,
  110. PCHAR in_pchFormat,
  111. ...
  112. )
  113. {
  114. va_list l_pArg;
  115. va_start(l_pArg, in_pchFormat);
  116. TestMsg(FALSE, in_bResult, in_pchFormat, l_pArg);
  117. }
  118. void
  119. TestCheck(
  120. ULONG in_lResult,
  121. const PCHAR in_pchOperator,
  122. const ULONG in_uExpectedResult,
  123. ULONG in_uResultLength,
  124. ULONG in_uExpectedLength,
  125. UCHAR in_chSw1,
  126. UCHAR in_chSw2,
  127. UCHAR in_chExpectedSw1,
  128. UCHAR in_chExpectedSw2,
  129. PUCHAR in_pchData,
  130. PUCHAR in_pchExpectedData,
  131. ULONG in_uDataLength
  132. )
  133. /*++
  134. Routine Description:
  135. This function checks the return code, the number of bytes
  136. returned, the card status bytes and the data returned by
  137. a call to CReader::Transmit.
  138. --*/
  139. {
  140. if (strcmp(in_pchOperator, "==") == 0 &&
  141. in_lResult != in_uExpectedResult ||
  142. strcmp(in_pchOperator, "!=") == 0 &&
  143. in_lResult == in_uExpectedResult) {
  144. TestCheck(
  145. FALSE,
  146. "IOCTL call failed:\nReturned %8lxH (NTSTATUS %8lxH)\nExpected %s%8lxH (NTSTATUS %8lxH)",
  147. in_lResult,
  148. MapWinErrorToNtStatus(in_lResult),
  149. (strcmp(in_pchOperator, "!=") == 0 ? "NOT " : ""),
  150. in_uExpectedResult,
  151. MapWinErrorToNtStatus(in_uExpectedResult)
  152. );
  153. } else if (in_uResultLength != in_uExpectedLength) {
  154. TestCheck(
  155. FALSE,
  156. "IOCTL returned wrong number of bytes:\nReturned %3ld bytes\nExpected %3ld bytes",
  157. in_uResultLength,
  158. in_uExpectedLength
  159. );
  160. } else if (in_chSw1 != in_chExpectedSw1 ||
  161. in_chSw2 != in_chExpectedSw2){
  162. TestCheck(
  163. FALSE,
  164. "Card returned wrong status:\nStatus SW1 = %02x SW2 = %02x\nExpected SW1 = %02x SW2 = %02x",
  165. in_chSw1,
  166. in_chSw2,
  167. in_chExpectedSw1,
  168. in_chExpectedSw2
  169. );
  170. } else if (memcmp(in_pchData, in_pchExpectedData, in_uDataLength)) {
  171. CHAR l_rgchData[2048], l_rgchExpectedData[2048];
  172. for (ULONG i = 0; i < in_uDataLength; i++) {
  173. sprintf(l_rgchData + i * 3, "%02X ", in_pchData[i]);
  174. sprintf(l_rgchExpectedData + i * 3, "%02X ", in_pchExpectedData[i]);
  175. if ((i + 1) % 24 == 0) {
  176. l_rgchData[i * 3 + 2] = '\n';
  177. l_rgchExpectedData[i * 3 + 2] = '\n';
  178. }
  179. }
  180. TestCheck(
  181. FALSE,
  182. "IOCTL returned incorrect data:\nData returned:\n%s\nExpected data:\n%s",
  183. l_rgchData,
  184. l_rgchExpectedData
  185. );
  186. }
  187. }
  188. void
  189. TestEnd(
  190. void
  191. )
  192. /*++
  193. Routine Description:
  194. A call to this function marks the end of a test sequence.
  195. A sequence is usually look like:
  196. TestStart(Message)
  197. CReaderTransmit or DeviceIoControl
  198. TestCheck(...)
  199. TestEnd()
  200. --*/
  201. {
  202. #ifdef _M_ALPHA
  203. va_list l_pArg;
  204. #else
  205. va_list l_pArg = NULL;
  206. #endif
  207. TestMsg(TRUE, TRUE, "", l_pArg);
  208. g_bTestStart = FALSE;
  209. }
  210. BOOL
  211. TestFailed(
  212. void
  213. )
  214. {
  215. return g_bTestFailed;
  216. }
  217. BOOL
  218. ReaderFailed(
  219. void
  220. )
  221. {
  222. return g_bReaderFailed;
  223. }