setupapi: Implemented SetupInstallServicesFromInfSectionW.
[wine] / dlls / setupapi / tests / install.c
1 /*
2  * Unit test for setupapi.dll install functions
3  *
4  * Copyright 2007 Misha Koshelev
5  *
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.
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  * Lesser General Public License for more details.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <assert.h>
25
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"
33
34 #include "wine/test.h"
35
36 static const char inffile[] = "test.inf";
37 static char CURR_DIR[MAX_PATH];
38
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.
50  */
51
52 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
53 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
54
55 /*
56  * Helpers
57  */
58
59 static void create_inf_file(LPCSTR filename, const char *data)
60 {
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);
67 }
68
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)
72 {
73     return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
74 }
75
76 /*
77  * Tests
78  */
79
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";
86
87 static void run_cmdline(LPCSTR section, int mode, LPCSTR path)
88 {
89     CHAR cmdline[MAX_PATH * 2];
90
91     sprintf(cmdline, "%s %d %s", section, mode, path);
92     if (pInstallHinfSectionA) pInstallHinfSectionA(NULL, NULL, cmdline, 0);
93     else
94     {
95         WCHAR cmdlinew[MAX_PATH * 2];
96         MultiByteToWideChar(CP_ACP, 0, cmdline, -1, cmdlinew, MAX_PATH*2);
97         pInstallHinfSectionW(NULL, NULL, cmdlinew, 0);
98     }
99 }
100
101 static void ok_registry(BOOL expectsuccess)
102 {
103     LONG ret;
104
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);
112 }
113
114 /* Test command line processing */
115 static void test_cmdline(void)
116 {
117     static const char infwithspaces[] = "test file.inf";
118     char path[MAX_PATH];
119
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());
125
126     /* Test handling of spaces in path, unquoted and quoted */
127     create_inf_file(infwithspaces, cmdline_inf);
128
129     sprintf(path, "%s\\%s", CURR_DIR, infwithspaces);
130     run_cmdline("DefaultInstall", 128, path);
131     ok_registry(TRUE);
132
133     sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
134     run_cmdline("DefaultInstall", 128, path);
135     ok_registry(FALSE);
136
137     ok(DeleteFile(infwithspaces), "Expected source inf to exist, last error was %d\n", GetLastError());
138 }
139
140 static void test_install_svc_from(void)
141 {
142     char inf[2048];
143     char path[MAX_PATH];
144     HINF infhandle;
145     BOOL ret;
146     SC_HANDLE scm_handle, svc_handle;
147
148     /* Bail out if we are on win98 */
149     SetLastError(0xdeadbeef);
150     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
151
152     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
153     {
154         skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
155         return;
156     }
157     CloseServiceHandle(scm_handle);
158
159     /* Basic inf file to satisfy SetupOpenInfFileA */
160     strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
161     create_inf_file(inffile, inf);
162     sprintf(path, "%s\\%s", CURR_DIR, inffile);
163     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
164
165     /* Nothing but the Version section */
166     SetLastError(0xdeadbeef);
167     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
168     ok(!ret, "Expected failure\n");
169     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
170         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
171     SetupCloseInfFile(infhandle);
172     DeleteFile(inffile);
173
174     /* Add the section */
175     strcat(inf, "[Winetest.Services]\n");
176     create_inf_file(inffile, inf);
177     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
178     SetLastError(0xdeadbeef);
179     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
180     ok(!ret, "Expected failure\n");
181     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
182         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
183     SetupCloseInfFile(infhandle);
184     DeleteFile(inffile);
185
186     /* Add a reference */
187     strcat(inf, "AddService=Winetest,,Winetest.Service\n");
188     create_inf_file(inffile, inf);
189     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
190     SetLastError(0xdeadbeef);
191     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
192     ok(!ret, "Expected failure\n");
193     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
194         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
195     SetupCloseInfFile(infhandle);
196     DeleteFile(inffile);
197
198     /* Add the section */
199     strcat(inf, "[Winetest.Service]\n");
200     create_inf_file(inffile, inf);
201     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
202     SetLastError(0xdeadbeef);
203     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
204     ok(!ret, "Expected failure\n");
205     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
206         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
207     SetupCloseInfFile(infhandle);
208     DeleteFile(inffile);
209
210     /* Just the ServiceBinary */
211     strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
212     create_inf_file(inffile, inf);
213     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
214     SetLastError(0xdeadbeef);
215     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
216     ok(!ret, "Expected failure\n");
217     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
218         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
219     SetupCloseInfFile(infhandle);
220     DeleteFile(inffile);
221
222     /* Add the ServiceType */
223     strcat(inf, "ServiceType=1\n");
224     create_inf_file(inffile, inf);
225     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
226     SetLastError(0xdeadbeef);
227     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
228     ok(!ret, "Expected failure\n");
229     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
230         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
231     SetupCloseInfFile(infhandle);
232     DeleteFile(inffile);
233
234     /* Add the StartType */
235     strcat(inf, "StartType=4\n");
236     create_inf_file(inffile, inf);
237     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
238     SetLastError(0xdeadbeef);
239     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
240     ok(!ret, "Expected failure\n");
241     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
242         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
243     SetupCloseInfFile(infhandle);
244     DeleteFile(inffile);
245
246     /* This should be it, the minimal entries to install a service */
247     strcat(inf, "ErrorControl=1");
248     create_inf_file(inffile, inf);
249     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
250     SetLastError(0xdeadbeef);
251     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
252     ok(ret, "Expected success\n");
253     ok(GetLastError() == ERROR_SUCCESS,
254         "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
255     SetupCloseInfFile(infhandle);
256     DeleteFile(inffile);
257
258     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
259
260     /* Open the service to see if it's really there */
261     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
262     ok(svc_handle != NULL, "Service was not created\n");
263
264     SetLastError(0xdeadbeef);
265     ret = DeleteService(svc_handle);
266     ok(ret, "Service could not be deleted : %d\n", GetLastError());
267
268     CloseServiceHandle(svc_handle);
269     CloseServiceHandle(scm_handle);
270
271     /* TODO: Test the Flags */
272 }
273
274 static void test_driver_install(void)
275 {
276     HANDLE handle;
277     SC_HANDLE scm_handle, svc_handle;
278     BOOL ret;
279     char path[MAX_PATH], windir[MAX_PATH], driver[MAX_PATH];
280     DWORD attrs;
281     /* Minimal stuff needed */
282     static const char *inf =
283         "[Version]\n"
284         "Signature=\"$Chicago$\"\n"
285         "[DestinationDirs]\n"
286         "Winetest.DriverFiles=12\n"
287         "[DefaultInstall]\n"
288         "CopyFiles=Winetest.DriverFiles\n"
289         "[DefaultInstall.Services]\n"
290         "AddService=Winetest,,Winetest.Service\n"
291         "[Winetest.Service]\n"
292         "ServiceBinary=%12%\\winetest.sys\n"
293         "ServiceType=1\n"
294         "StartType=4\n"
295         "ErrorControl=1\n"
296         "[Winetest.DriverFiles]\n"
297         "winetest.sys";
298
299     /* Bail out if we are on win98 */
300     SetLastError(0xdeadbeef);
301     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
302
303     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
304     {
305         skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
306         return;
307     }
308     CloseServiceHandle(scm_handle);
309
310     /* Place where we expect the driver to be installed */
311     GetWindowsDirectoryA(windir, MAX_PATH);
312     lstrcpyA(driver, windir);
313     lstrcatA(driver, "\\system32\\drivers\\winetest.sys");
314
315     /* Create a dummy driver file */
316     handle = CreateFileA("winetest.sys", GENERIC_WRITE, 0, NULL,
317                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
318     CloseHandle(handle);
319
320     create_inf_file(inffile, inf);
321     sprintf(path, "%s\\%s", CURR_DIR, inffile);
322     run_cmdline("DefaultInstall", 128, path);
323
324     /* Driver should have been installed */
325     attrs = GetFileAttributes(driver);
326     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected driver to exist\n");
327
328     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
329
330     /* Open the service to see if it's really there */
331     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
332     todo_wine
333     ok(svc_handle != NULL, "Service was not created\n");
334
335     SetLastError(0xdeadbeef);
336     ret = DeleteService(svc_handle);
337     todo_wine
338     ok(ret, "Service could not be deleted : %d\n", GetLastError());
339
340     CloseServiceHandle(svc_handle);
341     CloseServiceHandle(scm_handle);
342
343     /* File cleanup */
344     DeleteFile(inffile);
345     DeleteFile("winetest.sys");
346     DeleteFile(driver);
347 }
348
349 START_TEST(install)
350 {
351     HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
352     char temp_path[MAX_PATH], prev_path[MAX_PATH];
353     DWORD len;
354
355     GetCurrentDirectory(MAX_PATH, prev_path);
356     GetTempPath(MAX_PATH, temp_path);
357     SetCurrentDirectory(temp_path);
358
359     strcpy(CURR_DIR, temp_path);
360     len = strlen(CURR_DIR);
361     if(len && (CURR_DIR[len - 1] == '\\'))
362         CURR_DIR[len - 1] = 0;
363
364     pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
365     pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
366     if (pInstallHinfSectionA)
367     {
368         /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
369         static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
370         char cmdline[MAX_PATH*2];
371         create_inf_file(inffile, minimal_inf);
372         sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
373         SetLastError(0xdeadbeef);
374         pInstallHinfSectionA(NULL, NULL, cmdline, 0);
375         if (GetLastError() == 0xdeadbeef)
376         {
377             skip("InstallHinfSectionA is broken (stub)\n");
378             pInstallHinfSectionA = NULL;
379         }
380         ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
381     }
382     if (!pInstallHinfSectionW && !pInstallHinfSectionA)
383         skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
384     else
385     {
386         /* Set CBT hook to disallow MessageBox creation in current thread */
387         hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
388         assert(hhook != 0);
389
390         test_cmdline();
391         test_install_svc_from();
392         test_driver_install();
393
394         UnhookWindowsHookEx(hhook);
395     }
396
397     SetCurrentDirectory(prev_path);
398 }