Leaked source code of windows server 2003
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.

272 lines
7.9 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. testaudit.cpp
  5. Abstract:
  6. Test Auditing routines. Used by development to document the locations
  7. and reach conditions for code checkpoints that test should be sure to cover.
  8. Used by test to ensure that the test meets the expectations of the developer
  9. and to locate points of interest in the source files.
  10. This file will compile to nothing if TESTAUDIT is not a defined symbol in
  11. the build environment. For this purpose, buildx.cmd has been modified to
  12. provide for setting this symbol.
  13. Author:
  14. georgema Nov., 2001 created
  15. Environment:
  16. Revision History:
  17. --*/
  18. #if TESTAUDIT
  19. #include <nt.h>
  20. #include <ntrtl.h>
  21. #include <nturtl.h>
  22. #include <windef.h>
  23. #include <windows.h>
  24. #include <tchar.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <testaudit.h>
  28. #include "audit.h"
  29. // Data structure element for the auditing data
  30. typedef struct _touchstruct
  31. {
  32. INT iPoint;
  33. BOOL fTouched;
  34. }TOUCHSTRUCT, *PTOUCHSTRUCT;
  35. #define TOUCHSIZE sizeof(TOUCHSTRUCT)
  36. // Arbitrary limit on point number range, in order to be able to constrain the
  37. // size of the runtime point-hit array to manageable size without having to
  38. // do anything cute.
  39. #define NUMBERLIMIT 500
  40. // Global variable to hold mem allocation for auditing data
  41. TOUCHSTRUCT *pTouch = NULL;
  42. // Global variable to hold last touched point
  43. INT iLastPoint = 0;
  44. // Global variable to hold BOOL value for enabling checkpoint hit messages
  45. // This way, the initial value is available to manipulate with the debugger
  46. #if TESTAUDITHITS
  47. BOOL fShowCheckpointHits = TRUE;
  48. #else
  49. BOOL fShowCheckpointHits = FALSE;
  50. #endif
  51. /*************************************************************************
  52. BranchInit
  53. Creates a memory allocation to hold data regarding whether particular
  54. checkpoints have been visited. Reads reference numbers from the
  55. AuditData structure, and creates pairs of the reference number and
  56. a boolean to be set true when the point is hit.
  57. arguments: none
  58. returns: none
  59. errors: May fail due to out of memory. On this case, returns with
  60. the pointer NULL. Further calls into these functions with
  61. a NULL ptr will do nothing at all.
  62. *************************************************************************/
  63. void BranchInit(void)
  64. {
  65. WCHAR sz[100];
  66. swprintf(sz,L"TEST: %d checkpoints defined\n",CHECKPOINTCOUNT);
  67. OutputDebugString(sz);
  68. pTouch = (TOUCHSTRUCT *) LocalAlloc(LMEM_FIXED,CHECKPOINTCOUNT * sizeof(TOUCHSTRUCT));
  69. if (NULL == pTouch) return;
  70. // table allocation successful. Initialize using point #s
  71. memset(pTouch,0,(CHECKPOINTCOUNT * sizeof(TOUCHSTRUCT)));
  72. for (INT i=0;i<CHECKPOINTCOUNT;i++)
  73. {
  74. // go through the AuditData[] struct and fetch the point numbers
  75. pTouch[i].iPoint = AuditData[i].iPoint;
  76. }
  77. // Init the last point variable, in case this is a re-init
  78. iLastPoint = 0;
  79. }
  80. /*************************************************************************
  81. BranchTouch
  82. Looks through the memory table created by BranchInit for a match with
  83. the passed point number. Sets the matching table entry to show that
  84. the point was visited. The string is not passed to this routine in
  85. order to minimize the amount of memory allocation, processing, and
  86. debug output.
  87. arguments: INT point number
  88. returns: none
  89. errors: May fail to find the point number. If so, does nothing.
  90. *************************************************************************/
  91. void BranchTouch(INT iBranch)
  92. {
  93. WCHAR sz[100];
  94. INT j;
  95. if (NULL == pTouch) return;
  96. // warn user about obviously bad checkpoint statements
  97. if (0 == iBranch) ASSERT(0);
  98. if (iBranch > NUMBERLIMIT) ASSERT(0);
  99. if (fShowCheckpointHits)
  100. {
  101. swprintf(sz,L"TEST: Checkpoint %d touched. \n",iBranch);
  102. OutputDebugString(sz);
  103. }
  104. // look for this point number and set its touched flag
  105. for (j=0;j<CHECKPOINTCOUNT;j++)
  106. {
  107. if (pTouch[j].iPoint == iBranch)
  108. {
  109. pTouch[j].fTouched = TRUE;
  110. break;
  111. }
  112. }
  113. // detect checkpoint for which no table entry exists
  114. if (j == CHECKPOINTCOUNT) ASSERT(0);
  115. }
  116. /*************************************************************************
  117. BranchSequence
  118. Modification of BranchTouch() that will mark a checkpoint as visited only if the checkpoint
  119. named as the second argument had been touched last. Uses the iLastPoint state variable
  120. to keep track of the last sequential checkpoint to be touched. Only the sequential macros
  121. will affect this value, so intervening CHECKPOINT macros will have no effect.
  122. arguments: INT point number, INT point number
  123. returns: none
  124. errors: May fail to find the point number. If so, does nothing.
  125. *************************************************************************/
  126. void BranchSequence(INT iThis,INT iPrevious)
  127. {
  128. WCHAR sz[100];
  129. INT j;
  130. if (NULL == pTouch) return;
  131. // warn user about obviously bad checkpoint statements
  132. if (iPrevious > NUMBERLIMIT) ASSERT(0);
  133. // Process force/reset of the sequence
  134. if (iPrevious == iThis)
  135. {
  136. iLastPoint = iThis;
  137. return;
  138. }
  139. // Reject out of sequence unless previous was 0
  140. if ((iPrevious != 0) &&
  141. (iPrevious != iLastPoint))
  142. {
  143. // If detect out of sequence, reset the sequence
  144. iLastPoint = 0;
  145. return;
  146. }
  147. // Process a touch
  148. iLastPoint = iThis;
  149. if (iThis != 0)
  150. {
  151. BranchTouch(iThis);
  152. }
  153. }
  154. /*************************************************************************
  155. BranchSummary
  156. Looks through the memory table created by BranchInit for entries
  157. which show that they were not reached. For these, scans the
  158. AuditData table looking for a match, and prints the entry number
  159. together with the associated string as part of a table shown to
  160. the operator in the debug output.
  161. arguments: none
  162. returns: none
  163. errors: none expected unless the table is corrupted
  164. *************************************************************************/
  165. void BranchSummary(void)
  166. {
  167. WCHAR sz[100];
  168. INT i;
  169. BOOL fUntouched = FALSE; // set true if any untouched found
  170. if (NULL == pTouch) return;
  171. // if TESTAUDITSUMMARY is false, this routine will do nothing but
  172. // free the touch array
  173. #if TESTAUDITSUMMARY
  174. swprintf(sz,L"TEST: Total of %d checkpoints.\n",CHECKPOINTCOUNT);
  175. OutputDebugString(sz);
  176. OutputDebugString(L"TEST: Untouched checkpoints:\n");
  177. // Scan every entry in the checkpoint table
  178. for (i=0;i<CHECKPOINTCOUNT;i++)
  179. {
  180. // find all untouched
  181. if (pTouch[i].fTouched == FALSE)
  182. {
  183. // find matching entry in data table
  184. // there should be no match failures unless the table becomes
  185. // corrupted
  186. INT j;
  187. for (j=0;j<CHECKPOINTCOUNT;j++)
  188. {
  189. // on match, print number and string
  190. if (pTouch[i].iPoint == AuditData[j].iPoint)
  191. {
  192. swprintf(sz,L" %d ",pTouch[i].iPoint);
  193. OutputDebugString(sz);
  194. if (AuditData[j].pszDescription != NULL)
  195. {
  196. OutputDebugString(AuditData[j].pszDescription);
  197. }
  198. OutputDebugString(L"\n");
  199. break;
  200. }
  201. }
  202. if (j == CHECKPOINTCOUNT) ASSERT(0);
  203. fUntouched = TRUE;
  204. }
  205. }
  206. if (!fUntouched)
  207. {
  208. OutputDebugString(L" ***NONE***\n");
  209. }
  210. #endif
  211. LocalFree(pTouch);
  212. pTouch = NULL;
  213. }
  214. #endif