ntdll: Fix compilation on systems that don't support nameless unions.
[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 void create_inf_file2(LPSTR filename)
105 {
106     char data[1024];
107     char *ptr = data;
108     DWORD dwNumberOfBytesWritten;
109     HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
110                            CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
111
112     append_str(&ptr, "[SourceFileInfo]\n");
113     append_str(&ptr, "sp1qfe\\bitsinst.exe=250B3702C7CCD7C2F9E4DAA1555C933E,000600060A28062C,27136,SP1QFE\n");
114     append_str(&ptr, "sp1qfe\\bitsprx2.dll=4EBEA67F4BB4EB402E725CA7CA2857AE,000600060A280621,7680,SP1QFE\n");
115     append_str(&ptr, "sp1qfe\\bitsprx3.dll=C788A1D9330DA011EF25E95D3BC7BDE5,000600060A280621,7168,SP1QFE\n");
116     append_str(&ptr, "sp1qfe\\qmgr.dll=696AC82FB290A03F205901442E0E9589,000600060A280621,361984,SP1QFE\n");
117     append_str(&ptr, "sp1qfe\\qmgrprxy.dll=8B5848144829E1BC985EA4C3D8CA7E3F,000600060A280621,17408,SP1QFE\n");
118     append_str(&ptr, "sp1qfe\\winhttp.dll=3EC6F518114606CA59D4160322077437,000500010A280615,331776,SP1QFE\n");
119     append_str(&ptr, "sp1qfe\\xpob2res.dll=DB83156B9F496F20D1EA70E4ABEC0166,000500010A280622,158720,SP1QFE\n");
120
121     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
122     CloseHandle(hf);
123 }
124
125 static BOOL check_info_filename(PSP_INF_INFORMATION info, LPSTR test)
126 {
127     LPSTR filename;
128     DWORD size;
129     BOOL ret = FALSE;
130
131     if (!pSetupQueryInfFileInformationA(info, 0, NULL, 0, &size))
132         return FALSE;
133
134     filename = HeapAlloc(GetProcessHeap(), 0, size);
135     if (!filename)
136         return FALSE;
137
138     pSetupQueryInfFileInformationA(info, 0, filename, size, &size);
139
140     if (!lstrcmpiA(test, filename))
141         ret = TRUE;
142
143     HeapFree(GetProcessHeap(), 0, filename);
144     return ret;
145 }
146
147 static PSP_INF_INFORMATION alloc_inf_info(LPCSTR filename, DWORD search, PDWORD size)
148 {
149     PSP_INF_INFORMATION info;
150     BOOL ret;
151
152     ret = pSetupGetInfInformationA(filename, search, NULL, 0, size);
153     if (!ret)
154         return NULL;
155
156     info = HeapAlloc(GetProcessHeap(), 0, *size);
157     return info;
158 }
159
160 static void test_SetupGetInfInformation(void)
161 {
162     PSP_INF_INFORMATION info;
163     CHAR inf_filename[MAX_PATH];
164     CHAR inf_one[MAX_PATH], inf_two[MAX_PATH];
165     DWORD size;
166     HINF hinf;
167     BOOL ret;
168
169     lstrcpyA(inf_filename, CURR_DIR);
170     lstrcatA(inf_filename, "\\");
171     lstrcatA(inf_filename, "test.inf");
172
173     /* try an invalid inf handle */
174     size = 0xdeadbeef;
175     SetLastError(0xbeefcafe);
176     ret = pSetupGetInfInformationA(NULL, INFINFO_INF_SPEC_IS_HINF, NULL, 0, &size);
177     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
178     ok(GetLastError() == ERROR_INVALID_HANDLE,
179        "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
180     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
181
182     /* try an invalid inf filename */
183     size = 0xdeadbeef;
184     SetLastError(0xbeefcafe);
185     ret = pSetupGetInfInformationA(NULL, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
186     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
187     ok(GetLastError() == ERROR_INVALID_PARAMETER,
188        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
189     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
190
191     create_inf_file(inf_filename);
192
193     /* try an invalid search flag */
194     size = 0xdeadbeef;
195     SetLastError(0xbeefcafe);
196     ret = pSetupGetInfInformationA(inf_filename, -1, NULL, 0, &size);
197     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
198     ok(GetLastError() == ERROR_INVALID_PARAMETER,
199        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
200     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
201
202     /* try a nonexistent inf file */
203     size = 0xdeadbeef;
204     SetLastError(0xbeefcafe);
205     ret = pSetupGetInfInformationA("idontexist", INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
206     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
207     ok(GetLastError() == ERROR_FILE_NOT_FOUND,
208        "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
209     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
210
211     /* successfully open the inf file */
212     size = 0xdeadbeef;
213     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
214     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
215     ok(size != 0xdeadbeef, "Expected a valid size on return\n");
216
217     /* set ReturnBuffer to NULL and ReturnBufferSize to non-zero value 'size' */
218     SetLastError(0xbeefcafe);
219     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size, &size);
220     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
221     ok(GetLastError() == ERROR_INVALID_PARAMETER,
222        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
223
224     /* set ReturnBuffer to NULL and ReturnBufferSize to non-zero value 'size-1' */
225     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size-1, &size);
226     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
227
228     /* some tests for behaviour with a NULL RequiredSize pointer */
229     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, NULL);
230     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
231     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size - 1, NULL);
232     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
233     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size, NULL);
234     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
235
236     info = HeapAlloc(GetProcessHeap(), 0, size);
237
238     /* try valid ReturnBuffer but too small size */
239     SetLastError(0xbeefcafe);
240     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, info, size - 1, &size);
241     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
242     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
243        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
244
245     /* successfully get the inf information */
246     ret = pSetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, info, size, &size);
247     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
248     ok(check_info_filename(info, inf_filename), "Expected returned filename to be equal\n");
249
250     HeapFree(GetProcessHeap(), 0, info);
251
252     /* try the INFINFO_INF_SPEC_IS_HINF search flag */
253     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
254     info = alloc_inf_info(hinf, INFINFO_INF_SPEC_IS_HINF, &size);
255     ret = pSetupGetInfInformationA(hinf, INFINFO_INF_SPEC_IS_HINF, info, size, &size);
256     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
257     ok(check_info_filename(info, inf_filename), "Expected returned filename to be equal\n");
258     pSetupCloseInfFile(hinf);
259
260     lstrcpyA(inf_one, WIN_DIR);
261     lstrcatA(inf_one, "\\inf\\");
262     lstrcatA(inf_one, "test.inf");
263     create_inf_file(inf_one);
264
265     lstrcpyA(inf_two, WIN_DIR);
266     lstrcatA(inf_two, "\\system32\\");
267     lstrcatA(inf_two, "test.inf");
268     create_inf_file(inf_two);
269
270     HeapFree(GetProcessHeap(), 0, info);
271     info = alloc_inf_info("test.inf", INFINFO_DEFAULT_SEARCH, &size);
272
273     /* test the INFINFO_DEFAULT_SEARCH search flag */
274     ret = pSetupGetInfInformationA("test.inf", INFINFO_DEFAULT_SEARCH, info, size, &size);
275     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
276     ok(check_info_filename(info, inf_one), "Expected returned filename to be equal\n");
277
278     HeapFree(GetProcessHeap(), 0, info);
279     info = alloc_inf_info("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, &size);
280
281     /* test the INFINFO_REVERSE_DEFAULT_SEARCH search flag */
282     ret = pSetupGetInfInformationA("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, info, size, &size);
283     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
284     ok(check_info_filename(info, inf_two), "Expected returned filename to be equal\n");
285
286     DeleteFileA(inf_filename);
287     DeleteFileA(inf_one);
288     DeleteFileA(inf_two);
289 }
290
291 static void test_SetupGetSourceFileLocation(void)
292 {
293     char buffer[MAX_PATH] = "not empty", inf_filename[MAX_PATH];
294     UINT source_id;
295     DWORD required, error;
296     HINF hinf;
297     BOOL ret;
298
299     lstrcpyA(inf_filename, CURR_DIR);
300     lstrcatA(inf_filename, "\\");
301     lstrcatA(inf_filename, "test.inf");
302
303     create_inf_file(inf_filename);
304
305     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
306     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
307
308     required = 0;
309     source_id = 0;
310
311     ret = pSetupGetSourceFileLocationA(hinf, NULL, "lanconf.exe", &source_id, buffer, sizeof(buffer), &required);
312     ok(ret, "SetupGetSourceFileLocation failed\n");
313
314     ok(required == 1, "unexpected required size: %d\n", required);
315     ok(source_id == 2, "unexpected source id: %d\n", source_id);
316     ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
317
318     pSetupCloseInfFile(hinf);
319     DeleteFileA(inf_filename);
320
321     create_inf_file2(inf_filename);
322
323     SetLastError(0xdeadbeef);
324     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
325     error = GetLastError();
326     ok(hinf == INVALID_HANDLE_VALUE, "could open inf file\n");
327     ok(error == ERROR_WRONG_INF_STYLE, "got wrong error: %d\n", error);
328
329     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_OLDNT, NULL);
330     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
331
332     ret = pSetupGetSourceFileLocationA(hinf, NULL, "", &source_id, buffer, sizeof(buffer), &required);
333     ok(!ret, "SetupGetSourceFileLocation succeeded\n");
334
335     pSetupCloseInfFile(hinf);
336     DeleteFileA(inf_filename);
337 }
338
339 static void test_SetupGetSourceInfo(void)
340 {
341     char buffer[MAX_PATH], inf_filename[MAX_PATH];
342     DWORD required;
343     HINF hinf;
344     BOOL ret;
345
346     lstrcpyA(inf_filename, CURR_DIR);
347     lstrcatA(inf_filename, "\\");
348     lstrcatA(inf_filename, "test.inf");
349
350     create_inf_file(inf_filename);
351
352     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
353     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
354
355     required = 0;
356
357     ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_PATH, buffer, sizeof(buffer), &required);
358     ok(ret, "SetupGetSourceInfoA failed\n");
359
360     ok(required == 1, "unexpected required size: %d\n", required);
361     ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
362
363     required = 0;
364     buffer[0] = 0;
365
366     ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_TAGFILE, buffer, sizeof(buffer), &required);
367     ok(ret, "SetupGetSourceInfoA failed\n");
368
369     ok(required == 28, "unexpected required size: %d\n", required);
370     ok(!lstrcmpA("LANCOM\\LANtools\\lanconf.cab", buffer), "unexpected result string: %s\n", buffer);
371
372     required = 0;
373     buffer[0] = 0;
374
375     ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_DESCRIPTION, buffer, sizeof(buffer), &required);
376     ok(ret, "SetupGetSourceInfoA failed\n");
377
378     ok(required == 19, "unexpected required size: %d\n", required);
379     ok(!lstrcmpA("LANCOM Software CD", buffer), "unexpected result string: %s\n", buffer);
380
381     pSetupCloseInfFile(hinf);
382     DeleteFileA(inf_filename);
383 }
384
385 static void test_SetupGetTargetPath(void)
386 {
387     char buffer[MAX_PATH], inf_filename[MAX_PATH];
388     DWORD required;
389     HINF hinf;
390     INFCONTEXT ctx;
391     BOOL ret;
392
393     lstrcpyA(inf_filename, CURR_DIR);
394     lstrcatA(inf_filename, "\\");
395     lstrcatA(inf_filename, "test.inf");
396
397     create_inf_file(inf_filename);
398
399     hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
400     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
401
402     ctx.Inf = hinf;
403     ctx.CurrentInf = hinf;
404     ctx.Section = 7;
405     ctx.Line = 0;
406
407     required = 0;
408
409     ret = pSetupGetTargetPathA(hinf, &ctx, NULL, buffer, sizeof(buffer), &required);
410     ok(ret, "SetupGetTargetPathA failed\n");
411
412     ok(required == 10, "unexpected required size: %d\n", required);
413     ok(!lstrcmpiA("C:\\LANCOM", buffer), "unexpected result string: %s\n", buffer);
414
415     pSetupCloseInfFile(hinf);
416     DeleteFileA(inf_filename);
417 }
418
419 START_TEST(query)
420 {
421     init_function_pointers();
422     get_directories();
423
424     test_SetupGetInfInformation();
425     test_SetupGetSourceFileLocation();
426     test_SetupGetSourceInfo();
427     test_SetupGetTargetPath();
428 }