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