1c2c66affSColin Finck /*
2c2c66affSColin Finck  * PROJECT:         ReactOS api tests
3c2c66affSColin Finck  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4c2c66affSColin Finck  * PURPOSE:         Test for CreateService
5c2c66affSColin Finck  * PROGRAMMER:      Thomas Faber <thomas.faber@reactos.org>
6c2c66affSColin Finck  */
7c2c66affSColin Finck 
8*24f89469SAmine Khaldi #include "precomp.h"
9c2c66affSColin Finck 
MakeService(SC_HANDLE hScm,const wchar_t * serviceName,SC_HANDLE * hService,DWORD * tag)10c2c66affSColin Finck static int MakeService(SC_HANDLE hScm, const wchar_t *serviceName, SC_HANDLE *hService, DWORD *tag)
11c2c66affSColin Finck {
12c2c66affSColin Finck     DWORD ret;
13c2c66affSColin Finck     HKEY hKey = NULL;
14c2c66affSColin Finck     DWORD type = 0, tagData = 0, tagSize;
15c2c66affSColin Finck     wchar_t keyName[256];
16c2c66affSColin Finck 
17c2c66affSColin Finck     SetLastError(DNS_ERROR_RCODE_NXRRSET);
18c2c66affSColin Finck     *hService = CreateServiceW(
19c2c66affSColin Finck                     hScm,
20c2c66affSColin Finck                     serviceName,
21c2c66affSColin Finck                     NULL,
22c2c66affSColin Finck                     DELETE,
23c2c66affSColin Finck                     SERVICE_KERNEL_DRIVER,
24c2c66affSColin Finck                     SERVICE_BOOT_START,
25c2c66affSColin Finck                     SERVICE_ERROR_IGNORE,
26c2c66affSColin Finck                     L"%systemroot%\\drivers\\win32k.sys",
27c2c66affSColin Finck                     L"advapi32_apitest_CreateService_Test_Group",
28c2c66affSColin Finck                     tag,
29c2c66affSColin Finck                     NULL,
30c2c66affSColin Finck                     NULL,
31c2c66affSColin Finck                     NULL);
32c2c66affSColin Finck 
33c2c66affSColin Finck     ok(*hService != NULL, "Failed to create service, error=0x%08lx\n", GetLastError());
34c2c66affSColin Finck     if (!*hService)
35c2c66affSColin Finck     {
36c2c66affSColin Finck         skip("No service; cannot proceed with CreateService test\n");
37c2c66affSColin Finck         return 1;
38c2c66affSColin Finck     }
39c2c66affSColin Finck 
40c2c66affSColin Finck     ok_err(ERROR_SUCCESS);
41c2c66affSColin Finck 
42c2c66affSColin Finck     ok(*tag != 0, "tag is zero, expected nonzero\n");
43c2c66affSColin Finck 
44c2c66affSColin Finck     StringCbPrintfW(keyName, sizeof keyName, L"System\\CurrentControlSet\\Services\\%ls", serviceName);
45c2c66affSColin Finck     ret = RegOpenKeyW(HKEY_LOCAL_MACHINE, keyName, &hKey);
46c2c66affSColin Finck     ok(ret == ERROR_SUCCESS, "RegOpenKeyW failed with 0x%08lx\n", ret);
47c2c66affSColin Finck     if (ret)
48c2c66affSColin Finck     {
49c2c66affSColin Finck         skip("No regkey; cannot proceed with CreateService test\n");
50c2c66affSColin Finck         return 2;
51c2c66affSColin Finck     }
52c2c66affSColin Finck 
53c2c66affSColin Finck     tagSize = sizeof tagData;
54c2c66affSColin Finck     ret = RegQueryValueExW(hKey, L"Tag", NULL, &type, (PBYTE)&tagData, &tagSize);
55c2c66affSColin Finck     ok(ret == ERROR_SUCCESS, "RegQueryValueExW returned 0x%08lx\n", ret);
56c2c66affSColin Finck     ok(type == REG_DWORD, "type=%lu, expected REG_DWORD\n", type);
57c2c66affSColin Finck     ok(tagSize == sizeof tagData, "tagSize=%lu, expected 4\n", tagSize);
58c2c66affSColin Finck     ok(tagData == *tag, "tag=%lu, but registry says %lu\n", *tag, tagData);
59c2c66affSColin Finck 
60c2c66affSColin Finck     RegCloseKey(hKey);
61c2c66affSColin Finck 
62c2c66affSColin Finck     return 0;
63c2c66affSColin Finck }
64c2c66affSColin Finck 
DestroyService(SC_HANDLE hService)65c2c66affSColin Finck static void DestroyService(SC_HANDLE hService)
66c2c66affSColin Finck {
67c2c66affSColin Finck     LONG ret;
68c2c66affSColin Finck 
69c2c66affSColin Finck     if (!hService)
70c2c66affSColin Finck         return;
71c2c66affSColin Finck     SetLastError(DNS_ERROR_RCODE_NXRRSET);
72c2c66affSColin Finck     ret = DeleteService(hService);
73c2c66affSColin Finck     ok(ret == TRUE, "DeleteService returned %ld, expected 1\n", ret);
74c2c66affSColin Finck     ok(GetLastError() == DNS_ERROR_RCODE_NXRRSET /* win7 */
75c2c66affSColin Finck         || GetLastError() == ERROR_SUCCESS /* win2k3 */, "DeleteService GetLastError()=0x%08lx\n", GetLastError());
76c2c66affSColin Finck 
77c2c66affSColin Finck     CloseServiceHandle(hService);
78c2c66affSColin Finck }
79c2c66affSColin Finck 
Test_CreateService(void)80c2c66affSColin Finck static void Test_CreateService(void)
81c2c66affSColin Finck {
82c2c66affSColin Finck     SC_HANDLE hScm = NULL;
83c2c66affSColin Finck     SC_HANDLE hService1 = NULL, hService2 = NULL;
84c2c66affSColin Finck     SC_HANDLE hService3 = NULL;
85c2c66affSColin Finck     DWORD tag1 = 0, tag2 = 0;
86c2c66affSColin Finck     DWORD tag3 = 785;
87c2c66affSColin Finck 
88c2c66affSColin Finck     SetLastError(DNS_ERROR_RCODE_NXRRSET);
89c2c66affSColin Finck     hScm = OpenSCManagerW(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
90c2c66affSColin Finck     ok(hScm != NULL, "Failed to open service manager, error=0x%08lx\n", GetLastError());
91c2c66affSColin Finck     if (!hScm)
92c2c66affSColin Finck     {
93c2c66affSColin Finck         skip("No service control manager; cannot proceed with CreateService test\n");
94c2c66affSColin Finck         goto cleanup;
95c2c66affSColin Finck     }
96c2c66affSColin Finck 
97c2c66affSColin Finck     ok_err(ERROR_SUCCESS);
98c2c66affSColin Finck 
99c2c66affSColin Finck     if (MakeService(hScm, L"advapi32_apitest_CreateService_Test_Service1", &hService1, &tag1))
100c2c66affSColin Finck         goto cleanup;
101c2c66affSColin Finck 
102c2c66affSColin Finck     if (MakeService(hScm, L"advapi32_apitest_CreateService_Test_Service2", &hService2, &tag2))
103c2c66affSColin Finck         goto cleanup;
104c2c66affSColin Finck 
105c2c66affSColin Finck     ok(tag1 != tag2, "tag1=%lu, tag2=%lu\n", tag1, tag2);
106c2c66affSColin Finck 
107c2c66affSColin Finck     /* ask for a tag, but don't have a group */
108c2c66affSColin Finck     hService3 = CreateServiceW(
109c2c66affSColin Finck                     hScm,
110c2c66affSColin Finck                     L"advapi32_apitest_CreateService_Test_Service2",
111c2c66affSColin Finck                     NULL,
112c2c66affSColin Finck                     DELETE,
113c2c66affSColin Finck                     SERVICE_KERNEL_DRIVER,
114c2c66affSColin Finck                     SERVICE_BOOT_START,
115c2c66affSColin Finck                     SERVICE_ERROR_IGNORE,
116c2c66affSColin Finck                     L"%systemroot%\\drivers\\win32k.sys",
117c2c66affSColin Finck                     NULL,
118c2c66affSColin Finck                     &tag3,
119c2c66affSColin Finck                     NULL,
120c2c66affSColin Finck                     NULL,
121c2c66affSColin Finck                     NULL);
122c2c66affSColin Finck     ok(hService3 == NULL, "hService3=%p\n", hService3);
123c2c66affSColin Finck     ok(GetLastError() == ERROR_INVALID_PARAMETER, "error=%lu\n", GetLastError());
124c2c66affSColin Finck     ok(tag3 == 785, "tag3=%lu\n", tag3);
125c2c66affSColin Finck     DestroyService(hService3);
126c2c66affSColin Finck 
127c2c66affSColin Finck     hService3 = CreateServiceW(
128c2c66affSColin Finck                     hScm,
129c2c66affSColin Finck                     L"advapi32_apitest_CreateService_Test_Service2",
130c2c66affSColin Finck                     NULL,
131c2c66affSColin Finck                     DELETE,
132c2c66affSColin Finck                     SERVICE_KERNEL_DRIVER,
133c2c66affSColin Finck                     SERVICE_BOOT_START,
134c2c66affSColin Finck                     SERVICE_ERROR_IGNORE,
135c2c66affSColin Finck                     L"%systemroot%\\drivers\\win32k.sys",
136c2c66affSColin Finck                     L"",
137c2c66affSColin Finck                     &tag3,
138c2c66affSColin Finck                     NULL,
139c2c66affSColin Finck                     NULL,
140c2c66affSColin Finck                     NULL);
141c2c66affSColin Finck     ok(hService3 == NULL, "hService3=%p\n", hService3);
142c2c66affSColin Finck     ok(GetLastError() == ERROR_INVALID_PARAMETER, "error=%lu\n", GetLastError());
143c2c66affSColin Finck     ok(tag3 == 785, "tag3=%lu\n", tag3);
144c2c66affSColin Finck     DestroyService(hService3);
145c2c66affSColin Finck 
146c2c66affSColin Finck cleanup:
147c2c66affSColin Finck 
148c2c66affSColin Finck     DestroyService(hService2);
149c2c66affSColin Finck     DestroyService(hService1);
150c2c66affSColin Finck 
151c2c66affSColin Finck     if (hScm)
152c2c66affSColin Finck         CloseServiceHandle(hScm);
153c2c66affSColin Finck }
154c2c66affSColin Finck 
START_TEST(CreateService)155c2c66affSColin Finck START_TEST(CreateService)
156c2c66affSColin Finck {
157c2c66affSColin Finck     Test_CreateService();
158c2c66affSColin Finck }
159