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