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.

196 lines
4.6 KiB

  1. #include <windows.h>
  2. #include <unk.h>
  3. #include <arrtempl.h>
  4. #include <FlexArry.h>
  5. #include "pagemgr.h"
  6. #include "VarObjHeap.h"
  7. #include <wbemutil.h>
  8. #include <stdio.h>
  9. #include <tchar.h>
  10. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. // Assertion code
  12. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. DWORD _RetAssert(TCHAR *msg, DWORD dwRes, const char *filename, int line)
  14. {
  15. if (dwRes == ERROR_SUCCESS)
  16. return dwRes;
  17. TCHAR *buf = new TCHAR[512];
  18. if (buf == NULL)
  19. {
  20. return dwRes;
  21. }
  22. wsprintf(buf, __TEXT("%s\ndwRes = 0x%X\nFile: %S, Line: %lu\n\nPress Cancel to stop in debugger, OK to continue"), msg, dwRes, filename, line);
  23. _tprintf(buf);
  24. int mbRet = MessageBox(0, buf, __TEXT("WMI Assert"), MB_OKCANCEL | MB_ICONSTOP | MB_SYSTEMMODAL | MB_SERVICE_NOTIFICATION);
  25. delete [] buf;
  26. if (mbRet == IDCANCEL)
  27. {
  28. DebugBreak();
  29. }
  30. return dwRes;
  31. }
  32. #define TEST_ASSERT(msg, dwRes) _RetAssert(msg, dwRes, __FILE__, __LINE__)
  33. CVarObjHeap *pHeap = NULL;
  34. CPageSource *pTranMan = NULL;
  35. #define TEST_OBJECT_SIZE 30000
  36. #define TEST_OBJECT_ITER 25000
  37. DWORD TestMem(BYTE *buff, DWORD dwLen, BYTE dwSig)
  38. {
  39. for (DWORD dwIndex = 0; dwIndex != dwLen; dwIndex++)
  40. {
  41. if (buff[dwIndex] != dwSig)
  42. return ERROR_INVALID_DATA;
  43. }
  44. return ERROR_SUCCESS;
  45. }
  46. DWORD Init()
  47. {
  48. DWORD dwRes = ERROR_SUCCESS;
  49. pHeap = new CVarObjHeap;
  50. pTranMan = new CPageSource;
  51. if ((pHeap == NULL) || (pTranMan == NULL))
  52. {
  53. dwRes = ERROR_OUTOFMEMORY;
  54. }
  55. if (dwRes == ERROR_SUCCESS)
  56. {
  57. dwRes = pTranMan->Init();
  58. TEST_ASSERT(L"Initialization of Transaction Manager failed!", dwRes);
  59. }
  60. if (dwRes == ERROR_SUCCESS)
  61. {
  62. dwRes = pHeap->Initialize(pTranMan);
  63. TEST_ASSERT(L"Initialization of object heap failed!", dwRes);
  64. }
  65. return dwRes;
  66. }
  67. DWORD WriteBuffer(BYTE *pbBuffer, DWORD dwLength, DWORD *pdwPage, DWORD *pdwOffset)
  68. {
  69. DWORD dwRes;
  70. dwRes = pTranMan->BeginTrans();
  71. TEST_ASSERT(L"Begin Transaction Failed!", dwRes);
  72. if (dwRes == ERROR_SUCCESS)
  73. dwRes = pHeap->WriteNewBuffer(dwLength, pbBuffer, pdwPage, pdwOffset);
  74. printf("Written %d bytes with signature of <%d> to pageId <0x%X>, offset <0x%X>\n", dwLength, pbBuffer[0], *pdwPage, *pdwOffset);
  75. TEST_ASSERT(L"Write failed!", dwRes);
  76. if (dwRes == ERROR_SUCCESS)
  77. {
  78. dwRes = pTranMan->CommitTrans();
  79. TEST_ASSERT(L"Commit Transaction Failed!", dwRes);
  80. }
  81. if (dwRes != ERROR_SUCCESS)
  82. {
  83. dwRes = pTranMan->RollbackTrans();
  84. TEST_ASSERT(L"Abort Transaction Failed!", dwRes);
  85. }
  86. return dwRes;
  87. }
  88. DWORD DeleteBuffer(DWORD dwPage, DWORD dwOffset)
  89. {
  90. DWORD dwRes;
  91. dwRes = pTranMan->BeginTrans();
  92. TEST_ASSERT(L"Begin Transaction Failed!", dwRes);
  93. dwRes = pHeap->DeleteBuffer(dwPage, dwOffset);
  94. TEST_ASSERT(L"DeleteBuffer failed!", dwRes);
  95. if (dwRes == ERROR_SUCCESS)
  96. {
  97. dwRes = pTranMan->CommitTrans();
  98. TEST_ASSERT(L"Commit Transaction Failed!", dwRes);
  99. }
  100. if (dwRes != ERROR_SUCCESS)
  101. {
  102. dwRes = pTranMan->RollbackTrans();
  103. TEST_ASSERT(L"Abort Transaction Failed!", dwRes);
  104. }
  105. return dwRes;
  106. }
  107. void __cdecl main(void)
  108. {
  109. DWORD dwRes = ERROR_SUCCESS;
  110. BYTE* buff = new BYTE[TEST_OBJECT_SIZE];
  111. DWORD* aPageId = new DWORD[TEST_OBJECT_ITER];
  112. DWORD* aOffsetId = new DWORD [TEST_OBJECT_ITER];
  113. DWORD* aSize = new DWORD [TEST_OBJECT_ITER];
  114. BYTE *pBuffer;
  115. if ((buff == 0) || (aPageId == 0) || (aOffsetId == 0) || (aSize == 0))
  116. {
  117. dwRes = ERROR_OUTOFMEMORY;
  118. }
  119. if (dwRes == ERROR_SUCCESS)
  120. dwRes = Init();
  121. if (dwRes == ERROR_SUCCESS)
  122. {
  123. for (int i = 0; i < TEST_OBJECT_ITER; i++)
  124. {
  125. aSize[i] = rand() % TEST_OBJECT_SIZE;
  126. memset(buff, i, aSize[i]);
  127. dwRes = WriteBuffer(buff, aSize[i], &aPageId[i], &aOffsetId[i]);
  128. }
  129. for (i = 0; i < TEST_OBJECT_ITER; i++)
  130. {
  131. pBuffer = NULL;
  132. DWORD dwLen;
  133. dwRes = pHeap->ReadBuffer(aPageId[i], aOffsetId[i], &pBuffer, &dwLen);
  134. TEST_ASSERT(L"ReadBuffer failed!", dwRes);
  135. dwRes = TestMem(pBuffer, aSize[i], (BYTE)i);
  136. TEST_ASSERT(L"Buffer read was not what we wrote!", dwRes);
  137. printf("Read %d bytes with signature of <%d> from pageId <0x%X>, offset <0x%X>\n", aSize[i], i, aPageId[i], aOffsetId[i]);
  138. delete [] pBuffer;
  139. }
  140. for (int i = 0; i < TEST_OBJECT_ITER; i++)
  141. {
  142. dwRes = DeleteBuffer(aPageId[i], aOffsetId[i]);
  143. TEST_ASSERT(L"DeleteBuffer failed!", dwRes);
  144. printf("Deleted %d bytes with signature of <%d> from pageId <0x%X>, offset <0x%X>\n", aSize[i], i, aPageId[i], aOffsetId[i]);
  145. }
  146. }
  147. if (dwRes == ERROR_SUCCESS)
  148. {
  149. dwRes = pHeap->Shutdown(0);
  150. TEST_ASSERT(L"Heap Shutdown failed!", dwRes);
  151. dwRes = pTranMan->Shutdown(0);
  152. TEST_ASSERT(L"Transaction manager Shutdown failed!", dwRes);
  153. }
  154. delete [] buff;
  155. delete [] aPageId;
  156. delete [] aOffsetId;
  157. delete [] aSize;
  158. delete pHeap;
  159. delete pTranMan;
  160. }