| 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 (DuplicateHandle) |
| 8 | ** |
| 9 | ** Purpose: Tests the PAL implementation of the DuplicateHandle function. |
| 10 | ** This will test duplication of an CreateEvent 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 | |
| 20 | int __cdecl main(int argc, char **argv) |
| 21 | { |
| 22 | HANDLE hEvent; |
| 23 | HANDLE hDupEvent; |
| 24 | |
| 25 | /*Initalize the PAL.*/ |
| 26 | if ((PAL_Initialize(argc,argv)) != 0) |
| 27 | { |
| 28 | return (FAIL); |
| 29 | } |
| 30 | |
| 31 | /*Create an Event, and set it in the signaled state.*/ |
| 32 | hEvent = CreateEvent(0, TRUE, TRUE, 0); |
| 33 | if (hEvent == NULL) |
| 34 | { |
| 35 | Fail("ERROR: %u :unable to create event\n" , |
| 36 | GetLastError()); |
| 37 | } |
| 38 | |
| 39 | /*Create a duplicate Event handle.*/ |
| 40 | if (!(DuplicateHandle(GetCurrentProcess(), |
| 41 | hEvent,GetCurrentProcess(), |
| 42 | &hDupEvent, |
| 43 | GENERIC_READ|GENERIC_WRITE, |
| 44 | FALSE, DUPLICATE_SAME_ACCESS))) |
| 45 | { |
| 46 | Trace("ERROR: %u :Fail to create the duplicate handle" |
| 47 | " to hEvent=0x%lx\n" , |
| 48 | GetLastError(), |
| 49 | hEvent); |
| 50 | CloseHandle(hEvent); |
| 51 | Fail("" ); |
| 52 | } |
| 53 | |
| 54 | /*Perform wait on Event that is in signaled state.*/ |
| 55 | if ((WaitForSingleObject(hEvent, 1000)) != WAIT_OBJECT_0) |
| 56 | { |
| 57 | Trace("ERROR: %u :WaitForSignalObject on Event=0x%lx set to " |
| 58 | " signaled state failed" , |
| 59 | GetLastError(), |
| 60 | hEvent); |
| 61 | CloseHandle(hEvent); |
| 62 | CloseHandle(hDupEvent); |
| 63 | Fail("" ); |
| 64 | } |
| 65 | |
| 66 | /*Set the Duplicate Event handle to nonsignaled state.*/ |
| 67 | if ((ResetEvent(hDupEvent)) == 0) |
| 68 | { |
| 69 | Trace("ERROR: %u :unable to reset dup event\n" , |
| 70 | GetLastError()); |
| 71 | CloseHandle(hEvent); |
| 72 | CloseHandle(hDupEvent); |
| 73 | Fail("" ); |
| 74 | } |
| 75 | |
| 76 | /*Perform wait on Event that is in signaled state.*/ |
| 77 | if ((WaitForSingleObject(hEvent, 1000)) == WAIT_OBJECT_0) |
| 78 | { |
| 79 | Trace("ERROR: %u: WaitForSignalObject succeeded on Event=0x%lx " |
| 80 | " when Duplicate Event=0x%lx set to nonsignaled state" |
| 81 | " succeeded\n" , |
| 82 | GetLastError(), |
| 83 | hEvent, |
| 84 | hDupEvent); |
| 85 | CloseHandle(hEvent); |
| 86 | CloseHandle(hDupEvent); |
| 87 | Fail("" ); |
| 88 | } |
| 89 | |
| 90 | /*Close handles to events.*/ |
| 91 | CloseHandle(hEvent); |
| 92 | CloseHandle(hDupEvent); |
| 93 | |
| 94 | PAL_Terminate(); |
| 95 | return (PASS); |
| 96 | } |
| 97 | |