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.

264 lines
7.8 KiB

  1. //--------------------------------------------------------------------
  2. // TAutoEnr - implementation
  3. // Copyright (C) Microsoft Corporation, 1997 - 1999
  4. //
  5. // test autoenrollment
  6. //
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <windows.h>
  10. #include <stdio.h>
  11. #include <certca.h>
  12. #include <autoenr.h>
  13. #include <conio.h>
  14. typedef HANDLE (WINAPI * PFNCertAutoenrollment)(
  15. HWND hwndParent,
  16. DWORD dwStatus);
  17. //move to autoenr.h file
  18. //#define MACHINE_AUTOENROLLMENT_TIMER_NAME L"AUTOENRL:MachineEnrollmentTimer"
  19. //#define USER_AUTOENROLLMENT_TIMER_NAME L"AUTOENRL:UserEnrollmentTimer"
  20. #define MACHINE_AUTOENROLLMENT_MUTEX_NAME L"AUTOENRL:MachineEnrollmentMutex"
  21. #define USER_AUTOENROLLMENT_MUTEX_NAME L"AUTOENRL:UserEnrollmentMutex"
  22. //--------------------------------------------------------------------
  23. void PrintHelp(void) {
  24. wprintf(
  25. L"tautoenrl <testID>\n"
  26. L" Available tests:\n"
  27. L" startup - test autoenroll in startup mode\n"
  28. L" wakeup - test autoenroll in startup mode\n"
  29. L" triguser - test user autoenroll trigger event\n"
  30. L" trigmachine - test machine autoenroll trigger event\n"
  31. L" timeruser - test user autoenroll timer\n"
  32. L" timermachine - test machine autoenroll timer\n"
  33. L" remove_commit - test domain disjoin commit\n"
  34. L" remove_rollback - test domain disjoin roll back\n"
  35. );
  36. }
  37. //--------------------------------------------------------------------
  38. HRESULT TimerTest(WCHAR * wszTimerName) {
  39. HRESULT hr;
  40. LARGE_INTEGER liDueTime;
  41. // must be cleaned up
  42. HANDLE hTimer=NULL;
  43. wprintf(L"Testing the '%ws' timer.\n", wszTimerName);
  44. hTimer=OpenWaitableTimer(TIMER_MODIFY_STATE, false, wszTimerName);
  45. if (NULL==hTimer) {
  46. hr=HRESULT_FROM_WIN32(GetLastError());
  47. wprintf(L"OpenWaitableTimer(%ws) failed with 0x%08X.\n", wszTimerName, hr);
  48. goto error;
  49. }
  50. liDueTime.QuadPart=((signed __int64)-10000000)*60;
  51. if (!SetWaitableTimer (hTimer, &liDueTime, 0, NULL, 0, FALSE)) {
  52. hr=HRESULT_FROM_WIN32(GetLastError());
  53. wprintf(L"SetWaitableTimer failed with 0x%08X.\n", hr);
  54. goto error;
  55. }
  56. wprintf(L"Timer '%ws' will go off in 1 minute.\n", wszTimerName);
  57. hr=S_OK;
  58. error:
  59. if (NULL!=hTimer) {
  60. CloseHandle(hTimer);
  61. }
  62. return hr;
  63. }
  64. //--------------------------------------------------------------------
  65. HRESULT MutexTest(WCHAR * wszMutexName) {
  66. HRESULT hr;
  67. DWORD dwWaitResult;
  68. // must be cleaned up
  69. HANDLE hMutex=NULL;
  70. wprintf(L"Testing the '%ws' mutex.\n", wszMutexName);
  71. hMutex=OpenMutex(SYNCHRONIZE, false, wszMutexName);
  72. if (NULL==hMutex) {
  73. hr=HRESULT_FROM_WIN32(GetLastError());
  74. wprintf(L"OpenMutex(%ws) failed with 0x%08X.\n", wszMutexName, hr);
  75. goto error;
  76. }
  77. dwWaitResult=WaitForSingleObject(hMutex, 0);
  78. if (WAIT_FAILED==dwWaitResult) {
  79. hr=HRESULT_FROM_WIN32(GetLastError());
  80. wprintf(L"WaitForSingleObject failed with 0x%08X.\n", hr);
  81. goto error;
  82. }
  83. if (WAIT_TIMEOUT==dwWaitResult) {
  84. wprintf(L"Mutex held by someone else.\n");
  85. } else {
  86. wprintf(L"Mutex Acquired. Press an key to release.\n");
  87. _getch();
  88. if (!ReleaseMutex(hMutex)) {
  89. hr=HRESULT_FROM_WIN32(GetLastError());
  90. wprintf(L"ReleaseMutex failed with 0x%08X.\n", hr);
  91. goto error;
  92. }
  93. wprintf(L"Mutex released.\n");
  94. }
  95. hr=S_OK;
  96. error:
  97. if (NULL!=hMutex) {
  98. CloseHandle(hMutex);
  99. }
  100. return hr;
  101. }
  102. //--------------------------------------------------------------------
  103. HRESULT EventTest(WCHAR * wszEventName) {
  104. HRESULT hr;
  105. // must be cleaned up
  106. HANDLE hEvent=NULL;
  107. wprintf(L"Signaling the '%ws' event.\n", wszEventName);
  108. hEvent=OpenEvent(EVENT_MODIFY_STATE, false, wszEventName);
  109. if (NULL==hEvent) {
  110. hr=HRESULT_FROM_WIN32(GetLastError());
  111. wprintf(L"OpenEvent(%ws) failed with 0x%08X.\n", wszEventName, hr);
  112. goto error;
  113. }
  114. if (!SetEvent(hEvent)) {
  115. hr=HRESULT_FROM_WIN32(GetLastError());
  116. wprintf(L"SetEvent failed with 0x%08X.\n", hr);
  117. goto error;
  118. }
  119. hr=S_OK;
  120. error:
  121. if (NULL!=hEvent) {
  122. CloseHandle(hEvent);
  123. }
  124. return hr;
  125. }
  126. //--------------------------------------------------------------------
  127. HRESULT BasicTest(DWORD dwStatus) {
  128. HRESULT hr;
  129. PFNCertAutoenrollment pfnCertAutoenrollment;
  130. DWORD dwWaitResult;
  131. // must be cleaned up
  132. HANDLE hThread=NULL;
  133. HMODULE hInstAuto=NULL;
  134. if (dwStatus==CERT_AUTO_ENROLLMENT_START_UP) {
  135. wprintf(L"Invoking autoenrollment in startup mode...\n");
  136. } else {
  137. wprintf(L"Invoking autoenrollment in wakeup mode...\n");
  138. }
  139. hInstAuto=LoadLibrary(L"pautoenr.dll");
  140. if (NULL==hInstAuto) {
  141. hr=HRESULT_FROM_WIN32(GetLastError());
  142. wprintf(L"LoadLibrary failed with 0x%08X.\n", hr);
  143. goto error;
  144. }
  145. pfnCertAutoenrollment=(PFNCertAutoenrollment)GetProcAddress(hInstAuto, "CertAutoEnrollment");
  146. if (NULL==pfnCertAutoenrollment) {
  147. hr=HRESULT_FROM_WIN32(GetLastError());
  148. wprintf(L"GetProcAddress(CertAutoEnrollment) failed with 0x%08X.\n", hr);
  149. goto error;
  150. }
  151. // call the autoenrollment
  152. hThread=pfnCertAutoenrollment(GetDesktopWindow(), dwStatus);
  153. if (NULL!=hThread) {
  154. wprintf(L"Waiting for background thread to finish...\n");
  155. dwWaitResult=WaitForSingleObject(hThread, INFINITE);
  156. if (WAIT_FAILED==dwWaitResult) {
  157. hr=HRESULT_FROM_WIN32(GetLastError());
  158. wprintf(L"WaitForSingleObject failed with 0x%08X.\n", hr);
  159. goto error;
  160. }
  161. }
  162. hr=S_OK;
  163. error:
  164. if (NULL!=hThread) {
  165. CloseHandle(hThread);
  166. }
  167. if (NULL!=hInstAuto) {
  168. FreeLibrary(hInstAuto);
  169. }
  170. return hr;
  171. }
  172. //--------------------------------------------------------------------
  173. HRESULT TestRemove(DWORD dwFlags) {
  174. HRESULT hr;
  175. if(!CertAutoRemove(dwFlags))
  176. {
  177. hr=GetLastError();
  178. goto error;
  179. }
  180. hr=S_OK;
  181. error:
  182. return hr;
  183. }
  184. //--------------------------------------------------------------------
  185. extern "C" int __cdecl wmain(int nArgs, WCHAR ** rgwszArgs) {
  186. HRESULT hr;
  187. if (2!=nArgs || 0==wcscmp(rgwszArgs[1], L"/?") || 0==wcscmp(rgwszArgs[1], L"-?")) {
  188. PrintHelp();
  189. goto done;
  190. }
  191. if (0==_wcsicmp(L"startup", rgwszArgs[1])) {
  192. hr=BasicTest(CERT_AUTO_ENROLLMENT_START_UP);
  193. } else if (0==_wcsicmp(L"wakeup", rgwszArgs[1])) {
  194. hr=BasicTest(CERT_AUTO_ENROLLMENT_WAKE_UP);
  195. } else if (0==_wcsicmp(L"triguser", rgwszArgs[1])) {
  196. hr=EventTest(USER_AUTOENROLLMENT_TRIGGER_EVENT);
  197. } else if (0==_wcsicmp(L"trigmachine", rgwszArgs[1])) {
  198. hr=EventTest(L"Global\\" MACHINE_AUTOENROLLMENT_TRIGGER_EVENT);
  199. } else if (0==_wcsicmp(L"timeruser", rgwszArgs[1])) {
  200. hr=TimerTest(USER_AUTOENROLLMENT_TIMER_NAME);
  201. } else if (0==_wcsicmp(L"timermachine", rgwszArgs[1])) {
  202. hr=TimerTest(L"Global\\" MACHINE_AUTOENROLLMENT_TIMER_NAME);
  203. } else if (0==_wcsicmp(L"remove_commit", rgwszArgs[1])) {
  204. hr=TestRemove(CERT_AUTO_REMOVE_COMMIT);
  205. } else if (0==_wcsicmp(L"remove_rollback", rgwszArgs[1])) {
  206. hr=TestRemove(CERT_AUTO_REMOVE_ROLL_BACK);
  207. } else {
  208. wprintf(L"Command '%ws' unknown.\n", rgwszArgs[1]);
  209. goto done;
  210. }
  211. if (FAILED(hr)) {
  212. wprintf(L"Command '%ws' failed with error 0x%08X.\n", rgwszArgs[1], hr);
  213. } else {
  214. wprintf(L"Command '%ws' completed successfully.\n", rgwszArgs[1]);
  215. }
  216. done:
  217. return hr;
  218. }