1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*=====================================================================
6**
7** Source: test3.c (DuplicateHandle)
8**
9** Purpose: Tests the PAL implementation of the DuplicateHandle function.
10** This will test duplication of an OpenEvent handle. Test an
11** event in a signaled state to wait, and then set the duplicate
12** to nonsignaled state and perform the wait again. The wait on
13** the event should fail. Test the duplication of closed and NULL
14** events, these should fail.
15**
16**
17**===================================================================*/
18#include <palsuite.h>
19
20int __cdecl main(int argc, char **argv)
21{
22 HANDLE hCreateEvent;
23 HANDLE hOpenEvent;
24 HANDLE hDupEvent;
25 WCHAR lpEventName[]={'E','v','e','n','t','\0'};
26
27 /*Initialize the PAL.*/
28 if ((PAL_Initialize(argc,argv)) != 0)
29 {
30 return (FAIL);
31 }
32
33 /*Create an Event, and set it in the signaled state.*/
34 hCreateEvent = CreateEventW(0,
35 TRUE,
36 TRUE,
37 lpEventName);
38 if (hCreateEvent == NULL)
39 {
40 Fail("ERROR: %u :unable to create event %s\n",
41 GetLastError(),
42 lpEventName);
43 }
44
45 /*Open another handle to hCreateHandle with OpenEvent*/
46 hOpenEvent = OpenEventW(EVENT_ALL_ACCESS,
47 TRUE,
48 lpEventName);
49 if (hOpenEvent == NULL)
50 {
51 Trace("ERROR: %u :unable to create handle with OpenEvent to %s\n",
52 GetLastError(),
53 lpEventName);
54 CloseHandle(hCreateEvent);
55 Fail("");
56 }
57
58 /*Create a duplicate Event handle*/
59 if (!(DuplicateHandle(GetCurrentProcess(),
60 hOpenEvent,
61 GetCurrentProcess(),
62 &hDupEvent,
63 GENERIC_READ|GENERIC_WRITE,
64 FALSE,
65 DUPLICATE_SAME_ACCESS)))
66 {
67 Trace("ERROR: %u :Fail to create the duplicate handle"
68 " to hCreateEvent=0x%lx\n",
69 GetLastError(),
70 hCreateEvent);
71 CloseHandle(hCreateEvent);
72 CloseHandle(hOpenEvent);
73 Fail("");
74 }
75
76 /*Perform wait on Event that is in signaled state*/
77 if ((WaitForSingleObject(hOpenEvent, 1000)) != WAIT_OBJECT_0)
78 {
79 Trace("ERROR: %u :WaitForSignalObject on hOpenEvent=0x%lx set to "
80 " signaled state failed\n",
81 GetLastError(),
82 hOpenEvent);
83 CloseHandle(hCreateEvent);
84 CloseHandle(hOpenEvent);
85 CloseHandle(hDupEvent);
86 Fail("");
87 }
88
89 /*Set the Duplicate Event handle to nonsignaled state*/
90 if ((ResetEvent(hDupEvent)) == 0)
91 {
92 Trace("ERROR: %u: unable to reset hDupEvent=0x%lx\n",
93 GetLastError(),
94 hDupEvent);
95 CloseHandle(hCreateEvent);
96 CloseHandle(hOpenEvent);
97 CloseHandle(hDupEvent);
98 Fail("");
99 }
100
101 /*Perform wait on Event that is in signaled state*/
102 if ((WaitForSingleObject(hOpenEvent, 1000)) == WAIT_OBJECT_0)
103 {
104 Trace("ERROR: %u :WaitForSignalObject succeeded on hOpenEvent=0x%lx "
105 " when Duplicate hDupEvent=0x%lx set to nonsignaled state"
106 " succeeded\n",
107 GetLastError(),
108 hOpenEvent,
109 hDupEvent);
110 CloseHandle(hCreateEvent);
111 CloseHandle(hOpenEvent);
112 CloseHandle(hDupEvent);
113 Fail("");
114 }
115
116 /*Close handles the events*/
117 CloseHandle(hCreateEvent);
118 CloseHandle(hOpenEvent);
119 CloseHandle(hDupEvent);
120
121 PAL_Terminate();
122 return (PASS);
123}
124