[IPHLPAPI_APITEST]
[reactos.git] / rostests / apitests / setupapi / SetupInstallServicesFromInfSectionEx.c
1 /*
2 * SetupAPI device class-related functions tests
3 *
4 * Copyright 2015 Víctor Martínez (victor.martinez@reactos.org)
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public Licence as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this library; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <apitest.h>
22 #include <stdio.h>
23 #include <assert.h>
24 #include <winuser.h>
25 #include <winreg.h>
26 #include <winsvc.h>
27 #include <setupapi.h>
28
29 static const char inffile[] = "test.inf";
30 static char CURR_DIR[MAX_PATH];
31
32 /* Helpers */
33
34 static void create_inf_file(LPCSTR filename, const char *data)
35 {
36 DWORD res;
37 BOOL ret;
38 HANDLE handle = CreateFileA(filename, GENERIC_WRITE, 0, NULL,
39 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
40 assert(handle != INVALID_HANDLE_VALUE);
41 ret = WriteFile(handle, data, strlen(data), &res, NULL);
42 assert(ret != 0);
43 CloseHandle(handle);
44 }
45
46 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
47 static HHOOK hhook;
48 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
49 {
50 return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
51 }
52
53 static void test_SetupInstallServicesFromInfSectionExA(void)
54 {
55 char inf[2048];
56 char path[MAX_PATH];
57 HINF infhandle;
58 BOOL ret;
59 SC_HANDLE scm_handle, svc_handle;
60
61 /* Bail out if we are on win98 */
62 SetLastError(0xdeadbeef);
63 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
64
65 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
66 {
67 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
68 return;
69 }
70 CloseServiceHandle(scm_handle);
71
72 /* Basic inf file to satisfy SetupOpenInfFileA */
73 strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
74 create_inf_file(inffile, inf);
75 sprintf(path, "%s\\%s", CURR_DIR, inffile);
76 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
77
78 /* Nothing but the Version section */
79 SetLastError(0xdeadbeef);
80 ret = SetupInstallServicesFromInfSectionExA(infhandle, "trolololo", 0, NULL, NULL, NULL,NULL);
81 ok(!ret, "Expected failure\n");
82 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
83 "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
84 SetupCloseInfFile(infhandle);
85 DeleteFileA(inffile);
86
87
88 /* Add the section */
89 strcat(inf, "[Winetest.Services]\n");
90 create_inf_file(inffile, inf);
91 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
92 SetLastError(0xdeadbeef);
93 ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
94 ok(!ret, "Expected failure\n");
95 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
96 "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
97 SetupCloseInfFile(infhandle);
98 DeleteFileA(inffile);
99
100 /* Add a reference */
101 strcat(inf, "AddService=Winetest,,Winetest.Service\n");
102 create_inf_file(inffile, inf);
103 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
104 SetLastError(0xdeadbeef);
105 ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
106 ok(!ret, "Expected failure\n");
107 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
108 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
109 SetupCloseInfFile(infhandle);
110 DeleteFileA(inffile);
111
112 /* Add the section */
113 strcat(inf, "[Winetest.Service]\n");
114 create_inf_file(inffile, inf);
115 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
116 SetLastError(0xdeadbeef);
117 ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
118 ok(!ret, "Expected failure\n");
119 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
120 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
121 SetupCloseInfFile(infhandle);
122 DeleteFileA(inffile);
123
124 /* Just the ServiceBinary */
125 strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
126 create_inf_file(inffile, inf);
127 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
128 SetLastError(0xdeadbeef);
129 ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
130 ok(!ret, "Expected failure\n");
131 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
132 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
133 SetupCloseInfFile(infhandle);
134 DeleteFileA(inffile);
135
136 /* Add the ServiceType */
137 strcat(inf, "ServiceType=1\n");
138 create_inf_file(inffile, inf);
139 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
140 SetLastError(0xdeadbeef);
141 ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
142 ok(!ret, "Expected failure\n");
143 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
144 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
145 SetupCloseInfFile(infhandle);
146 DeleteFileA(inffile);
147
148 /* Add the StartType */
149 strcat(inf, "StartType=4\n");
150 create_inf_file(inffile, inf);
151 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
152 SetLastError(0xdeadbeef);
153 ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
154 ok(!ret, "Expected failure\n");
155 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
156 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
157 SetupCloseInfFile(infhandle);
158 DeleteFileA(inffile);
159
160 /* This should be it, the minimal entries to install a service */
161 strcat(inf, "ErrorControl=1");
162 create_inf_file(inffile, inf);
163 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
164 SetLastError(0xdeadbeef);
165 ret = SetupInstallServicesFromInfSectionExA(infhandle, "Winetest.Services", 0, NULL, NULL, NULL, NULL);
166 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
167 {
168 skip("Not enough rights to install the service\n");
169 SetupCloseInfFile(infhandle);
170 DeleteFileA(inffile);
171 return;
172 }
173 ok(ret, "Expected success\n");
174 ok(GetLastError() == ERROR_SUCCESS,
175 "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
176 SetupCloseInfFile(infhandle);
177 DeleteFileA(inffile);
178
179 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
180
181 /* Open the service to see if it's really there */
182 svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
183 ok(svc_handle != NULL, "Service was not created\n");
184
185 SetLastError(0xdeadbeef);
186 ret = DeleteService(svc_handle);
187 ok(ret, "Service could not be deleted : %d\n", (int)GetLastError());
188
189 CloseServiceHandle(svc_handle);
190 CloseServiceHandle(scm_handle);
191
192 strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
193 strcat(inf, "[XSP.InstallPerVer]\n");
194 strcat(inf, "AddReg=AspEventlogMsg.Reg,Perf.Reg,AspVersions.Reg,FreeADO.Reg,IndexServer.Reg\n");
195 create_inf_file(inffile, inf);
196 sprintf(path, "%s\\%s", CURR_DIR, inffile);
197 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
198
199 SetLastError(0xdeadbeef);
200 ret = SetupInstallServicesFromInfSectionExA(infhandle, "XSP.InstallPerVer", 0, NULL, NULL, NULL,NULL);
201 ok(ret, "Expected success\n");
202 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
203 SetupCloseInfFile(infhandle);
204 DeleteFileA(inffile);
205
206 /* TODO: Test the Flags */
207 }
208 static void test_SetupInstallServicesFromInfSectionExW(void)
209 {
210 char inf[2048];
211 char path[MAX_PATH];
212 HINF infhandle;
213 BOOL ret;
214 SC_HANDLE scm_handle, svc_handle;
215
216 /* Bail out if we are on win98 */
217 SetLastError(0xdeadbeef);
218 scm_handle = OpenSCManagerW(NULL, NULL, GENERIC_ALL);
219
220 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
221 {
222 win_skip("OpenSCManagerW is not implemented, we are most likely on win9x\n");
223 return;
224 }
225 CloseServiceHandle(scm_handle);
226
227 /* Basic inf file to satisfy SetupOpenInfFileA */
228 strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
229 create_inf_file(inffile, inf);
230 sprintf(path, "%s\\%s", CURR_DIR, inffile);
231 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
232
233 /* Nothing but the Version section */
234 SetLastError(0xdeadbeef);
235 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"trolololo", 0, NULL, NULL, NULL,NULL);
236 ok(!ret, "Expected failure\n");
237 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
238 "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
239 SetupCloseInfFile(infhandle);
240 DeleteFileA(inffile);
241
242
243 /* Add the section */
244 strcat(inf, "[Winetest.Services]\n");
245 create_inf_file(inffile, inf);
246 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
247 SetLastError(0xdeadbeef);
248 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
249 ok(!ret, "Expected failure\n");
250 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
251 "Expected ERROR_SECTION_NOT_FOUND, got %08d\n", (int)GetLastError());
252 SetupCloseInfFile(infhandle);
253 DeleteFileA(inffile);
254
255 /* Add a reference */
256 strcat(inf, "AddService=Winetest,,Winetest.Service\n");
257 create_inf_file(inffile, inf);
258 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
259 SetLastError(0xdeadbeef);
260 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
261 ok(!ret, "Expected failure\n");
262 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
263 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
264 SetupCloseInfFile(infhandle);
265 DeleteFileA(inffile);
266
267 /* Add the section */
268 strcat(inf, "[Winetest.Service]\n");
269 create_inf_file(inffile, inf);
270 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
271 SetLastError(0xdeadbeef);
272 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
273 ok(!ret, "Expected failure\n");
274 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
275 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
276 SetupCloseInfFile(infhandle);
277 DeleteFileA(inffile);
278
279 /* Just the ServiceBinary */
280 strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
281 create_inf_file(inffile, inf);
282 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
283 SetLastError(0xdeadbeef);
284 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
285 ok(!ret, "Expected failure\n");
286 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
287 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
288 SetupCloseInfFile(infhandle);
289 DeleteFileA(inffile);
290
291 /* Add the ServiceType */
292 strcat(inf, "ServiceType=1\n");
293 create_inf_file(inffile, inf);
294 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
295 SetLastError(0xdeadbeef);
296 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
297 ok(!ret, "Expected failure\n");
298 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
299 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
300 SetupCloseInfFile(infhandle);
301 DeleteFileA(inffile);
302
303 /* Add the StartType */
304 strcat(inf, "StartType=4\n");
305 create_inf_file(inffile, inf);
306 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
307 SetLastError(0xdeadbeef);
308 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
309 ok(!ret, "Expected failure\n");
310 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
311 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08d\n", (int)GetLastError());
312 SetupCloseInfFile(infhandle);
313 DeleteFileA(inffile);
314
315 /* This should be it, the minimal entries to install a service */
316 strcat(inf, "ErrorControl=1");
317 create_inf_file(inffile, inf);
318 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
319 SetLastError(0xdeadbeef);
320 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"Winetest.Services", 0, NULL, NULL, NULL, NULL);
321 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
322 {
323 skip("Not enough rights to install the service\n");
324 SetupCloseInfFile(infhandle);
325 DeleteFileA(inffile);
326 return;
327 }
328 ok(ret, "Expected success\n");
329 ok(GetLastError() == ERROR_SUCCESS,
330 "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
331 SetupCloseInfFile(infhandle);
332 DeleteFileA(inffile);
333
334 scm_handle = OpenSCManagerW(NULL, NULL, GENERIC_ALL);
335
336 /* Open the service to see if it's really there */
337 svc_handle = OpenServiceW(scm_handle, L"Winetest", DELETE);
338 ok(svc_handle != NULL, "Service was not created\n");
339
340 SetLastError(0xdeadbeef);
341 ret = DeleteService(svc_handle);
342 ok(ret, "Service could not be deleted : %d\n", (int)GetLastError());
343
344 CloseServiceHandle(svc_handle);
345 CloseServiceHandle(scm_handle);
346
347 strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
348 strcat(inf, "[XSP.InstallPerVer]\n");
349 strcat(inf, "AddReg=AspEventlogMsg.Reg,Perf.Reg,AspVersions.Reg,FreeADO.Reg,IndexServer.Reg\n");
350 create_inf_file(inffile, inf);
351 sprintf(path, "%s\\%s", CURR_DIR, inffile);
352 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
353
354 SetLastError(0xdeadbeef);
355 ret = SetupInstallServicesFromInfSectionExW(infhandle, L"XSP.InstallPerVer", 0, NULL, NULL, NULL, NULL);
356 ok(ret, "Expected success\n");
357 ok(GetLastError() == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %08d\n", (int)GetLastError());
358 SetupCloseInfFile(infhandle);
359 DeleteFileA(inffile);
360
361 /* TODO: Test the Flags */
362 }
363
364
365 START_TEST(SetupInstallServicesFromInfSectionEx)
366 {
367 char temp_path[MAX_PATH], prev_path[MAX_PATH];
368
369 GetCurrentDirectoryA(MAX_PATH, prev_path);
370 GetTempPathA(MAX_PATH, temp_path);
371 SetCurrentDirectoryA(temp_path);
372
373 strcpy(CURR_DIR, temp_path);
374
375 /* Set CBT hook to disallow MessageBox creation in current thread */
376 hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
377 assert(hhook != 0);
378
379 test_SetupInstallServicesFromInfSectionExA();
380 test_SetupInstallServicesFromInfSectionExW();
381
382 UnhookWindowsHookEx(hhook);
383 SetCurrentDirectoryA(prev_path);
384 }