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: test5.c |
8 | ** |
9 | ** Purpose: Positive test for OpenEventW. |
10 | ** |
11 | ** Dependencies: PAL_Initialize |
12 | ** PAL_Terminate |
13 | ** CreateEvent |
14 | ** CloseHandle |
15 | ** WaitForSingleObject |
16 | ** |
17 | ** Purpose: |
18 | ** |
19 | ** Test to ensure proper operation of the OpenEventW() |
20 | ** API by creating a new named event with CreateEventA() |
21 | ** and verifying that it can be opened with OpenEventW(). |
22 | ** It should be possible to use the event handles |
23 | ** interchangeably, we test by setting the event with the |
24 | ** original handle and waiting on it with the new one, |
25 | ** then resetting it with the new one and waiting |
26 | ** on it with the original one. |
27 | ** |
28 | ** |
29 | **===========================================================================*/ |
30 | #include <palsuite.h> |
31 | |
32 | |
33 | |
34 | int __cdecl main( int argc, char **argv ) |
35 | |
36 | { |
37 | /* local variables */ |
38 | BOOL ret = FAIL; |
39 | DWORD dwRet = 0; |
40 | HANDLE hEvent = NULL; |
41 | HANDLE hTestEvent = NULL; |
42 | LPSECURITY_ATTRIBUTES lpEventAttributes = NULL; |
43 | BOOL bManualReset = TRUE; |
44 | BOOL bInitialState = FALSE; |
45 | LPSTR lpNameA = "ShakeIt" ; |
46 | WCHAR wcName[] = {'S','h','a','k','e','I','t','\0'}; |
47 | LPWSTR lpNameW = wcName; |
48 | |
49 | |
50 | /* PAL initialization */ |
51 | if( (PAL_Initialize(argc, argv)) != 0 ) |
52 | { |
53 | return( FAIL ); |
54 | } |
55 | |
56 | |
57 | /* create an event which we can use with SetEvent */ |
58 | hEvent = CreateEventA( lpEventAttributes, |
59 | bManualReset, |
60 | bInitialState, |
61 | lpNameA ); |
62 | |
63 | if( hEvent == NULL ) |
64 | { |
65 | /* ERROR */ |
66 | Fail( "ERROR:%lu:CreateEvent() call failed\n" , GetLastError() ); |
67 | } |
68 | |
69 | /* open a new handle to our event */ |
70 | hTestEvent = OpenEventW(EVENT_ALL_ACCESS, /* we want all rights */ |
71 | FALSE, /* no inherit */ |
72 | lpNameW ); |
73 | |
74 | if( hTestEvent == NULL ) |
75 | { |
76 | /* ERROR */ |
77 | Trace( "ERROR:%lu:OpenEventW() call failed\n" , GetLastError() ); |
78 | goto cleanup; |
79 | } |
80 | |
81 | /* verify that the event isn't signalled yet by waiting on both */ |
82 | /* handles to the event object */ |
83 | dwRet = WaitForSingleObject( hEvent, 0 ); |
84 | if( dwRet != WAIT_TIMEOUT ) |
85 | { |
86 | /* ERROR */ |
87 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
88 | "expected WAIT_TIMEOUT\n" , |
89 | dwRet ); |
90 | goto cleanup; |
91 | } |
92 | |
93 | dwRet = WaitForSingleObject( hTestEvent, 0 ); |
94 | if( dwRet != WAIT_TIMEOUT ) |
95 | { |
96 | /* ERROR */ |
97 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
98 | "expected WAIT_TIMEOUT\n" , |
99 | dwRet ); |
100 | goto cleanup; |
101 | } |
102 | |
103 | |
104 | /* set the event using the original handle */ |
105 | if( ! SetEvent( hEvent ) ) |
106 | { |
107 | /* ERROR */ |
108 | Trace( "ERROR:%lu:SetEvent() call failed\n" , GetLastError() ); |
109 | goto cleanup; |
110 | } |
111 | |
112 | /* verify that the event is signalled by waiting on both handles */ |
113 | dwRet = WaitForSingleObject( hEvent, 0 ); |
114 | if( dwRet != WAIT_OBJECT_0 ) |
115 | { |
116 | /* ERROR */ |
117 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
118 | "expected WAIT_OBJECT_0\n" , |
119 | dwRet ); |
120 | goto cleanup; |
121 | } |
122 | |
123 | dwRet = WaitForSingleObject( hTestEvent, 0 ); |
124 | if( dwRet != WAIT_OBJECT_0 ) |
125 | { |
126 | /* ERROR */ |
127 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
128 | "expected WAIT_OBJECT_0\n" , |
129 | dwRet ); |
130 | goto cleanup; |
131 | } |
132 | |
133 | /* reset the event using the new handle */ |
134 | if( ! ResetEvent( hTestEvent ) ) |
135 | { |
136 | /* ERROR */ |
137 | Trace( "ERROR:%lu:ResetEvent() call failed\n" , GetLastError() ); |
138 | goto cleanup; |
139 | } |
140 | |
141 | /* verify that the event isn't signalled by waiting on both handles */ |
142 | dwRet = WaitForSingleObject( hEvent, 0 ); |
143 | if( dwRet != WAIT_TIMEOUT ) |
144 | { |
145 | /* ERROR */ |
146 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
147 | "expected WAIT_TIMEOUT\n" , |
148 | dwRet ); |
149 | goto cleanup; |
150 | } |
151 | |
152 | dwRet = WaitForSingleObject( hTestEvent, 0 ); |
153 | if( dwRet != WAIT_TIMEOUT ) |
154 | { |
155 | /* ERROR */ |
156 | Trace( "ERROR:WaitForSingleObject() call returned %lu, " |
157 | "expected WAIT_TIMEOUT\n" , |
158 | dwRet ); |
159 | goto cleanup; |
160 | } |
161 | |
162 | |
163 | /* test was successful */ |
164 | ret = PASS; |
165 | |
166 | |
167 | cleanup: |
168 | /* close the new event handle */ |
169 | if( hTestEvent != NULL ) |
170 | { |
171 | if( ! CloseHandle( hTestEvent ) ) |
172 | { |
173 | ret = FAIL; |
174 | Trace( "ERROR:%lu:CloseHandle() call failed\n" , GetLastError() ); |
175 | } |
176 | } |
177 | |
178 | /* close the original event handle */ |
179 | if( ! CloseHandle( hEvent ) ) |
180 | { |
181 | ret = FAIL; |
182 | Trace( "ERROR:%lu:CloseHandle() call failed\n" , GetLastError() ); |
183 | } |
184 | |
185 | /* failure message */ |
186 | if( ret != PASS ) |
187 | { |
188 | Fail( "Test failed\n" ); |
189 | } |
190 | |
191 | |
192 | /* PAL termination */ |
193 | PAL_Terminate(); |
194 | |
195 | /* return success or failure */ |
196 | return ret; |
197 | } |
198 | |