summaryrefslogtreecommitdiff
path: root/src/pal/tests/palsuite/threading/OpenEventW/test1/test1.cpp
blob: 9dcb3a4a68ba7ba1d83e9a5488ee17a34145cda3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

/*============================================================================
**
** Source:  test1.c
**
** Purpose: Test for OpenEventW.  This test creates an event,
**          opens a handle to the same event, then waits on both handles
**          in both a signalled and non-signalled state to verify they're.
**          pointing to the same event object.
**
**
**==========================================================================*/
#include <palsuite.h>

int __cdecl main(int argc, char **argv)
{
    BOOL bRet = FAIL;
    DWORD dwRet;
    HANDLE hEvent;
    HANDLE hOpenEvent;
    WCHAR theName[] = {'E','v','e','n','t','\0'};
    LPCWSTR lpName = theName;

    /* PAL initialization */
    if( (PAL_Initialize(argc, argv)) != 0 )
    {
        return( FAIL );
    }

    /* Create an event (with a 0 intial state!) and ensure that the
       HANDLE is valid
    */
    hEvent = CreateEventW( NULL, TRUE, FALSE, lpName );
    if( hEvent == NULL )
    {
        Fail( "ERROR:%lu:CreateEvent call failed\n", GetLastError() );
    }


    /* Call OpenEventW to get another HANDLE on
       this event.  Ensure the HANDLE is valid.
    */
    hOpenEvent = OpenEventW( EVENT_ALL_ACCESS, TRUE, lpName );
    if( hOpenEvent == NULL )
    {
        Trace( "ERROR:%lu:OpenEventW call failed\n", GetLastError() );
        goto cleanup2;
    }

    /* wait on the original event to verify that it's not signalled */
    dwRet = WaitForSingleObject( hEvent, 0 );
    if( dwRet != WAIT_TIMEOUT )
    {
        Trace( "ERROR:WaitForSingleObject returned %lu, "
                "expected WAIT_TIMEOUT\n",
                dwRet );
        goto cleanup;
    }

    /* wait on the opened event to verify that it's not signalled either */
    dwRet = WaitForSingleObject( hOpenEvent, 0 );
    if( dwRet != WAIT_TIMEOUT )
    {
        Trace( "ERROR:WaitForSingleObject returned %lu, "
                "expected WAIT_TIMEOUT\n",
                dwRet );
        goto cleanup;
    }


    /* Set this opened HANDLE */
    if( ! SetEvent( hOpenEvent ) )
    {
        Trace( "ERROR:%lu:SetEvent call failed\n", GetLastError() );
        goto cleanup;
    }

    /* wait on the original event to verify that it's signalled */
    dwRet = WaitForSingleObject( hEvent, 0 );
    if( dwRet != WAIT_OBJECT_0 )
    {
        Trace( "ERROR:WaitForSingleObject returned %lu, "
                "expected WAIT_OBJECT_0\n",
                dwRet );
        goto cleanup;
    }

    /* wait on the opened event to verify that it's signalled too */
    dwRet = WaitForSingleObject( hOpenEvent, 0 );
    if( dwRet != WAIT_OBJECT_0 )
    {
        Trace( "ERROR:WaitForSingleObject returned %lu, "
                "expected WAIT_OBJECT_0\n",
                dwRet );
        goto cleanup;
    }

    /* success if we get here */
    bRet = PASS;

cleanup:
    /* close the opened handle */
    if( ! CloseHandle( hOpenEvent ) )
    {
        Trace( "ERROR:%lu:CloseHandle call failed\n", GetLastError() );
        bRet = FAIL;
    }

cleanup2:
    /* close the original event handle */
    if( ! CloseHandle( hEvent ) )
    {
        Trace( "ERROR:%lu:CloseHandle call failed\n", GetLastError() );
        bRet = FAIL;
    }

    /* check for failure */
    if( bRet == FAIL )
    {
        Fail( "test failed\n" );
    }


    /* terminate the PAL */
    PAL_Terminate();

    /* return success */
    return ( PASS );

}