| 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: test1.c |
| 8 | ** |
| 9 | ** Purpose: Test for OpenEventW. This test creates an event, |
| 10 | ** opens a handle to the same event, then waits on both handles |
| 11 | ** in both a signalled and non-signalled state to verify they're. |
| 12 | ** pointing to the same event object. |
| 13 | ** |
| 14 | ** |
| 15 | **==========================================================================*/ |
| 16 | #include <palsuite.h> |
| 17 | |
| 18 | int __cdecl main(int argc, char **argv) |
| 19 | { |
| 20 | BOOL bRet = FAIL; |
| 21 | DWORD dwRet; |
| 22 | HANDLE hEvent; |
| 23 | HANDLE hOpenEvent; |
| 24 | WCHAR theName[] = {'E','v','e','n','t','\0'}; |
| 25 | LPCWSTR lpName = theName; |
| 26 | |
| 27 | /* PAL initialization */ |
| 28 | if( (PAL_Initialize(argc, argv)) != 0 ) |
| 29 | { |
| 30 | return( FAIL ); |
| 31 | } |
| 32 | |
| 33 | /* Create an event (with a 0 intial state!) and ensure that the |
| 34 | HANDLE is valid |
| 35 | */ |
| 36 | hEvent = CreateEventW( NULL, TRUE, FALSE, lpName ); |
| 37 | if( hEvent == NULL ) |
| 38 | { |
| 39 | Fail( "ERROR:%lu:CreateEvent call failed\n" , GetLastError() ); |
| 40 | } |
| 41 | |
| 42 | |
| 43 | /* Call OpenEventW to get another HANDLE on |
| 44 | this event. Ensure the HANDLE is valid. |
| 45 | */ |
| 46 | hOpenEvent = OpenEventW( EVENT_ALL_ACCESS, TRUE, lpName ); |
| 47 | if( hOpenEvent == NULL ) |
| 48 | { |
| 49 | Trace( "ERROR:%lu:OpenEventW call failed\n" , GetLastError() ); |
| 50 | goto cleanup2; |
| 51 | } |
| 52 | |
| 53 | /* wait on the original event to verify that it's not signalled */ |
| 54 | dwRet = WaitForSingleObject( hEvent, 0 ); |
| 55 | if( dwRet != WAIT_TIMEOUT ) |
| 56 | { |
| 57 | Trace( "ERROR:WaitForSingleObject returned %lu, " |
| 58 | "expected WAIT_TIMEOUT\n" , |
| 59 | dwRet ); |
| 60 | goto cleanup; |
| 61 | } |
| 62 | |
| 63 | /* wait on the opened event to verify that it's not signalled either */ |
| 64 | dwRet = WaitForSingleObject( hOpenEvent, 0 ); |
| 65 | if( dwRet != WAIT_TIMEOUT ) |
| 66 | { |
| 67 | Trace( "ERROR:WaitForSingleObject returned %lu, " |
| 68 | "expected WAIT_TIMEOUT\n" , |
| 69 | dwRet ); |
| 70 | goto cleanup; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /* Set this opened HANDLE */ |
| 75 | if( ! SetEvent( hOpenEvent ) ) |
| 76 | { |
| 77 | Trace( "ERROR:%lu:SetEvent call failed\n" , GetLastError() ); |
| 78 | goto cleanup; |
| 79 | } |
| 80 | |
| 81 | /* wait on the original event to verify that it's signalled */ |
| 82 | dwRet = WaitForSingleObject( hEvent, 0 ); |
| 83 | if( dwRet != WAIT_OBJECT_0 ) |
| 84 | { |
| 85 | Trace( "ERROR:WaitForSingleObject returned %lu, " |
| 86 | "expected WAIT_OBJECT_0\n" , |
| 87 | dwRet ); |
| 88 | goto cleanup; |
| 89 | } |
| 90 | |
| 91 | /* wait on the opened event to verify that it's signalled too */ |
| 92 | dwRet = WaitForSingleObject( hOpenEvent, 0 ); |
| 93 | if( dwRet != WAIT_OBJECT_0 ) |
| 94 | { |
| 95 | Trace( "ERROR:WaitForSingleObject returned %lu, " |
| 96 | "expected WAIT_OBJECT_0\n" , |
| 97 | dwRet ); |
| 98 | goto cleanup; |
| 99 | } |
| 100 | |
| 101 | /* success if we get here */ |
| 102 | bRet = PASS; |
| 103 | |
| 104 | cleanup: |
| 105 | /* close the opened handle */ |
| 106 | if( ! CloseHandle( hOpenEvent ) ) |
| 107 | { |
| 108 | Trace( "ERROR:%lu:CloseHandle call failed\n" , GetLastError() ); |
| 109 | bRet = FAIL; |
| 110 | } |
| 111 | |
| 112 | cleanup2: |
| 113 | /* close the original event handle */ |
| 114 | if( ! CloseHandle( hEvent ) ) |
| 115 | { |
| 116 | Trace( "ERROR:%lu:CloseHandle call failed\n" , GetLastError() ); |
| 117 | bRet = FAIL; |
| 118 | } |
| 119 | |
| 120 | /* check for failure */ |
| 121 | if( bRet == FAIL ) |
| 122 | { |
| 123 | Fail( "test failed\n" ); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /* terminate the PAL */ |
| 128 | PAL_Terminate(); |
| 129 | |
| 130 | /* return success */ |
| 131 | return ( PASS ); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | |