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