comctl32/listview: Free ID array when removing all items.
[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 #include "shlobj.h"
34
35 #include "wine/test.h"
36
37 static const char inffile[] = "test.inf";
38 static char CURR_DIR[MAX_PATH];
39
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
50  *   CBT hook.
51  */
52
53 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
54 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
55
56 /*
57  * Helpers
58  */
59
60 static void create_inf_file(LPCSTR filename, const char *data)
61 {
62     DWORD res;
63     HANDLE handle = CreateFile(filename, GENERIC_WRITE, 0, NULL,
64                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
65     assert(handle != INVALID_HANDLE_VALUE);
66     assert(WriteFile(handle, data, strlen(data), &res, NULL));
67     CloseHandle(handle);
68 }
69
70 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
71 static HHOOK hhook;
72 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
73 {
74     return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
75 }
76
77 /*
78  * Tests
79  */
80
81 static const char *cmdline_inf = "[Version]\n"
82     "Signature=\"$Chicago$\"\n"
83     "[DefaultInstall]\n"
84     "AddReg=Add.Settings\n"
85     "[Add.Settings]\n"
86     "HKCU,Software\\Wine\\setupapitest,,\n";
87
88 static void run_cmdline(LPCSTR section, int mode, LPCSTR path)
89 {
90     CHAR cmdline[MAX_PATH * 2];
91
92     sprintf(cmdline, "%s %d %s", section, mode, path);
93     if (pInstallHinfSectionA) pInstallHinfSectionA(NULL, NULL, cmdline, 0);
94     else
95     {
96         WCHAR cmdlinew[MAX_PATH * 2];
97         MultiByteToWideChar(CP_ACP, 0, cmdline, -1, cmdlinew, MAX_PATH*2);
98         pInstallHinfSectionW(NULL, NULL, cmdlinew, 0);
99     }
100 }
101
102 static void ok_registry(BOOL expectsuccess)
103 {
104     LONG ret;
105
106     /* Functional tests for success of install and clean up */
107     ret = RegDeleteKey(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
108     ok((expectsuccess && ret == ERROR_SUCCESS) ||
109        (!expectsuccess && ret == ERROR_FILE_NOT_FOUND),
110        "Expected registry key Software\\Wine\\setupapitest to %s, RegDeleteKey returned %d\n",
111        expectsuccess ? "exist" : "not exist",
112        ret);
113 }
114
115 /* Test command line processing */
116 static void test_cmdline(void)
117 {
118     static const char infwithspaces[] = "test file.inf";
119     char path[MAX_PATH];
120
121     create_inf_file(inffile, cmdline_inf);
122     sprintf(path, "%s\\%s", CURR_DIR, inffile);
123     run_cmdline("DefaultInstall", 128, path);
124     ok_registry(TRUE);
125     ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
126
127     /* Test handling of spaces in path, unquoted and quoted */
128     create_inf_file(infwithspaces, cmdline_inf);
129
130     sprintf(path, "%s\\%s", CURR_DIR, infwithspaces);
131     run_cmdline("DefaultInstall", 128, path);
132     ok_registry(TRUE);
133
134     sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
135     run_cmdline("DefaultInstall", 128, path);
136     ok_registry(FALSE);
137
138     ok(DeleteFile(infwithspaces), "Expected source inf to exist, last error was %d\n", GetLastError());
139 }
140
141 static const char *cmdline_inf_reg = "[Version]\n"
142     "Signature=\"$Chicago$\"\n"
143     "[DefaultInstall]\n"
144     "DelReg=Del.Settings\n"
145     "[Del.Settings]\n"
146     "HKCU,Software\\Wine\\setupapitest\n";
147
148 static void test_registry(void)
149 {
150     HKEY key;
151     LONG res;
152     char path[MAX_PATH];
153
154     /* First create a registry structure we would like to be deleted */
155     ok(!RegCreateKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
156         "Expected RegCreateKeyA to succeed\n");
157
158     /* Doublecheck if the registry key is present */
159     ok(!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest", &key),
160         "Expected registry key to exist\n");
161
162     create_inf_file(inffile, cmdline_inf_reg);
163     sprintf(path, "%s\\%s", CURR_DIR, inffile);
164     run_cmdline("DefaultInstall", 128, path);
165
166     /* Check if the registry key is recursively deleted */
167     res = RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest", &key);
168     todo_wine
169     ok(res == ERROR_FILE_NOT_FOUND, "Didn't expect the registry key to exist\n");
170     /* Just in case */
171     if (res == ERROR_SUCCESS)
172     {
173         RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest\\setupapitest");
174         RegDeleteKeyA(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
175     }
176     ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
177 }
178
179 static void test_install_svc_from(void)
180 {
181     char inf[2048];
182     char path[MAX_PATH];
183     HINF infhandle;
184     BOOL ret;
185     SC_HANDLE scm_handle, svc_handle;
186
187     /* Bail out if we are on win98 */
188     SetLastError(0xdeadbeef);
189     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
190
191     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
192     {
193         win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
194         return;
195     }
196     CloseServiceHandle(scm_handle);
197
198     /* Basic inf file to satisfy SetupOpenInfFileA */
199     strcpy(inf, "[Version]\nSignature=\"$Chicago$\"\n");
200     create_inf_file(inffile, inf);
201     sprintf(path, "%s\\%s", CURR_DIR, inffile);
202     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
203
204     /* Nothing but the Version section */
205     SetLastError(0xdeadbeef);
206     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
207     ok(!ret, "Expected failure\n");
208     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
209         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
210     SetupCloseInfFile(infhandle);
211     DeleteFile(inffile);
212
213     /* Add the section */
214     strcat(inf, "[Winetest.Services]\n");
215     create_inf_file(inffile, inf);
216     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
217     SetLastError(0xdeadbeef);
218     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
219     ok(!ret, "Expected failure\n");
220     ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
221         "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
222     SetupCloseInfFile(infhandle);
223     DeleteFile(inffile);
224
225     /* Add a reference */
226     strcat(inf, "AddService=Winetest,,Winetest.Service\n");
227     create_inf_file(inffile, inf);
228     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
229     SetLastError(0xdeadbeef);
230     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
231     ok(!ret, "Expected failure\n");
232     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
233         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
234     SetupCloseInfFile(infhandle);
235     DeleteFile(inffile);
236
237     /* Add the section */
238     strcat(inf, "[Winetest.Service]\n");
239     create_inf_file(inffile, inf);
240     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
241     SetLastError(0xdeadbeef);
242     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
243     ok(!ret, "Expected failure\n");
244     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
245         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
246     SetupCloseInfFile(infhandle);
247     DeleteFile(inffile);
248
249     /* Just the ServiceBinary */
250     strcat(inf, "ServiceBinary=%12%\\winetest.sys\n");
251     create_inf_file(inffile, inf);
252     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
253     SetLastError(0xdeadbeef);
254     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
255     ok(!ret, "Expected failure\n");
256     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
257         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
258     SetupCloseInfFile(infhandle);
259     DeleteFile(inffile);
260
261     /* Add the ServiceType */
262     strcat(inf, "ServiceType=1\n");
263     create_inf_file(inffile, inf);
264     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
265     SetLastError(0xdeadbeef);
266     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
267     ok(!ret, "Expected failure\n");
268     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
269         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
270     SetupCloseInfFile(infhandle);
271     DeleteFile(inffile);
272
273     /* Add the StartType */
274     strcat(inf, "StartType=4\n");
275     create_inf_file(inffile, inf);
276     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
277     SetLastError(0xdeadbeef);
278     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
279     ok(!ret, "Expected failure\n");
280     ok(GetLastError() == ERROR_BAD_SERVICE_INSTALLSECT,
281         "Expected ERROR_BAD_SERVICE_INSTALLSECT, got %08x\n", GetLastError());
282     SetupCloseInfFile(infhandle);
283     DeleteFile(inffile);
284
285     /* This should be it, the minimal entries to install a service */
286     strcat(inf, "ErrorControl=1");
287     create_inf_file(inffile, inf);
288     infhandle = SetupOpenInfFileA(path, NULL, INF_STYLE_WIN4, NULL);
289     SetLastError(0xdeadbeef);
290     ret = SetupInstallServicesFromInfSectionA(infhandle, "Winetest.Services", 0);
291     if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
292     {
293         skip("Not enough rights to install the service\n");
294         SetupCloseInfFile(infhandle);
295         DeleteFile(inffile);
296         return;
297     }
298     ok(ret, "Expected success\n");
299     ok(GetLastError() == ERROR_SUCCESS,
300         "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
301     SetupCloseInfFile(infhandle);
302     DeleteFile(inffile);
303
304     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
305
306     /* Open the service to see if it's really there */
307     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
308     ok(svc_handle != NULL, "Service was not created\n");
309
310     SetLastError(0xdeadbeef);
311     ret = DeleteService(svc_handle);
312     ok(ret, "Service could not be deleted : %d\n", GetLastError());
313
314     CloseServiceHandle(svc_handle);
315     CloseServiceHandle(scm_handle);
316
317     /* TODO: Test the Flags */
318 }
319
320 static void test_driver_install(void)
321 {
322     HANDLE handle;
323     SC_HANDLE scm_handle, svc_handle;
324     BOOL ret;
325     char path[MAX_PATH], windir[MAX_PATH], driver[MAX_PATH];
326     DWORD attrs;
327     /* Minimal stuff needed */
328     static const char *inf =
329         "[Version]\n"
330         "Signature=\"$Chicago$\"\n"
331         "[DestinationDirs]\n"
332         "Winetest.DriverFiles=12\n"
333         "[DefaultInstall]\n"
334         "CopyFiles=Winetest.DriverFiles\n"
335         "[DefaultInstall.Services]\n"
336         "AddService=Winetest,,Winetest.Service\n"
337         "[Winetest.Service]\n"
338         "ServiceBinary=%12%\\winetest.sys\n"
339         "ServiceType=1\n"
340         "StartType=4\n"
341         "ErrorControl=1\n"
342         "[Winetest.DriverFiles]\n"
343         "winetest.sys";
344
345     /* Bail out if we are on win98 */
346     SetLastError(0xdeadbeef);
347     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
348
349     if (!scm_handle && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
350     {
351         win_skip("OpenSCManagerA is not implemented, we are most likely on win9x\n");
352         return;
353     }
354     else if (!scm_handle && (GetLastError() == ERROR_ACCESS_DENIED))
355     {
356         skip("Not enough rights to install the service\n");
357         return;
358     }
359     CloseServiceHandle(scm_handle);
360
361     /* Place where we expect the driver to be installed */
362     GetWindowsDirectoryA(windir, MAX_PATH);
363     lstrcpyA(driver, windir);
364     lstrcatA(driver, "\\system32\\drivers\\winetest.sys");
365
366     /* Create a dummy driver file */
367     handle = CreateFileA("winetest.sys", GENERIC_WRITE, 0, NULL,
368                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
369     CloseHandle(handle);
370
371     create_inf_file(inffile, inf);
372     sprintf(path, "%s\\%s", CURR_DIR, inffile);
373     run_cmdline("DefaultInstall", 128, path);
374
375     /* Driver should have been installed */
376     attrs = GetFileAttributes(driver);
377     ok(attrs != INVALID_FILE_ATTRIBUTES, "Expected driver to exist\n");
378
379     scm_handle = OpenSCManagerA(NULL, NULL, GENERIC_ALL);
380
381     /* Open the service to see if it's really there */
382     svc_handle = OpenServiceA(scm_handle, "Winetest", DELETE);
383     ok(svc_handle != NULL, "Service was not created\n");
384
385     SetLastError(0xdeadbeef);
386     ret = DeleteService(svc_handle);
387     ok(ret, "Service could not be deleted : %d\n", GetLastError());
388
389     CloseServiceHandle(svc_handle);
390     CloseServiceHandle(scm_handle);
391
392     /* File cleanup */
393     DeleteFile(inffile);
394     DeleteFile("winetest.sys");
395     DeleteFile(driver);
396 }
397
398 static void test_profile_items(void)
399 {
400     char path[MAX_PATH], commonprogs[MAX_PATH];
401     HMODULE hShell32;
402     BOOL (WINAPI *pSHGetFolderPathA)(HWND hwnd, int nFolder, HANDLE hToken, DWORD dwFlags, LPSTR pszPath);
403
404     static const char *inf =
405         "[Version]\n"
406         "Signature=\"$Chicago$\"\n"
407         "[DefaultInstall]\n"
408         "ProfileItems=TestItem,TestItem2,TestGroup\n"
409         "[TestItem]\n"
410         "Name=TestItem\n"
411         "CmdLine=11,,notepad.exe\n"
412         "[TestItem2]\n"
413         "Name=TestItem2\n"
414         "CmdLine=11,,notepad.exe\n"
415         "SubDir=TestDir\n"
416         "[TestGroup]\n"
417         "Name=TestGroup,4\n"
418         ;
419
420     hShell32 = LoadLibraryA("shell32");
421     pSHGetFolderPathA = (void*)GetProcAddress(hShell32, "SHGetFolderPathA");
422     if (!pSHGetFolderPathA)
423     {
424         win_skip("SHGetFolderPathA is not available\n");
425         goto cleanup;
426     }
427
428     if (S_OK != pSHGetFolderPathA(NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, commonprogs))
429     {
430         skip("No common program files directory exists\n");
431         goto cleanup;
432     }
433
434     create_inf_file(inffile, inf);
435     sprintf(path, "%s\\%s", CURR_DIR, inffile);
436     run_cmdline("DefaultInstall", 128, path);
437
438     snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
439     if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(path))
440     {
441         win_skip("ProfileItems not implemented on this system\n");
442     }
443     else
444     {
445         snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
446         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "directory not created\n");
447         snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
448         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "link not created\n");
449         snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
450         ok(INVALID_FILE_ATTRIBUTES != GetFileAttributes(path), "group not created\n");
451     }
452
453     snprintf(path, MAX_PATH, "%s\\TestItem.lnk", commonprogs);
454     DeleteFile(path);
455     snprintf(path, MAX_PATH, "%s\\TestDir\\TestItem2.lnk", commonprogs);
456     DeleteFile(path);
457     snprintf(path, MAX_PATH, "%s\\TestItem2.lnk", commonprogs);
458     DeleteFile(path);
459     snprintf(path, MAX_PATH, "%s\\TestDir", commonprogs);
460     RemoveDirectory(path);
461     snprintf(path, MAX_PATH, "%s\\TestGroup", commonprogs);
462     RemoveDirectory(path);
463
464 cleanup:
465     if (hShell32) FreeLibrary(hShell32);
466     DeleteFile(inffile);
467 }
468
469 START_TEST(install)
470 {
471     HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
472     char temp_path[MAX_PATH], prev_path[MAX_PATH];
473     DWORD len;
474
475     GetCurrentDirectory(MAX_PATH, prev_path);
476     GetTempPath(MAX_PATH, temp_path);
477     SetCurrentDirectory(temp_path);
478
479     strcpy(CURR_DIR, temp_path);
480     len = strlen(CURR_DIR);
481     if(len && (CURR_DIR[len - 1] == '\\'))
482         CURR_DIR[len - 1] = 0;
483
484     pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
485     pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
486     if (pInstallHinfSectionA)
487     {
488         /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
489         static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
490         char cmdline[MAX_PATH*2];
491         create_inf_file(inffile, minimal_inf);
492         sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
493         SetLastError(0xdeadbeef);
494         pInstallHinfSectionA(NULL, NULL, cmdline, 0);
495         if (GetLastError() == 0xdeadbeef)
496         {
497             skip("InstallHinfSectionA is broken (stub)\n");
498             pInstallHinfSectionA = NULL;
499         }
500         ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
501     }
502     if (!pInstallHinfSectionW && !pInstallHinfSectionA)
503         win_skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
504     else
505     {
506         /* Set CBT hook to disallow MessageBox creation in current thread */
507         hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
508         assert(hhook != 0);
509
510         test_cmdline();
511         test_registry();
512         test_install_svc_from();
513         test_driver_install();
514
515         UnhookWindowsHookEx(hhook);
516
517         /* We have to run this test after the CBT hook is disabled because
518             ProfileItems needs to create a window on Windows XP. */
519         test_profile_items();
520     }
521
522     SetCurrentDirectory(prev_path);
523 }