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