advapi32/tests: Add some BackupEventLog tests.
[wine/wine-gecko.git] / dlls / advapi32 / tests / eventlog.c
blob41319ca888ee48b219b48db8a2266b1a0b4669ab
1 /*
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
21 #include <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "winnt.h"
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)
41 HANDLE handle;
42 BOOL ret;
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);
76 todo_wine
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)
94 HANDLE handle;
95 BOOL ret;
96 DWORD needed;
97 EVENTLOG_FULL_INFORMATION efi;
99 if (!pGetEventLogInformation)
101 /* NT4 */
102 win_skip("GetEventLogInformation is not available\n");
103 return;
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);
133 needed = 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;
143 needed *= 2;
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)
154 HANDLE handle;
155 BOOL ret;
156 DWORD count;
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);
164 count = 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());
177 count = 0xdeadbeef;
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)
187 HANDLE handle;
188 BOOL ret;
189 DWORD oldest;
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);
197 oldest = 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());
210 oldest = 0xdeadbeef;
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)
220 HANDLE handle;
221 BOOL ret;
222 const char backup[] = "backup.evt";
224 SetLastError(0xdeadbeef);
225 ret = BackupEventLogA(NULL, NULL);
226 todo_wine
228 ok(!ret, "Expected failure\n");
229 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
232 SetLastError(0xdeadbeef);
233 ret = BackupEventLogA(NULL, backup);
234 todo_wine
235 ok(!ret, "Expected failure\n");
236 ok(GetFileAttributesA(backup) == INVALID_FILE_ATTRIBUTES, "Expected no backup file\n");
238 handle = OpenEventLogA(NULL, "Application");
240 SetLastError(0xdeadbeef);
241 ret = BackupEventLogA(handle, NULL);
242 todo_wine
244 ok(!ret, "Expected failure\n");
245 ok(GetLastError() == ERROR_INVALID_PARAMETER, "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
248 ret = BackupEventLogA(handle, backup);
249 ok(ret, "Expected succes\n");
250 todo_wine
251 ok(GetFileAttributesA("backup.evt") != INVALID_FILE_ATTRIBUTES, "Expected a backup file\n");
253 /* Try to overwrite */
254 SetLastError(0xdeadbeef);
255 ret = BackupEventLogA(handle, backup);
256 todo_wine
258 ok(!ret, "Expected failure\n");
259 ok(GetLastError() == ERROR_ALREADY_EXISTS, "Expected ERROR_ALREADY_EXISTS, got %d\n", GetLastError());
262 CloseEventLog(handle);
263 DeleteFileA(backup);
266 START_TEST(eventlog)
268 SetLastError(0xdeadbeef);
269 CloseEventLog(NULL);
270 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
272 win_skip("Event log functions are not implemented\n");
273 return;
276 init_function_pointers();
278 /* Parameters only */
279 test_open_close();
280 test_info();
281 test_count();
282 test_oldest();
283 test_backup();