winmm: Fix a failing mixer test on 98 and ME.
[wine] / dlls / advpack / tests / install.c
1 /*
2  * Unit tests for advpack.dll install functions
3  *
4  * Copyright (C) 2006 James Hawkins
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 <stdio.h>
22 #include <windows.h>
23 #include <advpub.h>
24 #include "wine/test.h"
25
26 static HMODULE hAdvPack;
27 /* function pointers */
28 static HRESULT (WINAPI *pRunSetupCommand)(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, HANDLE*, DWORD, LPVOID);
29 static HRESULT (WINAPI *pLaunchINFSection)(HWND, HINSTANCE, LPSTR, INT);
30 static HRESULT (WINAPI *pLaunchINFSectionEx)(HWND, HINSTANCE, LPSTR, INT);
31
32 static char CURR_DIR[MAX_PATH];
33
34 static BOOL init_function_pointers(void)
35 {
36     hAdvPack = LoadLibraryA("advpack.dll");
37     if (!hAdvPack)
38         return FALSE;
39
40     pRunSetupCommand = (void *)GetProcAddress(hAdvPack, "RunSetupCommand");
41     pLaunchINFSection = (void *)GetProcAddress(hAdvPack, "LaunchINFSection");
42     pLaunchINFSectionEx = (void *)GetProcAddress(hAdvPack, "LaunchINFSectionEx");
43
44     if (!pRunSetupCommand || !pLaunchINFSection || !pLaunchINFSectionEx)
45         return FALSE;
46
47     return TRUE;
48 }
49
50 static BOOL is_spapi_err(DWORD err)
51 {
52     const DWORD SPAPI_PREFIX = 0x800F0000L;
53     const DWORD SPAPI_MASK = 0xFFFF0000L;
54
55     return (((err & SPAPI_MASK) ^ SPAPI_PREFIX) == 0);
56 }
57
58 static void append_str(char **str, const char *data)
59 {
60     sprintf(*str, data);
61     *str += strlen(*str);
62 }
63
64 static void create_inf_file(LPCSTR filename)
65 {
66     char data[1024];
67     char *ptr = data;
68     DWORD dwNumberOfBytesWritten;
69     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
70                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
71
72     append_str(&ptr, "[Version]\n");
73     append_str(&ptr, "Signature=\"$Chicago$\"\n");
74     append_str(&ptr, "AdvancedINF=2.5\n");
75     append_str(&ptr, "[DefaultInstall]\n");
76     append_str(&ptr, "CheckAdminRights=1\n");
77
78     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
79     CloseHandle(hf);
80 }
81
82 static void test_RunSetupCommand(void)
83 {
84     HRESULT hr;
85     HANDLE hexe;
86     char path[MAX_PATH];
87     char dir[MAX_PATH];
88     char systemdir[MAX_PATH];
89
90     GetSystemDirectoryA(systemdir, sizeof(systemdir));
91
92     /* try an invalid cmd name */
93     hr = pRunSetupCommand(NULL, NULL, "Install", "Dir", "Title", NULL, 0, NULL);
94     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
95
96     /* try an invalid directory */
97     hr = pRunSetupCommand(NULL, "winver.exe", "Install", NULL, "Title", NULL, 0, NULL);
98     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
99
100     /* try to run a nonexistent exe */
101     hexe = (HANDLE)0xdeadbeef;
102     hr = pRunSetupCommand(NULL, "idontexist.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
103     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
104        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
105     ok(hexe == NULL, "Expcted hexe to be NULL\n");
106     ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
107
108     /* try a bad directory */
109     hexe = (HANDLE)0xdeadbeef;
110     hr = pRunSetupCommand(NULL, "winver.exe", "Install", "non\\existent\\directory", "Title", &hexe, 0, NULL);
111     ok(hr == HRESULT_FROM_WIN32(ERROR_DIRECTORY),
112        "Expected HRESULT_FROM_WIN32(ERROR_DIRECTORY), got %d\n", hr);
113     ok(hexe == NULL, "Expcted hexe to be NULL\n");
114     ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
115
116     /* try to run an exe with the RSC_FLAG_INF flag */
117     hexe = (HANDLE)0xdeadbeef;
118     hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
119     ok(is_spapi_err(hr) ||
120        hr == E_FAIL, /* win9x */
121        "Expected a setupapi error or E_FAIL, got %d\n", hr);
122     ok(hexe == (HANDLE)0xdeadbeef, "Expected hexe to be 0xdeadbeef\n");
123     ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
124
125     /* run winver.exe */
126     hexe = (HANDLE)0xdeadbeef;
127     hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
128     ok(hr == S_ASYNCHRONOUS, "Expected S_ASYNCHRONOUS, got %d\n", hr);
129     ok(hexe != NULL, "Expected hexe to be non-NULL\n");
130     ok(TerminateProcess(hexe, 0), "Expected TerminateProcess to succeed\n");
131
132     CreateDirectoryA("one", NULL);
133     create_inf_file("one\\test.inf");
134
135     /* try a full path to the INF, with working dir provided */
136     lstrcpy(path, CURR_DIR);
137     lstrcat(path, "\\one\\test.inf");
138     lstrcpy(dir, CURR_DIR);
139     lstrcat(dir, "\\one");
140     hr = pRunSetupCommand(NULL, path, "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
141     ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
142
143     /* try a full path to the INF, NULL working dir */
144     hr = pRunSetupCommand(NULL, path, "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
145     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
146        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
147
148     /* try a full path to the INF, empty working dir */
149     hr = pRunSetupCommand(NULL, path, "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
150     ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
151
152     /* try a relative path to the INF, with working dir provided */
153     hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
154     ok(hr == ERROR_SUCCESS ||
155        hr == E_FAIL, /* win9x */
156        "Expected ERROR_SUCCESS, got %d\n", hr);
157
158     /* try a relative path to the INF, NULL working dir */
159     hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
160     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
161        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
162
163     /* try a relative path to the INF, empty working dir */
164     hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
165     ok(hr == ERROR_SUCCESS ||
166        hr == E_FAIL, /* win9x */
167        "Expected ERROR_SUCCESS or E_FAIL, got %d\n", hr);
168
169     /* try only the INF filename, with working dir provided */
170     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
171     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
172        hr == E_FAIL, /* win9x */
173        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) or E_FAIL, got %d\n", hr);
174
175     /* try only the INF filename, NULL working dir */
176     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
177     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
178        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
179
180     /* try only the INF filename, empty working dir */
181     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
182     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
183        hr == E_FAIL, /* win9x */
184        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) or E_FAIL, got %d\n", hr);
185
186     DeleteFileA("one\\test.inf");
187     RemoveDirectoryA("one");
188
189     create_inf_file("test.inf");
190
191     /* try INF file in the current directory, working directory provided */
192     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
193     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
194        hr == E_FAIL, /* win9x */
195        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) or E_FAIL, got %d\n", hr);
196
197     /* try INF file in the current directory, NULL working directory */
198     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
199     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
200        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
201
202     /* try INF file in the current directory, empty working directory */
203     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
204     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) ||
205        hr == E_FAIL, /* win9x */
206        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) or E_FAIL, got %d\n", hr);
207 }
208
209 static void test_LaunchINFSection(void)
210 {
211     HRESULT hr;
212     char cmdline[MAX_PATH];
213     static char file[] = "test.inf,DefaultInstall,4,0";
214
215     /* The 'No UI' flag seems to have no effect whatsoever on Windows.
216      * So only do this test in interactive mode.
217      */
218     if (winetest_interactive)
219     {
220         /* try an invalid cmdline */
221         hr = pLaunchINFSection(NULL, NULL, NULL, 0);
222         ok(hr == 1, "Expected 1, got %d\n", hr);
223     }
224
225     CreateDirectoryA("one", NULL);
226     create_inf_file("one\\test.inf");
227
228     /* try a full path to the INF */
229     lstrcpy(cmdline, CURR_DIR);
230     lstrcat(cmdline, "\\");
231     lstrcat(cmdline, "one\\test.inf,DefaultInstall,,4");
232     hr = pLaunchINFSection(NULL, NULL, cmdline, 0);
233     ok(hr == 0, "Expected 0, got %d\n", hr);
234
235     DeleteFileA("one\\test.inf");
236     RemoveDirectoryA("one");
237
238     create_inf_file("test.inf");
239
240     /* try just the INF filename */
241     hr = pLaunchINFSection(NULL, NULL, file, 0);
242     ok(hr == 0, "Expected 0, got %d\n", hr);
243
244     DeleteFileA("test.inf");
245 }
246
247 static void test_LaunchINFSectionEx(void)
248 {
249     HRESULT hr;
250     char cmdline[MAX_PATH];
251
252     create_inf_file("test.inf");
253
254     /* try an invalid CAB filename with an absolute INF name */
255     lstrcpy(cmdline, CURR_DIR);
256     lstrcat(cmdline, "\\");
257     lstrcat(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
258     hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
259     ok(hr == 0, "Expected 0, got %d\n", hr);
260
261     /* The 'No UI' flag seems to have no effect whatsoever on Windows.
262      * So only do this test in interactive mode.
263      */
264     if (winetest_interactive)
265     {
266         /* try an invalid CAB filename with a relative INF name */
267         lstrcpy(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
268         hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
269         ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
270     }
271
272     DeleteFileA("test.inf");
273 }
274
275 START_TEST(install)
276 {
277     DWORD len;
278     char temp_path[MAX_PATH], prev_path[MAX_PATH];
279
280     if (!init_function_pointers())
281         return;
282
283     GetCurrentDirectoryA(MAX_PATH, prev_path);
284     GetTempPath(MAX_PATH, temp_path);
285     SetCurrentDirectoryA(temp_path);
286
287     lstrcpyA(CURR_DIR, temp_path);
288     len = lstrlenA(CURR_DIR);
289
290     if(len && (CURR_DIR[len - 1] == '\\'))
291         CURR_DIR[len - 1] = 0;
292
293     test_RunSetupCommand();
294     test_LaunchINFSection();
295     test_LaunchINFSectionEx();
296
297     FreeLibrary(hAdvPack);
298     SetCurrentDirectoryA(prev_path);
299 }