advpack: Add another test.
[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 /* function pointers */
27 static HRESULT (WINAPI *pRunSetupCommand)(HWND, LPCSTR, LPCSTR, LPCSTR, LPCSTR, HANDLE*, DWORD, LPVOID);
28 static HRESULT (WINAPI *pLaunchINFSection)(HWND, HINSTANCE, LPSTR, INT);
29 static HRESULT (WINAPI *pLaunchINFSectionEx)(HWND, HINSTANCE, LPSTR, INT);
30
31 static char CURR_DIR[MAX_PATH];
32
33 static BOOL init_function_pointers()
34 {
35     HMODULE hAdvPack = LoadLibraryA("advpack.dll");
36     if (!hAdvPack)
37         return FALSE;
38
39     pRunSetupCommand = (void *)GetProcAddress(hAdvPack, "RunSetupCommand");
40     pLaunchINFSection = (void *)GetProcAddress(hAdvPack, "LaunchINFSection");
41     pLaunchINFSectionEx = (void *)GetProcAddress(hAdvPack, "LaunchINFSectionEx");
42
43     if (!pRunSetupCommand || !pLaunchINFSection || !pLaunchINFSectionEx)
44         return FALSE;
45
46     return TRUE;
47 }
48
49 static BOOL is_spapi_err(DWORD err)
50 {
51     const DWORD SPAPI_PREFIX = 0x800F0000L;
52     const DWORD SPAPI_MASK = 0xFFFF0000L;
53
54     return (((err & SPAPI_MASK) ^ SPAPI_PREFIX) == 0);
55 }
56
57 static void append_str(char **str, const char *data)
58 {
59     sprintf(*str, data);
60     *str += strlen(*str);
61 }
62
63 static void create_inf_file(LPCSTR filename)
64 {
65     char data[1024];
66     char *ptr = data;
67     DWORD dwNumberOfBytesWritten;
68     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
69                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
70
71     append_str(&ptr, "[Version]\n");
72     append_str(&ptr, "Signature=\"$Chicago$\"\n");
73     append_str(&ptr, "AdvancedINF=2.5\n");
74     append_str(&ptr, "[DefaultInstall]\n");
75     append_str(&ptr, "RegisterOCXs=RegisterOCXsSection\n");
76     append_str(&ptr, "[RegisterOCXsSection]\n");
77     append_str(&ptr, "%%11%%\\ole32.dll\n");
78
79     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
80     CloseHandle(hf);
81 }
82
83 static void test_RunSetupCommand()
84 {
85     HRESULT hr;
86     HANDLE hexe;
87     char path[MAX_PATH];
88     char dir[MAX_PATH];
89
90     /* try an invalid cmd name */
91     hr = pRunSetupCommand(NULL, NULL, "Install", "Dir", "Title", NULL, 0, NULL);
92     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
93
94     /* try an invalid directory */
95     hr = pRunSetupCommand(NULL, "winver.exe", "Install", NULL, "Title", NULL, 0, NULL);
96     ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
97
98     /* try to run a nonexistent exe */
99     hexe = (HANDLE)0xdeadbeef;
100     hr = pRunSetupCommand(NULL, "idontexist.exe", "Install", "c:\\windows\\system32", "Title", &hexe, 0, NULL);
101     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
102        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
103     ok(hexe == NULL, "Expcted hexe to be NULL\n");
104     ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
105
106     /* try a bad directory */
107     hexe = (HANDLE)0xdeadbeef;
108     hr = pRunSetupCommand(NULL, "winver.exe", "Install", "non\\existent\\directory", "Title", &hexe, 0, NULL);
109     ok(hr == HRESULT_FROM_WIN32(ERROR_DIRECTORY),
110        "Expected HRESULT_FROM_WIN32(ERROR_DIRECTORY), got %d\n", hr);
111     ok(hexe == NULL, "Expcted hexe to be NULL\n");
112     ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
113
114     /* try to run an exe with the RSC_FLAG_INF flag */
115     hexe = (HANDLE)0xdeadbeef;
116     hr = pRunSetupCommand(NULL, "winver.exe", "Install", "c:\\windows\\system32", "Title", &hexe, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
117     ok(is_spapi_err(hr), "Expected a setupapi error, got %d\n", hr);
118     ok(hexe == (HANDLE)0xdeadbeef, "Expected hexe to be 0xdeadbeef\n");
119     ok(!TerminateProcess(hexe, 0), "Expected TerminateProcess to fail\n");
120
121     /* run winver.exe */
122     hexe = (HANDLE)0xdeadbeef;
123     hr = pRunSetupCommand(NULL, "winver.exe", "Install", "c:\\windows\\system32", "Title", &hexe, 0, NULL);
124     ok(hr == S_ASYNCHRONOUS, "Expected S_ASYNCHRONOUS, got %d\n", hr);
125     ok(hexe != NULL, "Expected hexe to be non-NULL\n");
126     ok(TerminateProcess(hexe, 0), "Expected TerminateProcess to succeed\n");
127
128     CreateDirectoryA("one", NULL);
129     create_inf_file("one\\test.inf");
130
131     /* try a full path to the INF, with working dir provided */
132     lstrcpy(path, CURR_DIR);
133     lstrcat(path, "\\one\\test.inf");
134     lstrcpy(dir, CURR_DIR);
135     lstrcat(dir, "\\one");
136     hr = pRunSetupCommand(NULL, path, "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
137     ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
138
139     /* try a full path to the INF, NULL working dir */
140     hr = pRunSetupCommand(NULL, path, "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
141     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
142        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
143
144     /* try a full path to the INF, empty working dir */
145     hr = pRunSetupCommand(NULL, path, "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
146     ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
147
148     /* try a relative path to the INF, with working dir provided */
149     hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", dir, "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, NULL working dir */
153     hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
154     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
155        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
156
157     /* try a relative path to the INF, empty working dir */
158     hr = pRunSetupCommand(NULL, "one\\test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
159     ok(hr == ERROR_SUCCESS, "Expected ERROR_SUCCESS, got %d\n", hr);
160
161     /* try only the INF filename, with working dir provided */
162     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", dir, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
163     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
164        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
165
166     /* try only the INF filename, NULL working dir */
167     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
168     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
169        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
170
171     /* try only the INF filename, empty working dir */
172     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", "", "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
173     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
174        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
175
176     DeleteFileA("one\\test.inf");
177     RemoveDirectoryA("one");
178
179     create_inf_file("test.inf");
180
181     /* try INF file in the current directory, working directory provided */
182     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", CURR_DIR, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
183     ok(hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND),
184        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
185
186     /* try INF file in the current directory, NULL working directory */
187     hr = pRunSetupCommand(NULL, "test.inf", "DefaultInstall", NULL, "Title", NULL, RSC_FLAG_INF | RSC_FLAG_QUIET, NULL);
188     ok(hr == HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER),
189        "Expected HRESULT_FROM_WIN32(ERROR_INVALID_PARAMETER), got %d\n", hr);
190
191     /* try INF file in the current directory, empty working directory */
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        "Expected HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND), got %d\n", hr);
195 }
196
197 static void test_LaunchINFSection()
198 {
199     HRESULT hr;
200     char cmdline[MAX_PATH];
201     static char file[] = "test.inf,DefaultInstall,4,0";
202
203     /* try an invalid cmdline */
204     hr = pLaunchINFSection(NULL, NULL, NULL, 0);
205     ok(hr == 1, "Expected 1, got %d\n", hr);
206
207     CreateDirectoryA("one", NULL);
208     create_inf_file("one\\test.inf");
209
210     /* try a full path to the INF */
211     lstrcpy(cmdline, CURR_DIR);
212     lstrcat(cmdline, "\\");
213     lstrcat(cmdline, "one\\test.inf,DefaultInstall,,4");
214     hr = pLaunchINFSection(NULL, NULL, cmdline, 0);
215     ok(hr == 0, "Expected 0, got %d\n", hr);
216
217     DeleteFileA("one\\test.inf");
218     RemoveDirectoryA("one");
219
220     create_inf_file("test.inf");
221
222     /* try just the INF filename */
223     hr = pLaunchINFSection(NULL, NULL, file, 0);
224     ok(hr == 0, "Expected 0, got %d\n", hr);
225
226     DeleteFileA("test.inf");
227 }
228
229 static void test_LaunchINFSectionEx()
230 {
231     HRESULT hr;
232     char cmdline[MAX_PATH];
233
234     create_inf_file("test.inf");
235
236     /* try an invalid CAB filename with an absolute INF name */
237     lstrcpy(cmdline, CURR_DIR);
238     lstrcat(cmdline, "\\");
239     lstrcat(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
240     hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
241     ok(hr == 0, "Expected 0, got %d\n", hr);
242
243     /* try an invalid CAB filename with a relative INF name */
244     lstrcpy(cmdline, "test.inf,DefaultInstall,c:imacab.cab,4");
245     hr = pLaunchINFSectionEx(NULL, NULL, cmdline, 0);
246     todo_wine
247         ok(hr == E_INVALIDARG, "Expected E_INVALIDARG, got %d\n", hr);
248
249     DeleteFileA("test.inf");
250 }
251
252 START_TEST(install)
253 {
254     if (!init_function_pointers())
255         return;
256
257     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
258
259     test_RunSetupCommand();
260     test_LaunchINFSection();
261     test_LaunchINFSectionEx();
262 }