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.

673 lines
18 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. CreateEventUnitTest.c <CreateEvent Component Unit Test>
  5. Abstract:
  6. This is the source file for the CreateEvent unit test sample
  7. Author(s):
  8. Vincent Geglia
  9. Environment:
  10. User Mode
  11. Notes:
  12. Revision History:
  13. --*/
  14. //
  15. // General includes
  16. //
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <excpt.h>
  20. //
  21. // Project specific includes
  22. //
  23. #include <unittest.h>
  24. //
  25. // Template information
  26. //
  27. #define COMPONENT_UNIT_TEST_NAME "CreateEventUnitTest"
  28. #define COMPONENT_UNIT_TEST_PARAMLIST "[/?]"
  29. #define COMPONENT_UNIT_TEST_PARAMDESC1 "/? - Displays the usage message"
  30. #define COMPONENT_UNIT_TEST_PARAMDESC2 ""
  31. #define COMPONENT_UNIT_TEST_PARAMDESC3 ""
  32. #define COMPONENT_UNIT_TEST_PARAMDESC4 ""
  33. #define COMPONENT_UNIT_TEST_PARAMDESC5 ""
  34. #define COMPONENT_UNIT_TEST_PARAMDESC6 ""
  35. #define COMPONENT_UNIT_TEST_PARAMDESC7 ""
  36. #define COMPONENT_UNIT_TEST_PARAMDESC8 ""
  37. #define COMPONENT_UNIT_TEST_ABSTRACT "This module executes the sample CreateEvent Component Unit Test"
  38. #define COMPONENT_UNIT_TEST_AUTHORS "VincentG"
  39. //
  40. // Definitions
  41. //
  42. #define CREATEEVENT_TEST_TIMEOUT 5000
  43. //
  44. // Private function prototypes
  45. //
  46. INT
  47. __cdecl main
  48. (
  49. INT argc,
  50. CHAR *argv[]
  51. );
  52. //
  53. // Code
  54. //
  55. INT
  56. __cdecl main
  57. (
  58. INT argc,
  59. CHAR *argv[]
  60. )
  61. /*++
  62. Routine Description:
  63. This is the main function.
  64. Arguments:
  65. argc - Argument count
  66. argv - Argument pointers
  67. Return Value:
  68. None
  69. --*/
  70. {
  71. UNIT_TEST_STATUS teststatus = UNIT_TEST_STATUS_NOT_RUN;
  72. INT count;
  73. UCHAR logfilepath [MAX_PATH];
  74. HANDLE log;
  75. BOOL bstatus = FALSE;
  76. //
  77. // Check to see if user passed in /?
  78. //
  79. if (UtParseCmdLine ("/D", argc, argv)) {
  80. printf("/D specified.\n");
  81. }
  82. for (count = 0; count < argc; count++) {
  83. if (strstr (argv[count], "/?") || strstr (argv[count], "-?")) {
  84. printf("Usage: %s %s\n\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n\n\nAbstract: %s\n\nContact(s): %s",
  85. COMPONENT_UNIT_TEST_NAME,
  86. COMPONENT_UNIT_TEST_PARAMLIST,
  87. COMPONENT_UNIT_TEST_PARAMDESC1,
  88. COMPONENT_UNIT_TEST_PARAMDESC2,
  89. COMPONENT_UNIT_TEST_PARAMDESC3,
  90. COMPONENT_UNIT_TEST_PARAMDESC4,
  91. COMPONENT_UNIT_TEST_PARAMDESC5,
  92. COMPONENT_UNIT_TEST_PARAMDESC6,
  93. COMPONENT_UNIT_TEST_PARAMDESC7,
  94. COMPONENT_UNIT_TEST_PARAMDESC8,
  95. COMPONENT_UNIT_TEST_ABSTRACT,
  96. COMPONENT_UNIT_TEST_AUTHORS
  97. );
  98. goto exitcleanup;
  99. }
  100. }
  101. if (UtInitLog (COMPONENT_UNIT_TEST_NAME) == FALSE) {
  102. printf("FATAL ERROR: Unable to initialize log file.\n");
  103. teststatus = UNIT_TEST_STATUS_NOT_RUN;
  104. goto exitcleanup;
  105. }
  106. //
  107. // Begin individual test cases
  108. //
  109. UtLogINFO ("** BEGIN INDIVIDUAL TEST CASES **");
  110. //
  111. // Calling CreateEvent with NULL as a first parameter
  112. // Calling CreateEvent with TRUE as a second parameter
  113. // Calling CreateEvent with FALSE as third parameter
  114. // Calling CreateEvent with NULL as fourth parameter
  115. //
  116. //
  117. // Expected result: Valid event handle and no exceptions
  118. //
  119. UtLogINFO ("TEST CASE: Calling CreateEvent with NULL as a first parameter...");
  120. UtLogINFO ("TEST CASE: Calling CreateEvent with TRUE as a second parameter...");
  121. UtLogINFO ("TEST CASE: Calling CreateEvent with FALSE as third parameter...");
  122. UtLogINFO ("TEST CASE: Calling CreateEvent with NULL as fourth parameter...");
  123. bstatus = FALSE;
  124. __try {
  125. HANDLE hevent = INVALID_HANDLE_VALUE;
  126. hevent = CreateEvent (NULL, TRUE, FALSE, NULL);
  127. if (hevent == NULL) {
  128. UtLogFAIL ("FAILURE: CreateEvent returned an invalid handle.");
  129. __leave;
  130. }
  131. CloseHandle (hevent);
  132. bstatus = TRUE;
  133. }__except (1) {
  134. UtLogFAIL ("FAILURE: CreateEvent threw an exception.");
  135. bstatus = FALSE;
  136. }
  137. if (bstatus == TRUE) {
  138. UtLogPASS ("PASS: CreateEvent returned a valid handle.");
  139. }
  140. //
  141. // Calling CreateEvent with TRUE as third parameter
  142. //
  143. //
  144. // Expected result: Valid event handle and no exceptions
  145. //
  146. UtLogINFO ("TEST CASE: Calling CreateEvent with TRUE as third parameter...");
  147. bstatus = FALSE;
  148. __try {
  149. HANDLE hevent = INVALID_HANDLE_VALUE;
  150. hevent = CreateEvent (NULL, TRUE, TRUE, NULL);
  151. if (hevent == NULL) {
  152. UtLogFAIL ("FAILURE: CreateEvent returned an invalid handle.");
  153. __leave;
  154. }
  155. CloseHandle (hevent);
  156. bstatus = TRUE;
  157. }__except (1) {
  158. UtLogFAIL ("FAILURE: CreateEvent threw an exception.");
  159. bstatus = FALSE;
  160. }
  161. if (bstatus == TRUE) {
  162. UtLogPASS ("PASS: CreateEvent returned a valid handle.");
  163. }
  164. UtLogINFO ("** END INDIVIDUAL TEST CASES **");
  165. //
  166. // Begin test scenarios
  167. //
  168. UtLogINFO ("** BEGIN TEST SCENARIOS **");
  169. //
  170. // Test Scenario: Create an event, then close it.
  171. // Description: Create an event, then close it. Verify all return codes are as expected.
  172. //
  173. // Expected Result: The test case is a success if valid handle is returned and
  174. // no exceptions are thrown.
  175. //
  176. UtLogINFO ("TEST SCENARIO: Create an event, then close it...");
  177. bstatus = FALSE;
  178. __try {
  179. HANDLE hevent = INVALID_HANDLE_VALUE;
  180. hevent = CreateEvent (NULL, TRUE, FALSE, NULL);
  181. if (hevent == NULL) {
  182. UtLogFAIL ("FAILURE: CreateEvent returned an invalid handle.");
  183. __leave;
  184. }
  185. CloseHandle (hevent);
  186. bstatus = TRUE;
  187. }__except (1) {
  188. UtLogFAIL ("FAILURE: CreateEvent threw an exception.");
  189. bstatus = FALSE;
  190. }
  191. if (bstatus == TRUE) {
  192. UtLogPASS ("PASS: CreateEvent returned a valid handle, and no exception was thrown.");
  193. }
  194. //
  195. // Test Scenario: Create a signaled event, wait on it, then close it.
  196. //
  197. // Description: Create an signaled event, wait on it, and then close it.
  198. // The wait should return WAIT_OBJECT_0, indicating the event was already
  199. // signaled when the wait was processed. The wait period specified is 0,
  200. // so there is no actual wait, rather the event state is evaluated and
  201. // WaitForSingleObject returns immediately.
  202. //
  203. // Expected Result: WaitForSingleObject returns WAIT_OBJECT_0, and no
  204. // exceptions are thrown.
  205. //
  206. UtLogINFO ("TEST SCENARIO: Create an event, wait on it, then close it...");
  207. bstatus = FALSE;
  208. __try {
  209. HANDLE hevent = INVALID_HANDLE_VALUE;
  210. DWORD waitvalue = 0;
  211. hevent = CreateEvent (NULL, TRUE, TRUE, NULL);
  212. if (hevent == NULL) {
  213. UtLogFAIL ("FAILURE: CreateEvent returned an invalid handle.");
  214. __leave;
  215. }
  216. waitvalue = WaitForSingleObject (hevent, 0);
  217. if (waitvalue != WAIT_OBJECT_0) {
  218. UtLogFAIL ("FAILURE: WaitForSingleObject did NOT return WAIT_OBJECT_0.");
  219. CloseHandle (hevent);
  220. __leave;
  221. }
  222. CloseHandle (hevent);
  223. bstatus = TRUE;
  224. }__except (1) {
  225. UtLogFAIL ("FAILURE: Test threw an exception.");
  226. bstatus = FALSE;
  227. }
  228. if (bstatus == TRUE) {
  229. UtLogPASS ("PASS: Wait returned WAIT_OBJECT_0, and no exception was thrown.");
  230. }
  231. //
  232. // Test Scenario: Create a signaled event, set it to non-signaled, wait on it,
  233. // then close it.
  234. //
  235. // Description: : Create an signaled event. Use ResetEvent to set the event
  236. // to a non-signaled state. Call GetSystemTime to acquire the current system time.
  237. // Call WaitForSingleObject to wait on the event, and set the wait time to 5000 ms.
  238. // The wait should return WAIT_TIMEOUT, indicating the event was not signaled within
  239. // the timeout period. Call GetSystemTime again and compare result to original call.
  240. // The delta should be no less then 5000 ms. Close the event. The test case is a
  241. // success if ResetEvent succeeds, WaitForSingleObject returns WAIT_TIMEOUT, the time
  242. // delta is no less then 5000 ms, and no exceptions are thrown.
  243. //
  244. // Expected Result: The test case is a success if ResetEvent succeeds, WaitForSingleObject
  245. // returns WAIT_TIMEOUT, the time delta is no less then 5000 ms, time delta is no greater
  246. // then 5500ms, and no exceptions are thrown.
  247. //
  248. UtLogINFO ("TEST SCENARIO: Create a signaled event, set it to non-signaled, wait on it, then close it...");
  249. bstatus = FALSE;
  250. __try {
  251. HANDLE hevent = INVALID_HANDLE_VALUE;
  252. DWORD waitvalue = 0;
  253. BOOL status = FALSE;
  254. SYSTEMTIME systemtime;
  255. FILETIME filetime;
  256. ULONG64 timestamp1, timestamp2;
  257. hevent = CreateEvent (NULL, TRUE, TRUE, NULL);
  258. if (hevent == NULL) {
  259. UtLogFAIL ("FAILURE: CreateEvent returned an invalid handle.");
  260. __leave;
  261. }
  262. status = ResetEvent (hevent);
  263. if (status == FALSE) {
  264. UtLogFAIL ("FAILURE: ResetEvent returned a failure status.");
  265. CloseHandle (hevent);
  266. __leave;
  267. }
  268. GetSystemTime (&systemtime);
  269. if (!SystemTimeToFileTime (&systemtime, &filetime)) {
  270. UtLogFAIL ("FAILURE: Unable to convert system time to file time.");
  271. CloseHandle (hevent);
  272. __leave;
  273. }
  274. timestamp1 = (ULONG64) (filetime.dwLowDateTime + (filetime.dwHighDateTime * 0x10000000));
  275. waitvalue = WaitForSingleObject (hevent, CREATEEVENT_TEST_TIMEOUT);
  276. if (waitvalue != WAIT_TIMEOUT) {
  277. UtLogFAIL ("FAILURE: WaitForSingleObject did NOT return WAIT_TIMEOUT.");
  278. CloseHandle (hevent);
  279. __leave;
  280. }
  281. GetSystemTime (&systemtime);
  282. if (!SystemTimeToFileTime (&systemtime, &filetime)) {
  283. UtLogFAIL ("FAILURE: Unable to convert system time to file time.");
  284. CloseHandle (hevent);
  285. __leave;
  286. }
  287. timestamp2 = (ULONG64) (filetime.dwLowDateTime + (filetime.dwHighDateTime * 0x10000000));
  288. if ((timestamp2 - timestamp1) > 55000000) {
  289. UtLogFAIL ("FAILURE: Wait took excessive amount of time.");
  290. CloseHandle (hevent);
  291. __leave;
  292. }
  293. if ((timestamp2 - timestamp1) < 50000000) {
  294. UtLogFAIL ("FAILURE: Wait took inadequate amount of time.");
  295. CloseHandle (hevent);
  296. __leave;
  297. }
  298. CloseHandle (hevent);
  299. bstatus = TRUE;
  300. }__except (1) {
  301. UtLogFAIL ("FAILURE: Test threw an exception.");
  302. bstatus = FALSE;
  303. }
  304. if (bstatus == TRUE) {
  305. UtLogPASS ("PASS: ResetEvent succeeded, WaitForSingleObject returned WAIT_TIMEOUT, the time delta was no less then 5000 ms, time delta was no greater then 5500ms, and no exceptions were thrown.");
  306. }
  307. // Test Scenario: Create a non-signaled event, signal it, wait on it, then close it
  308. //
  309. // Description: Create a non-signaled event. Set it to the signaled state using SetEvent.
  310. // The setting should succeed. Next, use WaitForSingleObject with a zero wait parameter to
  311. // determine the state of the event, then return immediately. WaitForSingleObject should
  312. // return WAIT_OBJECT_0.
  313. //
  314. // Expected Result: The test case is a success if SetEvent succeeds, WaitForSingleObject
  315. // returns WAIT_OBJECT_0, and no exceptions are thrown.
  316. //
  317. UtLogINFO ("TEST SCENARIO: Create a non-signaled event, signal it, wait on it, then close it...");
  318. bstatus = FALSE;
  319. __try {
  320. HANDLE hevent = INVALID_HANDLE_VALUE;
  321. DWORD waitvalue = 0;
  322. BOOL status = FALSE;
  323. hevent = CreateEvent (NULL, TRUE, FALSE, NULL);
  324. if (hevent == NULL) {
  325. UtLogFAIL ("FAILURE: CreateEvent returned an invalid handle.");
  326. __leave;
  327. }
  328. status = SetEvent (hevent);
  329. if (status == FALSE) {
  330. UtLogFAIL ("FAILURE: SetEvent returned a failure status.");
  331. CloseHandle (hevent);
  332. __leave;
  333. }
  334. waitvalue = WaitForSingleObject (hevent, 0);
  335. if (waitvalue != WAIT_OBJECT_0) {
  336. UtLogFAIL ("FAILURE: WaitForSingleObject did NOT return WAIT_OBJECT_0.");
  337. CloseHandle (hevent);
  338. __leave;
  339. }
  340. CloseHandle (hevent);
  341. bstatus = TRUE;
  342. }__except (1) {
  343. UtLogFAIL ("FAILURE: Test threw an exception.");
  344. bstatus = FALSE;
  345. }
  346. if (bstatus == TRUE) {
  347. UtLogPASS ("PASS: SetEvent succeeded, Wait returned WAIT_OBJECT_0, and no exception was thrown.");
  348. }
  349. // Test Scenario: Create a non-signaled event, wait on it, then close it
  350. //
  351. // Description: Create a non-signaled event, wait on it, then close it. Call
  352. // GetSystemTime to acquire the current system time. Call WaitForSingleObject
  353. // to wait on the event, and set the wait time to 5000 ms. The wait should
  354. // return WAIT_TIMEOUT, indicating the event was not signaled within the timeout period.
  355. // Call GetSystemTime again and compare result to original call. The delta should be no
  356. // less then 5000 ms, and no more then 5500ms. Close the event.
  357. //
  358. // Expected Result: The test case is a success if WaitForSingleObject returns
  359. // WAIT_TIMEOUT, the time delta is no less then 5000 ms, the time delta is no more then
  360. // 5500ms, and no exceptions are thrown.
  361. //
  362. UtLogINFO ("TEST SCENARIO: Create a non-signaled event, wait on it, then close it...");
  363. bstatus = FALSE;
  364. __try {
  365. HANDLE hevent = INVALID_HANDLE_VALUE;
  366. DWORD waitvalue = 0;
  367. BOOL status = FALSE;
  368. SYSTEMTIME systemtime;
  369. FILETIME filetime;
  370. ULONG64 timestamp1, timestamp2;
  371. hevent = CreateEvent (NULL, TRUE, FALSE, NULL);
  372. if (hevent == NULL) {
  373. UtLogFAIL ("FAILURE: CreateEvent returned an invalid handle.");
  374. __leave;
  375. }
  376. GetSystemTime (&systemtime);
  377. if (!SystemTimeToFileTime (&systemtime, &filetime)) {
  378. UtLogFAIL ("FAILURE: Unable to convert system time to file time.");
  379. CloseHandle (hevent);
  380. __leave;
  381. }
  382. timestamp1 = (ULONG64) (filetime.dwLowDateTime + (filetime.dwHighDateTime * 0x10000000));
  383. waitvalue = WaitForSingleObject (hevent, CREATEEVENT_TEST_TIMEOUT);
  384. if (waitvalue != WAIT_TIMEOUT) {
  385. UtLogFAIL ("FAILURE: WaitForSingleObject did NOT return WAIT_TIMEOUT.");
  386. CloseHandle (hevent);
  387. __leave;
  388. }
  389. GetSystemTime (&systemtime);
  390. if (!SystemTimeToFileTime (&systemtime, &filetime)) {
  391. UtLogFAIL ("FAILURE: Unable to convert system time to file time.");
  392. CloseHandle (hevent);
  393. __leave;
  394. }
  395. timestamp2 = (ULONG64) (filetime.dwLowDateTime + (filetime.dwHighDateTime * 0x10000000));
  396. if ((timestamp2 - timestamp1) > 55000000) {
  397. UtLogFAIL ("FAILURE: Wait took excessive amount of time.");
  398. CloseHandle (hevent);
  399. __leave;
  400. }
  401. if ((timestamp2 - timestamp1) < 50000000) {
  402. UtLogFAIL ("FAILURE: Wait took inadequate amount of time.");
  403. CloseHandle (hevent);
  404. __leave;
  405. }
  406. CloseHandle (hevent);
  407. bstatus = TRUE;
  408. }__except (1) {
  409. UtLogFAIL ("FAILURE: Test threw an exception.");
  410. bstatus = FALSE;
  411. }
  412. if (bstatus == TRUE) {
  413. UtLogPASS ("PASS: WaitForSingleObject returned WAIT_TIMEOUT, the time delta was no less then 5000 ms, the time delta was no more then 5500ms, and no exceptions were thrown.");
  414. }
  415. // Test Scenario: Create a named mutex object, then attempt to create a named event
  416. // with the same name.
  417. //
  418. // Description: Call CreateMutex and provide a name. Verify the mutex object is
  419. // created properly. Call CreateEvent and pass in the same name.
  420. //
  421. // Expected Result: Test case is a success if mutex is created properly, CreateEvent
  422. // returns proper error code (ERROR_INVALID_HANDLE), and no exception is thrown.
  423. //
  424. UtLogINFO ("TEST SCENARIO: Create a named mutex object, then attempt to create a named event with the same name....");
  425. bstatus = FALSE;
  426. __try {
  427. HANDLE hevent = INVALID_HANDLE_VALUE;
  428. HANDLE hmutex = INVALID_HANDLE_VALUE;
  429. UCHAR objectname[] = {'T','e','s','t','\0'};
  430. hmutex = CreateMutex (NULL, TRUE, objectname);
  431. if (hmutex == NULL) {
  432. UtLogFAIL ("FAILURE: CreateMutex returned an invalid handle.");
  433. __leave;
  434. }
  435. hevent = CreateEvent (NULL, TRUE, FALSE, objectname);
  436. if (hevent != NULL) {
  437. UtLogFAIL ("FAILURE: CreateEvent returned a valid handle.");
  438. __leave;
  439. }
  440. if (GetLastError() != ERROR_INVALID_HANDLE) {
  441. UtLogFAIL ("FAILURE: CreateEvent did not return ERROR_INVALID_HANDLE.");
  442. __leave;
  443. }
  444. CloseHandle (hmutex);
  445. bstatus = TRUE;
  446. }__except (1) {
  447. UtLogFAIL ("FAILURE: Test threw an exception.");
  448. bstatus = FALSE;
  449. }
  450. if (bstatus == TRUE) {
  451. UtLogPASS ("PASS: CreateEvent returns proper error code (ERROR_INVALID_HANDLE), and no exception is thrown.");
  452. }
  453. exitcleanup:
  454. UtCloseLog ();
  455. return ((INT) teststatus);
  456. }