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