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: test4.c |
8 | ** |
9 | ** Dependencies: PAL_Initialize |
10 | ** PAL_Terminate |
11 | ** CreateEvent |
12 | ** CloseHandle |
13 | ** WaitForSingleObject |
14 | ** DuplicateHandle |
15 | ** GetCurrentProcess |
16 | ** |
17 | ** Purpose: |
18 | ** |
19 | ** Test to ensure proper operation of the SetEvent() |
20 | ** API by calling it on an event handle that's the |
21 | ** result of a DuplicateHandle() call on another event |
22 | ** handle. |
23 | ** |
24 | |
25 | ** |
26 | **===========================================================================*/ |
27 | #include <palsuite.h> |
28 | |
29 | |
30 | |
31 | int __cdecl main( int argc, char **argv ) |
32 | |
33 | { |
34 | /* local variables */ |
35 | DWORD dwRet = 0; |
36 | HANDLE hEvent = NULL; |
37 | HANDLE hDupEvent = NULL; |
38 | LPSECURITY_ATTRIBUTES lpEventAttributes = NULL; |
39 | BOOL bManualReset = TRUE; |
40 | BOOL bInitialState = FALSE; |
41 | |
42 | |
43 | /* PAL initialization */ |
44 | if( (PAL_Initialize(argc, argv)) != 0 ) |
45 | { |
46 | return( FAIL ); |
47 | } |
48 | |
49 | |
50 | /* create an event which we can use with SetEvent */ |
51 | hEvent = CreateEvent( lpEventAttributes, |
52 | bManualReset, |
53 | bInitialState, |
54 | NULL ); |
55 | |
56 | if( hEvent == INVALID_HANDLE_VALUE ) |
57 | { |
58 | /* ERROR */ |
59 | Fail( "ERROR:%lu:CreateEvent() call failed\n" , GetLastError() ); |
60 | } |
61 | |
62 | /* verify that the event isn't signalled yet */ |
63 | dwRet = WaitForSingleObject( hEvent, 0 ); |
64 | if( dwRet != WAIT_TIMEOUT ) |
65 | { |
66 | /* ERROR */ |
67 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
68 | "expected WAIT_TIMEOUT\n" , |
69 | dwRet ); |
70 | CloseHandle( hEvent ); |
71 | Fail( "Test failed\n" ); |
72 | } |
73 | |
74 | |
75 | /* duplicate the event handle */ |
76 | if( ! (DuplicateHandle( |
77 | GetCurrentProcess(), |
78 | hEvent, |
79 | GetCurrentProcess(), |
80 | &hDupEvent, |
81 | GENERIC_READ|GENERIC_WRITE, /* ignored in PAL */ |
82 | FALSE, |
83 | DUPLICATE_SAME_ACCESS ) ) ) |
84 | { |
85 | Trace("ERROR:%u:DuplicateHandle() call failed\n" , |
86 | GetLastError() ); |
87 | CloseHandle( hEvent ); |
88 | Fail("Test failed\n" ); |
89 | } |
90 | |
91 | /* verify that the event isn't signalled yet with the duplicate handle */ |
92 | dwRet = WaitForSingleObject( hDupEvent, 0 ); |
93 | if( dwRet != WAIT_TIMEOUT ) |
94 | { |
95 | /* ERROR */ |
96 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
97 | "expected WAIT_TIMEOUT\n" , |
98 | dwRet ); |
99 | CloseHandle( hEvent ); |
100 | CloseHandle( hDupEvent ); |
101 | Fail( "Test failed\n" ); |
102 | } |
103 | |
104 | |
105 | /* set the event using the duplicate handle */ |
106 | if( ! SetEvent( hDupEvent ) ) |
107 | { |
108 | /* ERROR */ |
109 | Trace( "ERROR:%lu:SetEvent() call failed\n" , GetLastError() ); |
110 | CloseHandle( hEvent ); |
111 | CloseHandle( hDupEvent ); |
112 | Fail( "Test failed\n" ); |
113 | } |
114 | |
115 | /* verify that the event is signalled using the duplicate handle*/ |
116 | dwRet = WaitForSingleObject( hDupEvent, 0 ); |
117 | if( dwRet != WAIT_OBJECT_0 ) |
118 | { |
119 | /* ERROR */ |
120 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
121 | "expected WAIT_OBJECT_0\n" , |
122 | dwRet ); |
123 | CloseHandle( hEvent ); |
124 | Fail( "Test failed\n" ); |
125 | } |
126 | |
127 | /* verify that the event is signalled using the original event handle */ |
128 | dwRet = WaitForSingleObject( hEvent, 0 ); |
129 | if( dwRet != WAIT_OBJECT_0 ) |
130 | { |
131 | /* ERROR */ |
132 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
133 | "expected WAIT_OBJECT_0\n" , |
134 | dwRet ); |
135 | CloseHandle( hEvent ); |
136 | Fail( "Test failed\n" ); |
137 | } |
138 | |
139 | |
140 | /* close the duplicate event handle */ |
141 | if( ! CloseHandle( hDupEvent ) ) |
142 | { |
143 | Fail( "ERROR:%lu:CloseHandle() call failed for duplicate handle\n" , |
144 | GetLastError() ); |
145 | } |
146 | |
147 | |
148 | /* close the event handle */ |
149 | if( ! CloseHandle( hEvent ) ) |
150 | { |
151 | Fail( "ERROR:%lu:CloseHandle() call failed for original handle\n" , |
152 | GetLastError() ); |
153 | } |
154 | |
155 | |
156 | /* PAL termination */ |
157 | PAL_Terminate(); |
158 | |
159 | /* return success */ |
160 | return PASS; |
161 | } |
162 | |