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
24 #include "wine/test.h"
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);
36 CHAR CURR_DIR[MAX_PATH];
37 CHAR WIN_DIR[MAX_PATH];
39 static void init_function_pointers(void)
41 hSetupAPI = LoadLibraryA("setupapi.dll");
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");
55 static void get_directories(void)
59 GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
60 len = lstrlenA(CURR_DIR);
62 if(len && (CURR_DIR[len-1] == '\\'))
65 GetWindowsDirectoryA(WIN_DIR, MAX_PATH);
66 len = lstrlenA(WIN_DIR);
68 if (len && (WIN_DIR[len-1] == '\\'))
72 static void append_str(char **str, const char *data)
78 static void create_inf_file(LPSTR filename)
82 DWORD dwNumberOfBytesWritten;
83 HANDLE hf = CreateFile(filename, GENERIC_WRITE, 0, NULL,
84 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
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");
100 WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
104 static BOOL check_info_filename(PSP_INF_INFORMATION info, LPSTR test)
110 if (!pSetupQueryInfFileInformationA(info, 0, NULL, 0, &size))
113 filename = HeapAlloc(GetProcessHeap(), 0, size);
117 pSetupQueryInfFileInformationA(info, 0, filename, size, &size);
119 if (!lstrcmpiA(test, filename))
122 HeapFree(GetProcessHeap(), 0, filename);
126 static PSP_INF_INFORMATION alloc_inf_info(LPCSTR filename, DWORD search, PDWORD size)
128 PSP_INF_INFORMATION info;
131 ret = pSetupGetInfInformationA(filename, search, NULL, 0, size);
135 info = HeapAlloc(GetProcessHeap(), 0, *size);
139 static void test_SetupGetInfInformation(void)
141 PSP_INF_INFORMATION info;
142 CHAR inf_filename[MAX_PATH];
143 CHAR inf_one[MAX_PATH], inf_two[MAX_PATH];
148 lstrcpyA(inf_filename, CURR_DIR);
149 lstrcatA(inf_filename, "\\");
150 lstrcatA(inf_filename, "test.inf");
152 /* try an invalid inf handle */
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");
161 /* try an invalid inf filename */
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");
170 create_inf_file(inf_filename);
172 /* try an invalid search flag */
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");
181 /* try a nonexistent inf file */
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");
190 /* successfully open the inf file */
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");
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());
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");
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");
215 info = HeapAlloc(GetProcessHeap(), 0, size);
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());
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");
229 HeapFree(GetProcessHeap(), 0, info);
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);
239 lstrcpyA(inf_one, WIN_DIR);
240 lstrcatA(inf_one, "\\inf\\");
241 lstrcatA(inf_one, "test.inf");
242 create_inf_file(inf_one);
244 lstrcpyA(inf_two, WIN_DIR);
245 lstrcatA(inf_two, "\\system32\\");
246 lstrcatA(inf_two, "test.inf");
247 create_inf_file(inf_two);
249 HeapFree(GetProcessHeap(), 0, info);
250 info = alloc_inf_info("test.inf", INFINFO_DEFAULT_SEARCH, &size);
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");
257 HeapFree(GetProcessHeap(), 0, info);
258 info = alloc_inf_info("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, &size);
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");
265 DeleteFileA(inf_filename);
266 DeleteFileA(inf_one);
267 DeleteFileA(inf_two);
270 static void test_SetupGetSourceFileLocation(void)
272 char buffer[MAX_PATH] = "not empty", inf_filename[MAX_PATH];
278 lstrcpyA(inf_filename, CURR_DIR);
279 lstrcatA(inf_filename, "\\");
280 lstrcatA(inf_filename, "test.inf");
282 create_inf_file(inf_filename);
284 hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
285 ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
290 ret = pSetupGetSourceFileLocationA(hinf, NULL, "lanconf.exe", &source_id, buffer, sizeof(buffer), &required);
291 ok(ret, "SetupGetSourceFileLocation failed\n");
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);
297 pSetupCloseInfFile(hinf);
298 DeleteFileA(inf_filename);
301 static void test_SetupGetSourceInfo(void)
303 char buffer[MAX_PATH], inf_filename[MAX_PATH];
308 lstrcpyA(inf_filename, CURR_DIR);
309 lstrcatA(inf_filename, "\\");
310 lstrcatA(inf_filename, "test.inf");
312 create_inf_file(inf_filename);
314 hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
315 ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
319 ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_PATH, buffer, sizeof(buffer), &required);
320 ok(ret, "SetupGetSourceInfoA failed\n");
322 ok(required == 1, "unexpected required size: %d\n", required);
323 ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
328 ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_TAGFILE, buffer, sizeof(buffer), &required);
329 ok(ret, "SetupGetSourceInfoA failed\n");
331 ok(required == 28, "unexpected required size: %d\n", required);
332 ok(!lstrcmpA("LANCOM\\LANtools\\lanconf.cab", buffer), "unexpected result string: %s\n", buffer);
337 ret = pSetupGetSourceInfoA(hinf, 2, SRCINFO_DESCRIPTION, buffer, sizeof(buffer), &required);
338 ok(ret, "SetupGetSourceInfoA failed\n");
340 ok(required == 19, "unexpected required size: %d\n", required);
341 ok(!lstrcmpA("LANCOM Software CD", buffer), "unexpected result string: %s\n", buffer);
343 pSetupCloseInfFile(hinf);
344 DeleteFileA(inf_filename);
347 static void test_SetupGetTargetPath(void)
349 char buffer[MAX_PATH], inf_filename[MAX_PATH];
355 lstrcpyA(inf_filename, CURR_DIR);
356 lstrcatA(inf_filename, "\\");
357 lstrcatA(inf_filename, "test.inf");
359 create_inf_file(inf_filename);
361 hinf = pSetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
362 ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
365 ctx.CurrentInf = hinf;
371 ret = pSetupGetTargetPathA(hinf, &ctx, NULL, buffer, sizeof(buffer), &required);
372 ok(ret, "SetupGetTargetPathA failed\n");
374 ok(required == 10, "unexpected required size: %d\n", required);
375 ok(!lstrcmpiA("C:\\LANCOM", buffer), "unexpected result string: %s\n", buffer);
377 pSetupCloseInfFile(hinf);
378 DeleteFileA(inf_filename);
383 init_function_pointers();
386 test_SetupGetInfInformation();
387 test_SetupGetSourceFileLocation();
388 test_SetupGetSourceInfo();
389 test_SetupGetTargetPath();