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.

216 lines
6.4 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 BOOL value for enabling checkpoint hit messages
  43. // This way, the initial value is available to manipulate with the debugger
  44. #if TESTAUDITHITS
  45. BOOL fShowCheckpointHits = TRUE;
  46. #else
  47. BOOL fShowCheckpointHits = FALSE;
  48. #endif
  49. /*************************************************************************
  50. BranchInit
  51. Creates a memory allocation to hold data regarding whether particular
  52. checkpoints have been visited. Reads reference numbers from the
  53. AuditData structure, and creates pairs of the reference number and
  54. a boolean to be set true when the point is hit.
  55. arguments: none
  56. returns: none
  57. errors: May fail due to out of memory. On this case, returns with
  58. the pointer NULL. Further calls into these functions with
  59. a NULL ptr will do nothing at all.
  60. *************************************************************************/
  61. void BranchInit(void)
  62. {
  63. WCHAR sz[100];
  64. swprintf(sz,L"TEST: %d checkpoints defined\n",CHECKPOINTCOUNT);
  65. OutputDebugString(sz);
  66. pTouch = (TOUCHSTRUCT *) malloc(CHECKPOINTCOUNT * sizeof(TOUCHSTRUCT));
  67. if (NULL == pTouch) return;
  68. // table allocation successful. Initialize using point #s
  69. memset(pTouch,0,(CHECKPOINTCOUNT * sizeof(TOUCHSTRUCT)));
  70. for (INT i=0;i<CHECKPOINTCOUNT;i++)
  71. {
  72. // go through the AuditData[] struct and fetch the point numbers
  73. pTouch[i].iPoint = AuditData[i].iPoint;
  74. }
  75. }
  76. /*************************************************************************
  77. BranchTouch
  78. Looks through the memory table created by BranchInit for a match with
  79. the passed point number. Sets the matching table entry to show that
  80. the point was visited. The string is not passed to this routine in
  81. order to minimize the amount of memory allocation, processing, and
  82. debug output.
  83. arguments: INT point number
  84. returns: none
  85. errors: May fail to find the point number. If so, does nothing.
  86. *************************************************************************/
  87. void BranchTouch(INT iBranch)
  88. {
  89. WCHAR sz[100];
  90. INT j;
  91. if (NULL == pTouch) return;
  92. // warn user about obviously bad checkpoint statements
  93. if (0 == iBranch) ASSERT(0);
  94. if (iBranch > NUMBERLIMIT) ASSERT(0);
  95. if (fShowCheckpointHits)
  96. {
  97. swprintf(sz,L"TEST: Checkpoint %d touched. \n",iBranch);
  98. OutputDebugString(sz);
  99. }
  100. // look for this point number and set its touched flag
  101. for (j=0;j<CHECKPOINTCOUNT;j++)
  102. {
  103. if (pTouch[j].iPoint == iBranch)
  104. {
  105. pTouch[j].fTouched = TRUE;
  106. break;
  107. }
  108. }
  109. // detect checkpoint for which no table entry exists
  110. if (j == CHECKPOINTCOUNT) ASSERT(0);
  111. }
  112. /*************************************************************************
  113. BranchSummary
  114. Looks through the memory table created by BranchInit for entries
  115. which show that they were not reached. For these, scans the
  116. AuditData table looking for a match, and prints the entry number
  117. together with the associated string as part of a table shown to
  118. the operator in the debug output.
  119. arguments: none
  120. returns: none
  121. errors: none expected unless the table is corrupted
  122. *************************************************************************/
  123. void BranchSummary(void)
  124. {
  125. WCHAR sz[100];
  126. INT i;
  127. BOOL fUntouched = FALSE; // set true if any untouched found
  128. if (NULL == pTouch) return;
  129. // if TESTAUDITSUMMARY is false, this routine will do nothing but
  130. // free the touch array
  131. #if TESTAUDITSUMMARY
  132. swprintf(sz,L"TEST: Total of %d checkpoints.\n",CHECKPOINTCOUNT);
  133. OutputDebugString(sz);
  134. OutputDebugString(L"TEST: Untouched checkpoints:\n");
  135. // Scan every entry in the checkpoint table
  136. for (i=0;i<CHECKPOINTCOUNT;i++)
  137. {
  138. // find all untouched
  139. if (pTouch[i].fTouched == FALSE)
  140. {
  141. // find matching entry in data table
  142. // there should be no match failures unless the table becomes
  143. // corrupted
  144. INT j;
  145. for (j=0;j<CHECKPOINTCOUNT;j++)
  146. {
  147. // on match, print number and string
  148. if (pTouch[i].iPoint == AuditData[j].iPoint)
  149. {
  150. swprintf(sz,L" %d ",pTouch[i].iPoint);
  151. OutputDebugString(sz);
  152. if (AuditData[j].pszDescription != NULL)
  153. {
  154. OutputDebugString(AuditData[j].pszDescription);
  155. }
  156. OutputDebugString(L"\n");
  157. break;
  158. }
  159. }
  160. if (j == CHECKPOINTCOUNT) ASSERT(0);
  161. fUntouched = TRUE;
  162. }
  163. }
  164. if (!fUntouched)
  165. {
  166. OutputDebugString(L" ***NONE***\n");
  167. }
  168. #endif
  169. free(pTouch);
  170. pTouch = NULL;
  171. }
  172. #endif