[ADVAPI32_APITEST]
[reactos.git] / rostests / apitests / advapi32 / LockDatabase.c
1 /*
2 * PROJECT: ReactOS api tests
3 * LICENSE: GPLv2+ - See COPYING in the top level directory
4 * PURPOSE: Tests for Lock/UnlockServiceDatabase and QueryServiceLockStatusA/W
5 * PROGRAMMER: Hermès BÉLUSCA - MAÏTO
6 */
7
8 #include <wine/test.h>
9 #include <windows.h>
10 #include <strsafe.h>
11
12 #define TESTING_SERVICE L"Spooler"
13
14 static void Test_LockUnlockServiceDatabase(void)
15 {
16 BOOL bError = FALSE;
17
18 SC_HANDLE hScm = NULL;
19 SC_LOCK hLock = NULL;
20
21 /* First of all, try to lock / unlock the services database with invalid handles */
22 SetLastError(0xdeadbeef);
23 hScm = NULL;
24 hLock = LockServiceDatabase(hScm);
25 ok(hLock == NULL, "hLock = 0x%p, expected 0\n", hLock);
26 ok_err(ERROR_INVALID_HANDLE);
27
28 SetLastError(0xdeadbeef);
29 hScm = (SC_HANDLE)0xdeadbeef;
30 hLock = LockServiceDatabase(hScm);
31 ok(hLock == NULL, "hLock = 0x%p, expected 0\n", hLock);
32 ok_err(ERROR_INVALID_HANDLE);
33
34 /** This test seems to make this application crash on Windows 7... I do not know why... **/
35 SetLastError(0xdeadbeef);
36 hLock = NULL;
37 bError = UnlockServiceDatabase(hLock);
38 ok(bError == FALSE, "bError = %u, expected FALSE\n", bError);
39 ok_err(ERROR_INVALID_SERVICE_LOCK);
40 /*****************************************************************************************/
41
42 SetLastError(0xdeadbeef);
43 hLock = (SC_LOCK)0xdeadbeef;
44 bError = UnlockServiceDatabase(hLock);
45 ok(bError == FALSE, "bError = %u, expected FALSE\n", bError);
46 ok_err(ERROR_INVALID_SERVICE_LOCK);
47
48
49 /* Then, try to lock the services database without having rights */
50 SetLastError(0xdeadbeef);
51 hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
52 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
53 if (!hScm)
54 {
55 skip("No service control manager; cannot proceed with LockUnlockServiceDatabase test\n");
56 goto cleanup;
57 }
58 ok_err(ERROR_SUCCESS);
59
60 SetLastError(0xdeadbeef);
61 hLock = LockServiceDatabase(hScm);
62 ok(hLock == NULL, "hLock = 0x%p, expected 0\n", hLock);
63 ok_err(ERROR_ACCESS_DENIED);
64
65 if (hLock)
66 UnlockServiceDatabase(hLock);
67 CloseServiceHandle(hScm);
68
69 /* Try to lock the services database with good rights */
70 SetLastError(0xdeadbeef);
71 hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_LOCK);
72 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
73 if (!hScm)
74 {
75 skip("No service control manager; cannot proceed with LockUnlockServiceDatabase test\n");
76 goto cleanup;
77 }
78 ok_err(ERROR_SUCCESS);
79
80 SetLastError(0xdeadbeef);
81 hLock = LockServiceDatabase(hScm);
82 ok(hLock != NULL, "hLock = 0x%p, expected non-zero\n", hLock);
83 ok_err(ERROR_SUCCESS);
84
85 /* Now unlock it */
86 if (hLock)
87 {
88 SetLastError(0xdeadbeef);
89 bError = UnlockServiceDatabase(hLock);
90 ok(bError == TRUE, "bError = %u, expected TRUE\n", bError);
91 ok_err(ERROR_SUCCESS);
92 }
93
94
95 cleanup:
96 if (hScm)
97 CloseServiceHandle(hScm);
98
99 return;
100 }
101
102 static void Test_LockUnlockServiceDatabaseWithServiceStart(void)
103 {
104 BOOL bError = FALSE;
105
106 SC_HANDLE hScm = NULL;
107 SC_HANDLE hSvc = NULL;
108 SC_LOCK hLock = NULL;
109
110 LPQUERY_SERVICE_CONFIGW lpConfig = NULL;
111 DWORD dwRequiredSize = 0;
112 SERVICE_STATUS status;
113 BOOL bWasRunning = FALSE;
114 DWORD dwOldStartType = 0;
115
116 /* Open the services database */
117 SetLastError(0xdeadbeef);
118 hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_LOCK);
119 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
120 if (!hScm)
121 {
122 skip("No service control manager; cannot proceed with LockUnlockServiceDatabaseWithServiceStart test\n");
123 goto cleanup;
124 }
125 ok_err(ERROR_SUCCESS);
126
127 /* Grab a handle to the testing service */
128 SetLastError(0xdeadbeef);
129 hSvc = OpenServiceW(hScm, TESTING_SERVICE, SERVICE_START | SERVICE_STOP | SERVICE_CHANGE_CONFIG | SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS);
130 ok(hSvc != NULL, "hSvc = 0x%p, expected non-null, error=0x%08lx\n", hSvc, GetLastError());
131 if (!hSvc)
132 {
133 skip("Cannot open a handle to service %S; cannot proceed with LockUnlockServiceDatabaseWithServiceStart test\n", TESTING_SERVICE);
134 goto cleanup;
135 }
136 ok_err(ERROR_SUCCESS);
137
138 /* Lock the services database */
139 SetLastError(0xdeadbeef);
140 hLock = LockServiceDatabase(hScm);
141 ok(hLock != NULL, "hLock = 0x%p, expected non-zero, error=0x%08lx\n", hLock, GetLastError());
142 if (!hLock)
143 {
144 skip("Cannot lock the services database; cannot proceed with LockUnlockServiceDatabaseWithServiceStart test\n");
145 goto cleanup;
146 }
147 ok_err(ERROR_SUCCESS);
148
149 /* To proceed further, firstly attempt to stop the testing service */
150 QueryServiceConfigW(hSvc, NULL, 0, &dwRequiredSize);
151 lpConfig = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
152 QueryServiceConfigW(hSvc, lpConfig, dwRequiredSize, &dwRequiredSize);
153 dwOldStartType = lpConfig->dwStartType;
154 HeapFree(GetProcessHeap(), 0, lpConfig);
155 if (dwOldStartType == SERVICE_DISABLED)
156 {
157 ChangeServiceConfigW(hSvc,
158 SERVICE_NO_CHANGE,
159 SERVICE_DEMAND_START,
160 SERVICE_NO_CHANGE,
161 NULL,
162 NULL,
163 NULL,
164 NULL,
165 NULL,
166 NULL,
167 NULL);
168 }
169 QueryServiceStatus(hSvc, &status);
170 bWasRunning = (status.dwCurrentState != SERVICE_STOPPED);
171 if (bWasRunning)
172 {
173 ControlService(hSvc, SERVICE_CONTROL_STOP, &status);
174 Sleep(1000); /* Wait 1 second for the service to stop */
175 }
176
177 /* Now try to start it (this test won't work under Windows Vista / 7 / 8) */
178 SetLastError(0xdeadbeef);
179 bError = StartServiceW(hSvc, 0, NULL);
180 ok(bError == FALSE, "bError = %u, expected FALSE\n", bError);
181 ok_err(ERROR_SERVICE_DATABASE_LOCKED);
182 Sleep(1000); /* Wait 1 second for the service to start */
183
184 /* Stop the testing service */
185 ControlService(hSvc, SERVICE_CONTROL_STOP, &status);
186 Sleep(1000); /* Wait 1 second for the service to stop */
187
188 /* Now unlock the services database */
189 SetLastError(0xdeadbeef);
190 bError = UnlockServiceDatabase(hLock);
191 ok(bError == TRUE, "bError = %u, expected TRUE\n", bError);
192 ok_err(ERROR_SUCCESS);
193
194 /* Try to start again the service, this time the database unlocked */
195 SetLastError(0xdeadbeef);
196 bError = StartServiceW(hSvc, 0, NULL);
197 ok(bError == TRUE, "bError = %u, expected TRUE\n", bError);
198 ok_err(ERROR_SUCCESS);
199 Sleep(1000); /* Wait 1 second for the service to start */
200
201 /* Stop the testing service */
202 ControlService(hSvc, SERVICE_CONTROL_STOP, &status);
203 Sleep(1000); /* Wait 1 second for the service to stop */
204
205 /* Restore its original state */
206 if (bWasRunning)
207 {
208 StartServiceW(hSvc, 0, NULL);
209 }
210
211 if (dwOldStartType == SERVICE_DISABLED)
212 {
213 ChangeServiceConfigW(hSvc,
214 SERVICE_NO_CHANGE,
215 SERVICE_DISABLED,
216 SERVICE_NO_CHANGE,
217 NULL,
218 NULL,
219 NULL,
220 NULL,
221 NULL,
222 NULL,
223 NULL);
224 }
225
226
227 cleanup:
228 if (hSvc)
229 CloseServiceHandle(hSvc);
230
231 if (hScm)
232 CloseServiceHandle(hScm);
233
234 return;
235 }
236
237 static void Test_QueryLockStatusW(void)
238 {
239 BOOL bError = FALSE;
240
241 SC_HANDLE hScm = NULL;
242 SC_LOCK hLock = NULL;
243 LPQUERY_SERVICE_LOCK_STATUSW lpLockStatus = NULL;
244 DWORD dwRequiredSize = 0;
245
246 /* Firstly try to get lock status with invalid handles */
247 SetLastError(0xdeadbeef);
248 bError = QueryServiceLockStatusW(hScm,
249 NULL,
250 0,
251 &dwRequiredSize);
252 ok(bError == FALSE && GetLastError() == ERROR_INVALID_HANDLE, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INVALID_HANDLE);
253 ok(dwRequiredSize == 0, "dwRequiredSize is non-zero, expected zero\n");
254
255 /* Open the services database without having rights */
256 SetLastError(0xdeadbeef);
257 hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
258 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
259 if (!hScm)
260 {
261 skip("No service control manager; cannot proceed with QueryLockStatusW test\n");
262 goto cleanup;
263 }
264 ok_err(ERROR_SUCCESS);
265
266 /* Try to get lock status */
267 SetLastError(0xdeadbeef);
268 bError = QueryServiceLockStatusW(hScm,
269 NULL,
270 0,
271 &dwRequiredSize);
272 ok(bError == FALSE && GetLastError() == ERROR_ACCESS_DENIED, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_ACCESS_DENIED);
273 ok(dwRequiredSize == 0, "dwRequiredSize is non-zero, expected zero\n");
274
275 CloseServiceHandle(hScm);
276
277
278 /*
279 * Query only the lock status.
280 */
281
282 SetLastError(0xdeadbeef);
283 hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_QUERY_LOCK_STATUS);
284 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
285 if (!hScm)
286 {
287 skip("No service control manager; cannot proceed with QueryLockStatusW test\n");
288 goto cleanup;
289 }
290 ok_err(ERROR_SUCCESS);
291
292 /* Get the needed size */
293 SetLastError(0xdeadbeef);
294 bError = QueryServiceLockStatusW(hScm,
295 NULL,
296 0,
297 &dwRequiredSize);
298 ok(bError == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INSUFFICIENT_BUFFER);
299 ok(dwRequiredSize != 0, "dwRequiredSize is zero, expected non-zero\n");
300 if (dwRequiredSize == 0)
301 {
302 skip("Required size is null; cannot proceed with QueryLockStatusW test\n");
303 goto cleanup;
304 }
305
306 /* Allocate memory */
307 lpLockStatus = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
308 if (lpLockStatus == NULL)
309 {
310 skip("Cannot allocate %lu bytes of memory\n", dwRequiredSize);
311 goto cleanup;
312 }
313
314 /* Get the actual value */
315 SetLastError(0xdeadbeef);
316 bError = QueryServiceLockStatusW(hScm,
317 lpLockStatus,
318 dwRequiredSize,
319 &dwRequiredSize);
320 ok(bError, "bError = %u, expected TRUE\n", bError);
321
322 /* These conditions must be verified iff the services database wasn't previously locked */
323 ok(lpLockStatus->fIsLocked == 0, "lpLockStatus->fIsLocked = %lu, expected 0\n", lpLockStatus->fIsLocked);
324 ok(lpLockStatus->lpLockOwner != NULL, "lpLockStatus->lpLockOwner is null, expected non-null\n");
325 ok(lpLockStatus->lpLockOwner && *lpLockStatus->lpLockOwner == 0, "*lpLockStatus->lpLockOwner != \"\\0\", expected \"\\0\"\n");
326 ok(lpLockStatus->dwLockDuration == 0, "lpLockStatus->dwLockDuration = %lu, expected 0\n", lpLockStatus->dwLockDuration);
327
328 HeapFree(GetProcessHeap(), 0, lpLockStatus);
329
330 CloseServiceHandle(hScm);
331
332
333 /*
334 * Now, try to lock the database and check its lock status.
335 */
336
337 SetLastError(0xdeadbeef);
338 hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_LOCK | SC_MANAGER_QUERY_LOCK_STATUS);
339 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
340 if (!hScm)
341 {
342 skip("No service control manager; cannot proceed with QueryLockStatusW test\n");
343 goto cleanup;
344 }
345 ok_err(ERROR_SUCCESS);
346
347 /* Get the needed size */
348 SetLastError(0xdeadbeef);
349 bError = QueryServiceLockStatusW(hScm,
350 NULL,
351 0,
352 &dwRequiredSize);
353 ok(bError == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INSUFFICIENT_BUFFER);
354 ok(dwRequiredSize != 0, "dwRequiredSize is zero, expected non-zero\n");
355 if (dwRequiredSize == 0)
356 {
357 skip("Required size is null; cannot proceed with QueryLockStatusW test\n");
358 goto cleanup;
359 }
360
361 /* Allocate memory */
362 lpLockStatus = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
363 if (lpLockStatus == NULL)
364 {
365 skip("Cannot allocate %lu bytes of memory\n", dwRequiredSize);
366 goto cleanup;
367 }
368
369 /* Get the actual value */
370 SetLastError(0xdeadbeef);
371 bError = QueryServiceLockStatusW(hScm,
372 lpLockStatus,
373 dwRequiredSize,
374 &dwRequiredSize);
375 ok(bError, "bError = %u, expected TRUE\n", bError);
376
377 /* These conditions must be verified iff the services database wasn't previously locked */
378 ok(lpLockStatus->fIsLocked == 0, "lpLockStatus->fIsLocked = %lu, expected 0\n", lpLockStatus->fIsLocked);
379 ok(lpLockStatus->lpLockOwner != NULL, "lpLockStatus->lpLockOwner is null, expected non-null\n");
380 ok(lpLockStatus->lpLockOwner && *lpLockStatus->lpLockOwner == 0, "*lpLockStatus->lpLockOwner != \"\\0\", expected \"\\0\"\n");
381 ok(lpLockStatus->dwLockDuration == 0, "lpLockStatus->dwLockDuration = %lu, expected 0\n", lpLockStatus->dwLockDuration);
382
383 HeapFree(GetProcessHeap(), 0, lpLockStatus);
384
385
386 /*
387 * Try again, this time with the database locked.
388 */
389
390 SetLastError(0xdeadbeef);
391 hLock = LockServiceDatabase(hScm);
392 ok(hLock != NULL, "hLock = 0x%p, expected non-zero\n", hLock);
393 ok_err(ERROR_SUCCESS);
394
395 Sleep(1000); /* Wait 1 second to let lpLockStatus->dwLockDuration increment */
396
397 /* Get the needed size */
398 SetLastError(0xdeadbeef);
399 bError = QueryServiceLockStatusW(hScm,
400 NULL,
401 0,
402 &dwRequiredSize);
403 ok(bError == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INSUFFICIENT_BUFFER);
404 ok(dwRequiredSize != 0, "dwRequiredSize is zero, expected non-zero\n");
405 if (dwRequiredSize == 0)
406 {
407 skip("Required size is null; cannot proceed with QueryLockStatusW test\n");
408 goto cleanup;
409 }
410
411 /* Allocate memory */
412 lpLockStatus = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
413 if (lpLockStatus == NULL)
414 {
415 skip("Cannot allocate %lu bytes of memory\n", dwRequiredSize);
416 goto cleanup;
417 }
418
419 /* Get the actual value */
420 SetLastError(0xdeadbeef);
421 bError = QueryServiceLockStatusW(hScm,
422 lpLockStatus,
423 dwRequiredSize,
424 &dwRequiredSize);
425 ok(bError, "bError = %u, expected TRUE\n", bError);
426
427 /* These conditions must be verified iff the services database is locked */
428 ok(lpLockStatus->fIsLocked != 0, "lpLockStatus->fIsLocked = %lu, expected non-zero\n", lpLockStatus->fIsLocked);
429 ok(lpLockStatus->lpLockOwner != NULL, "lpLockStatus->lpLockOwner is null, expected non-null\n");
430 ok(lpLockStatus->lpLockOwner && *lpLockStatus->lpLockOwner != 0, "*lpLockStatus->lpLockOwner = \"\\0\", expected non-zero\n");
431 ok(lpLockStatus->dwLockDuration != 0, "lpLockStatus->dwLockDuration = %lu, expected non-zero\n", lpLockStatus->dwLockDuration);
432
433 HeapFree(GetProcessHeap(), 0, lpLockStatus);
434
435
436 /*
437 * Last try, with the database again unlocked.
438 */
439
440 SetLastError(0xdeadbeef);
441 bError = UnlockServiceDatabase(hLock);
442 ok(bError == TRUE, "bError = %u, expected TRUE\n", bError);
443 ok_err(ERROR_SUCCESS);
444 hLock = NULL;
445
446 /* Get the needed size */
447 SetLastError(0xdeadbeef);
448 bError = QueryServiceLockStatusW(hScm,
449 NULL,
450 0,
451 &dwRequiredSize);
452 ok(bError == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INSUFFICIENT_BUFFER);
453 ok(dwRequiredSize != 0, "dwRequiredSize is zero, expected non-zero\n");
454 if (dwRequiredSize == 0)
455 {
456 skip("Required size is null; cannot proceed with QueryLockStatusW test\n");
457 goto cleanup;
458 }
459
460 /* Allocate memory */
461 lpLockStatus = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
462 if (lpLockStatus == NULL)
463 {
464 skip("Cannot allocate %lu bytes of memory\n", dwRequiredSize);
465 goto cleanup;
466 }
467
468 /* Get the actual value */
469 SetLastError(0xdeadbeef);
470 bError = QueryServiceLockStatusW(hScm,
471 lpLockStatus,
472 dwRequiredSize,
473 &dwRequiredSize);
474 ok(bError, "bError = %u, expected TRUE\n", bError);
475
476 /* These conditions must be verified iff the services database is unlocked */
477 ok(lpLockStatus->fIsLocked == 0, "lpLockStatus->fIsLocked = %lu, expected 0\n", lpLockStatus->fIsLocked);
478 ok(lpLockStatus->lpLockOwner != NULL, "lpLockStatus->lpLockOwner is null, expected non-null\n");
479 ok(lpLockStatus->lpLockOwner && *lpLockStatus->lpLockOwner == 0, "*lpLockStatus->lpLockOwner != \"\\0\", expected \"\\0\"\n");
480 ok(lpLockStatus->dwLockDuration == 0, "lpLockStatus->dwLockDuration = %lu, expected 0\n", lpLockStatus->dwLockDuration);
481
482 HeapFree(GetProcessHeap(), 0, lpLockStatus);
483
484
485 cleanup:
486 if (hLock)
487 UnlockServiceDatabase(hLock);
488
489 if (hScm)
490 CloseServiceHandle(hScm);
491
492 return;
493 }
494
495 static void Test_QueryLockStatusA(void)
496 {
497 BOOL bError = FALSE;
498
499 SC_HANDLE hScm = NULL;
500 SC_LOCK hLock = NULL;
501 LPQUERY_SERVICE_LOCK_STATUSA lpLockStatus = NULL;
502 DWORD dwRequiredSize = 0;
503
504 /* Firstly try to get lock status with invalid handles */
505 SetLastError(0xdeadbeef);
506 bError = QueryServiceLockStatusA(hScm,
507 NULL,
508 0,
509 &dwRequiredSize);
510 ok(bError == FALSE && GetLastError() == ERROR_INVALID_HANDLE, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INVALID_HANDLE);
511 ok(dwRequiredSize == 0, "dwRequiredSize is non-zero, expected zero\n");
512
513 /* Open the services database without having rights */
514 SetLastError(0xdeadbeef);
515 hScm = OpenSCManagerA(NULL, NULL, SC_MANAGER_CONNECT);
516 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
517 if (!hScm)
518 {
519 skip("No service control manager; cannot proceed with QueryLockStatusA test\n");
520 goto cleanup;
521 }
522 ok_err(ERROR_SUCCESS);
523
524 /* Try to get lock status */
525 SetLastError(0xdeadbeef);
526 bError = QueryServiceLockStatusA(hScm,
527 NULL,
528 0,
529 &dwRequiredSize);
530 ok(bError == FALSE && GetLastError() == ERROR_ACCESS_DENIED, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_ACCESS_DENIED);
531 ok(dwRequiredSize == 0, "dwRequiredSize is non-zero, expected zero\n");
532
533 CloseServiceHandle(hScm);
534
535
536 /*
537 * Query only the lock status.
538 */
539
540 SetLastError(0xdeadbeef);
541 hScm = OpenSCManagerA(NULL, NULL, SC_MANAGER_QUERY_LOCK_STATUS);
542 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
543 if (!hScm)
544 {
545 skip("No service control manager; cannot proceed with QueryLockStatusA test\n");
546 goto cleanup;
547 }
548 ok_err(ERROR_SUCCESS);
549
550 /* Get the needed size */
551 SetLastError(0xdeadbeef);
552 bError = QueryServiceLockStatusA(hScm,
553 NULL,
554 0,
555 &dwRequiredSize);
556 ok(bError == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INSUFFICIENT_BUFFER);
557 ok(dwRequiredSize != 0, "dwRequiredSize is zero, expected non-zero\n");
558 if (dwRequiredSize == 0)
559 {
560 skip("Required size is null; cannot proceed with QueryLockStatusA test\n");
561 goto cleanup;
562 }
563
564 /* Allocate memory */
565 lpLockStatus = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
566 if (lpLockStatus == NULL)
567 {
568 skip("Cannot allocate %lu bytes of memory\n", dwRequiredSize);
569 goto cleanup;
570 }
571
572 /* Get the actual value */
573 SetLastError(0xdeadbeef);
574 bError = QueryServiceLockStatusA(hScm,
575 lpLockStatus,
576 dwRequiredSize,
577 &dwRequiredSize);
578 ok(bError, "bError = %u, expected TRUE\n", bError);
579
580 /* These conditions must be verified iff the services database wasn't previously locked */
581 ok(lpLockStatus->fIsLocked == 0, "lpLockStatus->fIsLocked = %lu, expected 0\n", lpLockStatus->fIsLocked);
582 ok(lpLockStatus->lpLockOwner != NULL, "lpLockStatus->lpLockOwner is null, expected non-null\n");
583 ok(lpLockStatus->lpLockOwner && *lpLockStatus->lpLockOwner == 0, "*lpLockStatus->lpLockOwner != \"\\0\", expected \"\\0\"\n");
584 ok(lpLockStatus->dwLockDuration == 0, "lpLockStatus->dwLockDuration = %lu, expected 0\n", lpLockStatus->dwLockDuration);
585
586 HeapFree(GetProcessHeap(), 0, lpLockStatus);
587
588 CloseServiceHandle(hScm);
589
590
591 /*
592 * Now, try to lock the database and check its lock status.
593 */
594
595 SetLastError(0xdeadbeef);
596 hScm = OpenSCManagerA(NULL, NULL, SC_MANAGER_LOCK | SC_MANAGER_QUERY_LOCK_STATUS);
597 ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
598 if (!hScm)
599 {
600 skip("No service control manager; cannot proceed with QueryLockStatusA test\n");
601 goto cleanup;
602 }
603 ok_err(ERROR_SUCCESS);
604
605 /* Get the needed size */
606 SetLastError(0xdeadbeef);
607 bError = QueryServiceLockStatusA(hScm,
608 NULL,
609 0,
610 &dwRequiredSize);
611 ok(bError == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INSUFFICIENT_BUFFER);
612 ok(dwRequiredSize != 0, "dwRequiredSize is zero, expected non-zero\n");
613 if (dwRequiredSize == 0)
614 {
615 skip("Required size is null; cannot proceed with QueryLockStatusA test\n");
616 goto cleanup;
617 }
618
619 /* Allocate memory */
620 lpLockStatus = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
621 if (lpLockStatus == NULL)
622 {
623 skip("Cannot allocate %lu bytes of memory\n", dwRequiredSize);
624 goto cleanup;
625 }
626
627 /* Get the actual value */
628 SetLastError(0xdeadbeef);
629 bError = QueryServiceLockStatusA(hScm,
630 lpLockStatus,
631 dwRequiredSize,
632 &dwRequiredSize);
633 ok(bError, "bError = %u, expected TRUE\n", bError);
634
635 /* These conditions must be verified iff the services database wasn't previously locked */
636 ok(lpLockStatus->fIsLocked == 0, "lpLockStatus->fIsLocked = %lu, expected 0\n", lpLockStatus->fIsLocked);
637 ok(lpLockStatus->lpLockOwner != NULL, "lpLockStatus->lpLockOwner is null, expected non-null\n");
638 ok(lpLockStatus->lpLockOwner && *lpLockStatus->lpLockOwner == 0, "*lpLockStatus->lpLockOwner != \"\\0\", expected \"\\0\"\n");
639 ok(lpLockStatus->dwLockDuration == 0, "lpLockStatus->dwLockDuration = %lu, expected 0\n", lpLockStatus->dwLockDuration);
640
641 HeapFree(GetProcessHeap(), 0, lpLockStatus);
642
643
644 /*
645 * Try again, this time with the database locked.
646 */
647
648 SetLastError(0xdeadbeef);
649 hLock = LockServiceDatabase(hScm);
650 ok(hLock != NULL, "hLock = 0x%p, expected non-zero\n", hLock);
651 ok_err(ERROR_SUCCESS);
652
653 Sleep(1000); /* Wait 1 second to let lpLockStatus->dwLockDuration increment */
654
655 /* Get the needed size */
656 SetLastError(0xdeadbeef);
657 bError = QueryServiceLockStatusA(hScm,
658 NULL,
659 0,
660 &dwRequiredSize);
661 ok(bError == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INSUFFICIENT_BUFFER);
662 ok(dwRequiredSize != 0, "dwRequiredSize is zero, expected non-zero\n");
663 if (dwRequiredSize == 0)
664 {
665 skip("Required size is null; cannot proceed with QueryLockStatusA test\n");
666 goto cleanup;
667 }
668
669 /* Allocate memory */
670 lpLockStatus = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
671 if (lpLockStatus == NULL)
672 {
673 skip("Cannot allocate %lu bytes of memory\n", dwRequiredSize);
674 goto cleanup;
675 }
676
677 /* Get the actual value */
678 SetLastError(0xdeadbeef);
679 bError = QueryServiceLockStatusA(hScm,
680 lpLockStatus,
681 dwRequiredSize,
682 &dwRequiredSize);
683 ok(bError, "bError = %u, expected TRUE\n", bError);
684
685 /* These conditions must be verified iff the services database is locked */
686 ok(lpLockStatus->fIsLocked != 0, "lpLockStatus->fIsLocked = %lu, expected non-zero\n", lpLockStatus->fIsLocked);
687 ok(lpLockStatus->lpLockOwner != NULL, "lpLockStatus->lpLockOwner is null, expected non-null\n");
688 ok(lpLockStatus->lpLockOwner && *lpLockStatus->lpLockOwner != 0, "*lpLockStatus->lpLockOwner = \"\\0\", expected non-zero\n");
689 ok(lpLockStatus->dwLockDuration != 0, "lpLockStatus->dwLockDuration = %lu, expected non-zero\n", lpLockStatus->dwLockDuration);
690
691 HeapFree(GetProcessHeap(), 0, lpLockStatus);
692
693
694 /*
695 * Last try, with the database again unlocked.
696 */
697
698 SetLastError(0xdeadbeef);
699 bError = UnlockServiceDatabase(hLock);
700 ok(bError == TRUE, "bError = %u, expected TRUE\n", bError);
701 ok_err(ERROR_SUCCESS);
702 hLock = NULL;
703
704 /* Get the needed size */
705 SetLastError(0xdeadbeef);
706 bError = QueryServiceLockStatusA(hScm,
707 NULL,
708 0,
709 &dwRequiredSize);
710 ok(bError == FALSE && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "(bError, GetLastError()) = (%u, 0x%08lx), expected (FALSE, 0x%08lx)\n", bError, GetLastError(), (DWORD)ERROR_INSUFFICIENT_BUFFER);
711 ok(dwRequiredSize != 0, "dwRequiredSize is zero, expected non-zero\n");
712 if (dwRequiredSize == 0)
713 {
714 skip("Required size is null; cannot proceed with QueryLockStatusA test\n");
715 goto cleanup;
716 }
717
718 /* Allocate memory */
719 lpLockStatus = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwRequiredSize);
720 if (lpLockStatus == NULL)
721 {
722 skip("Cannot allocate %lu bytes of memory\n", dwRequiredSize);
723 goto cleanup;
724 }
725
726 /* Get the actual value */
727 SetLastError(0xdeadbeef);
728 bError = QueryServiceLockStatusA(hScm,
729 lpLockStatus,
730 dwRequiredSize,
731 &dwRequiredSize);
732 ok(bError, "bError = %u, expected TRUE\n", bError);
733
734 /* These conditions must be verified iff the services database is unlocked */
735 ok(lpLockStatus->fIsLocked == 0, "lpLockStatus->fIsLocked = %lu, expected 0\n", lpLockStatus->fIsLocked);
736 ok(lpLockStatus->lpLockOwner != NULL, "lpLockStatus->lpLockOwner is null, expected non-null\n");
737 ok(lpLockStatus->lpLockOwner && *lpLockStatus->lpLockOwner == 0, "*lpLockStatus->lpLockOwner != \"\\0\", expected \"\\0\"\n");
738 ok(lpLockStatus->dwLockDuration == 0, "lpLockStatus->dwLockDuration = %lu, expected 0\n", lpLockStatus->dwLockDuration);
739
740 HeapFree(GetProcessHeap(), 0, lpLockStatus);
741
742
743 cleanup:
744 if (hLock)
745 UnlockServiceDatabase(hLock);
746
747 if (hScm)
748 CloseServiceHandle(hScm);
749
750 return;
751 }
752
753
754 START_TEST(LockDatabase)
755 {
756 Test_LockUnlockServiceDatabase();
757 Test_LockUnlockServiceDatabaseWithServiceStart();
758 Test_QueryLockStatusW();
759 Test_QueryLockStatusA();
760 }