setupapi: Handle unquoted paths in InstallHinfSection as native.
[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 "setupapi.h"
32
33 #include "wine/test.h"
34
35 static const char inffile[] = "test.inf";
36 static char CURR_DIR[MAX_PATH];
37
38 /* Notes on InstallHinfSectionA/W:
39  * - InstallHinfSectionW on Win98 and InstallHinfSectionA on WinXP seem to be stubs - they do not do anything
40  *   and simply return without displaying any error message or setting last error. We test whether
41  *   InstallHinfSectionA sets last error, and if it doesn't we set it to NULL and use the W version if available.
42  * - These functions do not return a value and do not always set last error to ERROR_SUCCESS when installation still
43  *   occurs (e.g., unquoted inf file with spaces, registry keys are written but last error is 6). Also, on Win98 last error
44  *   is set to ERROR_SUCCESS even if install fails (e.g., quoted inf file with spaces, no registry keys set, MessageBox with
45  *   "Installation Error" displayed). Thus, we must use functional tests (e.g., is registry key created) to determine whether
46  *   or not installation occured.
47  * - On installation problems, a MessageBox() is displayed and a Beep() is issued. The MessageBox() is disabled with a
48  *   CBT hook.
49  */
50
51 static void (WINAPI *pInstallHinfSectionA)(HWND, HINSTANCE, LPCSTR, INT);
52 static void (WINAPI *pInstallHinfSectionW)(HWND, HINSTANCE, LPCWSTR, INT);
53
54 /*
55  * Helpers
56  */
57
58 static void create_inf_file(LPCSTR filename, const char *data)
59 {
60     DWORD res;
61     HANDLE handle = CreateFile(filename, GENERIC_WRITE, 0, NULL,
62                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
63     assert(handle != INVALID_HANDLE_VALUE);
64     assert(WriteFile(handle, data, strlen(data), &res, NULL));
65     CloseHandle(handle);
66 }
67
68 /* CBT hook to ensure a window (e.g., MessageBox) cannot be created */
69 static HHOOK hhook;
70 static LRESULT CALLBACK cbt_hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
71 {
72     return nCode == HCBT_CREATEWND ? 1: CallNextHookEx(hhook, nCode, wParam, lParam);
73 }
74
75 /*
76  * Tests
77  */
78
79 static const char *cmdline_inf = "[Version]\n"
80     "Signature=\"$Chicago$\"\n"
81     "[DefaultInstall]\n"
82     "AddReg=Add.Settings\n"
83     "[Add.Settings]\n"
84     "HKCU,Software\\Wine\\setupapitest,,\n";
85
86 static void ok_cmdline(LPCSTR section, int mode, LPCSTR path, BOOL expectsuccess)
87 {
88     CHAR cmdline[MAX_PATH * 2];
89     LONG ret;
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     /* Functional tests for success of install and clean up */
101     ret = RegDeleteKey(HKEY_CURRENT_USER, "Software\\Wine\\setupapitest");
102     ok((expectsuccess && ret == ERROR_SUCCESS) ||
103        (!expectsuccess && ret == ERROR_FILE_NOT_FOUND),
104        "Expected registry key Software\\Wine\\setupapitest to %s, RegDeleteKey returned %d\n",
105        expectsuccess ? "exist" : "not exist",
106        ret);
107 }
108
109 /* Test command line processing */
110 static void test_cmdline(void)
111 {
112     static const char infwithspaces[] = "test file.inf";
113     char path[MAX_PATH];
114
115     create_inf_file(inffile, cmdline_inf);
116     sprintf(path, "%s\\%s", CURR_DIR, inffile);
117     ok_cmdline("DefaultInstall", 128, path, TRUE);
118     ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
119
120     /* Test handling of spaces in path, unquoted and quoted */
121     create_inf_file(infwithspaces, cmdline_inf);
122
123     sprintf(path, "%s\\%s", CURR_DIR, infwithspaces);
124     ok_cmdline("DefaultInstall", 128, path, TRUE);
125
126     sprintf(path, "\"%s\\%s\"", CURR_DIR, infwithspaces);
127     todo_wine ok_cmdline("DefaultInstall", 128, path, FALSE);
128
129     ok(DeleteFile(infwithspaces), "Expected source inf to exist, last error was %d\n", GetLastError());
130 }
131
132 START_TEST(install)
133 {
134     HMODULE hsetupapi = GetModuleHandle("setupapi.dll");
135     char temp_path[MAX_PATH], prev_path[MAX_PATH];
136     DWORD len;
137
138     GetCurrentDirectory(MAX_PATH, prev_path);
139     GetTempPath(MAX_PATH, temp_path);
140     SetCurrentDirectory(temp_path);
141
142     strcpy(CURR_DIR, temp_path);
143     len = strlen(CURR_DIR);
144     if(len && (CURR_DIR[len - 1] == '\\'))
145         CURR_DIR[len - 1] = 0;
146
147     pInstallHinfSectionA = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionA");
148     pInstallHinfSectionW = (void *)GetProcAddress(hsetupapi, "InstallHinfSectionW");
149     if (pInstallHinfSectionA)
150     {
151         /* Check if pInstallHinfSectionA sets last error or is a stub (as on WinXP) */
152         static const char *minimal_inf = "[Version]\nSignature=\"$Chicago$\"\n";
153         char cmdline[MAX_PATH*2];
154         create_inf_file(inffile, minimal_inf);
155         sprintf(cmdline, "DefaultInstall 128 %s\\%s", CURR_DIR, inffile);
156         SetLastError(0xdeadbeef);
157         pInstallHinfSectionA(NULL, NULL, cmdline, 0);
158         if (GetLastError() == 0xdeadbeef)
159         {
160             skip("InstallHinfSectionA is broken (stub)\n");
161             pInstallHinfSectionA = NULL;
162         }
163         ok(DeleteFile(inffile), "Expected source inf to exist, last error was %d\n", GetLastError());
164     }
165     if (!pInstallHinfSectionW && !pInstallHinfSectionA)
166         skip("InstallHinfSectionA and InstallHinfSectionW are not available\n");
167     else
168     {
169         /* Set CBT hook to disallow MessageBox creation in current thread */
170         hhook = SetWindowsHookExA(WH_CBT, cbt_hook_proc, 0, GetCurrentThreadId());
171         assert(hhook != 0);
172
173         test_cmdline();
174
175         UnhookWindowsHookEx(hhook);
176     }
177
178     SetCurrentDirectory(prev_path);
179 }