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
35 #include "wine/test.h"
37 static const char inffile[] = "test.inf";
38 static char CURR_DIR[MAX_PATH];
40 /* Notes on InstallHinfSectionA/W:
41 * - InstallHinfSectionW on Win98 and InstallHinfSectionA on WinXP seem to be stubs - they do not do anything
42 * and simply return without displaying any error message or setting last error. We test whether
43 * InstallHinfSectionA sets last error, and if it doesn't we set it to NULL and use the W version if available.
44 * - These functions do not return a value and do not always set last error to ERROR_SUCCESS when installation still
45 * occurs (e.g., unquoted inf file with spaces, registry keys are written but last error is 6). Also, on Win98 last error
46 * is set to ERROR_SUCCESS even if install fails (e.g., quoted inf file with spaces, no registry keys set, MessageBox with
47 * "Installation Error" displayed). Thus, we must use functional tests (e.g., is registry key created) to determine whether
48 * or not installation occurred.
49 * - On installation problems, a MessageBox() is displayed and a Beep() is issued. The MessageBox() is disabled with a
53 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
54 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
55 static BOOL (WINAPI *pSetupGetInfFileListW)(PCWSTR, DWORD, PWSTR, DWORD, PDWORD);
61 static void create_inf_file(LPCSTR filename, const char *data)
64 HANDLE handle = CreateFile(filename, GENERIC_WRITE, 0, NULL,
65 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
66 assert(handle != INVALID_HANDLE_VALUE);
67 assert(WriteFile(handle, data, strlen(data), &res, NULL));
71 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
73 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
75 return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
82 static const char *cmdline_inf = "[Version]\n"
83 "Signature=\"$Chicago$\"\n"
85 "AddReg=Add.Settings\n"
87 "HKCU,Software\\Wine\\setupapitest,,\n";
89 static void run_cmdline(LPCSTR section, int mode, LPCSTR path)
91 CHAR cmdline[MAX_PATH * 2];
93 sprintf(cmdline, "%s %d %s", section, mode, path);
94 if (pInstallHinfSectionA) pInstallHinfSectionA(NULL, NULL, cmdline, 0);
97 WCHAR cmdlinew[MAX_PATH * 2];
98 MultiByteToWideChar(CP_ACP, 0, cmdline, -1, cmdlinew, MAX_PATH*2);
99 pInstallHinfSectionW(NULL, NULL, cmdlinew, 0);
103 static void ok_registry(BOOL expectsuccess)
107 /* Functional tests for success of install and clean up */
108 ret = RegDeleteKey(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
109 ok((expectsuccess && ret == ERROR_SUCCESS) ||
110 (!expectsuccess && ret == ERROR_FILE_NOT_FOUND),
111 "Expected registry key Software\\Wine\\setupapitest to %s, RegDeleteKey returned %d\n",
112 expectsuccess ? "exist" : "not exist",
116 /* Test command line processing */
117 static void test_cmdline(void)
119 static const char infwithspaces[] = "test file.inf";
122 create_inf_file(inffile, cmdline_inf);
123 sprintf(path, "%s\\%s", CURR_DIR, inffile);
124 run_cmdline("DefaultInstall", 128, path);
126 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
128 /* Test handling of spaces in path, unquoted and quoted */
129 create_inf_file(infwithspaces, cmdline_inf);
131 sprintf(path, "%s\\%s", CURR_DIR, infwithspaces);
132 run_cmdline("DefaultInstall", 128, path);
135 sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
136 run_cmdline("DefaultInstall", 128, path);
139 ok(DeleteFile(infwithspaces), "Expected source inf to exist, last error was %d\n", GetLastError());
142 static const char *cmdline_inf_reg = "[Version]\n"
143 "Signature=\"$Chicago$\"\n"
145 "DelReg=Del.Settings\n"
147 "HKCU,Software\\Wine\\setupapitest\n";
149 static void test_registry(void)
155 /* First create a registry structure we would like to be deleted */
156 ok(!RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
157 "Expected RegCreateKeyA to succeed\n");
159 /* Doublecheck if the registry key is present */
160 ok(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
161 "Expected registry key to exist\n");
163 create_inf_file(inffile, cmdline_inf_reg);
164 sprintf(path, "%s\\%s", CURR_DIR, inffile);
165 run_cmdline("DefaultInstall", 128, path);
167 /* Check if the registry key is recursively deleted */
168 res = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest", &key);
170 ok(res == ERROR_FILE_NOT_FOUND, "Didn't expect the registry key to exist\n");
172 if (res == ERROR_SUCCESS)
174 RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest");
175 RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
177 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
180 static void test_install_svc_from(void)
186 SC_HANDLE scm_handle, svc_handle;
188 /* Bail out if we are on win98 */
189 SetLastError(0xdeadbeef);
190 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
192 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
194 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
197 CloseServiceHandle(scm_handle);
199 /* Basic inf file to satisfy SetupOpenInfFileA */
200 strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
201 create_inf_file(inffile, inf);
202 sprintf(path, "%s\\%s", CURR_DIR, inffile);
203 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
205 /* Nothing but the Version section */
206 SetLastError(0xdeadbeef);
207 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
208 ok(!ret, "Expected failure\n");
209 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
210 "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
211 SetupCloseInfFile(infhandle);
214 /* Add the section */
215 strcat(inf, "[Winetest.Services]\n");
216 create_inf_file(inffile, inf);
217 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
218 SetLastError(0xdeadbeef);
219 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
220 ok(!ret, "Expected failure\n");
221 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
222 "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
223 SetupCloseInfFile(infhandle);
226 /* Add a reference */
227 strcat(inf, "AddService=Winetest,,Winetest.Service\n");
228 create_inf_file(inffile, inf);
229 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
230 SetLastError(0xdeadbeef);
231 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
232 ok(!ret, "Expected failure\n");
233 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
234 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
235 SetupCloseInfFile(infhandle);
238 /* Add the section */
239 strcat(inf, "[Winetest.Service]\n");
240 create_inf_file(inffile, inf);
241 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
242 SetLastError(0xdeadbeef);
243 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
244 ok(!ret, "Expected failure\n");
245 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
246 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
247 SetupCloseInfFile(infhandle);
250 /* Just the ServiceBinary */
251 strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
252 create_inf_file(inffile, inf);
253 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
254 SetLastError(0xdeadbeef);
255 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
256 ok(!ret, "Expected failure\n");
257 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
258 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
259 SetupCloseInfFile(infhandle);
262 /* Add the ServiceType */
263 strcat(inf, "ServiceType=1\n");
264 create_inf_file(inffile, inf);
265 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
266 SetLastError(0xdeadbeef);
267 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
268 ok(!ret, "Expected failure\n");
269 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
270 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
271 SetupCloseInfFile(infhandle);
274 /* Add the StartType */
275 strcat(inf, "StartType=4\n");
276 create_inf_file(inffile, inf);
277 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
278 SetLastError(0xdeadbeef);
279 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
280 ok(!ret, "Expected failure\n");
281 ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
282 "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
283 SetupCloseInfFile(infhandle);
286 /* This should be it, the minimal entries to install a service */
287 strcat(inf, "ErrorControl=1");
288 create_inf_file(inffile, inf);
289 infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
290 SetLastError(0xdeadbeef);
291 ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
292 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
294 skip("Not enough rights to install the service\n");
295 SetupCloseInfFile(infhandle);
299 ok(ret, "Expected success\n");
300 ok(GetLastError() == ERROR_SUCCESS,
301 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
302 SetupCloseInfFile(infhandle);
305 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
307 /* Open the service to see if it's really there */
308 svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
309 ok(svc_handle != NULL, "Service was not created\n");
311 SetLastError(0xdeadbeef);
312 ret = DeleteService(svc_handle);
313 ok(ret, "Service could not be deleted : %d\n", GetLastError());
315 CloseServiceHandle(svc_handle);
316 CloseServiceHandle(scm_handle);
318 /* TODO: Test the Flags */
321 static void test_driver_install(void)
324 SC_HANDLE scm_handle, svc_handle;
326 char path[MAX_PATH], windir[MAX_PATH], driver[MAX_PATH];
328 /* Minimal stuff needed */
329 static const char *inf =
331 "Signature=\"$Chicago$\"\n"
332 "[DestinationDirs]\n"
333 "Winetest.DriverFiles=12\n"
335 "CopyFiles=Winetest.DriverFiles\n"
336 "[DefaultInstall.Services]\n"
337 "AddService=Winetest,,Winetest.Service\n"
338 "[Winetest.Service]\n"
339 "ServiceBinary=%12%\\winetest.sys\n"
343 "[Winetest.DriverFiles]\n"
346 /* Bail out if we are on win98 */
347 SetLastError(0xdeadbeef);
348 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
350 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
352 win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
355 else if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
357 skip("Not enough rights to install the service\n");
360 CloseServiceHandle(scm_handle);
362 /* Place where we expect the driver to be installed */
363 GetWindowsDirectoryA(windir, MAX_PATH);
364 lstrcpyA(driver, windir);
365 lstrcatA(driver, "\\system32\\drivers\\winetest.sys");
367 /* Create a dummy driver file */
368 handle = CreateFileA("winetest.sys", GENERIC_WRITE, 0, NULL,
369 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
372 create_inf_file(inffile, inf);
373 sprintf(path, "%s\\%s", CURR_DIR, inffile);
374 run_cmdline("DefaultInstall", 128, path);
376 /* Driver should have been installed */
377 attrs = GetFileAttributes(driver);
378 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected driver to exist\n");
380 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
382 /* Open the service to see if it's really there */
383 svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
384 ok(svc_handle != NULL, "Service was not created\n");
386 SetLastError(0xdeadbeef);
387 ret = DeleteService(svc_handle);
388 ok(ret, "Service could not be deleted : %d\n", GetLastError());
390 CloseServiceHandle(svc_handle);
391 CloseServiceHandle(scm_handle);
395 DeleteFile("winetest.sys");
399 static void test_profile_items(void)
401 char path[MAX_PATH], commonprogs[MAX_PATH];
403 BOOL (WINAPI *pSHGetFolderPathA)(HWND hwnd, int nFolder, HANDLE hToken, DWORD dwFlags, LPSTR pszPath);
405 static const char *inf =
407 "Signature=\"$Chicago$\"\n"
409 "ProfileItems=TestItem,TestItem2,TestGroup\n"
412 "CmdLine=11,,notepad.exe\n"
415 "CmdLine=11,,notepad.exe\n"
421 hShell32 = LoadLibraryA("shell32");
422 pSHGetFolderPathA = (void*)GetProcAddress(hShell32, "SHGetFolderPathA");
423 if (!pSHGetFolderPathA)
425 win_skip("SHGetFolderPathA is not available\n");
429 if (S_OK != pSHGetFolderPathA(NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, commonprogs))
431 skip("No common program files directory exists\n");
435 create_inf_file(inffile, inf);
436 sprintf(path, "%s\\%s", CURR_DIR, inffile);
437 run_cmdline("DefaultInstall", 128, path);
439 snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
440 if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(path))
442 win_skip("ProfileItems not implemented on this system\n");
446 snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
447 ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "directory not created\n");
448 snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
449 ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "link not created\n");
450 snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
451 ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "group not created\n");
454 snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
456 snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
458 snprintf(path, MAX_PATH, "%s\\TestItem2.lnk", commonprogs);
460 snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
461 RemoveDirectory(path);
462 snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
463 RemoveDirectory(path);
466 if (hShell32) FreeLibrary(hShell32);
470 static void test_inffilelist(void)
472 static const char inffile2[] = "test2.inf";
473 static const char invalid_inf[] = "invalid.inf";
474 static const char *inf =
476 "Signature=\"$Chicago$\"";
479 WCHAR dir[MAX_PATH] = { 0 };
480 WCHAR buffer[MAX_PATH] = { 0 };
481 DWORD expected, outsize;
484 if(!pSetupGetInfFileListW)
486 win_skip("SetupGetInfFileListW not present\n");
490 /* NULL means %windir%\\inf
491 * get the value as reference
494 ret = pSetupGetInfFileListW(NULL, INF_STYLE_WIN4, NULL, 0, &expected);
495 ok(ret, "expected SetupGetInfFileListW to succeed! Error: %d\n", GetLastError());
496 ok(expected > 0, "expected required buffersize to be at least 1\n");
498 /* check if an empty string doesn't behaves like NULL */
500 SetLastError(0xdeadbeef);
501 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
503 ok(!ret, "expected SetupGetInfFileListW to fail!\n");
505 /* check a not existing directory
507 MultiByteToWideChar(CP_ACP, 0, CURR_DIR, -1, dir, MAX_PATH);
508 ptr = dir + lstrlenW(dir);
509 MultiByteToWideChar(CP_ACP, 0, "\\not_existent", -1, ptr, MAX_PATH - lstrlenW(dir));
510 outsize = 0xffffffff;
511 SetLastError(0xdeadbeef);
512 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
513 ok(ret, "expected SetupGetInfFileListW to succeed!\n");
514 ok(outsize == 1, "expected required buffersize to be 1, got %d\n", outsize);
516 ok(ERROR_PATH_NOT_FOUND == GetLastError(),
517 "expected error ERROR_PATH_NOT_FOUND, got %d\n", GetLastError());
519 create_inf_file(inffile, inf);
520 create_inf_file(inffile2, inf);
521 create_inf_file(invalid_inf, "This content does not match the inf file format");
523 /* pass a filename instead of a directory
526 MultiByteToWideChar(CP_ACP, 0, invalid_inf, -1, ptr+1, MAX_PATH - lstrlenW(dir));
527 outsize = 0xffffffff;
528 SetLastError(0xdeadbeef);
529 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
531 ok(!ret, "expected SetupGetInfFileListW to fail!\n");
533 ok(ERROR_DIRECTORY == GetLastError(),
534 "expected error ERROR_DIRECTORY, got %d\n", GetLastError());
536 /* make the filename look like directory
538 dir[1 + lstrlenW(dir)] = 0;
539 dir[lstrlenW(dir)] = '\\';
540 SetLastError(0xdeadbeef);
541 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, NULL, 0, &outsize);
543 ok(!ret, "expected SetupGetInfFileListW to fail!\n");
545 ok(ERROR_DIRECTORY == GetLastError(),
546 "expected error ERROR_DIRECTORY, got %d\n", GetLastError());
548 /* now check the buffer content of a vaild call
551 expected = 3 + strlen(inffile) + strlen(inffile2);
552 ret = pSetupGetInfFileListW(dir, INF_STYLE_WIN4, buffer, MAX_PATH, &outsize);
553 ok(ret, "expected SetupGetInfFileListW to succeed!\n");
555 ok(expected == outsize, "expected required buffersize to be %d, got %d\n",
560 DeleteFile(inffile2);
561 DeleteFile(invalid_inf);
566 HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
567 char temp_path[MAX_PATH], prev_path[MAX_PATH];
570 GetCurrentDirectory(MAX_PATH, prev_path);
571 GetTempPath(MAX_PATH, temp_path);
572 SetCurrentDirectory(temp_path);
574 strcpy(CURR_DIR, temp_path);
575 len = strlen(CURR_DIR);
576 if(len && (CURR_DIR[len - 1] == '\\'))
577 CURR_DIR[len - 1] = 0;
579 pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
580 pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
581 pSetupGetInfFileListW = (void *)GetProcAddress(hsetupapi, "SetupGetInfFileListW");
583 if (pInstallHinfSectionA)
585 /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
586 static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
587 char cmdline[MAX_PATH*2];
588 create_inf_file(inffile, minimal_inf);
589 sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
590 SetLastError(0xdeadbeef);
591 pInstallHinfSectionA(NULL, NULL, cmdline, 0);
592 if (GetLastError() == 0xdeadbeef)
594 skip("InstallHinfSectionA is broken (stub)\n");
595 pInstallHinfSectionA = NULL;
597 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
599 if (!pInstallHinfSectionW && !pInstallHinfSectionA)
600 win_skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
603 /* Set CBT hook to disallow MessageBox creation in current thread */
604 hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
609 test_install_svc_from();
610 test_driver_install();
612 UnhookWindowsHookEx(hhook);
614 /* We have to run this test after the CBT hook is disabled because
615 ProfileItems needs to create a window on Windows XP. */
616 test_profile_items();
621 SetCurrentDirectory(prev_path);