msi: Only insert entries into listbox if property value matches.
[wine] / dlls / setupapi / tests / query.c
1 /*
2  * Unit tests for setupapi.dll query 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 <setupapi.h>
24 #include "wine/test.h"
25
26 /* function pointers */
27 static HMODULE hSetupAPI;
28 static void (WINAPI *pSetupCloseInfFile)(HINF);
29 static BOOL (WINAPI *pSetupGetInfInformationA)(LPCVOID,DWORD,PSP_INF_INFORMATION,DWORD,PDWORD);
30 static HINF (WINAPI *pSetupOpenInfFileA)(PCSTR,PCSTR,DWORD,PUINT);
31 static BOOL (WINAPI *pSetupQueryInfFileInformationA)(PSP_INF_INFORMATION,UINT,PSTR,DWORD,PDWORD);
32 static BOOL (WINAPI *pSetupGetSourceFileLocationA)(HINF,PINFCONTEXT,PCSTR,PUINT,PSTR,DWORD,PDWORD);
33 static BOOL (WINAPI *pSetupGetSourceInfoA)(HINF,UINT,UINT,PSTR,DWORD,PDWORD);
34 static BOOL (WINAPI *pSetupGetTargetPathA)(HINF,PINFCONTEXT,PCSTR,PSTR,DWORD,PDWORD);
35
36 CHAR CURR_DIR[MAX_PATH];
37 CHAR WIN_DIR[MAX_PATH];
38
39 static void init_function_pointers(void)
40 {
41     hSetupAPI = LoadLibraryA("setupapi.dll");
42
43     if (hSetupAPI)
44     {
45         pSetupCloseInfFile = (void *)GetProcAddress(hSetupAPI, "SetupCloseInfFile");
46         pSetupGetInfInformationA = (void *)GetProcAddress(hSetupAPI, "SetupGetInfInformationA");
47         pSetupOpenInfFileA = (void *)GetProcAddress(hSetupAPI, "SetupOpenInfFileA");
48         pSetupQueryInfFileInformationA = (void *)GetProcAddress(hSetupAPI, "SetupQueryInfFileInformationA");
49         pSetupGetSourceFileLocationA = (void *)GetProcAddress(hSetupAPI, "SetupGetSourceFileLocationA");
50         pSetupGetSourceInfoA = (void *)GetProcAddress(hSetupAPI, "SetupGetSourceInfoA");
51         pSetupGetTargetPathA = (void *)GetProcAddress(hSetupAPI, "SetupGetTargetPathA");
52     }
53 }
54
55 static void get_directories(void)
56 {
57     int len;
58
59     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
60     len = lstrlenA(CURR_DIR);
61
62     if(len && (CURR_DIR[len-1] == '\\'))
63         CURR_DIR[len-1] = 0;
64
65     GetWindowsDirectoryA(WIN_DIR, MAX_PATH);
66     len = lstrlenA(WIN_DIR);
67
68     if (len && (WIN_DIR[len-1] == '\\'))
69         WIN_DIR[len-1] = 0;
70 }
71
72 static void append_str(char **str, const char *data)
73 {
74     sprintf(*str, data);
75     *str += strlen(*str);
76 }
77
78 static void create_inf_file(LPSTR filename)
79 {
80     char data[1024];
81     char *ptr = data;
82     DWORD dwNumberOfBytesWritten;
83     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
84                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
85
86     append_str(&ptr, "[Version]\n");
87     append_str(&ptr, "Signature=\"$Chicago$\"\n");
88     append_str(&ptr, "AdvancedINF=2.5\n");
89     append_str(&ptr, "[SourceDisksNames]\n");
90     append_str(&ptr, "2 = %%SrcDiskName%%, LANCOM\\LANtools\\lanconf.cab\n");
91     append_str(&ptr, "[SourceDisksFiles]\n");
92     append_str(&ptr, "lanconf.exe = 2\n");
93     append_str(&ptr, "[DestinationDirs]\n");
94     append_str(&ptr, "DefaultDestDir = 24, %%DefaultDest%%\n");
95     append_str(&ptr, "[Strings]\n");
96     append_str(&ptr, "LangDir = english\n");
97     append_str(&ptr, "DefaultDest = LANCOM\n");
98     append_str(&ptr, "SrcDiskName = \"LANCOM Software CD\"\n");
99
100     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
101     CloseHandle(hf);
102 }
103
104 static BOOL check_info_filename(PSP_INF_INFORMATION info, LPSTR test)
105 {
106     LPSTR filename;
107     DWORD size;
108     BOOL ret = FALSE;
109
110     if (!pSetupQueryInfFileInformationA(info, 0, NULL, 0, &size))
111         return FALSE;
112
113     filename = HeapAlloc(GetProcessHeap(), 0, size);
114     if (!filename)
115         return FALSE;
116
117     pSetupQueryInfFileInformationA(info, 0, filename, size, &size);
118
119     if (!lstrcmpiA(test, filename))
120         ret = TRUE;
121
122     HeapFree(GetProcessHeap(), 0, filename);
123     return ret;
124 }
125
126 static PSP_INF_INFORMATION alloc_inf_info(LPCSTR filename, DWORD search, PDWORD size)
127 {
128     PSP_INF_INFORMATION info;
129     BOOL ret;
130
131     ret = pSetupGetInfInformationA(filename, search, NULL, 0, size);
132     if (!ret)
133         return NULL;
134
135     info = HeapAlloc(GetProcessHeap(), 0, *size);
136     return info;
137 }
138
139 static void test_SetupGetInfInformation(void)
140 {
141     PSP_INF_INFORMATION info;
142     CHAR inf_filename[MAX_PATH];
143     CHAR inf_one[MAX_PATH], inf_two[MAX_PATH];
144     DWORD size;
145     HINF hinf;
146     BOOL ret;
147
148     lstrcpyA(inf_filename, CURR_DIR);
149     lstrcatA(inf_filename, "\\");
150     lstrcatA(inf_filename, "test.inf");
151
152     /* try an invalid inf handle */
153     size = 0xdeadbeef;
154     SetLastError(0xbeefcafe);
155     ret = pSetupGetInfInformationA(NULL, INFINFO_INF_SPEC_IS_HINF, NULL, 0, &size);
156     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
157     ok(GetLastError() == ERROR_INVALID_HANDLE,
158        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
159     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
160
161     /* try an invalid inf filename */
162     size = 0xdeadbeef;
163     SetLastError(0xbeefcafe);
164     ret = pSetupGetInfInformationA(NULL, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
165     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
166     ok(GetLastError() == ERROR_INVALID_PARAMETER,
167        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
168     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
169
170     create_inf_file(inf_filename);
171
172     /* try an invalid search flag */
173     size = 0xdeadbeef;
174     SetLastError(0xbeefcafe);
175     ret = pSetupGetInfInformationA(inf_filename, -1, NULL, 0, &size);
176     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
177     ok(GetLastError() == ERROR_INVALID_PARAMETER,
178        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
179     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
180
181     /* try a nonexistent inf file */
182     size = 0xdeadbeef;
183     SetLastError(0xbeefcafe);
184     ret = pSetupGetInfInformationA("idontexist", INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
185     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
186     ok(GetLastError() == ERROR_FILE_NOT_FOUND,
187        "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
188     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
189
190     /* successfully open the inf file */
191     size = 0xdeadbeef;
192     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
193     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
194     ok(size != 0xdeadbeef, "Expected a valid size on return\n");
195
196     /* set ReturnBuffer to NULL and ReturnBufferSize to non-zero value 'size' */
197     SetLastError(0xbeefcafe);
198     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size, &size);
199     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
200     ok(GetLastError() == ERROR_INVALID_PARAMETER,
201        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
202
203     /* set ReturnBuffer to NULL and ReturnBufferSize to non-zero value 'size-1' */
204     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size-1, &size);
205     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
206
207     /* some tests for behaviour with a NULL RequiredSize pointer */
208     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, NULL);
209     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
210     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size - 1, NULL);
211     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
212     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size, NULL);
213     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
214
215     info = HeapAlloc(GetProcessHeap(), 0, size);
216
217     /* try valid ReturnBuffer but too small size */
218     SetLastError(0xbeefcafe);
219     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, info, size - 1, &size);
220     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
221     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
222        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
223
224     /* successfully get the inf information */
225     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, info, size, &size);
226     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
227     ok(check_info_filename(info, inf_filename), "Expected returned filename to be equal\n");
228
229     HeapFree(GetProcessHeap(), 0, info);
230
231     /* try the INFINFO_INF_SPEC_IS_HINF search flag */
232     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
233     info = alloc_inf_info(hinf, INFINFO_INF_SPEC_IS_HINF, &size);
234     ret = pSetupGetInfInformationA(hinf, INFINFO_INF_SPEC_IS_HINF, info, size, &size);
235     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
236     ok(check_info_filename(info, inf_filename), "Expected returned filename to be equal\n");
237     pSetupCloseInfFile(hinf);
238
239     lstrcpyA(inf_one, WIN_DIR);
240     lstrcatA(inf_one, "\\inf\\");
241     lstrcatA(inf_one, "test.inf");
242     create_inf_file(inf_one);
243
244     lstrcpyA(inf_two, WIN_DIR);
245     lstrcatA(inf_two, "\\system32\\");
246     lstrcatA(inf_two, "test.inf");
247     create_inf_file(inf_two);
248
249     HeapFree(GetProcessHeap(), 0, info);
250     info = alloc_inf_info("test.inf", INFINFO_DEFAULT_SEARCH, &size);
251
252     /* test the INFINFO_DEFAULT_SEARCH search flag */
253     ret = pSetupGetInfInformationA("test.inf", INFINFO_DEFAULT_SEARCH, info, size, &size);
254     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
255     ok(check_info_filename(info, inf_one), "Expected returned filename to be equal\n");
256
257     HeapFree(GetProcessHeap(), 0, info);
258     info = alloc_inf_info("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, &size);
259
260     /* test the INFINFO_REVERSE_DEFAULT_SEARCH search flag */
261     ret = pSetupGetInfInformationA("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, info, size, &size);
262     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
263     ok(check_info_filename(info, inf_two), "Expected returned filename to be equal\n");
264
265     DeleteFileA(inf_filename);
266     DeleteFileA(inf_one);
267     DeleteFileA(inf_two);
268 }
269
270 static void test_SetupGetSourceFileLocation(void)
271 {
272     char buffer[MAX_PATH] = "not empty", inf_filename[MAX_PATH];
273     UINT source_id;
274     DWORD required;
275     HINF hinf;
276     BOOL ret;
277
278     lstrcpyA(inf_filename, CURR_DIR);
279     lstrcatA(inf_filename, "\\");
280     lstrcatA(inf_filename, "test.inf");
281
282     create_inf_file(inf_filename);
283
284     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
285     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
286
287     required = 0;
288     source_id = 0;
289
290     ret = pSetupGetSourceFileLocationA(hinf, NULL, "lanconf.exe", &source_id, buffer, sizeof(buffer), &required);
291     ok(ret, "SetupGetSourceFileLocation failed\n");
292
293     ok(required == 1, "unexpected required size: %d\n", required);
294     ok(source_id == 2, "unexpected source id: %d\n", source_id);
295     ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
296
297     pSetupCloseInfFile(hinf);
298     DeleteFileA(inf_filename);
299 }
300
301 static void test_SetupGetSourceInfo(void)
302 {
303     char buffer[MAX_PATH], inf_filename[MAX_PATH];
304     DWORD required;
305     HINF hinf;
306     BOOL ret;
307
308     lstrcpyA(inf_filename, CURR_DIR);
309     lstrcatA(inf_filename, "\\");
310     lstrcatA(inf_filename, "test.inf");
311
312     create_inf_file(inf_filename);
313
314     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
315     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
316
317     required = 0;
318
319     ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_PATH, buffer, sizeof(buffer), &required);
320     ok(ret, "SetupGetSourceInfoA failed\n");
321
322     ok(required == 1, "unexpected required size: %d\n", required);
323     ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
324
325     required = 0;
326     buffer[0] = 0;
327
328     ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_TAGFILE, buffer, sizeof(buffer), &required);
329     ok(ret, "SetupGetSourceInfoA failed\n");
330
331     ok(required == 28, "unexpected required size: %d\n", required);
332     ok(!lstrcmpA("LANCOM\\LANtools\\lanconf.cab", buffer), "unexpected result string: %s\n", buffer);
333
334     required = 0;
335     buffer[0] = 0;
336
337     ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_DESCRIPTION, buffer, sizeof(buffer), &required);
338     ok(ret, "SetupGetSourceInfoA failed\n");
339
340     ok(required == 19, "unexpected required size: %d\n", required);
341     ok(!lstrcmpA("LANCOM Software CD", buffer), "unexpected result string: %s\n", buffer);
342
343     pSetupCloseInfFile(hinf);
344     DeleteFileA(inf_filename);
345 }
346
347 static void test_SetupGetTargetPath(void)
348 {
349     char buffer[MAX_PATH], inf_filename[MAX_PATH];
350     DWORD required;
351     HINF hinf;
352     INFCONTEXT ctx;
353     BOOL ret;
354
355     lstrcpyA(inf_filename, CURR_DIR);
356     lstrcatA(inf_filename, "\\");
357     lstrcatA(inf_filename, "test.inf");
358
359     create_inf_file(inf_filename);
360
361     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
362     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
363
364     ctx.Inf = hinf;
365     ctx.CurrentInf = hinf;
366     ctx.Section = 7;
367     ctx.Line = 0;
368
369     required = 0;
370
371     ret = pSetupGetTargetPathA(hinf, &ctx, NULL, buffer, sizeof(buffer), &required);
372     ok(ret, "SetupGetTargetPathA failed\n");
373
374     ok(required == 10, "unexpected required size: %d\n", required);
375     ok(!lstrcmpiA("C:\\LANCOM", buffer), "unexpected result string: %s\n", buffer);
376
377     pSetupCloseInfFile(hinf);
378     DeleteFileA(inf_filename);
379 }
380
381 START_TEST(query)
382 {
383     init_function_pointers();
384     get_directories();
385
386     test_SetupGetInfInformation();
387     test_SetupGetSourceFileLocation();
388     test_SetupGetSourceInfo();
389     test_SetupGetTargetPath();
390 }