push 649ef288af5d6e2bc91d3ec4d104a0c9bf084d22
[wine/hacks.git] / dlls / setupapi / tests / install.c
blob77c74f803447fec9ccf5db081e3eabfe167ddfcc
1 /*
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
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winnls.h"
29 #include "winuser.h"
30 #include "winreg.h"
31 #include "winsvc.h"
32 #include "setupapi.h"
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
49 * CBT hook.
52 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
53 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
56 * Helpers
59 static void create_inf_file(LPCSTR filename, const char *data)
61 DWORD res;
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));
66 CloseHandle(handle);
69 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
70 static HHOOK hhook;
71 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
73 return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
77 * Tests
80 static const char *cmdline_inf = "[Version]\n"
81 "Signature=\"$Chicago$\"\n"
82 "[DefaultInstall]\n"
83 "AddReg=Add.Settings\n"
84 "[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);
93 else
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)
103 LONG ret;
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",
111 ret);
114 /* Test command line processing */
115 static void test_cmdline(void)
117 static const char infwithspaces[] = "test file.inf";
118 char path[MAX_PATH];
120 create_inf_file(inffile, cmdline_inf);
121 sprintf(path, "%s\\%s", CURR_DIR, inffile);
122 run_cmdline("DefaultInstall", 128, path);
123 ok_registry(TRUE);
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);
131 ok_registry(TRUE);
133 sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
134 run_cmdline("DefaultInstall", 128, path);
135 ok_registry(FALSE);
137 ok(DeleteFile(infwithspaces), "Expected source inf to exist, last error was %d\n", GetLastError());
140 static void test_driver_install(void)
142 HANDLE handle;
143 SC_HANDLE scm_handle, svc_handle;
144 BOOL ret;
145 char path[MAX_PATH], windir[MAX_PATH], driver[MAX_PATH];
146 DWORD attrs;
147 /* Minimal stuff needed */
148 static const char *inf =
149 "[Version]\n"
150 "Signature=\"$Chicago$\"\n"
151 "[DestinationDirs]\n"
152 "Winetest.DriverFiles=12\n"
153 "[DefaultInstall]\n"
154 "CopyFiles=Winetest.DriverFiles\n"
155 "[DefaultInstall.Services]\n"
156 "AddService=Winetest,,Winetest.Service\n"
157 "[Winetest.Service]\n"
158 "ServiceBinary=%12%\\winetest.sys\n"
159 "ServiceType=1\n"
160 "StartType=4\n"
161 "ErrorControl=1\n"
162 "[Winetest.DriverFiles]\n"
163 "winetest.sys";
165 /* Bail out if we are on win98 */
166 SetLastError(0xdeadbeef);
167 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
169 if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
171 skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
172 return;
174 CloseServiceHandle(scm_handle);
176 /* Place where we expect the driver to be installed */
177 GetWindowsDirectoryA(windir, MAX_PATH);
178 lstrcpyA(driver, windir);
179 lstrcatA(driver, "\\system32\\drivers\\winetest.sys");
181 /* Create a dummy driver file */
182 handle = CreateFileA("winetest.sys", GENERIC_WRITE, 0, NULL,
183 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
184 CloseHandle(handle);
186 create_inf_file(inffile, inf);
187 sprintf(path, "%s\\%s", CURR_DIR, inffile);
188 run_cmdline("DefaultInstall", 128, path);
190 /* Driver should have been installed */
191 attrs = GetFileAttributes(driver);
192 ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected driver to exist\n");
194 scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
196 /* Open the service to see if it's really there */
197 svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
198 todo_wine
199 ok(svc_handle != NULL, "Service was not created\n");
201 SetLastError(0xdeadbeef);
202 ret = DeleteService(svc_handle);
203 todo_wine
204 ok(ret, "Service could not be deleted : %d\n", GetLastError());
206 CloseServiceHandle(svc_handle);
207 CloseServiceHandle(scm_handle);
209 /* File cleanup */
210 DeleteFile(inffile);
211 DeleteFile("winetest.sys");
212 DeleteFile(driver);
215 START_TEST(install)
217 HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
218 char temp_path[MAX_PATH], prev_path[MAX_PATH];
219 DWORD len;
221 GetCurrentDirectory(MAX_PATH, prev_path);
222 GetTempPath(MAX_PATH, temp_path);
223 SetCurrentDirectory(temp_path);
225 strcpy(CURR_DIR, temp_path);
226 len = strlen(CURR_DIR);
227 if(len && (CURR_DIR[len - 1] == '\\'))
228 CURR_DIR[len - 1] = 0;
230 pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
231 pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
232 if (pInstallHinfSectionA)
234 /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
235 static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
236 char cmdline[MAX_PATH*2];
237 create_inf_file(inffile, minimal_inf);
238 sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
239 SetLastError(0xdeadbeef);
240 pInstallHinfSectionA(NULL, NULL, cmdline, 0);
241 if (GetLastError() == 0xdeadbeef)
243 skip("InstallHinfSectionA is broken (stub)\n");
244 pInstallHinfSectionA = NULL;
246 ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
248 if (!pInstallHinfSectionW && !pInstallHinfSectionA)
249 skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
250 else
252 /* Set CBT hook to disallow MessageBox creation in current thread */
253 hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
254 assert(hhook != 0);
256 test_cmdline();
257 test_driver_install();
259 UnhookWindowsHookEx(hhook);
262 SetCurrentDirectory(prev_path);