advpack/tests: Avoid test failures without high access rights.
[wine/multimedia.git] / dlls / advpack / tests / install.c
blob4876d718817ac02bb1822f52592883b74876a286
1 /*
2 * Unit tests for advpack.dll install functions
4 * Copyright (C) 2006 James Hawkins
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.
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.
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
21 #include <stdio.h>
22 #include <windows.h>
23 #include <advpub.h>
24 #include "wine/test.h"
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);
32 static char CURR_DIR[MAX_PATH];
34 static BOOL init_function_pointers(void)
36 hAdvPack = LoadLibraryA("advpack.dll");
37 if (!hAdvPack)
38 return FALSE;
40 pRunSetupCommand = (void *)GetProcAddress(hAdvPack, "RunSetupCommand");
41 pLaunchINFSection = (void *)GetProcAddress(hAdvPack, "LaunchINFSection");
42 pLaunchINFSectionEx = (void *)GetProcAddress(hAdvPack, "LaunchINFSectionEx");
44 if (!pRunSetupCommand || !pLaunchINFSection || !pLaunchINFSectionEx)
45 return FALSE;
47 return TRUE;
50 static BOOL is_spapi_err(DWORD err)
52 const DWORD SPAPI_PREFIX = 0x800F0000L;
53 const DWORD SPAPI_MASK = 0xFFFF0000L;
55 return (((err & SPAPI_MASK) ^ SPAPI_PREFIX) == 0);
58 static void create_inf_file(LPCSTR filename)
60 DWORD dwNumberOfBytesWritten;
61 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
62 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
64 static const char data[] =
65 "[Version]\n"
66 "Signature=\"$Chicago$\"\n"
67 "AdvancedINF=2.5\n"
68 "[DefaultInstall]\n"
69 "CheckAdminRights=1\n";
71 WriteFile(hf, data, sizeof(data) - 1, &dwNumberOfBytesWritten, NULL);
72 CloseHandle(hf);
75 static void test_RunSetupCommand(void)
77 HRESULT hr;
78 HANDLE hexe;
79 char path[MAX_PATH];
80 char dir[MAX_PATH];
81 char systemdir[MAX_PATH];
83 GetSystemDirectoryA(systemdir, sizeof(systemdir));
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);
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);
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");
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");
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");
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");
123 CreateDirectoryA("one", NULL);
124 create_inf_file("one\\test.inf");
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);
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);
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);
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);
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);
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);
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);
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);
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);
169 DeleteFileA("one\\test.inf");
170 RemoveDirectoryA("one");
172 create_inf_file("test.inf");
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);
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);
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);
188 static void test_LaunchINFSection(void)
190 HRESULT hr;
191 char cmdline[MAX_PATH];
192 static char file[] = "test.inf,DefaultInstall,4,0";
194 /* The 'No UI' flag seems to have no effect whatsoever on Windows.
195 * So only do this test in interactive mode.
197 if (winetest_interactive)
199 /* try an invalid cmdline */
200 hr = pLaunchINFSection(NULL, NULL, NULL, 0);
201 ok(hr == 1, "Expected 1, got %d\n", hr);
204 CreateDirectoryA("one", NULL);
205 create_inf_file("one\\test.inf");
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);
214 DeleteFileA("one\\test.inf");
215 RemoveDirectoryA("one");
217 create_inf_file("test.inf");
219 /* try just the INF filename */
220 hr = pLaunchINFSection(NULL, NULL, file, 0);
221 ok(hr == 0, "Expected 0, got %d\n", hr);
223 DeleteFileA("test.inf");
226 static void test_LaunchINFSectionEx(void)
228 HRESULT hr;
229 char cmdline[MAX_PATH];
231 create_inf_file("test.inf");
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);
240 /* The 'No UI' flag seems to have no effect whatsoever on Windows.
241 * So only do this test in interactive mode.
243 if (winetest_interactive)
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);
251 DeleteFileA("test.inf");
254 START_TEST(install)
256 DWORD len;
257 char temp_path[MAX_PATH], prev_path[MAX_PATH];
259 if (!init_function_pointers())
260 return;
262 if (!IsNTAdmin(0, NULL))
264 skip("Most tests need admin rights\n");
265 return;
268 GetCurrentDirectoryA(MAX_PATH, prev_path);
269 GetTempPath(MAX_PATH, temp_path);
270 SetCurrentDirectoryA(temp_path);
272 lstrcpyA(CURR_DIR, temp_path);
273 len = lstrlenA(CURR_DIR);
275 if(len && (CURR_DIR[len - 1] == '\\'))
276 CURR_DIR[len - 1] = 0;
278 test_RunSetupCommand();
279 test_LaunchINFSection();
280 test_LaunchINFSectionEx();
282 FreeLibrary(hAdvPack);
283 SetCurrentDirectoryA(prev_path);