quartz: Make dwSamplesProcessed a longlong.
[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, "RegisterOCXs=RegisterOCXsSection\n");
77     append_str(&ptr, "[RegisterOCXsSection]\n");
78     append_str(&ptr, "%%11%%\\ole32.dll\n");
79
80     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
81     CloseHandle(hf);
82 }
83
84 static void test_RunSetupCommand(void)
85 {
86     HRESULT hr;
87     HANDLE hexe;
88     char path[MAX_PATH];
89     char dir[MAX_PATH];
90     char systemdir[MAX_PATH];
91
92     GetSystemDirectoryA(systemdir, sizeof(systemdir));
93
94     /* try an invalid cmd name */
95     hr = pRunSetupCommand(NULL, NULL, "Install", "Dir", "Title", NULL, 0, NULL);
96     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
97
98     /* try an invalid directory */
99     hr = pRunSetupCommand(NULL, "winver.exe", "Install", NULL, "Title", NULL, 0, NULL);
100     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
101
102     /* try to run a nonexistent exe */
103     hexe = (HANDLE)0xdeadbeef;
104     hr = pRunSetupCommand(NULL, "idontexist.exe", "Install", systemdir, "Title", &hexe, 0, NULL);
105     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
106        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
107     ok(hexe == NULL, "Expcted hexe to be NULL\n");
108     ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
109
110     /* try a bad directory */
111     hexe = (HANDLE)0xdeadbeef;
112     hr = pRunSetupCommand(NULL, "winver.exe", "Install", "non\\existent\\directory", "Title", &hexe, 0, NULL);
113     ok(hr == HRESULT_FROM_WIN32(ERROR_DIRECTORY),
114        "Expected HRESULT_FROM_WIN32(ERROR_DIRECTORY), got %d\n", hr);
115     ok(hexe == NULL, "Expcted hexe to be NULL\n");
116     ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
117
118     /* try to run an exe with the RSC_FLAG_INF flag */
119     hexe = (HANDLE)0xdeadbeef;
120     hr = pRunSetupCommand(NULL, "winver.exe", "Install", systemdir, "Title", &hexe, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
121     ok(is_spapi_err(hr), "Expected a setupapi error, 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, "Expected ERROR_SUCCESS, got %d\n", hr);
155
156     /* try a relative path to the INF, NULL working dir */
157     hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
158     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
159        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
160
161     /* try a relative path to the INF, empty working dir */
162     hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
163     ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
164
165     /* try only the INF filename, with working dir provided */
166     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
167     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
168        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
169
170     /* try only the INF filename, NULL working dir */
171     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
172     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
173        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
174
175     /* try only the INF filename, empty working dir */
176     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
177     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
178        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
179
180     DeleteFileA("one\\test.inf");
181     RemoveDirectoryA("one");
182
183     create_inf_file("test.inf");
184
185     /* try INF file in the current directory, working directory provided */
186     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
187     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
188        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
189
190     /* try INF file in the current directory, NULL working directory */
191     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
192     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
193        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
194
195     /* try INF file in the current directory, empty working directory */
196     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
197     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
198        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
199 }
200
201 static void test_LaunchINFSection(void)
202 {
203     HRESULT hr;
204     char cmdline[MAX_PATH];
205     static char file[] = "test.inf,DefaultInstall,4,0";
206
207     /* The 'No UI' flag seems to have no effect whatsoever on Windows.
208      * So only do this test in interactive mode.
209      */
210     if (winetest_interactive)
211     {
212         /* try an invalid cmdline */
213         hr = pLaunchINFSection(NULL, NULL, NULL, 0);
214         ok(hr == 1, "Expected 1, got %d\n", hr);
215     }
216
217     CreateDirectoryA("one", NULL);
218     create_inf_file("one\\test.inf");
219
220     /* try a full path to the INF */
221     lstrcpy(cmdline, CURR_DIR);
222     lstrcat(cmdline, "\\");
223     lstrcat(cmdline, "one\\test.inf,DefaultInstall,,4");
224     hr = pLaunchINFSection(NULL, NULL, cmdline, 0);
225     ok(hr == 0, "Expected 0, got %d\n", hr);
226
227     DeleteFileA("one\\test.inf");
228     RemoveDirectoryA("one");
229
230     create_inf_file("test.inf");
231
232     /* try just the INF filename */
233     hr = pLaunchINFSection(NULL, NULL, file, 0);
234     ok(hr == 0, "Expected 0, got %d\n", hr);
235
236     DeleteFileA("test.inf");
237 }
238
239 static void test_LaunchINFSectionEx(void)
240 {
241     HRESULT hr;
242     char cmdline[MAX_PATH];
243
244     create_inf_file("test.inf");
245
246     /* try an invalid CAB filename with an absolute INF name */
247     lstrcpy(cmdline, CURR_DIR);
248     lstrcat(cmdline, "\\");
249     lstrcat(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
250     hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
251     ok(hr == 0, "Expected 0, got %d\n", hr);
252
253     /* The 'No UI' flag seems to have no effect whatsoever on Windows.
254      * So only do this test in interactive mode.
255      */
256     if (winetest_interactive)
257     {
258         /* try an invalid CAB filename with a relative INF name */
259         lstrcpy(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
260         hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
261         ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
262     }
263
264     DeleteFileA("test.inf");
265 }
266
267 START_TEST(install)
268 {
269     if (!init_function_pointers())
270         return;
271
272     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
273
274     test_RunSetupCommand();
275     test_LaunchINFSection();
276     test_LaunchINFSectionEx();
277
278     FreeLibrary(hAdvPack);
279 }