2 * Synchronization tests
4 * Copyright 2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/test.h"
29 static void test_signalandwait(void)
31 DWORD (WINAPI *pSignalObjectAndWait)(HANDLE, HANDLE, DWORD, BOOL);
35 HANDLE event[2], maxevents[MAXIMUM_WAIT_OBJECTS], semaphore[2], file;
37 kernel32 = GetModuleHandle("kernel32");
38 pSignalObjectAndWait = (void*) GetProcAddress(kernel32, "SignalObjectAndWait");
40 if (!pSignalObjectAndWait)
43 /* invalid parameters */
44 r = pSignalObjectAndWait(NULL, NULL, 0, 0);
45 if (r == ERROR_INVALID_FUNCTION)
47 trace("SignalObjectAndWait not implemented, skipping tests\n");
48 return; /* Win98/ME */
50 ok( r == WAIT_FAILED, "should fail\n");
52 event[0] = CreateEvent(NULL, 0, 0, NULL);
53 event[1] = CreateEvent(NULL, 1, 1, NULL);
55 ok( event[0] && event[1], "failed to create event flags\n");
57 r = pSignalObjectAndWait(event[0], NULL, 0, FALSE);
58 ok( r == WAIT_FAILED, "should fail\n");
60 r = pSignalObjectAndWait(NULL, event[0], 0, FALSE);
61 ok( r == WAIT_FAILED, "should fail\n");
64 /* valid parameters */
65 r = pSignalObjectAndWait(event[0], event[1], 0, FALSE);
66 ok( r == WAIT_OBJECT_0, "should succeed\n");
68 /* event[0] is now signalled */
69 r = pSignalObjectAndWait(event[0], event[0], 0, FALSE);
70 ok( r == WAIT_OBJECT_0, "should succeed\n");
72 /* event[0] is not signalled */
73 r = WaitForSingleObject(event[0], 0);
74 ok( r == WAIT_TIMEOUT, "event was signalled\n");
76 r = pSignalObjectAndWait(event[0], event[0], 0, FALSE);
77 ok( r == WAIT_OBJECT_0, "should succeed\n");
79 /* clear event[1] and check for a timeout */
80 ok(ResetEvent(event[1]), "failed to clear event[1]\n");
81 r = pSignalObjectAndWait(event[0], event[1], 0, FALSE);
82 ok( r == WAIT_TIMEOUT, "should timeout\n");
84 CloseHandle(event[0]);
85 CloseHandle(event[1]);
87 /* create the maximum number of events and make sure
88 * we can wait on that many */
89 for (i=0; i<MAXIMUM_WAIT_OBJECTS; i++)
91 maxevents[i] = CreateEvent(NULL, 1, 1, NULL);
92 ok( maxevents[i] != 0, "should create enough events\n");
94 r = WaitForMultipleObjects(MAXIMUM_WAIT_OBJECTS, maxevents, 0, 0);
95 ok( r != WAIT_FAILED && r != WAIT_TIMEOUT, "should succeed\n");
97 for (i=0; i<MAXIMUM_WAIT_OBJECTS; i++)
98 if (maxevents[i]) CloseHandle(maxevents[i]);
101 semaphore[0] = CreateSemaphore( NULL, 0, 1, NULL );
102 semaphore[1] = CreateSemaphore( NULL, 1, 1, NULL );
103 ok( semaphore[0] && semaphore[1], "failed to create semaphore\n");
105 r = pSignalObjectAndWait(semaphore[0], semaphore[1], 0, FALSE);
106 ok( r == WAIT_OBJECT_0, "should succeed\n");
108 r = pSignalObjectAndWait(semaphore[0], semaphore[1], 0, FALSE);
109 ok( r == WAIT_FAILED, "should fail\n");
111 r = ReleaseSemaphore(semaphore[0],1,NULL);
112 ok( r == FALSE, "should fail\n");
114 r = ReleaseSemaphore(semaphore[1],1,NULL);
115 ok( r == TRUE, "should succeed\n");
117 CloseHandle(semaphore[0]);
118 CloseHandle(semaphore[1]);
120 /* try a registry key */
121 file = CreateFile("x", GENERIC_READ|GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
122 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL);
123 r = pSignalObjectAndWait(file, file, 0, FALSE);
124 ok( r == WAIT_FAILED, "should fail\n");
125 ok( ERROR_INVALID_HANDLE == GetLastError(), "should return invalid handle error\n");
129 static void test_mutex(void)
136 hCreated = CreateMutex(NULL, FALSE, "WineTestMutex");
137 ok(hCreated != NULL, "CreateMutex failed with error %d\n", GetLastError());
138 wait_ret = WaitForSingleObject(hCreated, INFINITE);
139 ok(wait_ret == WAIT_OBJECT_0, "WaitForSingleObject failed with error 0x%08x\n", wait_ret);
141 /* yes, opening with just READ_CONTROL access allows us to successfully
142 * call ReleaseMutex */
143 hOpened = OpenMutex(READ_CONTROL, FALSE, "WineTestMutex");
144 ok(hOpened != NULL, "OpenMutex failed with error %d\n", GetLastError());
145 ret = ReleaseMutex(hOpened);
146 todo_wine ok(ret, "ReleaseMutex failed with error %d\n", GetLastError());
147 ret = ReleaseMutex(hCreated);
148 todo_wine ok(!ret && (GetLastError() == ERROR_NOT_OWNER),
149 "ReleaseMutex should have failed with ERROR_NOT_OWNER instead of %d\n", GetLastError());
151 CloseHandle(hOpened);
152 CloseHandle(hCreated);
155 static void test_slist(void)
161 } item1, item2, item3, *pitem;
163 SLIST_HEADER slist_header, test_header;
167 VOID (WINAPI *pInitializeSListHead)(PSLIST_HEADER);
168 USHORT (WINAPI *pQueryDepthSList)(PSLIST_HEADER);
169 PSLIST_ENTRY (WINAPI *pInterlockedFlushSList)(PSLIST_HEADER);
170 PSLIST_ENTRY (WINAPI *pInterlockedPopEntrySList)(PSLIST_HEADER);
171 PSLIST_ENTRY (WINAPI *pInterlockedPushEntrySList)(PSLIST_HEADER,PSLIST_ENTRY);
174 kernel32 = GetModuleHandle("KERNEL32.DLL");
175 pInitializeSListHead = (void*) GetProcAddress(kernel32, "InitializeSListHead");
176 pQueryDepthSList = (void*) GetProcAddress(kernel32, "QueryDepthSList");
177 pInterlockedFlushSList = (void*) GetProcAddress(kernel32, "InterlockedFlushSList");
178 pInterlockedPopEntrySList = (void*) GetProcAddress(kernel32, "InterlockedPopEntrySList");
179 pInterlockedPushEntrySList = (void*) GetProcAddress(kernel32, "InterlockedPushEntrySList");
180 if (pInitializeSListHead == NULL ||
181 pQueryDepthSList == NULL ||
182 pInterlockedFlushSList == NULL ||
183 pInterlockedPopEntrySList == NULL ||
184 pInterlockedPushEntrySList == NULL)
186 skip("some required slist entrypoints were not found, skipping tests\n");
190 memset(&test_header, 0, sizeof(test_header));
191 memset(&slist_header, 0xFF, sizeof(slist_header));
192 pInitializeSListHead(&slist_header);
193 ok(memcmp(&test_header, &slist_header, sizeof(SLIST_HEADER)) == 0,
194 "InitializeSListHead didn't zero-fill list header\n");
195 size = pQueryDepthSList(&slist_header);
196 ok(size == 0, "initially created slist has size %d, expected 0\n", size);
199 ok(pInterlockedPushEntrySList(&slist_header, &item1.entry) == NULL,
200 "previous entry in empty slist wasn't NULL\n");
201 size = pQueryDepthSList(&slist_header);
202 ok(size == 1, "slist with 1 item has size %d\n", size);
205 entry = pInterlockedPushEntrySList(&slist_header, &item2.entry);
206 ok(entry != NULL, "previous entry in non-empty slist was NULL\n");
209 pitem = (struct item*) entry;
210 ok(pitem->value == 1, "previous entry in slist wasn't the one added\n");
212 size = pQueryDepthSList(&slist_header);
213 ok(size == 2, "slist with 2 items has size %d\n", size);
216 entry = pInterlockedPushEntrySList(&slist_header, &item3.entry);
217 ok(entry != NULL, "previous entry in non-empty slist was NULL\n");
220 pitem = (struct item*) entry;
221 ok(pitem->value == 2, "previous entry in slist wasn't the one added\n");
223 size = pQueryDepthSList(&slist_header);
224 ok(size == 3, "slist with 3 items has size %d\n", size);
226 entry = pInterlockedPopEntrySList(&slist_header);
227 ok(entry != NULL, "entry shouldn't be NULL\n");
230 pitem = (struct item*) entry;
231 ok(pitem->value == 3, "unexpected entry removed\n");
233 size = pQueryDepthSList(&slist_header);
234 ok(size == 2, "slist with 2 items has size %d\n", size);
236 entry = pInterlockedFlushSList(&slist_header);
237 size = pQueryDepthSList(&slist_header);
238 ok(size == 0, "flushed slist should be empty, size is %d\n", size);
241 ok(pInterlockedPopEntrySList(&slist_header) == NULL,
242 "popping empty slist didn't return NULL\n");
244 ok(((struct item*)entry)->value == 2, "item 2 not in front of list\n");
245 ok(((struct item*)entry->Next)->value == 1, "item 1 not at the back of list\n");
248 static void test_event_security(void)
251 SECURITY_ATTRIBUTES sa;
252 SECURITY_DESCRIPTOR sd;
256 handle = CreateEventA(NULL, FALSE, FALSE, __FILE__ ": Test Event");
257 ok(handle != NULL, "CreateEventW with blank sd failed with error %d\n", GetLastError());
260 sa.nLength = sizeof(sa);
261 sa.lpSecurityDescriptor = &sd;
262 sa.bInheritHandle = FALSE;
264 InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
267 handle = CreateEventA(&sa, FALSE, FALSE, __FILE__ ": Test Event");
268 ok(handle != NULL, "CreateEventW with blank sd failed with error %d\n", GetLastError());
271 /* sd with NULL dacl */
272 SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE);
273 handle = CreateEventA(&sa, FALSE, FALSE, __FILE__ ": Test Event");
274 ok(handle != NULL, "CreateEventW with blank sd failed with error %d\n", GetLastError());
277 /* sd with empty dacl */
278 InitializeAcl(&acl, sizeof(acl), ACL_REVISION);
279 SetSecurityDescriptorDacl(&sd, TRUE, &acl, FALSE);
280 handle = CreateEventA(&sa, FALSE, FALSE, __FILE__ ": Test Event");
281 ok(handle != NULL, "CreateEventW with blank sd failed with error %d\n", GetLastError());
285 static HANDLE sem = 0;
287 static void CALLBACK iocp_callback(DWORD dwErrorCode, DWORD dwNumberOfBytesTransferred, LPOVERLAPPED lpOverlapped)
289 ReleaseSemaphore(sem, 1, NULL);
292 static BOOL WINAPI (*p_BindIoCompletionCallback)( HANDLE FileHandle, LPOVERLAPPED_COMPLETION_ROUTINE Function, ULONG Flags) = NULL;
294 static void test_iocp_callback(void)
296 char temp_path[MAX_PATH];
297 char filename[MAX_PATH];
300 static const char prefix[] = "pfx";
302 HMODULE hmod = GetModuleHandleA("kernel32.dll");
304 const char *buffer = "12345678123456781234567812345678";
305 OVERLAPPED overlapped;
307 p_BindIoCompletionCallback = (void*)GetProcAddress(hmod, "BindIoCompletionCallback");
308 if(!p_BindIoCompletionCallback) {
309 skip("BindIoCompletionCallback not found in this DLL\n");
313 sem = CreateSemaphore(NULL, 0, 1, NULL);
314 ok(sem != INVALID_HANDLE_VALUE, "Creating a semaphore failed\n");
316 ret = GetTempPathA(MAX_PATH, temp_path);
317 ok(ret != 0, "GetTempPathA error %d\n", GetLastError());
318 ok(ret < MAX_PATH, "temp path should fit into MAX_PATH\n");
320 ret = GetTempFileNameA(temp_path, prefix, 0, filename);
321 ok(ret != 0, "GetTempFileNameA error %d\n", GetLastError());
323 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
324 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS, 0);
325 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
327 retb = p_BindIoCompletionCallback(hFile, iocp_callback, 0);
328 ok(retb == FALSE, "BindIoCompletionCallback succeeded on a file that wasn't created with FILE_FLAG_OVERLAPPED\n");
329 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Last error is %d\n", GetLastError());
331 ret = CloseHandle(hFile);
332 ok( ret, "CloseHandle: error %d\n", GetLastError());
333 ret = DeleteFileA(filename);
334 ok( ret, "DeleteFileA: error %d\n", GetLastError());
336 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
337 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS | FILE_FLAG_OVERLAPPED, 0);
338 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
340 retb = p_BindIoCompletionCallback(hFile, iocp_callback, 0);
341 ok(retb == TRUE, "BindIoCompletionCallback failed\n");
343 memset(&overlapped, 0, sizeof(overlapped));
344 retb = WriteFile(hFile, (const void *) buffer, 4, &bytesWritten, &overlapped);
345 ok(retb == TRUE || GetLastError() == ERROR_IO_PENDING, "WriteFile failed, lastError = %d\n", GetLastError());
347 ret = WaitForSingleObject(sem, 5000);
348 ok(ret == WAIT_OBJECT_0, "Wait for the IO completion callback failed\n");
351 retb = p_BindIoCompletionCallback(hFile, iocp_callback, 0);
352 ok(retb == FALSE, "BindIoCompletionCallback succeeded when setting the same callback on the file again\n");
353 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Last error is %d\n", GetLastError());
354 retb = p_BindIoCompletionCallback(hFile, NULL, 0);
355 ok(retb == FALSE, "BindIoCompletionCallback succeeded when setting the callback to NULL\n");
356 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Last error is %d\n", GetLastError());
358 ret = CloseHandle(hFile);
359 ok( ret, "CloseHandle: error %d\n", GetLastError());
360 ret = DeleteFileA(filename);
361 ok( ret, "DeleteFileA: error %d\n", GetLastError());
363 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
364 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS | FILE_FLAG_OVERLAPPED, 0);
365 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
366 retb = p_BindIoCompletionCallback(hFile, NULL, 0);
367 ok(retb == TRUE, "BindIoCompletionCallback failed with a NULL callback(first time set)\n");
368 ret = CloseHandle(hFile);
369 ok( ret, "CloseHandle: error %d\n", GetLastError());
370 ret = DeleteFileA(filename);
371 ok( ret, "DeleteFileA: error %d\n", GetLastError());
373 hFile = CreateFileA(filename, GENERIC_READ | GENERIC_WRITE, 0, NULL,
374 CREATE_ALWAYS, FILE_FLAG_RANDOM_ACCESS | FILE_FLAG_OVERLAPPED, 0);
375 ok(hFile != INVALID_HANDLE_VALUE, "CreateFileA: error %d\n", GetLastError());
376 retb = p_BindIoCompletionCallback(hFile, iocp_callback, 12345);
377 ok(retb == TRUE, "BindIoCompletionCallback failed with Flags != 0\n");
378 ret = CloseHandle(hFile);
379 ok( ret, "CloseHandle: error %d\n", GetLastError());
380 ret = DeleteFileA(filename);
381 ok( ret, "DeleteFileA: error %d\n", GetLastError());
383 retb = p_BindIoCompletionCallback(NULL, iocp_callback, 0);
384 ok(retb == FALSE, "BindIoCompletionCallback succeeded on a NULL file\n");
385 ok(GetLastError() == ERROR_INVALID_HANDLE, "Last error is %d\n", GetLastError());
390 test_signalandwait();
393 test_event_security();
394 test_iocp_callback();