widl: Check that attributes are applicable for libraries, modules, dispinterfaces...
[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     todo_wine
170     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
171         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
172     SetupCloseInfFile(infhandle);
173     DeleteFile(inffile);
174
175     /* Add the section */
176     strcat(inf, "[Winetest.Services]\n");
177     create_inf_file(inffile, inf);
178     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
179     SetLastError(0xdeadbeef);
180     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
181     ok(!ret, "Expected failure\n");
182     todo_wine
183     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
184         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
185     SetupCloseInfFile(infhandle);
186     DeleteFile(inffile);
187
188     /* Add a reference */
189     strcat(inf, "AddService=Winetest,,Winetest.Service\n");
190     create_inf_file(inffile, inf);
191     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
192     SetLastError(0xdeadbeef);
193     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
194     ok(!ret, "Expected failure\n");
195     todo_wine
196     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
197         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
198     SetupCloseInfFile(infhandle);
199     DeleteFile(inffile);
200
201     /* Add the section */
202     strcat(inf, "[Winetest.Service]\n");
203     create_inf_file(inffile, inf);
204     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
205     SetLastError(0xdeadbeef);
206     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
207     ok(!ret, "Expected failure\n");
208     todo_wine
209     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
210         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
211     SetupCloseInfFile(infhandle);
212     DeleteFile(inffile);
213
214     /* Just the ServiceBinary */
215     strcat(inf, "ServiceBinary=%12%\\winetest.sys\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     todo_wine
222     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
223         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
224     SetupCloseInfFile(infhandle);
225     DeleteFile(inffile);
226
227     /* Add the ServiceType */
228     strcat(inf, "ServiceType=1\n");
229     create_inf_file(inffile, inf);
230     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
231     SetLastError(0xdeadbeef);
232     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
233     ok(!ret, "Expected failure\n");
234     todo_wine
235     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
236         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
237     SetupCloseInfFile(infhandle);
238     DeleteFile(inffile);
239
240     /* Add the StartType */
241     strcat(inf, "StartType=4\n");
242     create_inf_file(inffile, inf);
243     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
244     SetLastError(0xdeadbeef);
245     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
246     ok(!ret, "Expected failure\n");
247     todo_wine
248     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
249         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
250     SetupCloseInfFile(infhandle);
251     DeleteFile(inffile);
252
253     /* This should be it, the minimal entries to install a service */
254     strcat(inf, "ErrorControl=1");
255     create_inf_file(inffile, inf);
256     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
257     SetLastError(0xdeadbeef);
258     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
259     todo_wine
260     {
261     ok(ret, "Expected success\n");
262     ok(GetLastError() == ERROR_SUCCESS,
263         "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
264     }
265     SetupCloseInfFile(infhandle);
266     DeleteFile(inffile);
267
268     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
269
270     /* Open the service to see if it's really there */
271     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
272     todo_wine
273     ok(svc_handle != NULL, "Service was not created\n");
274
275     SetLastError(0xdeadbeef);
276     ret = DeleteService(svc_handle);
277     todo_wine
278     ok(ret, "Service could not be deleted : %d\n", GetLastError());
279
280     CloseServiceHandle(svc_handle);
281     CloseServiceHandle(scm_handle);
282
283     /* TODO: Test the Flags */
284 }
285
286 static void test_driver_install(void)
287 {
288     HANDLE handle;
289     SC_HANDLE scm_handle, svc_handle;
290     BOOL ret;
291     char path[MAX_PATH], windir[MAX_PATH], driver[MAX_PATH];
292     DWORD attrs;
293     /* Minimal stuff needed */
294     static const char *inf =
295         "[Version]\n"
296         "Signature=\"$Chicago$\"\n"
297         "[DestinationDirs]\n"
298         "Winetest.DriverFiles=12\n"
299         "[DefaultInstall]\n"
300         "CopyFiles=Winetest.DriverFiles\n"
301         "[DefaultInstall.Services]\n"
302         "AddService=Winetest,,Winetest.Service\n"
303         "[Winetest.Service]\n"
304         "ServiceBinary=%12%\\winetest.sys\n"
305         "ServiceType=1\n"
306         "StartType=4\n"
307         "ErrorControl=1\n"
308         "[Winetest.DriverFiles]\n"
309         "winetest.sys";
310
311     /* Bail out if we are on win98 */
312     SetLastError(0xdeadbeef);
313     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
314
315     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
316     {
317         skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
318         return;
319     }
320     CloseServiceHandle(scm_handle);
321
322     /* Place where we expect the driver to be installed */
323     GetWindowsDirectoryA(windir, MAX_PATH);
324     lstrcpyA(driver, windir);
325     lstrcatA(driver, "\\system32\\drivers\\winetest.sys");
326
327     /* Create a dummy driver file */
328     handle = CreateFileA("winetest.sys", GENERIC_WRITE, 0, NULL,
329                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
330     CloseHandle(handle);
331
332     create_inf_file(inffile, inf);
333     sprintf(path, "%s\\%s", CURR_DIR, inffile);
334     run_cmdline("DefaultInstall", 128, path);
335
336     /* Driver should have been installed */
337     attrs = GetFileAttributes(driver);
338     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected driver to exist\n");
339
340     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
341
342     /* Open the service to see if it's really there */
343     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
344     todo_wine
345     ok(svc_handle != NULL, "Service was not created\n");
346
347     SetLastError(0xdeadbeef);
348     ret = DeleteService(svc_handle);
349     todo_wine
350     ok(ret, "Service could not be deleted : %d\n", GetLastError());
351
352     CloseServiceHandle(svc_handle);
353     CloseServiceHandle(scm_handle);
354
355     /* File cleanup */
356     DeleteFile(inffile);
357     DeleteFile("winetest.sys");
358     DeleteFile(driver);
359 }
360
361 START_TEST(install)
362 {
363     HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
364     char temp_path[MAX_PATH], prev_path[MAX_PATH];
365     DWORD len;
366
367     GetCurrentDirectory(MAX_PATH, prev_path);
368     GetTempPath(MAX_PATH, temp_path);
369     SetCurrentDirectory(temp_path);
370
371     strcpy(CURR_DIR, temp_path);
372     len = strlen(CURR_DIR);
373     if(len && (CURR_DIR[len - 1] == '\\'))
374         CURR_DIR[len - 1] = 0;
375
376     pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
377     pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
378     if (pInstallHinfSectionA)
379     {
380         /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
381         static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
382         char cmdline[MAX_PATH*2];
383         create_inf_file(inffile, minimal_inf);
384         sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
385         SetLastError(0xdeadbeef);
386         pInstallHinfSectionA(NULL, NULL, cmdline, 0);
387         if (GetLastError() == 0xdeadbeef)
388         {
389             skip("InstallHinfSectionA is broken (stub)\n");
390             pInstallHinfSectionA = NULL;
391         }
392         ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
393     }
394     if (!pInstallHinfSectionW && !pInstallHinfSectionA)
395         skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
396     else
397     {
398         /* Set CBT hook to disallow MessageBox creation in current thread */
399         hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
400         assert(hhook != 0);
401
402         test_cmdline();
403         test_install_svc_from();
404         test_driver_install();
405
406         UnhookWindowsHookEx(hhook);
407     }
408
409     SetCurrentDirectory(prev_path);
410 }