2 * Unit test for setupapi.dll install functions
4 * Copyright 2007 Misha Koshelev
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
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 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "wine/test.h"
36 static const char inffile[] = "test.inf";
37 static char CURR_DIR[MAX_PATH];
39 /* Notes on InstallHinfSectionA/W:
40 * - InstallHinfSectionW on Win98 and InstallHinfSectionA on WinXP seem to be stubs - they do not do anything
41 * and simply return without displaying any error message or setting last error. We test whether
42 * InstallHinfSectionA sets last error, and if it doesn't we set it to NULL and use the W version if available.
43 * - These functions do not return a value and do not always set last error to ERROR_SUCCESS when installation still
44 * occurs (e.g., unquoted inf file with spaces, registry keys are written but last error is 6). Also, on Win98 last error
45 * is set to ERROR_SUCCESS even if install fails (e.g., quoted inf file with spaces, no registry keys set, MessageBox with
46 * "Installation Error" displayed). Thus, we must use functional tests (e.g., is registry key created) to determine whether
47 * or not installation occurred.
48 * - On installation problems, a MessageBox() is displayed and a Beep() is issued. The MessageBox() is disabled with a
52 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
53 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
59 static void create_inf_file(LPCSTR filename, const char *data)
62 HANDLE handle = CreateFile(filename, GENERIC_WRITE, 0, NULL,
63 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
64 assert(handle != INVALID_HANDLE_VALUE);
65 assert(WriteFile(handle, data, strlen(data), &res, NULL));
69 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
71 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
73 return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
80 static const char *cmdline_inf = "[Version]\n"
81 "Signature=\"$Chicago$\"\n"
83 "AddReg=Add.Settings\n"
85 "HKCU,Software\\Wine\\setupapitest,,\n";
87 static void run_cmdline(LPCSTR section, int mode, LPCSTR path)
89 CHAR cmdline[MAX_PATH * 2];
91 sprintf(cmdline, "%s %d %s", section, mode, path);
92 if (pInstallHinfSectionA) pInstallHinfSectionA(NULL, NULL, cmdline, 0);
95 WCHAR cmdlinew[MAX_PATH * 2];
96 MultiByteToWideChar(CP_ACP, 0, cmdline, -1, cmdlinew, MAX_PATH*2);
97 pInstallHinfSectionW(NULL, NULL, cmdlinew, 0);
101 static void ok_registry(BOOL expectsuccess)
105 /* Functional tests for success of install and clean up */
106 ret = RegDeleteKey(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
107 ok((expectsuccess && ret == ERROR_SUCCESS) ||
108 (!expectsuccess && ret == ERROR_FILE_NOT_FOUND),
109 "Expected registry key Software\\Wine\\setupapitest to %s, RegDeleteKey returned %d\n",
110 expectsuccess ? "exist" : "not exist",
114 /* Test command line processing */
115 static void test_cmdline(void)
117 static const char infwithspaces[] = "test file.inf";
120 create_inf_file(inffile, cmdline_inf);
121 sprintf(path, "%s\\%s", CURR_DIR, inffile);
122 run_cmdline("DefaultInstall", 128, path);
124 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
126 /* Test handling of spaces in path, unquoted and quoted */
127 create_inf_file(infwithspaces, cmdline_inf);
129 sprintf(path, "%s\\%s", CURR_DIR, infwithspaces);
130 run_cmdline("DefaultInstall", 128, path);
133 sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
134 run_cmdline("DefaultInstall", 128, path);
137 ok(DeleteFile(infwithspaces), "Expected source inf to exist, last error was %d\n", GetLastError());
140 static const char *cmdline_inf_reg = "[Version]\n"
141 "Signature=\"$Chicago$\"\n"
143 "DelReg=Del.Settings\n"
145 "HKCU,Software\\Wine\\setupapitest\n";
147 static void test_registry(void)
153 /* First create a registry structure we would like to be deleted */
154 ok(!RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
155 "Expected RegCreateKeyA to succeed\n");
157 /* Doublecheck if the registry key is present */
158 ok(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
159 "Expected registry key to exist\n");
161 create_inf_file(inffile, cmdline_inf_reg);
162 sprintf(path, "%s\\%s", CURR_DIR, inffile);
163 run_cmdline("DefaultInstall", 128, path);
165 /* Check if the registry key is recursively deleted */
166 res = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest", &key);
168 ok(res == ERROR_FILE_NOT_FOUND, "Didn't expect the registry key to exist\n");
170 if (res == ERROR_SUCCESS)
172 RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest");
173 RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
175 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
178 static void test_install_svc_from(void)
184 SC_HANDLE scm_handle, svc_handle;
186 /* Bail out if we are on win98 */
187 SetLastError(0xdeadbeef);
188 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
190 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
192 skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
195 CloseServiceHandle(scm_handle);
197 /* Basic inf file to satisfy SetupOpenInfFileA */
198 strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
199 create_inf_file(inffile, inf);
200 sprintf(path, "%s\\%s", CURR_DIR, inffile);
201 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
203 /* Nothing but the Version section */
204 SetLastError(0xdeadbeef);
205 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
206 ok(!ret, "Expected failure\n");
207 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
208 "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
209 SetupCloseInfFile(infhandle);
212 /* Add the section */
213 strcat(inf, "[Winetest.Services]\n");
214 create_inf_file(inffile, inf);
215 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
216 SetLastError(0xdeadbeef);
217 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
218 ok(!ret, "Expected failure\n");
219 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
220 "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
221 SetupCloseInfFile(infhandle);
224 /* Add a reference */
225 strcat(inf, "AddService=Winetest,,Winetest.Service\n");
226 create_inf_file(inffile, inf);
227 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
228 SetLastError(0xdeadbeef);
229 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
230 ok(!ret, "Expected failure\n");
231 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
232 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
233 SetupCloseInfFile(infhandle);
236 /* Add the section */
237 strcat(inf, "[Winetest.Service]\n");
238 create_inf_file(inffile, inf);
239 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
240 SetLastError(0xdeadbeef);
241 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
242 ok(!ret, "Expected failure\n");
243 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
244 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
245 SetupCloseInfFile(infhandle);
248 /* Just the ServiceBinary */
249 strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
250 create_inf_file(inffile, inf);
251 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
252 SetLastError(0xdeadbeef);
253 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
254 ok(!ret, "Expected failure\n");
255 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
256 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
257 SetupCloseInfFile(infhandle);
260 /* Add the ServiceType */
261 strcat(inf, "ServiceType=1\n");
262 create_inf_file(inffile, inf);
263 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
264 SetLastError(0xdeadbeef);
265 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
266 ok(!ret, "Expected failure\n");
267 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
268 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
269 SetupCloseInfFile(infhandle);
272 /* Add the StartType */
273 strcat(inf, "StartType=4\n");
274 create_inf_file(inffile, inf);
275 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
276 SetLastError(0xdeadbeef);
277 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
278 ok(!ret, "Expected failure\n");
279 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
280 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
281 SetupCloseInfFile(infhandle);
284 /* This should be it, the minimal entries to install a service */
285 strcat(inf, "ErrorControl=1");
286 create_inf_file(inffile, inf);
287 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
288 SetLastError(0xdeadbeef);
289 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
290 ok(ret, "Expected success\n");
291 ok(GetLastError() == ERROR_SUCCESS,
292 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
293 SetupCloseInfFile(infhandle);
296 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
298 /* Open the service to see if it's really there */
299 svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
300 ok(svc_handle != NULL, "Service was not created\n");
302 SetLastError(0xdeadbeef);
303 ret = DeleteService(svc_handle);
304 ok(ret, "Service could not be deleted : %d\n", GetLastError());
306 CloseServiceHandle(svc_handle);
307 CloseServiceHandle(scm_handle);
309 /* TODO: Test the Flags */
312 static void test_driver_install(void)
315 SC_HANDLE scm_handle, svc_handle;
317 char path[MAX_PATH], windir[MAX_PATH], driver[MAX_PATH];
319 /* Minimal stuff needed */
320 static const char *inf =
322 "Signature=\"$Chicago$\"\n"
323 "[DestinationDirs]\n"
324 "Winetest.DriverFiles=12\n"
326 "CopyFiles=Winetest.DriverFiles\n"
327 "[DefaultInstall.Services]\n"
328 "AddService=Winetest,,Winetest.Service\n"
329 "[Winetest.Service]\n"
330 "ServiceBinary=%12%\\winetest.sys\n"
334 "[Winetest.DriverFiles]\n"
337 /* Bail out if we are on win98 */
338 SetLastError(0xdeadbeef);
339 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
341 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
343 skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
346 CloseServiceHandle(scm_handle);
348 /* Place where we expect the driver to be installed */
349 GetWindowsDirectoryA(windir, MAX_PATH);
350 lstrcpyA(driver, windir);
351 lstrcatA(driver, "\\system32\\drivers\\winetest.sys");
353 /* Create a dummy driver file */
354 handle = CreateFileA("winetest.sys", GENERIC_WRITE, 0, NULL,
355 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
358 create_inf_file(inffile, inf);
359 sprintf(path, "%s\\%s", CURR_DIR, inffile);
360 run_cmdline("DefaultInstall", 128, path);
362 /* Driver should have been installed */
363 attrs = GetFileAttributes(driver);
364 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected driver to exist\n");
366 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
368 /* Open the service to see if it's really there */
369 svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
370 ok(svc_handle != NULL, "Service was not created\n");
372 SetLastError(0xdeadbeef);
373 ret = DeleteService(svc_handle);
374 ok(ret, "Service could not be deleted : %d\n", GetLastError());
376 CloseServiceHandle(svc_handle);
377 CloseServiceHandle(scm_handle);
381 DeleteFile("winetest.sys");
387 HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
388 char temp_path[MAX_PATH], prev_path[MAX_PATH];
391 GetCurrentDirectory(MAX_PATH, prev_path);
392 GetTempPath(MAX_PATH, temp_path);
393 SetCurrentDirectory(temp_path);
395 strcpy(CURR_DIR, temp_path);
396 len = strlen(CURR_DIR);
397 if(len && (CURR_DIR[len - 1] == '\\'))
398 CURR_DIR[len - 1] = 0;
400 pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
401 pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
402 if (pInstallHinfSectionA)
404 /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
405 static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
406 char cmdline[MAX_PATH*2];
407 create_inf_file(inffile, minimal_inf);
408 sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
409 SetLastError(0xdeadbeef);
410 pInstallHinfSectionA(NULL, NULL, cmdline, 0);
411 if (GetLastError() == 0xdeadbeef)
413 skip("InstallHinfSectionA is broken (stub)\n");
414 pInstallHinfSectionA = NULL;
416 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
418 if (!pInstallHinfSectionW && !pInstallHinfSectionA)
419 skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
422 /* Set CBT hook to disallow MessageBox creation in current thread */
423 hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
428 test_install_svc_from();
429 test_driver_install();
431 UnhookWindowsHookEx(hhook);
434 SetCurrentDirectory(prev_path);