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: test3.c
8**
9** Purpose: Tests for CreateEventA. Create an event with an empty name,
10** create an event with the same name as an already created event
11** object.
12**
13**
14**=========================================================*/
15
16#include <palsuite.h>
17
18#define SWAPPTR ((VOID *)(-1))
19
20struct testCase
21{
22 LPSECURITY_ATTRIBUTES lpEventAttributes;
23 BOOL bManualReset;
24 BOOL bInitialState;
25 char lpName[MAX_PATH + 2];
26 DWORD dwNameLen;
27 DWORD lastError;
28 BOOL bResult;
29};
30
31struct testCase testCases[]=
32{
33 {0, TRUE, FALSE, "", 0, ERROR_SUCCESS, PASS},
34 {0, TRUE, FALSE, "", 5, ERROR_SUCCESS, PASS},
35 {0, TRUE, FALSE, "", 5, ERROR_ALREADY_EXISTS, PASS},
36 {0, TRUE, FALSE, "", 6, ERROR_INVALID_HANDLE, PASS},
37 {0, TRUE, FALSE, "", MAX_PATH - 1 - 60, ERROR_SUCCESS, PASS},
38 {0, TRUE, FALSE, "", MAX_PATH + 1, ERROR_FILENAME_EXCED_RANGE, PASS}
39};
40
41static HANDLE hEvent[sizeof(testCases)/sizeof(struct testCase)];
42
43DWORD result[sizeof(testCases)/sizeof(struct testCase)];
44
45int __cdecl main(int argc, char **argv)
46{
47
48 BOOL bRet = TRUE;
49 const char *nonEventName = "aaaaaa";
50 HANDLE hUnnamedEvent;
51 HANDLE hFMap;
52 DWORD dwRet;
53 int i;
54
55 if(0 != (PAL_Initialize(argc, argv)))
56 {
57 return ( FAIL );
58 }
59
60 hUnnamedEvent = CreateEventA(0, TRUE, FALSE, NULL);
61
62 if ( NULL == hUnnamedEvent )
63 {
64 bRet = FALSE;
65 Trace ( "PALSUITE ERROR: CreateEventA (%d, %d, %d, NULL) call "
66 "returned NULL.\nGetLastError returned %u.\n", 0, TRUE, FALSE,
67 GetLastError());
68 }
69
70 if (!CloseHandle(hUnnamedEvent))
71 {
72 bRet = FALSE;
73 Trace("PALSUITE ERROR: CreateEventA: CloseHandle(%lp); call "
74 "failed\nGetLastError returned '%u'.\n", hUnnamedEvent,
75 GetLastError());
76 }
77
78 /* Create non-event with the same name as one of the testCases */
79 hFMap = CreateFileMappingA( SWAPPTR, NULL, PAGE_READONLY, 0, 1,
80 nonEventName );
81
82 if ( NULL == hFMap )
83 {
84 bRet = FALSE;
85 Trace ( "PALSUITE ERROR: CreateFileMapping (%p, %p, %d, %d, %d, %s)"
86 " call returned NULL.\nGetLastError returned %u.\n",
87 SWAPPTR, NULL, PAGE_READONLY, 0, 0, nonEventName,
88 GetLastError());
89 goto done;
90 }
91
92 /* Create Events */
93 for (i = 0; i < sizeof(testCases)/sizeof(struct testCase); i++)
94 {
95 /* create name */
96 memset (testCases[i].lpName, '\0', (MAX_PATH + 2));
97 memset (testCases[i].lpName, 'a', testCases[i].dwNameLen );
98
99 SetLastError(ERROR_SUCCESS);
100
101 hEvent[i] = CreateEventA( testCases[i].lpEventAttributes,
102 testCases[i].bManualReset,
103 testCases[i].bInitialState,
104 testCases[i].lpName);
105
106 if (hEvent[i] != INVALID_HANDLE_VALUE)
107 {
108 DWORD dwError = GetLastError();
109
110 if (dwError != testCases[i].lastError)
111 {
112 bRet = FALSE;
113 Trace ("PALSUITE ERROR:\nCreateEventA(%lp, %d, %d, %s)"
114 "\nGetLastError returned '%u', it should have returned"
115 "'%d' at index '%d'.\n", testCases[i].lpEventAttributes,
116 testCases[i].bManualReset, testCases[i].bInitialState,
117 testCases[i].lpName, dwError,
118 testCases[i].lastError, i);
119 }
120 if ( ERROR_FILENAME_EXCED_RANGE == testCases[i].lastError )
121 {
122 result [i] = 1;
123 }
124 if ( ERROR_INVALID_HANDLE == testCases[i].lastError )
125 {
126 result [i] = 1;
127 }
128 /*
129 * If we expected the testcase to FAIL and it passed,
130 * report an error.
131 */
132 if (testCases[i].bResult == FAIL)
133 {
134 bRet = FALSE;
135 Trace ("PALSUITE ERROR:\nCreateEventA(%lp, %d, %d, %s)"
136 "\nShould have returned INVALID_HANDLE_VALUE but "
137 "didn't at index '%d'.\n",
138 testCases[i].lpEventAttributes,
139 testCases[i].bManualReset,
140 testCases[i].bInitialState,
141 testCases[i].lpName, i);
142 }
143 /*
144 * If result hasn't been set already set it to 0 so all the
145 * resources will be freed.
146 */
147 if (!result[i] )
148 {
149 result[i] = 0;
150 }
151 }
152 else
153 {
154 /*
155 * If we get an INVALID_HANDLE_VALUE and we expected the
156 * test case to pass, report an error.
157 */
158 result[i] = 1;
159
160 if (testCases[i].bResult == PASS)
161 {
162 bRet = FALSE;
163 Trace ("PALSUITE ERROR:\nCreateEventA(%lp, %d, %d, %s);"
164 "\nReturned INVALID_HANDLE_VALUE at index '%d'.\n",
165 testCases[i].lpEventAttributes,
166 testCases[i].bManualReset, testCases[i].bInitialState,
167 testCases[i].lpName, i);
168 }
169 }
170 }
171
172 /* cleanup */
173 for (i = 0; i < sizeof(testCases)/sizeof(struct testCase); i++)
174 {
175 if (result[i])
176 {
177 continue;
178 }
179 dwRet = WaitForSingleObject ( hEvent[i], 0 );
180
181 if (dwRet != WAIT_TIMEOUT)
182 {
183 bRet = FALSE;
184 Trace("PALSUITE ERROR: CreateEventA:\nWaitForSingleObject (%lp, "
185 "%d) call failed at index %d .\nGetLastError returned "
186 "'%u'.\n", hEvent[i], 0, i, GetLastError());
187 }
188
189 if (!CloseHandle(hEvent[i]))
190 {
191 bRet = FALSE;
192 Trace("PALSUITE ERROR: CreateEventA: CloseHandle(%lp) call "
193 "failed at index %d\nGetLastError returned '%u'.\n",
194 hEvent[i], i, GetLastError());
195 }
196 }
197
198done:
199 if (!CloseHandle(hFMap))
200 {
201 bRet = FALSE;
202 Trace("PALSUITE ERROR: CreateEventW: CloseHandle(%p) call "
203 "failed\nGetLastError returned '%u'.\n", hFMap,
204 GetLastError());
205 }
206
207 if (FALSE == bRet)
208 {
209 bRet = FAIL;
210 }
211 else
212 {
213 bRet = PASS;
214 }
215
216 PAL_TerminateEx(bRet);
217
218 return(bRet);
219}
220