push ada2d4f150af430b0cf01adcbe8b6d3e34deeeb1
[wine/hacks.git] / dlls / setupapi / tests / query.c
blob167727a99785b75d23023a82aa0ab398ea0e965d
1 /*
2 * Unit tests for setupapi.dll query 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 <setupapi.h>
24 #include "wine/test.h"
26 CHAR CURR_DIR[MAX_PATH];
27 CHAR WIN_DIR[MAX_PATH];
29 static void get_directories(void)
31 int len;
33 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
34 len = lstrlenA(CURR_DIR);
36 if(len && (CURR_DIR[len-1] == '\\'))
37 CURR_DIR[len-1] = 0;
39 GetWindowsDirectoryA(WIN_DIR, MAX_PATH);
40 len = lstrlenA(WIN_DIR);
42 if (len && (WIN_DIR[len-1] == '\\'))
43 WIN_DIR[len-1] = 0;
46 static void append_str(char **str, const char *data)
48 sprintf(*str, data);
49 *str += strlen(*str);
52 static void create_inf_file(LPSTR filename)
54 char data[1024];
55 char *ptr = data;
56 DWORD dwNumberOfBytesWritten;
57 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
58 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
60 append_str(&ptr, "[Version]\n");
61 append_str(&ptr, "Signature=\"$Chicago$\"\n");
62 append_str(&ptr, "AdvancedINF=2.5\n");
63 append_str(&ptr, "[SourceDisksNames]\n");
64 append_str(&ptr, "2 = %%SrcDiskName%%, LANCOM\\LANtools\\lanconf.cab\n");
65 append_str(&ptr, "[SourceDisksFiles]\n");
66 append_str(&ptr, "lanconf.exe = 2\n");
67 append_str(&ptr, "[DestinationDirs]\n");
68 append_str(&ptr, "DefaultDestDir = 24, %%DefaultDest%%\n");
69 append_str(&ptr, "[Strings]\n");
70 append_str(&ptr, "LangDir = english\n");
71 append_str(&ptr, "DefaultDest = LANCOM\n");
72 append_str(&ptr, "SrcDiskName = \"LANCOM Software CD\"\n");
74 WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
75 CloseHandle(hf);
78 static void create_inf_file2(LPSTR filename)
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);
86 append_str(&ptr, "[SourceFileInfo]\n");
87 append_str(&ptr, "sp1qfe\\bitsinst.exe=250B3702C7CCD7C2F9E4DAA1555C933E,000600060A28062C,27136,SP1QFE\n");
88 append_str(&ptr, "sp1qfe\\bitsprx2.dll=4EBEA67F4BB4EB402E725CA7CA2857AE,000600060A280621,7680,SP1QFE\n");
89 append_str(&ptr, "sp1qfe\\bitsprx3.dll=C788A1D9330DA011EF25E95D3BC7BDE5,000600060A280621,7168,SP1QFE\n");
90 append_str(&ptr, "sp1qfe\\qmgr.dll=696AC82FB290A03F205901442E0E9589,000600060A280621,361984,SP1QFE\n");
91 append_str(&ptr, "sp1qfe\\qmgrprxy.dll=8B5848144829E1BC985EA4C3D8CA7E3F,000600060A280621,17408,SP1QFE\n");
92 append_str(&ptr, "sp1qfe\\winhttp.dll=3EC6F518114606CA59D4160322077437,000500010A280615,331776,SP1QFE\n");
93 append_str(&ptr, "sp1qfe\\xpob2res.dll=DB83156B9F496F20D1EA70E4ABEC0166,000500010A280622,158720,SP1QFE\n");
95 WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
96 CloseHandle(hf);
99 static BOOL check_info_filename(PSP_INF_INFORMATION info, LPSTR test)
101 LPSTR filename;
102 DWORD size;
103 BOOL ret = FALSE;
105 if (!SetupQueryInfFileInformationA(info, 0, NULL, 0, &size))
106 return FALSE;
108 filename = HeapAlloc(GetProcessHeap(), 0, size);
109 if (!filename)
110 return FALSE;
112 SetupQueryInfFileInformationA(info, 0, filename, size, &size);
114 if (!lstrcmpiA(test, filename))
115 ret = TRUE;
117 HeapFree(GetProcessHeap(), 0, filename);
118 return ret;
121 static PSP_INF_INFORMATION alloc_inf_info(LPCSTR filename, DWORD search, PDWORD size)
123 PSP_INF_INFORMATION info;
124 BOOL ret;
126 ret = SetupGetInfInformationA(filename, search, NULL, 0, size);
127 if (!ret)
128 return NULL;
130 info = HeapAlloc(GetProcessHeap(), 0, *size);
131 return info;
134 static void test_SetupGetInfInformation(void)
136 PSP_INF_INFORMATION info;
137 CHAR inf_filename[MAX_PATH];
138 CHAR inf_one[MAX_PATH], inf_two[MAX_PATH];
139 LPSTR revfile;
140 DWORD size;
141 HINF hinf;
142 BOOL ret;
144 lstrcpyA(inf_filename, CURR_DIR);
145 lstrcatA(inf_filename, "\\");
146 lstrcatA(inf_filename, "test.inf");
148 /* try an invalid inf handle */
149 size = 0xdeadbeef;
150 SetLastError(0xbeefcafe);
151 ret = SetupGetInfInformationA(NULL, INFINFO_INF_SPEC_IS_HINF, NULL, 0, &size);
152 ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
153 ok(GetLastError() == ERROR_INVALID_HANDLE ||
154 broken(GetLastError() == ERROR_BAD_PATHNAME) || /* win95 */
155 broken(GetLastError() == ERROR_FILE_NOT_FOUND) || /* win98 */
156 broken(GetLastError() == ERROR_PATH_NOT_FOUND) || /* NT4 */
157 broken(GetLastError() == ERROR_INVALID_NAME) || /* win2k */
158 broken(GetLastError() == ERROR_GENERAL_SYNTAX), /* another win2k */
159 "Expected ERROR_INVALID_HANDLE, got %d\n", GetLastError());
160 ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
162 /* try an invalid inf filename */
163 /* do not use NULL as absolute inf filename on win9x (crash) */
164 if ((GetLastError() != ERROR_BAD_PATHNAME) && /* win95 */
165 (GetLastError() != ERROR_FILE_NOT_FOUND)) /* win98 */
167 size = 0xdeadbeef;
168 SetLastError(0xbeefcafe);
169 ret = SetupGetInfInformationA(NULL, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
170 ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
171 ok(GetLastError() == ERROR_INVALID_PARAMETER,
172 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
173 ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
176 create_inf_file(inf_filename);
178 /* try an invalid search flag */
179 size = 0xdeadbeef;
180 SetLastError(0xbeefcafe);
181 ret = SetupGetInfInformationA(inf_filename, -1, NULL, 0, &size);
182 ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
183 ok(GetLastError() == ERROR_INVALID_PARAMETER,
184 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
185 ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
187 /* try a nonexistent inf file */
188 size = 0xdeadbeef;
189 SetLastError(0xbeefcafe);
190 ret = SetupGetInfInformationA("idontexist", INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
191 ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
192 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
193 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
194 ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
196 /* successfully open the inf file */
197 size = 0xdeadbeef;
198 ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
199 ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
200 ok(size != 0xdeadbeef, "Expected a valid size on return\n");
202 /* set ReturnBuffer to NULL and ReturnBufferSize to non-zero value 'size' */
203 SetLastError(0xbeefcafe);
204 ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size, &size);
205 ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
206 ok(GetLastError() == ERROR_INVALID_PARAMETER,
207 "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
209 /* set ReturnBuffer to NULL and ReturnBufferSize to non-zero value 'size-1' */
210 ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size-1, &size);
211 ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
213 /* some tests for behaviour with a NULL RequiredSize pointer */
214 ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, NULL);
215 ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
216 ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size - 1, NULL);
217 ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
218 ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size, NULL);
219 ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
221 info = HeapAlloc(GetProcessHeap(), 0, size);
223 /* try valid ReturnBuffer but too small size */
224 SetLastError(0xbeefcafe);
225 ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, info, size - 1, &size);
226 ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
227 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
228 "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
230 /* successfully get the inf information */
231 ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, info, size, &size);
232 ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
233 ok(check_info_filename(info, inf_filename), "Expected returned filename to be equal\n");
235 HeapFree(GetProcessHeap(), 0, info);
237 /* try the INFINFO_INF_SPEC_IS_HINF search flag */
238 hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
239 info = alloc_inf_info(hinf, INFINFO_INF_SPEC_IS_HINF, &size);
240 ret = SetupGetInfInformationA(hinf, INFINFO_INF_SPEC_IS_HINF, info, size, &size);
241 ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
242 ok(check_info_filename(info, inf_filename), "Expected returned filename to be equal\n");
243 SetupCloseInfFile(hinf);
245 lstrcpyA(inf_two, WIN_DIR);
246 lstrcatA(inf_two, "\\system32\\");
247 lstrcatA(inf_two, "test.inf");
248 create_inf_file(inf_two);
250 HeapFree(GetProcessHeap(), 0, info);
251 info = alloc_inf_info("test.inf", INFINFO_DEFAULT_SEARCH, &size);
253 /* check if system32 is searched for inf */
254 ret = SetupGetInfInformationA("test.inf", INFINFO_DEFAULT_SEARCH, info, size, &size);
255 if (!ret && GetLastError() == ERROR_FILE_NOT_FOUND)
256 revfile = inf_one; /* Vista */
257 else
258 revfile = inf_two;
260 lstrcpyA(inf_one, WIN_DIR);
261 lstrcatA(inf_one, "\\inf\\");
262 lstrcatA(inf_one, "test.inf");
263 create_inf_file(inf_one);
265 HeapFree(GetProcessHeap(), 0, info);
266 info = alloc_inf_info("test.inf", INFINFO_DEFAULT_SEARCH, &size);
268 /* test the INFINFO_DEFAULT_SEARCH search flag */
269 ret = SetupGetInfInformationA("test.inf", INFINFO_DEFAULT_SEARCH, info, size, &size);
270 ok(ret == TRUE, "Expected SetupGetInfInformation to succeed: %d\n", GetLastError());
271 ok(check_info_filename(info, inf_one), "Expected returned filename to be equal\n");
273 HeapFree(GetProcessHeap(), 0, info);
274 info = alloc_inf_info("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, &size);
276 /* test the INFINFO_REVERSE_DEFAULT_SEARCH search flag */
277 ret = SetupGetInfInformationA("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, info, size, &size);
278 ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
279 ok(check_info_filename(info, revfile), "Expected returned filename to be equal\n");
281 HeapFree(GetProcessHeap(), 0, info);
283 DeleteFileA(inf_filename);
284 DeleteFileA(inf_one);
285 DeleteFileA(inf_two);
288 static void test_SetupGetSourceFileLocation(void)
290 char buffer[MAX_PATH] = "not empty", inf_filename[MAX_PATH];
291 UINT source_id;
292 DWORD required, error;
293 HINF hinf;
294 BOOL ret;
296 lstrcpyA(inf_filename, CURR_DIR);
297 lstrcatA(inf_filename, "\\");
298 lstrcatA(inf_filename, "test.inf");
300 create_inf_file(inf_filename);
302 hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
303 ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
305 required = 0;
306 source_id = 0;
308 ret = SetupGetSourceFileLocationA(hinf, NULL, "lanconf.exe", &source_id, buffer, sizeof(buffer), &required);
309 ok(ret, "SetupGetSourceFileLocation failed\n");
311 ok(required == 1, "unexpected required size: %d\n", required);
312 ok(source_id == 2, "unexpected source id: %d\n", source_id);
313 ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
315 SetupCloseInfFile(hinf);
316 DeleteFileA(inf_filename);
318 create_inf_file2(inf_filename);
320 SetLastError(0xdeadbeef);
321 hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
322 error = GetLastError();
323 ok(hinf == INVALID_HANDLE_VALUE, "could open inf file\n");
324 ok(error == ERROR_WRONG_INF_STYLE, "got wrong error: %d\n", error);
326 hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_OLDNT, NULL);
327 ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
329 ret = SetupGetSourceFileLocationA(hinf, NULL, "", &source_id, buffer, sizeof(buffer), &required);
330 ok(!ret, "SetupGetSourceFileLocation succeeded\n");
332 SetupCloseInfFile(hinf);
333 DeleteFileA(inf_filename);
336 static void test_SetupGetSourceInfo(void)
338 char buffer[MAX_PATH], inf_filename[MAX_PATH];
339 DWORD required;
340 HINF hinf;
341 BOOL ret;
343 lstrcpyA(inf_filename, CURR_DIR);
344 lstrcatA(inf_filename, "\\");
345 lstrcatA(inf_filename, "test.inf");
347 create_inf_file(inf_filename);
349 hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
350 ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
352 required = 0;
354 ret = SetupGetSourceInfoA(hinf, 2, SRCINFO_PATH, buffer, sizeof(buffer), &required);
355 ok(ret, "SetupGetSourceInfoA failed\n");
357 ok(required == 1, "unexpected required size: %d\n", required);
358 ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
360 required = 0;
361 buffer[0] = 0;
363 ret = SetupGetSourceInfoA(hinf, 2, SRCINFO_TAGFILE, buffer, sizeof(buffer), &required);
364 ok(ret, "SetupGetSourceInfoA failed\n");
366 ok(required == 28, "unexpected required size: %d\n", required);
367 ok(!lstrcmpA("LANCOM\\LANtools\\lanconf.cab", buffer), "unexpected result string: %s\n", buffer);
369 required = 0;
370 buffer[0] = 0;
372 ret = SetupGetSourceInfoA(hinf, 2, SRCINFO_DESCRIPTION, buffer, sizeof(buffer), &required);
373 ok(ret, "SetupGetSourceInfoA failed\n");
375 ok(required == 19, "unexpected required size: %d\n", required);
376 ok(!lstrcmpA("LANCOM Software CD", buffer), "unexpected result string: %s\n", buffer);
378 SetupCloseInfFile(hinf);
379 DeleteFileA(inf_filename);
382 static void test_SetupGetTargetPath(void)
384 char buffer[MAX_PATH], inf_filename[MAX_PATH];
385 char destfile[MAX_PATH];
386 DWORD required;
387 HINF hinf;
388 INFCONTEXT ctx;
389 BOOL ret;
391 lstrcpyA(inf_filename, CURR_DIR);
392 lstrcatA(inf_filename, "\\");
393 lstrcatA(inf_filename, "test.inf");
395 create_inf_file(inf_filename);
397 hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
398 ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
400 ctx.Inf = hinf;
401 ctx.CurrentInf = hinf;
402 ctx.Section = 7;
403 ctx.Line = 0;
405 required = 0;
407 ret = SetupGetTargetPathA(hinf, &ctx, NULL, buffer, sizeof(buffer), &required);
408 ok(ret, "SetupGetTargetPathA failed\n");
409 ok(required == 10, "unexpected required size: %d\n", required);
410 /* Retrieve the system drive from the windows directory.
411 * (%SystemDrive% is not available on Win9x)
413 lstrcpyA(destfile, WIN_DIR);
414 destfile[3] = '\0';
415 lstrcatA(destfile, "LANCOM");
416 ok(!lstrcmpiA(destfile, buffer), "unexpected result string: %s\n", buffer);
418 SetupCloseInfFile(hinf);
419 DeleteFileA(inf_filename);
422 START_TEST(query)
424 get_directories();
426 test_SetupGetInfInformation();
427 test_SetupGetSourceFileLocation();
428 test_SetupGetSourceInfo();
429 test_SetupGetTargetPath();