mshtml: Add tests for get_scrollLeft.
[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 CHAR CURR_DIR[MAX_PATH];
27 CHAR WIN_DIR[MAX_PATH];
28
29 static void get_directories(void)
30 {
31     int len;
32
33     GetCurrentDirectoryA(MAX_PATH, CURR_DIR);
34     len = lstrlenA(CURR_DIR);
35
36     if(len && (CURR_DIR[len-1] == '\\'))
37         CURR_DIR[len-1] = 0;
38
39     GetWindowsDirectoryA(WIN_DIR, MAX_PATH);
40     len = lstrlenA(WIN_DIR);
41
42     if (len && (WIN_DIR[len-1] == '\\'))
43         WIN_DIR[len-1] = 0;
44 }
45
46 static void append_str(char **str, const char *data)
47 {
48     sprintf(*str, data);
49     *str += strlen(*str);
50 }
51
52 static void create_inf_file(LPSTR filename)
53 {
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);
59
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");
73
74     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
75     CloseHandle(hf);
76 }
77
78 static void create_inf_file2(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, "[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");
94
95     WriteFile(hf, data, ptr - data, &dwNumberOfBytesWritten, NULL);
96     CloseHandle(hf);
97 }
98
99 static BOOL check_info_filename(PSP_INF_INFORMATION info, LPSTR test)
100 {
101     LPSTR filename;
102     DWORD size;
103     BOOL ret = FALSE;
104
105     if (!SetupQueryInfFileInformationA(info, 0, NULL, 0, &size))
106         return FALSE;
107
108     filename = HeapAlloc(GetProcessHeap(), 0, size);
109     if (!filename)
110         return FALSE;
111
112     SetupQueryInfFileInformationA(info, 0, filename, size, &size);
113
114     if (!lstrcmpiA(test, filename))
115         ret = TRUE;
116
117     HeapFree(GetProcessHeap(), 0, filename);
118     return ret;
119 }
120
121 static PSP_INF_INFORMATION alloc_inf_info(LPCSTR filename, DWORD search, PDWORD size)
122 {
123     PSP_INF_INFORMATION info;
124     BOOL ret;
125
126     ret = SetupGetInfInformationA(filename, search, NULL, 0, size);
127     if (!ret)
128         return NULL;
129
130     info = HeapAlloc(GetProcessHeap(), 0, *size);
131     return info;
132 }
133
134 static void test_SetupGetInfInformation(void)
135 {
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;
143
144     lstrcpyA(inf_filename, CURR_DIR);
145     lstrcatA(inf_filename, "\\");
146     lstrcatA(inf_filename, "test.inf");
147
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        "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     /* do not use NULL as absolute inf filename on win9x (crash) */
163     if ((GetLastError() != ERROR_BAD_PATHNAME) &&   /* win95 */
164         (GetLastError() != ERROR_FILE_NOT_FOUND))  /* win98 */
165     {
166         size = 0xdeadbeef;
167         SetLastError(0xbeefcafe);
168         ret = SetupGetInfInformationA(NULL, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
169         ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
170         ok(GetLastError() == ERROR_INVALID_PARAMETER,
171            "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
172         ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
173     }
174
175     create_inf_file(inf_filename);
176
177     /* try an invalid search flag */
178     size = 0xdeadbeef;
179     SetLastError(0xbeefcafe);
180     ret = SetupGetInfInformationA(inf_filename, -1, NULL, 0, &size);
181     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
182     ok(GetLastError() == ERROR_INVALID_PARAMETER,
183        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
184     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
185
186     /* try a nonexistent inf file */
187     size = 0xdeadbeef;
188     SetLastError(0xbeefcafe);
189     ret = SetupGetInfInformationA("idontexist", INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
190     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
191     ok(GetLastError() == ERROR_FILE_NOT_FOUND,
192        "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
193     ok(size == 0xdeadbeef, "Expected size to remain unchanged\n");
194
195     /* successfully open the inf file */
196     size = 0xdeadbeef;
197     ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, &size);
198     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
199     ok(size != 0xdeadbeef, "Expected a valid size on return\n");
200
201     /* set ReturnBuffer to NULL and ReturnBufferSize to non-zero value 'size' */
202     SetLastError(0xbeefcafe);
203     ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size, &size);
204     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
205     ok(GetLastError() == ERROR_INVALID_PARAMETER,
206        "Expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
207
208     /* set ReturnBuffer to NULL and ReturnBufferSize to non-zero value 'size-1' */
209     ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size-1, &size);
210     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
211
212     /* some tests for behaviour with a NULL RequiredSize pointer */
213     ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, 0, NULL);
214     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
215     ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size - 1, NULL);
216     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
217     ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, NULL, size, NULL);
218     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
219
220     info = HeapAlloc(GetProcessHeap(), 0, size);
221
222     /* try valid ReturnBuffer but too small size */
223     SetLastError(0xbeefcafe);
224     ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, info, size - 1, &size);
225     ok(ret == FALSE, "Expected SetupGetInfInformation to fail\n");
226     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER,
227        "Expected ERROR_INSUFFICIENT_BUFFER, got %d\n", GetLastError());
228
229     /* successfully get the inf information */
230     ret = SetupGetInfInformationA(inf_filename, INFINFO_INF_NAME_IS_ABSOLUTE, info, size, &size);
231     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
232     ok(check_info_filename(info, inf_filename), "Expected returned filename to be equal\n");
233
234     HeapFree(GetProcessHeap(), 0, info);
235
236     /* try the INFINFO_INF_SPEC_IS_HINF search flag */
237     hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
238     info = alloc_inf_info(hinf, INFINFO_INF_SPEC_IS_HINF, &size);
239     ret = SetupGetInfInformationA(hinf, INFINFO_INF_SPEC_IS_HINF, info, size, &size);
240     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
241     ok(check_info_filename(info, inf_filename), "Expected returned filename to be equal\n");
242     SetupCloseInfFile(hinf);
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     /* check if system32 is searched for inf */
253     ret = SetupGetInfInformationA("test.inf", INFINFO_DEFAULT_SEARCH, info, size, &size);
254     if (!ret && GetLastError() == ERROR_FILE_NOT_FOUND)
255         revfile = inf_one; /* Vista */
256     else
257         revfile = inf_two;
258
259     lstrcpyA(inf_one, WIN_DIR);
260     lstrcatA(inf_one, "\\inf\\");
261     lstrcatA(inf_one, "test.inf");
262     create_inf_file(inf_one);
263
264     HeapFree(GetProcessHeap(), 0, info);
265     info = alloc_inf_info("test.inf", INFINFO_DEFAULT_SEARCH, &size);
266
267     /* test the INFINFO_DEFAULT_SEARCH search flag */
268     ret = SetupGetInfInformationA("test.inf", INFINFO_DEFAULT_SEARCH, info, size, &size);
269     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed: %d\n", GetLastError());
270     ok(check_info_filename(info, inf_one), "Expected returned filename to be equal\n");
271
272     HeapFree(GetProcessHeap(), 0, info);
273     info = alloc_inf_info("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, &size);
274
275     /* test the INFINFO_REVERSE_DEFAULT_SEARCH search flag */
276     ret = SetupGetInfInformationA("test.inf", INFINFO_REVERSE_DEFAULT_SEARCH, info, size, &size);
277     ok(ret == TRUE, "Expected SetupGetInfInformation to succeed\n");
278     ok(check_info_filename(info, revfile), "Expected returned filename to be equal\n");
279
280     HeapFree(GetProcessHeap(), 0, info);
281
282     DeleteFileA(inf_filename);
283     DeleteFileA(inf_one);
284     DeleteFileA(inf_two);
285 }
286
287 static void test_SetupGetSourceFileLocation(void)
288 {
289     char buffer[MAX_PATH] = "not empty", inf_filename[MAX_PATH];
290     UINT source_id;
291     DWORD required, error;
292     HINF hinf;
293     BOOL ret;
294
295     lstrcpyA(inf_filename, CURR_DIR);
296     lstrcatA(inf_filename, "\\");
297     lstrcatA(inf_filename, "test.inf");
298
299     create_inf_file(inf_filename);
300
301     hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
302     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
303
304     required = 0;
305     source_id = 0;
306
307     ret = SetupGetSourceFileLocationA(hinf, NULL, "lanconf.exe", &source_id, buffer, sizeof(buffer), &required);
308     ok(ret, "SetupGetSourceFileLocation failed\n");
309
310     ok(required == 1, "unexpected required size: %d\n", required);
311     ok(source_id == 2, "unexpected source id: %d\n", source_id);
312     ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
313
314     SetupCloseInfFile(hinf);
315     DeleteFileA(inf_filename);
316
317     create_inf_file2(inf_filename);
318
319     SetLastError(0xdeadbeef);
320     hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
321     error = GetLastError();
322     ok(hinf == INVALID_HANDLE_VALUE, "could open inf file\n");
323     ok(error == ERROR_WRONG_INF_STYLE, "got wrong error: %d\n", error);
324
325     hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_OLDNT, NULL);
326     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
327
328     ret = SetupGetSourceFileLocationA(hinf, NULL, "", &source_id, buffer, sizeof(buffer), &required);
329     ok(!ret, "SetupGetSourceFileLocation succeeded\n");
330
331     SetupCloseInfFile(hinf);
332     DeleteFileA(inf_filename);
333 }
334
335 static void test_SetupGetSourceInfo(void)
336 {
337     char buffer[MAX_PATH], inf_filename[MAX_PATH];
338     DWORD required;
339     HINF hinf;
340     BOOL ret;
341
342     lstrcpyA(inf_filename, CURR_DIR);
343     lstrcatA(inf_filename, "\\");
344     lstrcatA(inf_filename, "test.inf");
345
346     create_inf_file(inf_filename);
347
348     hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
349     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
350
351     required = 0;
352
353     ret = SetupGetSourceInfoA(hinf, 2, SRCINFO_PATH, buffer, sizeof(buffer), &required);
354     ok(ret, "SetupGetSourceInfoA failed\n");
355
356     ok(required == 1, "unexpected required size: %d\n", required);
357     ok(!lstrcmpA("", buffer), "unexpected result string: %s\n", buffer);
358
359     required = 0;
360     buffer[0] = 0;
361
362     ret = SetupGetSourceInfoA(hinf, 2, SRCINFO_TAGFILE, buffer, sizeof(buffer), &required);
363     ok(ret, "SetupGetSourceInfoA failed\n");
364
365     ok(required == 28, "unexpected required size: %d\n", required);
366     ok(!lstrcmpA("LANCOM\\LANtools\\lanconf.cab", buffer), "unexpected result string: %s\n", buffer);
367
368     required = 0;
369     buffer[0] = 0;
370
371     ret = SetupGetSourceInfoA(hinf, 2, SRCINFO_DESCRIPTION, buffer, sizeof(buffer), &required);
372     ok(ret, "SetupGetSourceInfoA failed\n");
373
374     ok(required == 19, "unexpected required size: %d\n", required);
375     ok(!lstrcmpA("LANCOM Software CD", buffer), "unexpected result string: %s\n", buffer);
376
377     SetupCloseInfFile(hinf);
378     DeleteFileA(inf_filename);
379 }
380
381 static void test_SetupGetTargetPath(void)
382 {
383     char buffer[MAX_PATH], inf_filename[MAX_PATH];
384     DWORD required;
385     HINF hinf;
386     INFCONTEXT ctx;
387     BOOL ret;
388
389     lstrcpyA(inf_filename, CURR_DIR);
390     lstrcatA(inf_filename, "\\");
391     lstrcatA(inf_filename, "test.inf");
392
393     create_inf_file(inf_filename);
394
395     hinf = SetupOpenInfFileA(inf_filename, NULL, INF_STYLE_WIN4, NULL);
396     ok(hinf != INVALID_HANDLE_VALUE, "could not open inf file\n");
397
398     ctx.Inf = hinf;
399     ctx.CurrentInf = hinf;
400     ctx.Section = 7;
401     ctx.Line = 0;
402
403     required = 0;
404
405     ret = SetupGetTargetPathA(hinf, &ctx, NULL, buffer, sizeof(buffer), &required);
406     ok(ret, "SetupGetTargetPathA failed\n");
407
408     ok(required == 10, "unexpected required size: %d\n", required);
409     ok(!lstrcmpiA("C:\\LANCOM", buffer), "unexpected result string: %s\n", buffer);
410
411     SetupCloseInfFile(hinf);
412     DeleteFileA(inf_filename);
413 }
414
415 START_TEST(query)
416 {
417     get_directories();
418
419     test_SetupGetInfInformation();
420     test_SetupGetSourceFileLocation();
421     test_SetupGetSourceInfo();
422     test_SetupGetTargetPath();
423 }