user32/tests: Don't cast NULL to an integer type.
[wine] / dlls / advapi32 / tests / eventlog.c
1 /*
2  * Unit tests for Event Logging functions
3  *
4  * Copyright (c) 2009 Paul Vriens
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "winnt.h"
27 #include "winreg.h"
28 #include "sddl.h"
29
30 #include "wine/test.h"
31
32 static BOOL (WINAPI *pCreateWellKnownSid)(WELL_KNOWN_SID_TYPE,PSID,PSID,DWORD*);
33 static BOOL (WINAPI *pGetEventLogInformation)(HANDLE,DWORD,LPVOID,DWORD,LPDWORD);
34
35 static BOOL (WINAPI *pGetComputerNameExA)(COMPUTER_NAME_FORMAT,LPSTR,LPDWORD);
36 static BOOL (WINAPI *pWow64DisableWow64FsRedirection)(PVOID *);
37 static BOOL (WINAPI *pWow64RevertWow64FsRedirection)(PVOID);
38
39 static void init_function_pointers(void)
40 {
41     HMODULE hadvapi32 = GetModuleHandleA("advapi32.dll");
42     HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
43
44     pCreateWellKnownSid = (void*)GetProcAddress(hadvapi32, "CreateWellKnownSid");
45     pGetEventLogInformation = (void*)GetProcAddress(hadvapi32, "GetEventLogInformation");
46
47     pGetComputerNameExA = (void*)GetProcAddress(hkernel32, "GetComputerNameExA");
48     pWow64DisableWow64FsRedirection = (void*)GetProcAddress(hkernel32, "Wow64DisableWow64FsRedirection");
49     pWow64RevertWow64FsRedirection = (void*)GetProcAddress(hkernel32, "Wow64RevertWow64FsRedirection");
50 }
51
52 static void create_backup(const char *filename)
53 {
54     HANDLE handle;
55
56     DeleteFileA(filename);
57     handle = OpenEventLogA(NULL, "Application");
58     BackupEventLogA(handle, filename);
59     CloseEventLog(handle);
60
61     todo_wine
62     ok(GetFileAttributesA(filename) != INVALID_FILE_ATTRIBUTES, "Expected a backup file\n");
63 }
64
65 static void test_open_close(void)
66 {
67     HANDLE handle;
68     BOOL ret;
69
70     SetLastError(0xdeadbeef);
71     ret = CloseEventLog(NULL);
72     ok(!ret, "Expected failure\n");
73     ok(GetLastError() == ERROR_INVALID_HANDLE ||
74        GetLastError() == ERROR_NOACCESS, /* W2K */
75        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
76
77     SetLastError(0xdeadbeef);
78     handle = OpenEventLogA(NULL, NULL);
79     ok(handle == NULL, "Didn't expect a handle\n");
80     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
81
82     SetLastError(0xdeadbeef);
83     handle = OpenEventLogA("IDontExist", NULL);
84     ok(handle == NULL, "Didn't expect a handle\n");
85     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
86
87     SetLastError(0xdeadbeef);
88     handle = OpenEventLogA("IDontExist", "deadbeef");
89     ok(handle == NULL, "Didn't expect a handle\n");
90     ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
91        GetLastError() == RPC_S_INVALID_NET_ADDR, /* Some Vista and Win7 */
92        "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError());
93
94     /* This one opens the Application log */
95     handle = OpenEventLogA(NULL, "deadbeef");
96     ok(handle != NULL, "Expected a handle\n");
97     ret = CloseEventLog(handle);
98     ok(ret, "Expected success\n");
99     /* Close a second time */
100     SetLastError(0xdeadbeef);
101     ret = CloseEventLog(handle);
102     todo_wine
103     {
104     ok(!ret, "Expected failure\n");
105     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
106     }
107
108     /* Empty servername should be read as local server */
109     handle = OpenEventLogA("", "Application");
110     ok(handle != NULL, "Expected a handle\n");
111     CloseEventLog(handle);
112
113     handle = OpenEventLogA(NULL, "Application");
114     ok(handle != NULL, "Expected a handle\n");
115     CloseEventLog(handle);
116 }
117
118 static void test_info(void)
119 {
120     HANDLE handle;
121     BOOL ret;
122     DWORD needed;
123     EVENTLOG_FULL_INFORMATION efi;
124
125     if (!pGetEventLogInformation)
126     {
127         /* NT4 */
128         win_skip("GetEventLogInformation is not available\n");
129         return;
130     }
131     SetLastError(0xdeadbeef);
132     ret = pGetEventLogInformation(NULL, 1, NULL, 0, NULL);
133     ok(!ret, "Expected failure\n");
134     ok(GetLastError() == ERROR_INVALID_LEVEL, "Expected ERROR_INVALID_LEVEL, got %d\n", GetLastError());
135
136     SetLastError(0xdeadbeef);
137     ret = pGetEventLogInformation(NULL, EVENTLOG_FULL_INFO, NULL, 0, NULL);
138     ok(!ret, "Expected failure\n");
139     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
140
141     handle = OpenEventLogA(NULL, "Application");
142
143     SetLastError(0xdeadbeef);
144     ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, NULL, 0, NULL);
145     ok(!ret, "Expected failure\n");
146     ok(GetLastError() == RPC_X_NULL_REF_POINTER, "Expected RPC_X_NULL_REF_POINTER, got %d\n", GetLastError());
147
148     SetLastError(0xdeadbeef);
149     ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, NULL, 0, &needed);
150     ok(!ret, "Expected failure\n");
151     ok(GetLastError() == RPC_X_NULL_REF_POINTER, "Expected RPC_X_NULL_REF_POINTER, got %d\n", GetLastError());
152
153     SetLastError(0xdeadbeef);
154     ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, (LPVOID)&efi, 0, NULL);
155     ok(!ret, "Expected failure\n");
156     ok(GetLastError() == RPC_X_NULL_REF_POINTER, "Expected RPC_X_NULL_REF_POINTER, got %d\n", GetLastError());
157
158     SetLastError(0xdeadbeef);
159     needed = 0xdeadbeef;
160     efi.dwFull = 0xdeadbeef;
161     ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, (LPVOID)&efi, 0, &needed);
162     ok(!ret, "Expected failure\n");
163     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
164     ok(needed == sizeof(EVENTLOG_FULL_INFORMATION), "Expected sizeof(EVENTLOG_FULL_INFORMATION), got %d\n", needed);
165     ok(efi.dwFull == 0xdeadbeef, "Expected no change to the dwFull member\n");
166
167     /* Not that we care, but on success last error is set to ERROR_IO_PENDING */
168     efi.dwFull = 0xdeadbeef;
169     needed *= 2;
170     ret = pGetEventLogInformation(handle, EVENTLOG_FULL_INFO, (LPVOID)&efi, needed, &needed);
171     ok(ret, "Expected success\n");
172     ok(needed == sizeof(EVENTLOG_FULL_INFORMATION), "Expected sizeof(EVENTLOG_FULL_INFORMATION), got %d\n", needed);
173     ok(efi.dwFull == 0 || efi.dwFull == 1, "Expected 0 (not full) or 1 (full), got %d\n", efi.dwFull);
174
175     CloseEventLog(handle);
176 }
177
178 static void test_count(void)
179 {
180     HANDLE handle;
181     BOOL ret;
182     DWORD count;
183     const char backup[] = "backup.evt";
184
185     SetLastError(0xdeadbeef);
186     ret = GetNumberOfEventLogRecords(NULL, NULL);
187     ok(!ret, "Expected failure\n");
188     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
189
190     SetLastError(0xdeadbeef);
191     count = 0xdeadbeef;
192     ret = GetNumberOfEventLogRecords(NULL, &count);
193     ok(!ret, "Expected failure\n");
194     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
195     ok(count == 0xdeadbeef, "Expected count to stay unchanged\n");
196
197     handle = OpenEventLogA(NULL, "Application");
198
199     SetLastError(0xdeadbeef);
200     ret = GetNumberOfEventLogRecords(handle, NULL);
201     ok(!ret, "Expected failure\n");
202     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
203
204     count = 0xdeadbeef;
205     ret = GetNumberOfEventLogRecords(handle, &count);
206     ok(ret, "Expected success\n");
207     ok(count != 0xdeadbeef, "Expected the number of records\n");
208
209     CloseEventLog(handle);
210
211     /* Make a backup eventlog to work with */
212     create_backup(backup);
213
214     handle = OpenBackupEventLogA(NULL, backup);
215     todo_wine
216     ok(handle != NULL, "Expected a handle\n");
217
218     /* Does GetNumberOfEventLogRecords work with backup eventlogs? */
219     count = 0xdeadbeef;
220     ret = GetNumberOfEventLogRecords(handle, &count);
221     todo_wine
222     {
223     ok(ret, "Expected success\n");
224     ok(count != 0xdeadbeef, "Expected the number of records\n");
225     }
226
227     CloseEventLog(handle);
228     DeleteFileA(backup);
229 }
230
231 static void test_oldest(void)
232 {
233     HANDLE handle;
234     BOOL ret;
235     DWORD oldest;
236     const char backup[] = "backup.evt";
237
238     SetLastError(0xdeadbeef);
239     ret = GetOldestEventLogRecord(NULL, NULL);
240     ok(!ret, "Expected failure\n");
241     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
242
243     SetLastError(0xdeadbeef);
244     oldest = 0xdeadbeef;
245     ret = GetOldestEventLogRecord(NULL, &oldest);
246     ok(!ret, "Expected failure\n");
247     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
248     ok(oldest == 0xdeadbeef, "Expected oldest to stay unchanged\n");
249
250     handle = OpenEventLogA(NULL, "Application");
251
252     SetLastError(0xdeadbeef);
253     ret = GetOldestEventLogRecord(handle, NULL);
254     ok(!ret, "Expected failure\n");
255     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
256
257     oldest = 0xdeadbeef;
258     ret = GetOldestEventLogRecord(handle, &oldest);
259     ok(ret, "Expected success\n");
260     ok(oldest != 0xdeadbeef, "Expected the number of the oldest record\n");
261
262     CloseEventLog(handle);
263
264     /* Make a backup eventlog to work with */
265     create_backup(backup);
266
267     handle = OpenBackupEventLogA(NULL, backup);
268     todo_wine
269     ok(handle != NULL, "Expected a handle\n");
270
271     /* Does GetOldestEventLogRecord work with backup eventlogs? */
272     oldest = 0xdeadbeef;
273     ret = GetOldestEventLogRecord(handle, &oldest);
274     todo_wine
275     {
276     ok(ret, "Expected success\n");
277     ok(oldest != 0xdeadbeef, "Expected the number of the oldest record\n");
278     }
279
280     CloseEventLog(handle);
281     DeleteFileA(backup);
282 }
283
284 static void test_backup(void)
285 {
286     HANDLE handle;
287     BOOL ret;
288     const char backup[] = "backup.evt";
289     const char backup2[] = "backup2.evt";
290
291     SetLastError(0xdeadbeef);
292     ret = BackupEventLogA(NULL, NULL);
293     ok(!ret, "Expected failure\n");
294     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
295
296     SetLastError(0xdeadbeef);
297     ret = BackupEventLogA(NULL, backup);
298     ok(!ret, "Expected failure\n");
299     ok(GetFileAttributesA(backup) == INVALID_FILE_ATTRIBUTES, "Expected no backup file\n");
300
301     handle = OpenEventLogA(NULL, "Application");
302
303     SetLastError(0xdeadbeef);
304     ret = BackupEventLogA(handle, NULL);
305     ok(!ret, "Expected failure\n");
306     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
307
308     ret = BackupEventLogA(handle, backup);
309     ok(ret, "Expected success\n");
310     todo_wine
311     ok(GetFileAttributesA(backup) != INVALID_FILE_ATTRIBUTES, "Expected a backup file\n");
312
313     /* Try to overwrite */
314     SetLastError(0xdeadbeef);
315     ret = BackupEventLogA(handle, backup);
316     todo_wine
317     {
318     ok(!ret, "Expected failure\n");
319     ok(GetLastError() == ERROR_ALREADY_EXISTS, "Expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
320     }
321
322     CloseEventLog(handle);
323
324     /* Can we make a backup of a backup? */
325     handle = OpenBackupEventLogA(NULL, backup);
326     todo_wine
327     ok(handle != NULL, "Expected a handle\n");
328
329     ret = BackupEventLogA(handle, backup2);
330     todo_wine
331     {
332     ok(ret, "Expected success\n");
333     ok(GetFileAttributesA(backup2) != INVALID_FILE_ATTRIBUTES, "Expected a backup file\n");
334     }
335
336     CloseEventLog(handle);
337     DeleteFileA(backup);
338     DeleteFileA(backup2);
339 }
340
341 static void test_read(void)
342 {
343     HANDLE handle;
344     BOOL ret;
345     DWORD count, toread, read, needed;
346     void *buf;
347
348     SetLastError(0xdeadbeef);
349     ret = ReadEventLogA(NULL, 0, 0, NULL, 0, NULL, NULL);
350     ok(!ret, "Expected failure\n");
351     todo_wine
352     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
353
354     read = 0xdeadbeef;
355     SetLastError(0xdeadbeef);
356     ret = ReadEventLogA(NULL, 0, 0, NULL, 0, &read, NULL);
357     ok(!ret, "Expected failure\n");
358     ok(read == 0xdeadbeef, "Expected 'read' parameter to remain unchanged\n");
359     todo_wine
360     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
361
362     needed = 0xdeadbeef;
363     SetLastError(0xdeadbeef);
364     ret = ReadEventLogA(NULL, 0, 0, NULL, 0, NULL, &needed);
365     ok(!ret, "Expected failure\n");
366     ok(needed == 0xdeadbeef, "Expected 'needed' parameter to remain unchanged\n");
367     todo_wine
368     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
369
370     /* 'read' and 'needed' are only filled when the needed buffer size is passed back or when the call succeeds */
371     SetLastError(0xdeadbeef);
372     ret = ReadEventLogA(NULL, 0, 0, NULL, 0, &read, &needed);
373     ok(!ret, "Expected failure\n");
374     todo_wine
375     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
376
377     SetLastError(0xdeadbeef);
378     ret = ReadEventLogA(NULL, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ, 0, NULL, 0, NULL, NULL);
379     ok(!ret, "Expected failure\n");
380     todo_wine
381     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
382
383     SetLastError(0xdeadbeef);
384     ret = ReadEventLogA(NULL, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ, 0, NULL, 0, &read, &needed);
385     ok(!ret, "Expected failure\n");
386     todo_wine
387     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
388
389     buf = NULL;
390     SetLastError(0xdeadbeef);
391     ret = ReadEventLogA(NULL, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
392                         0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
393     ok(!ret, "Expected failure\n");
394     todo_wine
395     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
396
397     buf = HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD));
398     SetLastError(0xdeadbeef);
399     ret = ReadEventLogA(NULL, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
400                         0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
401     ok(!ret, "Expected failure\n");
402     todo_wine
403     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
404     HeapFree(GetProcessHeap(), 0, buf);
405
406     handle = OpenEventLogA(NULL, "Application");
407
408     /* Show that we need the proper dwFlags with a (for the rest) proper call */
409     buf = HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD));
410
411     SetLastError(0xdeadbeef);
412     ret = ReadEventLogA(handle, 0, 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
413     ok(!ret, "Expected failure\n");
414     todo_wine
415     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
416
417     SetLastError(0xdeadbeef);
418     ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ, 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
419     ok(!ret, "Expected failure\n");
420     todo_wine
421     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
422
423     SetLastError(0xdeadbeef);
424     ret = ReadEventLogA(handle, EVENTLOG_SEEK_READ, 0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
425     ok(!ret, "Expected failure\n");
426     todo_wine
427     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
428
429     SetLastError(0xdeadbeef);
430     ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ | EVENTLOG_BACKWARDS_READ,
431                         0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
432     ok(!ret, "Expected failure\n");
433     todo_wine
434     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
435
436     SetLastError(0xdeadbeef);
437     ret = ReadEventLogA(handle, EVENTLOG_SEEK_READ | EVENTLOG_FORWARDS_READ | EVENTLOG_BACKWARDS_READ,
438                         0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
439     ok(!ret, "Expected failure\n");
440     todo_wine
441     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
442
443     SetLastError(0xdeadbeef);
444     ret = ReadEventLogA(handle, EVENTLOG_SEEK_READ | EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
445                         0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
446     ok(!ret, "Expected failure\n");
447     todo_wine
448     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
449
450     HeapFree(GetProcessHeap(), 0, buf);
451
452     /* First check if there are any records (in practice only on Wine: FIXME) */
453     count = 0;
454     GetNumberOfEventLogRecords(handle, &count);
455     if (!count)
456     {
457         skip("No records in the 'Application' log\n");
458         CloseEventLog(handle);
459         return;
460     }
461
462     /* Get the buffer size for the first record */
463     buf = HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD));
464     read = needed = 0xdeadbeef;
465     SetLastError(0xdeadbeef);
466     ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
467                         0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
468     ok(!ret, "Expected failure\n");
469     ok(read == 0, "Expected no bytes read\n");
470     ok(needed > sizeof(EVENTLOGRECORD), "Expected the needed buffersize to be bigger than sizeof(EVENTLOGRECORD)\n");
471     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
472
473     /* Read the first record */
474     toread = needed;
475     buf = HeapReAlloc(GetProcessHeap(), 0, buf, toread);
476     read = needed = 0xdeadbeef;
477     SetLastError(0xdeadbeef);
478     ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ, 0, buf, toread, &read, &needed);
479     ok(ret, "Expected success\n");
480     ok(read == toread ||
481        broken(read < toread), /* NT4 wants a buffer size way bigger than just 1 record */
482        "Expected the requested size to be read\n");
483     ok(needed == 0, "Expected no extra bytes to be read\n");
484     HeapFree(GetProcessHeap(), 0, buf);
485
486     CloseEventLog(handle);
487 }
488
489 static void test_openbackup(void)
490 {
491     HANDLE handle, handle2, file;
492     DWORD written;
493     const char backup[] = "backup.evt";
494     const char text[] = "Just some text";
495
496     SetLastError(0xdeadbeef);
497     handle = OpenBackupEventLogA(NULL, NULL);
498     ok(handle == NULL, "Didn't expect a handle\n");
499     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
500
501     SetLastError(0xdeadbeef);
502     handle = OpenBackupEventLogA(NULL, "idontexist.evt");
503     ok(handle == NULL, "Didn't expect a handle\n");
504     ok(GetLastError() == ERROR_FILE_NOT_FOUND, "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
505
506     SetLastError(0xdeadbeef);
507     handle = OpenBackupEventLogA("IDontExist", NULL);
508     ok(handle == NULL, "Didn't expect a handle\n");
509     ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
510
511     SetLastError(0xdeadbeef);
512     handle = OpenBackupEventLogA("IDontExist", "idontexist.evt");
513     ok(handle == NULL, "Didn't expect a handle\n");
514     ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
515        GetLastError() == RPC_S_INVALID_NET_ADDR, /* Some Vista and Win7 */
516        "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError());
517
518     /* Make a backup eventlog to work with */
519     create_backup(backup);
520
521     /* FIXME: Wine stops here */
522     if (GetFileAttributesA(backup) == INVALID_FILE_ATTRIBUTES)
523     {
524         skip("We don't have a backup eventlog to work with\n");
525         return;
526     }
527
528     SetLastError(0xdeadbeef);
529     handle = OpenBackupEventLogA("IDontExist", backup);
530     ok(handle == NULL, "Didn't expect a handle\n");
531     ok(GetLastError() == RPC_S_SERVER_UNAVAILABLE ||
532        GetLastError() == RPC_S_INVALID_NET_ADDR, /* Some Vista and Win7 */
533        "Expected RPC_S_SERVER_UNAVAILABLE, got %d\n", GetLastError());
534
535     /* Empty servername should be read as local server */
536     handle = OpenBackupEventLogA("", backup);
537     ok(handle != NULL, "Expected a handle\n");
538     CloseEventLog(handle);
539
540     handle = OpenBackupEventLogA(NULL, backup);
541     ok(handle != NULL, "Expected a handle\n");
542
543     /* Can we open that same backup eventlog more than once? */
544     handle2 = OpenBackupEventLogA(NULL, backup);
545     ok(handle2 != NULL, "Expected a handle\n");
546     ok(handle2 != handle, "Didn't expect the same handle\n");
547     CloseEventLog(handle2);
548
549     CloseEventLog(handle);
550     DeleteFileA(backup);
551
552     /* Is there any content checking done? */
553     file = CreateFileA(backup, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
554     CloseHandle(file);
555     SetLastError(0xdeadbeef);
556     handle = OpenBackupEventLogA(NULL, backup);
557     ok(handle == NULL, "Didn't expect a handle\n");
558     ok(GetLastError() == ERROR_NOT_ENOUGH_MEMORY ||
559        GetLastError() == ERROR_EVENTLOG_FILE_CORRUPT, /* Vista and Win7 */
560        "Expected ERROR_NOT_ENOUGH_MEMORY, got %d\n", GetLastError());
561     CloseEventLog(handle);
562     DeleteFileA(backup);
563
564     file = CreateFileA(backup, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, NULL);
565     WriteFile(file, text, sizeof(text), &written, NULL);
566     CloseHandle(file);
567     SetLastError(0xdeadbeef);
568     handle = OpenBackupEventLogA(NULL, backup);
569     ok(handle == NULL, "Didn't expect a handle\n");
570     ok(GetLastError() == ERROR_EVENTLOG_FILE_CORRUPT, "Expected ERROR_EVENTLOG_FILE_CORRUPT, got %d\n", GetLastError());
571     CloseEventLog(handle);
572     DeleteFileA(backup);
573 }
574
575 static void test_clear(void)
576 {
577     HANDLE handle;
578     BOOL ret;
579     const char backup[] = "backup.evt";
580     const char backup2[] = "backup2.evt";
581
582     SetLastError(0xdeadbeef);
583     ret = ClearEventLogA(NULL, NULL);
584     ok(!ret, "Expected failure\n");
585     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
586
587     /* Make a backup eventlog to work with */
588     create_backup(backup);
589
590     SetLastError(0xdeadbeef);
591     ret = ClearEventLogA(NULL, backup);
592     ok(!ret, "Expected failure\n");
593     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
594
595     handle = OpenBackupEventLogA(NULL, backup);
596     todo_wine
597     ok(handle != NULL, "Expected a handle\n");
598
599     /* A real eventlog would fail with ERROR_ALREADY_EXISTS */
600     SetLastError(0xdeadbeef);
601     ret = ClearEventLogA(handle, backup);
602     ok(!ret, "Expected failure\n");
603     /* The eventlog service runs under an account that doesn't have the necessary
604      * permissions on the users home directory on a default Vista+ system.
605      */
606     ok(GetLastError() == ERROR_INVALID_HANDLE ||
607        GetLastError() == ERROR_ACCESS_DENIED, /* Vista+ */
608        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
609
610     /* Show that ClearEventLog only works for real eventlogs. */
611     SetLastError(0xdeadbeef);
612     ret = ClearEventLogA(handle, backup2);
613     ok(!ret, "Expected failure\n");
614     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
615     ok(GetFileAttributesA(backup2) == INVALID_FILE_ATTRIBUTES, "Expected no backup file\n");
616
617     SetLastError(0xdeadbeef);
618     ret = ClearEventLogA(handle, NULL);
619     ok(!ret, "Expected failure\n");
620     ok(GetLastError() == ERROR_INVALID_HANDLE, "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
621
622     CloseEventLog(handle);
623     todo_wine
624     ok(DeleteFileA(backup), "Could not delete the backup file\n");
625 }
626
627 static const char eventlogsvc[] = "SYSTEM\\CurrentControlSet\\Services\\Eventlog";
628 static const char eventlogname[] = "Wine";
629 static const char eventsources[][11] = { "WineSrc", "WineSrc1", "WineSrc20", "WineSrc300" };
630
631 static BOOL create_new_eventlog(void)
632 {
633     HKEY key, eventkey;
634     BOOL bret = FALSE;
635     LONG lret;
636     int i;
637
638     /* First create our eventlog */
639     lret = RegOpenKeyA(HKEY_LOCAL_MACHINE, eventlogsvc, &key);
640      /* FIXME: Wine stops here */
641     if (lret != ERROR_SUCCESS)
642     {
643         skip("Could not open the EventLog service registry key\n");
644         return FALSE;
645     }
646     lret = RegCreateKeyA(key, eventlogname, &eventkey);
647     if (lret != ERROR_SUCCESS)
648     {
649         skip("Could not create the eventlog '%s' registry key\n", eventlogname);
650         goto cleanup;
651     }
652
653     /* Create some event sources, the registry value 'Sources' is updated automatically */
654     for (i = 0; i < sizeof(eventsources)/sizeof(eventsources[0]); i++)
655     {
656         HKEY srckey;
657
658         lret = RegCreateKeyA(eventkey, eventsources[i], &srckey);
659         if (lret != ERROR_SUCCESS)
660         {
661             skip("Could not create the eventsource '%s' registry key\n", eventsources[i]);
662             goto cleanup;
663         }
664         RegFlushKey(srckey);
665         RegCloseKey(srckey);
666     }
667
668     bret = TRUE;
669
670     /* The flushing of the registry (here and above) gives us some assurance
671      * that we are not to quickly writing events as 'Sources' could still be
672      * not updated.
673      */
674     RegFlushKey(eventkey);
675 cleanup:
676     RegCloseKey(eventkey);
677     RegCloseKey(key);
678
679     return bret;
680 }
681
682 static const char *one_string[] = { "First string" };
683 static const char *two_strings[] = { "First string", "Second string" };
684 static const struct
685 {
686     const char  *evt_src;
687     WORD         evt_type;
688     WORD         evt_cat;
689     DWORD        evt_id;
690     BOOL         evt_sid;
691     WORD         evt_numstrings;
692     const char **evt_strings;
693 } read_write [] =
694 {
695     { eventlogname,    EVENTLOG_INFORMATION_TYPE, 1, 1,  FALSE, 1, one_string },
696     { eventsources[0], EVENTLOG_WARNING_TYPE,     1, 2,  FALSE, 0, NULL },
697     { eventsources[1], EVENTLOG_AUDIT_FAILURE,    1, 3,  FALSE, 2, two_strings },
698     { eventsources[2], EVENTLOG_ERROR_TYPE,       1, 4,  FALSE, 0, NULL },
699     { eventsources[3], EVENTLOG_WARNING_TYPE,     1, 5,  FALSE, 1, one_string },
700     { eventlogname,    EVENTLOG_SUCCESS,          2, 6,  TRUE,  2, two_strings },
701     { eventsources[0], EVENTLOG_AUDIT_FAILURE,    2, 7,  TRUE,  0, NULL },
702     { eventsources[1], EVENTLOG_AUDIT_SUCCESS,    2, 8,  TRUE,  2, two_strings },
703     { eventsources[2], EVENTLOG_WARNING_TYPE,     2, 9,  TRUE,  0, NULL },
704     { eventsources[3], EVENTLOG_ERROR_TYPE,       2, 10, TRUE,  1, one_string }
705 };
706
707 static void test_readwrite(void)
708 {
709     HANDLE handle;
710     PSID user;
711     DWORD sidsize, count;
712     BOOL ret, sidavailable;
713     BOOL on_vista = FALSE; /* Used to indicate Vista, W2K8 or Win7 */
714     int i;
715     char *localcomputer = NULL;
716     DWORD size;
717
718     if (pCreateWellKnownSid)
719     {
720         sidsize = SECURITY_MAX_SID_SIZE;
721         user = HeapAlloc(GetProcessHeap(), 0, sidsize);
722         SetLastError(0xdeadbeef);
723         pCreateWellKnownSid(WinInteractiveSid, NULL, user, &sidsize);
724         sidavailable = TRUE;
725     }
726     else
727     {
728         win_skip("Skipping some SID related tests\n");
729         sidavailable = FALSE;
730         user = NULL;
731     }
732
733     /* Write an event with an incorrect event type. This will fail on Windows 7
734      * but succeed on all others, hence it's not part of the struct.
735      */
736     handle = OpenEventLogA(NULL, eventlogname);
737     if (!handle)
738     {
739         /* Intermittently seen on NT4 when tests are run immediately after boot */
740         win_skip("Could not get a handle to the eventlog\n");
741         goto cleanup;
742     }
743
744     count = 0xdeadbeef;
745     GetNumberOfEventLogRecords(handle, &count);
746     if (count != 0)
747     {
748         /* Needed for W2K3 without a service pack */
749         win_skip("We most likely opened the Application eventlog\n");
750         CloseEventLog(handle);
751         Sleep(2000);
752
753         handle = OpenEventLogA(NULL, eventlogname);
754         count = 0xdeadbeef;
755         GetNumberOfEventLogRecords(handle, &count);
756         if (count != 0)
757         {
758             win_skip("We didn't open our new eventlog\n");
759             CloseEventLog(handle);
760             goto cleanup;
761         }
762     }
763
764     SetLastError(0xdeadbeef);
765     ret = ReportEvent(handle, 0x20, 0, 0, NULL, 0, 0, NULL, NULL);
766     if (!ret && GetLastError() == ERROR_CRC)
767     {
768         win_skip("Win7 fails when using incorrect event types\n");
769         ret = ReportEvent(handle, 0, 0, 0, NULL, 0, 0, NULL, NULL);
770     }
771     else
772     {
773         void *buf;
774         DWORD read, needed;
775         EVENTLOGRECORD *record;
776
777         /* Needed to catch earlier Vista (with no ServicePack for example) */
778         buf = HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD));
779         ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
780                       0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
781
782         buf = HeapReAlloc(GetProcessHeap(), 0, buf, needed);
783         ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
784                       0, buf, needed, &read, &needed);
785
786         record = (EVENTLOGRECORD *)buf;
787
788         /* Vista and W2K8 return EVENTLOG_SUCCESS, Windows versions before return
789          * the written eventtype (0x20 in this case).
790          */
791         if (record->EventType == EVENTLOG_SUCCESS)
792             on_vista = TRUE;
793
794         HeapFree(GetProcessHeap(), 0, buf);
795     }
796     ok(ret, "Expected success : %d\n", GetLastError());
797
798     /* This will clear the eventlog. The record numbering for new
799      * events however differs on Vista SP1+. Before Vista the first
800      * event would be numbered 1, on Vista SP1+ it's higher as we already
801      * had at least one event (more in case of multiple test runs without
802      * a reboot).
803      */
804     ClearEventLogA(handle, NULL);
805     CloseEventLog(handle);
806
807     /* Write a bunch of events while using different event sources */
808     for (i = 0; i < sizeof(read_write)/sizeof(read_write[0]); i++)
809     {
810         DWORD oldest;
811         BOOL run_sidtests = read_write[i].evt_sid & sidavailable;
812
813         /* We don't need to use RegisterEventSource to report events */
814         if (i % 2)
815             handle = OpenEventLogA(NULL, read_write[i].evt_src);
816         else
817             handle = RegisterEventSourceA(NULL, read_write[i].evt_src);
818         ok(handle != NULL, "Expected a handle\n");
819
820         SetLastError(0xdeadbeef);
821         ret = ReportEvent(handle, read_write[i].evt_type, read_write[i].evt_cat,
822                           read_write[i].evt_id, run_sidtests ? user : NULL,
823                           read_write[i].evt_numstrings, 0, read_write[i].evt_strings, NULL);
824
825         count = 0xdeadbeef;
826         ret = GetNumberOfEventLogRecords(handle, &count);
827         ok(ret, "Expected success\n");
828         ok(count == (i + 1), "Expected %d records, got %d\n", i + 1, count);
829
830         oldest = 0xdeadbeef;
831         ret = GetOldestEventLogRecord(handle, &oldest);
832         ok(ret, "Expected success\n");
833         ok(oldest == 1 ||
834            (oldest > 1 && oldest != 0xdeadbeef), /* Vista SP1+, W2K8 and Win7 */
835            "Expected oldest to be 1 or higher, got %d\n", oldest);
836         if (oldest > 1 && oldest != 0xdeadbeef)
837             on_vista = TRUE;
838
839         if (i % 2)
840             ret = CloseEventLog(handle);
841         else
842             ret = DeregisterEventSource(handle);
843         ok(ret, "Expected success : %d\n", GetLastError());
844     }
845
846     handle = OpenEventLogA(NULL, eventlogname);
847     count = 0xdeadbeef;
848     ret = GetNumberOfEventLogRecords(handle, &count);
849     ok(ret, "Expected success\n");
850     ok(count == i, "Expected %d records, got %d\n", i, count);
851     CloseEventLog(handle);
852
853     if (count == 0)
854     {
855         skip("No events were written to the eventlog\n");
856         goto cleanup;
857     }
858
859     /* Report only once */
860     if (on_vista)
861         skip("There is no DWORD alignment enforced for UserSid on Vista, W2K8 or Win7\n");
862
863     if (on_vista && pGetComputerNameExA)
864     {
865         /* New Vista+ behavior */
866         size = 0;
867         SetLastError(0xdeadbeef);
868         pGetComputerNameExA(ComputerNameDnsFullyQualified, NULL, &size);
869         localcomputer = HeapAlloc(GetProcessHeap(), 0, size);
870         pGetComputerNameExA(ComputerNameDnsFullyQualified, localcomputer, &size);
871     }
872     else
873     {
874         size = MAX_COMPUTERNAME_LENGTH + 1;
875         localcomputer = HeapAlloc(GetProcessHeap(), 0, size);
876         GetComputerNameA(localcomputer, &size);
877     }
878
879     /* Read all events from our created eventlog, one by one */
880     handle = OpenEventLogA(NULL, eventlogname);
881     i = 0;
882     for (;;)
883     {
884         void *buf;
885         DWORD read, needed;
886         EVENTLOGRECORD *record;
887         char *sourcename, *computername;
888         int k;
889         char *ptr;
890         BOOL run_sidtests = read_write[i].evt_sid & sidavailable;
891
892         buf = HeapAlloc(GetProcessHeap(), 0, sizeof(EVENTLOGRECORD));
893         SetLastError(0xdeadbeef);
894         ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
895                             0, buf, sizeof(EVENTLOGRECORD), &read, &needed);
896         if (!ret && GetLastError() == ERROR_HANDLE_EOF)
897         {
898             HeapFree(GetProcessHeap(), 0, buf);
899             break;
900         }
901         ok(!ret, "Expected failure\n");
902         ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
903            "Expected ERROR_INVALID_PARAMETER, got %d\n",GetLastError());
904
905         buf = HeapReAlloc(GetProcessHeap(), 0, buf, needed);
906         ret = ReadEventLogA(handle, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_FORWARDS_READ,
907                             0, buf, needed, &read, &needed);
908         ok(ret, "Expected success: %d\n", GetLastError());
909
910         record = (EVENTLOGRECORD *)buf;
911
912         ok(record->Length == read,
913            "Expected %d, got %d\n", read, record->Length);
914         ok(record->Reserved == 0x654c664c,
915            "Expected 0x654c664c, got %d\n", record->Reserved);
916         ok(record->RecordNumber == i + 1 ||
917            (on_vista && (record->RecordNumber > i + 1)),
918            "Expected %d or higher, got %d\n", i + 1, record->RecordNumber);
919         ok(record->EventID == read_write[i].evt_id,
920            "Expected %d, got %d\n", read_write[i].evt_id, record->EventID);
921         ok(record->EventType == read_write[i].evt_type,
922            "Expected %d, got %d\n", read_write[i].evt_type, record->EventType);
923         ok(record->NumStrings == read_write[i].evt_numstrings,
924            "Expected %d, got %d\n", read_write[i].evt_numstrings, record->NumStrings);
925         ok(record->EventCategory == read_write[i].evt_cat,
926            "Expected %d, got %d\n", read_write[i].evt_cat, record->EventCategory);
927
928         sourcename = (char *)((BYTE *)buf + sizeof(EVENTLOGRECORD));
929         ok(!lstrcmpA(sourcename, read_write[i].evt_src), "Expected '%s', got '%s'\n",
930            read_write[i].evt_src, sourcename);
931
932         computername = (char *)((BYTE *)buf + sizeof(EVENTLOGRECORD) + lstrlenA(sourcename) + 1);
933         ok(!lstrcmpiA(computername, localcomputer), "Expected '%s', got '%s'\n",
934            localcomputer, computername);
935
936         /* Before Vista, UserSid was aligned on a DWORD boundary. Next to that if
937          * no padding was actually required a 0 DWORD was still used for padding. No
938          * application should be relying on the padding as we are working with offsets
939          * anyway.
940          */
941
942         if (!on_vista)
943         {
944             DWORD calculated_sidoffset = sizeof(EVENTLOGRECORD) + lstrlenA(sourcename) + 1 + lstrlenA(computername) + 1;
945
946             /* We are already DWORD aligned, there should still be some padding */
947             if ((((UINT_PTR)buf + calculated_sidoffset) % sizeof(DWORD)) == 0)
948                 ok(*(DWORD *)((BYTE *)buf + calculated_sidoffset) == 0, "Expected 0\n");
949
950             ok((((UINT_PTR)buf + record->UserSidOffset) % sizeof(DWORD)) == 0, "Expected DWORD alignment\n");
951         }
952
953         if (run_sidtests)
954         {
955             ok(record->UserSidLength == sidsize, "Expected %d, got %d\n", sidsize, record->UserSidLength);
956         }
957         else
958         {
959             ok(record->StringOffset == record->UserSidOffset, "Expected offsets to be the same\n");
960             ok(record->UserSidLength == 0, "Expected 0, got %d\n", record->UserSidLength);
961         }
962
963         ok(record->DataLength == 0, "Expected 0, got %d\n", record->DataLength);
964
965         ptr = (char *)((BYTE *)buf + record->StringOffset);
966         for (k = 0; k < record->NumStrings; k++)
967         {
968             ok(!lstrcmpA(ptr, two_strings[k]), "Expected '%s', got '%s'\n", two_strings[k], ptr);
969             ptr += lstrlenA(ptr) + 1;
970         }
971
972         ok(record->Length == *(DWORD *)((BYTE *)buf + record->Length - sizeof(DWORD)),
973            "Expected the closing DWORD to contain the length of the record\n");
974
975         HeapFree(GetProcessHeap(), 0, buf);
976         i++;
977     }
978     CloseEventLog(handle);
979
980     /* Test clearing a real eventlog */
981     handle = OpenEventLogA(NULL, eventlogname);
982
983     SetLastError(0xdeadbeef);
984     ret = ClearEventLogA(handle, NULL);
985     ok(ret, "Expected success\n");
986
987     count = 0xdeadbeef;
988     ret = GetNumberOfEventLogRecords(handle, &count);
989     ok(ret, "Expected success\n");
990     ok(count == 0, "Expected an empty eventlog, got %d records\n", count);
991
992     CloseEventLog(handle);
993
994 cleanup:
995     HeapFree(GetProcessHeap(), 0, localcomputer);
996     HeapFree(GetProcessHeap(), 0, user);
997 }
998
999 /* Before Vista:
1000  *
1001  * Creating an eventlog on Windows (via the registry) automatically leads
1002  * to creation of a REG_MULTI_SZ named 'Sources'. This value lists all the
1003  * potential event sources for this eventlog. 'Sources' is automatically
1004  * updated when a new key (aka event source) is created.
1005  *
1006  * Although the updating of registry keys is almost instantaneously, we
1007  * check it after some other tests to assure we are not querying the
1008  * registry or file system to quickly.
1009  *
1010  * NT4 and higher:
1011  *
1012  * The eventlog file itself is also automatically created, even before we
1013  * start writing events.
1014  */
1015 static char eventlogfile[MAX_PATH];
1016 static void test_autocreation(void)
1017 {
1018     HKEY key, eventkey;
1019     DWORD type, size;
1020     LONG ret;
1021     int i;
1022     char *p;
1023     char sources[sizeof(eventsources)];
1024     char sysdir[MAX_PATH];
1025     void *redir = 0;
1026
1027     RegOpenKeyA(HKEY_LOCAL_MACHINE, eventlogsvc, &key);
1028     RegOpenKeyA(key, eventlogname, &eventkey);
1029
1030     size = sizeof(sources);
1031     sources[0] = 0;
1032     ret = RegQueryValueExA(eventkey, "Sources", NULL, &type, (LPBYTE)sources, &size);
1033     if (ret == ERROR_SUCCESS)
1034     {
1035         char sources_verify[sizeof(eventsources)];
1036
1037         ok(type == REG_MULTI_SZ, "Expected a REG_MULTI_SZ, got %d\n", type);
1038
1039         /* Build the expected string */
1040         memset(sources_verify, 0, sizeof(sources_verify));
1041         p = sources_verify;
1042         for (i = sizeof(eventsources)/sizeof(eventsources[0]); i > 0; i--)
1043         {
1044             lstrcpyA(p, eventsources[i - 1]);
1045             p += (lstrlenA(eventsources[i - 1]) + 1);
1046         }
1047         lstrcpyA(p, eventlogname);
1048
1049         ok(!memcmp(sources, sources_verify, size),
1050            "Expected a correct 'Sources' value (size : %d)\n", size);
1051     }
1052
1053     RegCloseKey(eventkey);
1054     RegCloseKey(key);
1055
1056     /* The directory that holds the eventlog files could be redirected */
1057     if (pWow64DisableWow64FsRedirection)
1058         pWow64DisableWow64FsRedirection(&redir);
1059
1060     /* On Windows we also automatically get an eventlog file */
1061     GetSystemDirectoryA(sysdir, sizeof(sysdir));
1062
1063     /* NT4 - W2K3 */
1064     lstrcpyA(eventlogfile, sysdir);
1065     lstrcatA(eventlogfile, "\\config\\");
1066     lstrcatA(eventlogfile, eventlogname);
1067     lstrcatA(eventlogfile, ".evt");
1068
1069     if (GetFileAttributesA(eventlogfile) == INVALID_FILE_ATTRIBUTES)
1070     {
1071         /* Vista+ */
1072         lstrcpyA(eventlogfile, sysdir);
1073         lstrcatA(eventlogfile, "\\winevt\\Logs\\");
1074         lstrcatA(eventlogfile, eventlogname);
1075         lstrcatA(eventlogfile, ".evtx");
1076     }
1077
1078     ok(GetFileAttributesA(eventlogfile) != INVALID_FILE_ATTRIBUTES,
1079        "Expected an eventlog file\n");
1080
1081     if (pWow64RevertWow64FsRedirection)
1082         pWow64RevertWow64FsRedirection(redir);
1083 }
1084
1085 static void cleanup_eventlog(void)
1086 {
1087     BOOL bret;
1088     LONG lret;
1089     HKEY key;
1090     int i;
1091     char winesvc[MAX_PATH];
1092
1093     /* Delete the registry tree */
1094     lstrcpyA(winesvc, eventlogsvc);
1095     lstrcatA(winesvc, "\\");
1096     lstrcatA(winesvc, eventlogname);
1097
1098     RegOpenKeyA(HKEY_LOCAL_MACHINE, winesvc, &key);
1099     for (i = 0; i < sizeof(eventsources)/sizeof(eventsources[0]); i++)
1100         RegDeleteKeyA(key, eventsources[i]);
1101     RegDeleteValueA(key, "Sources");
1102     RegCloseKey(key);
1103     lret = RegDeleteKeyA(HKEY_LOCAL_MACHINE, winesvc);
1104     todo_wine
1105     ok(lret == ERROR_SUCCESS, "Could not delete the registry tree : %d\n", lret);
1106
1107     /* A handle to the eventlog is locked by services.exe. We can only
1108      * delete the eventlog file after reboot.
1109      */
1110     bret = MoveFileExA(eventlogfile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
1111     todo_wine
1112     ok(bret, "Expected MoveFileEx to succeed: %d\n", GetLastError());
1113 }
1114
1115 START_TEST(eventlog)
1116 {
1117     SetLastError(0xdeadbeef);
1118     CloseEventLog(NULL);
1119     if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
1120     {
1121         win_skip("Event log functions are not implemented\n");
1122         return;
1123     }
1124
1125     init_function_pointers();
1126
1127     /* Parameters only */
1128     test_open_close();
1129     test_info();
1130     test_count();
1131     test_oldest();
1132     test_backup();
1133     test_openbackup();
1134     test_read();
1135     test_clear();
1136
1137     /* Functional tests */
1138     if (create_new_eventlog())
1139     {
1140         test_readwrite();
1141         test_autocreation();
1142     }
1143     cleanup_eventlog();
1144 }