[ADVPACK_WINETEST]: Sync with Wine 1.5.19.
[reactos.git] / rostests / winetests / advpack / 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 create_inf_file(LPCSTR filename)
59 {
60 DWORD dwNumberOfBytesWritten;
61 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
62 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
63
64 static const char data[] =
65 "[Version]\n"
66 "Signature=\"$Chicago$\"\n"
67 "AdvancedINF=2.5\n"
68 "[DefaultInstall]\n"
69 "CheckAdminRights=1\n";
70
71 WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
72 CloseHandle(hf);
73 }
74
75 static void test_RunSetupCommand(void)
76 {
77 HRESULT hr;
78 HANDLE hexe;
79 char path[MAX_PATH];
80 char dir[MAX_PATH];
81 char systemdir[MAX_PATH];
82
83 GetSystemDirectoryA(systemdir, sizeof(systemdir));
84
85 /* try an invalid cmd name */
86 hr = pRunSetupCommand(NULL, NULL, "Install", "Dir", "Title", NULL, 0, NULL);
87 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
88
89 /* try an invalid directory */
90 hr = pRunSetupCommand(NULL, "winver.exe", "Install", NULL, "Title", NULL, 0, NULL);
91 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
92
93 /* try to run a nonexistent exe */
94 hexe = (HANDLE)0xdeadbeef;
95 hr = pRunSetupCommand(NULL, "idontexist.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
96 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
97 "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
98 ok(hexe == NULL, "Expected hexe to be NULL\n");
99 ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
100
101 /* try a bad directory */
102 hexe = (HANDLE)0xdeadbeef;
103 hr = pRunSetupCommand(NULL, "winver.exe", "Install", "non\\existent\\directory", "Title", &hexe, 0, NULL);
104 ok(hr == HRESULT_FROM_WIN32(ERROR_DIRECTORY),
105 "Expected HRESULT_FROM_WIN32(ERROR_DIRECTORY), got %d\n", hr);
106 ok(hexe == NULL, "Expected hexe to be NULL\n");
107 ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
108
109 /* try to run an exe with the RSC_FLAG_INF flag */
110 hexe = (HANDLE)0xdeadbeef;
111 hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
112 ok(is_spapi_err(hr), "Expected a setupapi error, got %d\n", hr);
113 ok(hexe == (HANDLE)0xdeadbeef, "Expected hexe to be 0xdeadbeef\n");
114 ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
115
116 /* run winver.exe */
117 hexe = (HANDLE)0xdeadbeef;
118 hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
119 ok(hr == S_ASYNCHRONOUS, "Expected S_ASYNCHRONOUS, got %d\n", hr);
120 ok(hexe != NULL, "Expected hexe to be non-NULL\n");
121 ok(TerminateProcess(hexe, 0), "Expected TerminateProcess to succeed\n");
122
123 CreateDirectoryA("one", NULL);
124 create_inf_file("one\\test.inf");
125
126 /* try a full path to the INF, with working dir provided */
127 lstrcpy(path, CURR_DIR);
128 lstrcat(path, "\\one\\test.inf");
129 lstrcpy(dir, CURR_DIR);
130 lstrcat(dir, "\\one");
131 hr = pRunSetupCommand(NULL, path, "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
132 ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
133
134 /* try a full path to the INF, NULL working dir */
135 hr = pRunSetupCommand(NULL, path, "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
136 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
137 "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
138
139 /* try a full path to the INF, empty working dir */
140 hr = pRunSetupCommand(NULL, path, "DefaultInstall", "", "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 relative path to the INF, with working dir provided */
144 hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
145 ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
146
147 /* try a relative path to the INF, NULL working dir */
148 hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
149 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
150 "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
151
152 /* try a relative path to the INF, empty working dir */
153 hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
154 ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
155
156 /* try only the INF filename, with working dir provided */
157 hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
158 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
159
160 /* try only the INF filename, NULL working dir */
161 hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
162 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
163 "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
164
165 /* try only the INF filename, empty working dir */
166 hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
167 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
168
169 DeleteFileA("one\\test.inf");
170 RemoveDirectoryA("one");
171
172 create_inf_file("test.inf");
173
174 /* try INF file in the current directory, working directory provided */
175 hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
176 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
177
178 /* try INF file in the current directory, NULL working directory */
179 hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
180 ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
181 "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
182
183 /* try INF file in the current directory, empty working directory */
184 hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
185 ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
186 }
187
188 static void test_LaunchINFSection(void)
189 {
190 HRESULT hr;
191 char cmdline[MAX_PATH];
192 static char file[] = "test.inf,DefaultInstall,4,0";
193
194 /* The 'No UI' flag seems to have no effect whatsoever on Windows.
195 * So only do this test in interactive mode.
196 */
197 if (winetest_interactive)
198 {
199 /* try an invalid cmdline */
200 hr = pLaunchINFSection(NULL, NULL, NULL, 0);
201 ok(hr == 1, "Expected 1, got %d\n", hr);
202 }
203
204 CreateDirectoryA("one", NULL);
205 create_inf_file("one\\test.inf");
206
207 /* try a full path to the INF */
208 lstrcpy(cmdline, CURR_DIR);
209 lstrcat(cmdline, "\\");
210 lstrcat(cmdline, "one\\test.inf,DefaultInstall,,4");
211 hr = pLaunchINFSection(NULL, NULL, cmdline, 0);
212 ok(hr == 0, "Expected 0, got %d\n", hr);
213
214 DeleteFileA("one\\test.inf");
215 RemoveDirectoryA("one");
216
217 create_inf_file("test.inf");
218
219 /* try just the INF filename */
220 hr = pLaunchINFSection(NULL, NULL, file, 0);
221 ok(hr == 0, "Expected 0, got %d\n", hr);
222
223 DeleteFileA("test.inf");
224 }
225
226 static void test_LaunchINFSectionEx(void)
227 {
228 HRESULT hr;
229 char cmdline[MAX_PATH];
230
231 create_inf_file("test.inf");
232
233 /* try an invalid CAB filename with an absolute INF name */
234 lstrcpy(cmdline, CURR_DIR);
235 lstrcat(cmdline, "\\");
236 lstrcat(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
237 hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
238 ok(hr == 0, "Expected 0, got %d\n", hr);
239
240 /* The 'No UI' flag seems to have no effect whatsoever on Windows.
241 * So only do this test in interactive mode.
242 */
243 if (winetest_interactive)
244 {
245 /* try an invalid CAB filename with a relative INF name */
246 lstrcpy(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
247 hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
248 ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
249 }
250
251 DeleteFileA("test.inf");
252 }
253
254 START_TEST(install)
255 {
256 DWORD len;
257 char temp_path[MAX_PATH], prev_path[MAX_PATH];
258
259 if (!init_function_pointers())
260 return;
261
262 if (!IsNTAdmin(0, NULL))
263 {
264 skip("Most tests need admin rights\n");
265 return;
266 }
267
268 GetCurrentDirectoryA(MAX_PATH, prev_path);
269 GetTempPath(MAX_PATH, temp_path);
270 SetCurrentDirectoryA(temp_path);
271
272 lstrcpyA(CURR_DIR, temp_path);
273 len = lstrlenA(CURR_DIR);
274
275 if(len && (CURR_DIR[len - 1] == '\\'))
276 CURR_DIR[len - 1] = 0;
277
278 test_RunSetupCommand();
279 test_LaunchINFSection();
280 test_LaunchINFSectionEx();
281
282 FreeLibrary(hAdvPack);
283 SetCurrentDirectoryA(prev_path);
284 }