2 * Unit tests for Event Logging functions
4 * Copyright (c) 2009 Paul Vriens
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
28 #include "wine/test.h"
30 static BOOL (WINAPI *pGetEventLogInformation)(HANDLE,DWORD,LPVOID,DWORD,LPDWORD);
32 static void init_function_pointers(void)
34 HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
36 pGetEventLogInformation = (void*)GetProcAddress(hadvapi32, "GetEventLogInformation");
39 static void test_open_close(void)
44 SetLastError(0xdeadbeef);
45 ret = CloseEventLog(NULL);
46 ok(!ret, "Expected failure\n");
47 ok(GetLastError() == ERROR_INVALID_HANDLE ||
48 GetLastError() == ERROR_NOACCESS, /* W2K */
49 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
51 SetLastError(0xdeadbeef);
52 handle = OpenEventLogA(NULL, NULL);
53 ok(handle == NULL, "Didn't expect a handle\n");
54 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
56 SetLastError(0xdeadbeef);
57 handle = OpenEventLogA("IDontExist", NULL);
58 ok(handle == NULL, "Didn't expect a handle\n");
59 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
61 SetLastError(0xdeadbeef);
62 handle = OpenEventLogA("IDontExist", "deadbeef");
63 ok(handle == NULL, "Didn't expect a handle\n");
64 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
65 GetLastError() == RPC_S_INVALID_NET_ADDR, /* Some Vista and Win7 */
66 "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError());
68 /* This one opens the Application log */
69 handle = OpenEventLogA(NULL, "deadbeef");
70 ok(handle != NULL, "Expected a handle\n");
71 ret = CloseEventLog(handle);
72 ok(ret, "Expected success\n");
73 /* Close a second time */
74 SetLastError(0xdeadbeef);
75 ret = CloseEventLog(handle);
78 ok(!ret, "Expected failure\n");
79 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
82 /* Empty servername should be read as local server */
83 handle = OpenEventLogA("", "Application");
84 ok(handle != NULL, "Expected a handle\n");
85 CloseEventLog(handle);
87 handle = OpenEventLogA(NULL, "Application");
88 ok(handle != NULL, "Expected a handle\n");
89 CloseEventLog(handle);
92 static void test_info(void)
97 EVENTLOG_FULL_INFORMATION efi;
99 if (!pGetEventLogInformation)
102 win_skip("GetEventLogInformation is not available\n");
105 SetLastError(0xdeadbeef);
106 ret = pGetEventLogInformation(NULL, 1, NULL, 0, NULL);
107 ok(!ret, "Expected failure\n");
108 ok(GetLastError() == ERROR_INVALID_LEVEL, "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
110 SetLastError(0xdeadbeef);
111 ret = pGetEventLogInformation(NULL, EVENTLOG_FULL_INFO, NULL, 0, NULL);
112 ok(!ret, "Expected failure\n");
113 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
115 handle = OpenEventLogA(NULL, "Application");
117 SetLastError(0xdeadbeef);
118 ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, NULL, 0, NULL);
119 ok(!ret, "Expected failure\n");
120 ok(GetLastError() == RPC_X_NULL_REF_POINTER, "Expected RPC_X_NULL_REF_POINTER, got %d\n", GetLastError());
122 SetLastError(0xdeadbeef);
123 ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, NULL, 0, &needed);
124 ok(!ret, "Expected failure\n");
125 ok(GetLastError() == RPC_X_NULL_REF_POINTER, "Expected RPC_X_NULL_REF_POINTER, got %d\n", GetLastError());
127 SetLastError(0xdeadbeef);
128 ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, (LPVOID)&efi, 0, NULL);
129 ok(!ret, "Expected failure\n");
130 ok(GetLastError() == RPC_X_NULL_REF_POINTER, "Expected RPC_X_NULL_REF_POINTER, got %d\n", GetLastError());
132 SetLastError(0xdeadbeef);
134 efi.dwFull = 0xdeadbeef;
135 ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, (LPVOID)&efi, 0, &needed);
136 ok(!ret, "Expected failure\n");
137 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
138 ok(needed == sizeof(EVENTLOG_FULL_INFORMATION), "Expected sizeof(EVENTLOG_FULL_INFORMATION), got %d\n", needed);
139 ok(efi.dwFull == 0xdeadbeef, "Expected no change to the dwFull member\n");
141 /* Not that we care, but on success last error is set to ERROR_IO_PENDING */
142 efi.dwFull = 0xdeadbeef;
144 ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, (LPVOID)&efi, needed, &needed);
145 ok(ret, "Expected succes\n");
146 ok(needed == sizeof(EVENTLOG_FULL_INFORMATION), "Expected sizeof(EVENTLOG_FULL_INFORMATION), got %d\n", needed);
147 ok(efi.dwFull == 0 || efi.dwFull == 1, "Expected 0 (not full) or 1 (full), got %d\n", efi.dwFull);
149 CloseEventLog(handle);
152 static void test_count(void)
158 SetLastError(0xdeadbeef);
159 ret = GetNumberOfEventLogRecords(NULL, NULL);
160 ok(!ret, "Expected failure\n");
161 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
163 SetLastError(0xdeadbeef);
165 ret = GetNumberOfEventLogRecords(NULL, &count);
166 ok(!ret, "Expected failure\n");
167 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
168 ok(count == 0xdeadbeef, "Expected count to stay unchanged\n");
170 handle = OpenEventLogA(NULL, "Application");
172 SetLastError(0xdeadbeef);
173 ret = GetNumberOfEventLogRecords(handle, NULL);
174 ok(!ret, "Expected failure\n");
175 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
178 ret = GetNumberOfEventLogRecords(handle, &count);
179 ok(ret, "Expected succes\n");
180 ok(count != 0xdeadbeef, "Expected the number of records\n");
182 CloseEventLog(handle);
185 static void test_oldest(void)
191 SetLastError(0xdeadbeef);
192 ret = GetOldestEventLogRecord(NULL, NULL);
193 ok(!ret, "Expected failure\n");
194 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
196 SetLastError(0xdeadbeef);
198 ret = GetOldestEventLogRecord(NULL, &oldest);
199 ok(!ret, "Expected failure\n");
200 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
201 ok(oldest == 0xdeadbeef, "Expected oldest to stay unchanged\n");
203 handle = OpenEventLogA(NULL, "Application");
205 SetLastError(0xdeadbeef);
206 ret = GetOldestEventLogRecord(handle, NULL);
207 ok(!ret, "Expected failure\n");
208 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
211 ret = GetOldestEventLogRecord(handle, &oldest);
212 ok(ret, "Expected succes\n");
213 ok(oldest != 0xdeadbeef, "Expected the number of the oldest record\n");
215 CloseEventLog(handle);
218 static void test_backup(void)
222 const char backup[] = "backup.evt";
224 SetLastError(0xdeadbeef);
225 ret = BackupEventLogA(NULL, NULL);
226 ok(!ret, "Expected failure\n");
227 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
229 SetLastError(0xdeadbeef);
230 ret = BackupEventLogA(NULL, backup);
231 ok(!ret, "Expected failure\n");
232 ok(GetFileAttributesA(backup) == INVALID_FILE_ATTRIBUTES, "Expected no backup file\n");
234 handle = OpenEventLogA(NULL, "Application");
236 SetLastError(0xdeadbeef);
237 ret = BackupEventLogA(handle, NULL);
238 ok(!ret, "Expected failure\n");
239 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
241 ret = BackupEventLogA(handle, backup);
242 ok(ret, "Expected succes\n");
244 ok(GetFileAttributesA("backup.evt") != INVALID_FILE_ATTRIBUTES, "Expected a backup file\n");
246 /* Try to overwrite */
247 SetLastError(0xdeadbeef);
248 ret = BackupEventLogA(handle, backup);
251 ok(!ret, "Expected failure\n");
252 ok(GetLastError() == ERROR_ALREADY_EXISTS, "Expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
255 CloseEventLog(handle);
259 static void test_read(void)
263 DWORD count, toread, read, needed;
266 SetLastError(0xdeadbeef);
267 ret = ReadEventLogA(NULL, 0, 0, NULL, 0, NULL, NULL);
268 ok(!ret, "Expected failure\n");
270 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
273 SetLastError(0xdeadbeef);
274 ret = ReadEventLogA(NULL, 0, 0, NULL, 0, &read, NULL);
275 ok(!ret, "Expected failure\n");
276 ok(read == 0xdeadbeef, "Expected 'read' parameter to remain unchanged\n");
278 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
281 SetLastError(0xdeadbeef);
282 ret = ReadEventLogA(NULL, 0, 0, NULL, 0, NULL, &needed);
283 ok(!ret, "Expected failure\n");
284 ok(needed == 0xdeadbeef, "Expected 'needed' parameter to remain unchanged\n");
286 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
288 /* 'read' and 'needed' are only filled when the needed buffer size is passed back or when the call succeeds */
289 SetLastError(0xdeadbeef);
290 ret = ReadEventLogA(NULL, 0, 0, NULL, 0, &read, &needed);
291 ok(!ret, "Expected failure\n");
293 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
295 SetLastError(0xdeadbeef);
296 ret = ReadEventLogA(NULL, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ, 0, NULL, 0, NULL, NULL);
297 ok(!ret, "Expected failure\n");
299 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
301 SetLastError(0xdeadbeef);
302 ret = ReadEventLogA(NULL, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ, 0, NULL, 0, &read, &needed);
303 ok(!ret, "Expected failure\n");
305 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
308 SetLastError(0xdeadbeef);
309 ret = ReadEventLogA(NULL, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
310 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
311 ok(!ret, "Expected failure\n");
313 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
315 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD));
316 SetLastError(0xdeadbeef);
317 ret = ReadEventLogA(NULL, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
318 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
319 ok(!ret, "Expected failure\n");
321 ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
322 HeapFree(GetProcessHeap(), 0, buf);
324 handle = OpenEventLogA(NULL, "Application");
326 /* Show that we need the proper dwFlags with a (for the rest) proper call */
327 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD));
329 SetLastError(0xdeadbeef);
330 ret = ReadEventLogA(handle, 0, 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
331 ok(!ret, "Expected failure\n");
333 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
335 SetLastError(0xdeadbeef);
336 ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ, 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
337 ok(!ret, "Expected failure\n");
339 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
341 SetLastError(0xdeadbeef);
342 ret = ReadEventLogA(handle, EVENTLOG_SEEK_READ, 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
343 ok(!ret, "Expected failure\n");
345 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
347 SetLastError(0xdeadbeef);
348 ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ | EVENTLOG_BACKWARDS_READ,
349 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
350 ok(!ret, "Expected failure\n");
352 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
354 SetLastError(0xdeadbeef);
355 ret = ReadEventLogA(handle, EVENTLOG_SEEK_READ | EVENTLOG_FORWARDS_READ | EVENTLOG_BACKWARDS_READ,
356 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
357 ok(!ret, "Expected failure\n");
359 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
361 SetLastError(0xdeadbeef);
362 ret = ReadEventLogA(handle, EVENTLOG_SEEK_READ | EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
363 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
364 ok(!ret, "Expected failure\n");
366 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
368 HeapFree(GetProcessHeap(), 0, buf);
370 /* First check if there are any records (in practice only on Wine: FIXME) */
372 GetNumberOfEventLogRecords(handle, &count);
375 skip("No records in the 'Application' log\n");
376 CloseEventLog(handle);
380 /* Get the buffer size for the first record */
381 buf = HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD));
382 read = needed = 0xdeadbeef;
383 SetLastError(0xdeadbeef);
384 ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
385 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
386 ok(!ret, "Expected failure\n");
387 ok(read == 0, "Expected no bytes read\n");
388 ok(needed > sizeof(EVENTLOGRECORD), "Expected the needed buffersize to be bigger than sizeof(EVENTLOGRECORD)\n");
389 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
391 /* Read the first record */
393 buf = HeapReAlloc(GetProcessHeap(), 0, buf, toread);
394 read = needed = 0xdeadbeef;
395 SetLastError(0xdeadbeef);
396 ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ, 0, buf, toread, &read, &needed);
397 ok(ret, "Expected succes\n");
399 broken(read < toread), /* NT4 wants a buffer size way bigger than just 1 record */
400 "Expected the requested size to be read\n");
401 ok(needed == 0, "Expected no extra bytes to be read\n");
402 HeapFree(GetProcessHeap(), 0, buf);
404 CloseEventLog(handle);
407 static void test_openbackup(void)
409 HANDLE handle, handle2, file;
411 const char backup[] = "backup.evt";
412 const char text[] = "Just some text";
414 SetLastError(0xdeadbeef);
415 handle = OpenBackupEventLogA(NULL, NULL);
418 ok(handle == NULL, "Didn't expect a handle\n");
419 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
422 SetLastError(0xdeadbeef);
423 handle = OpenBackupEventLogA(NULL, "idontexist.evt");
426 ok(handle == NULL, "Didn't expect a handle\n");
427 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
430 SetLastError(0xdeadbeef);
431 handle = OpenBackupEventLogA("IDontExist", NULL);
434 ok(handle == NULL, "Didn't expect a handle\n");
435 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
438 SetLastError(0xdeadbeef);
439 handle = OpenBackupEventLogA("IDontExist", "idontexist.evt");
442 ok(handle == NULL, "Didn't expect a handle\n");
443 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
444 GetLastError() == RPC_S_INVALID_NET_ADDR, /* Some Vista and Win7 */
445 "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError());
448 /* Make a backup eventlog to work with */
450 handle = OpenEventLogA(NULL, "Application");
451 BackupEventLogA(handle, backup);
452 CloseEventLog(handle);
454 /* FIXME: Wine stops here */
455 if (GetFileAttributesA(backup) == INVALID_FILE_ATTRIBUTES)
457 skip("We don't have a backup eventlog to work with\n");
461 SetLastError(0xdeadbeef);
462 handle = OpenBackupEventLogA("IDontExist", backup);
463 ok(handle == NULL, "Didn't expect a handle\n");
464 ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
465 GetLastError() == RPC_S_INVALID_NET_ADDR, /* Some Vista and Win7 */
466 "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError());
468 /* Empty servername should be read as local server */
469 handle = OpenBackupEventLogA("", backup);
470 ok(handle != NULL, "Expected a handle\n");
471 CloseEventLog(handle);
473 handle = OpenBackupEventLogA(NULL, backup);
474 ok(handle != NULL, "Expected a handle\n");
476 /* Can we open that same backup eventlog more than once? */
477 handle2 = OpenBackupEventLogA(NULL, backup);
478 ok(handle2 != NULL, "Expected a handle\n");
479 ok(handle2 != handle, "Didn't expect the same handle\n");
480 CloseEventLog(handle2);
482 CloseEventLog(handle);
485 /* Is there any content checking done? */
486 file = CreateFileA(backup, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
488 SetLastError(0xdeadbeef);
489 handle = OpenBackupEventLogA(NULL, backup);
490 ok(handle == NULL, "Didn't expect a handle\n");
491 ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY ||
492 GetLastError() == ERROR_EVENTLOG_FILE_CORRUPT, /* Vista and Win7 */
493 "Expected ERROR_NOT_ENOUGH_MEMORY, got %d\n", GetLastError());
494 CloseEventLog(handle);
497 file = CreateFileA(backup, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
498 WriteFile(file, text, sizeof(text), &written, NULL);
500 SetLastError(0xdeadbeef);
501 handle = OpenBackupEventLogA(NULL, backup);
502 ok(handle == NULL, "Didn't expect a handle\n");
503 ok(GetLastError() == ERROR_EVENTLOG_FILE_CORRUPT, "Expected ERROR_EVENTLOG_FILE_CORRUPT, got %d\n", GetLastError());
504 CloseEventLog(handle);
510 SetLastError(0xdeadbeef);
512 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
514 win_skip("Event log functions are not implemented\n");
518 init_function_pointers();
520 /* Parameters only */