setupapi/tests: Use BOOL type where appropriate.
[wine/multimedia.git] / dlls / setupapi / tests / parser.c
blobc2d554a661645f173ae45885c0c640d2f841fbbc
1 /*
2 * INF file parsing tests
4 * Copyright 2002, 2005 Alexandre Julliard for CodeWeavers
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 <stdarg.h>
23 #include "windef.h"
24 #include "winbase.h"
25 #include "wingdi.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "setupapi.h"
30 #include "wine/test.h"
32 /* function pointers */
33 static HMODULE hSetupAPI;
34 static LPCSTR (WINAPI *pSetupGetFieldA)(PINFCONTEXT,DWORD);
35 static LPCWSTR (WINAPI *pSetupGetFieldW)(PINFCONTEXT,DWORD);
36 static BOOL (WINAPI *pSetupEnumInfSectionsA)( HINF hinf, UINT index, PSTR buffer, DWORD size, UINT *need );
38 static void init_function_pointers(void)
40 hSetupAPI = GetModuleHandleA("setupapi.dll");
42 /* Nice, pSetupGetField is either A or W depending on the Windows version! The actual test
43 * takes care of this difference */
44 pSetupGetFieldA = (void *)GetProcAddress(hSetupAPI, "pSetupGetField");
45 pSetupGetFieldW = (void *)GetProcAddress(hSetupAPI, "pSetupGetField");
46 pSetupEnumInfSectionsA = (void *)GetProcAddress(hSetupAPI, "SetupEnumInfSectionsA" );
49 static const char tmpfilename[] = ".\\tmp.inf";
51 /* some large strings */
52 #define A255 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
53 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
54 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
55 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
56 #define A256 "a" A255
57 #define A400 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
58 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
59 "aaaaaaaaaaaaaaaa" A256
60 #define A1200 A400 A400 A400
61 #define A511 A255 A256
62 #define A4097 "a" A256 A256 A256 A256 A256 A256 A256 A256 A256 A256 A256 A256 A256 A256 A256 A256
64 #define STD_HEADER "[Version]\r\nSignature=\"$CHICAGO$\"\r\n"
66 #define STR_SECTION "[Strings]\nfoo=aaa\nbar=bbb\nloop=%loop2%\nloop2=%loop%\n" \
67 "per%%cent=abcd\nper=1\ncent=2\n22=foo\n" \
68 "big=" A400 "\n" \
69 "mydrive=\"C:\\\"\n" \
70 "verybig=" A1200 "\n"
72 /* create a new file with specified contents and open it */
73 static HINF test_file_contents( const char *data, UINT *err_line )
75 DWORD res;
76 HANDLE handle = CreateFileA( tmpfilename, GENERIC_READ|GENERIC_WRITE,
77 FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, 0 );
78 if (handle == INVALID_HANDLE_VALUE) return 0;
79 if (!WriteFile( handle, data, strlen(data), &res, NULL )) trace( "write error\n" );
80 CloseHandle( handle );
81 return SetupOpenInfFileA( tmpfilename, 0, INF_STYLE_WIN4, err_line );
84 static const char *get_string_field( INFCONTEXT *context, DWORD index )
86 static char buffer[MAX_INF_STRING_LENGTH+32];
87 if (SetupGetStringFieldA( context, index, buffer, sizeof(buffer), NULL )) return buffer;
88 return NULL;
91 static const char *get_line_text( INFCONTEXT *context )
93 static char buffer[MAX_INF_STRING_LENGTH+32];
94 if (SetupGetLineTextA( context, 0, 0, 0, buffer, sizeof(buffer), NULL )) return buffer;
95 return NULL;
99 /* Test various valid/invalid file formats */
101 static const struct
103 const char *data;
104 DWORD error;
105 UINT err_line;
106 BOOL todo;
107 } invalid_files[] =
109 /* file contents expected error (or 0) errline todo */
110 { "\r\n", ERROR_WRONG_INF_STYLE, 0, FALSE },
111 { "abcd\r\n", ERROR_WRONG_INF_STYLE, 0, TRUE },
112 { "[Version]\r\n", ERROR_WRONG_INF_STYLE, 0, FALSE },
113 { "[Version]\nSignature=", ERROR_WRONG_INF_STYLE, 0, FALSE },
114 { "[Version]\nSignature=foo", ERROR_WRONG_INF_STYLE, 0, FALSE },
115 { "[version]\nsignature=$chicago$", 0, 0, FALSE },
116 { "[VERSION]\nSIGNATURE=$CHICAGO$", 0, 0, FALSE },
117 { "[Version]\nSignature=$chicago$,abcd", 0, 0, FALSE },
118 { "[Version]\nabc=def\nSignature=$chicago$", 0, 0, FALSE },
119 { "[Version]\nabc=def\n[Version]\nSignature=$chicago$", 0, 0, FALSE },
120 { STD_HEADER, 0, 0, FALSE },
121 { STD_HEADER "[]\r\n", 0, 0, FALSE },
122 { STD_HEADER "]\r\n", 0, 0, FALSE },
123 { STD_HEADER "[" A255 "]\r\n", 0, 0, FALSE },
124 { STD_HEADER "[ab\r\n", ERROR_BAD_SECTION_NAME_LINE, 3, FALSE },
125 { STD_HEADER "\n\n[ab\x1a]\n", ERROR_BAD_SECTION_NAME_LINE, 5, FALSE },
126 { STD_HEADER "[" A256 "]\r\n", ERROR_SECTION_NAME_TOO_LONG, 3, FALSE },
127 { "[abc]\n" STD_HEADER, 0, 0, FALSE },
128 { "abc\r\n" STD_HEADER, ERROR_EXPECTED_SECTION_NAME, 1, FALSE },
129 { ";\n;\nabc\r\n" STD_HEADER, ERROR_EXPECTED_SECTION_NAME, 3, FALSE },
130 { ";\n;\nab\nab\n" STD_HEADER, ERROR_EXPECTED_SECTION_NAME, 3, FALSE },
131 { ";aa\n;bb\n" STD_HEADER, 0, 0, FALSE },
132 { STD_HEADER " [TestSection\x00]\n", ERROR_BAD_SECTION_NAME_LINE, 3, FALSE },
133 { STD_HEADER " [Test\x00Section]\n", ERROR_BAD_SECTION_NAME_LINE, 3, FALSE },
134 { STD_HEADER " [TestSection\x00]\n", ERROR_BAD_SECTION_NAME_LINE, 3, FALSE },
135 { STD_HEADER " [Test\x00Section]\n", ERROR_BAD_SECTION_NAME_LINE, 3, FALSE },
138 static void test_invalid_files(void)
140 unsigned int i;
141 UINT err_line;
142 HINF hinf;
143 DWORD err;
145 for (i = 0; i < sizeof(invalid_files)/sizeof(invalid_files[0]); i++)
147 SetLastError( 0xdeadbeef );
148 err_line = 0xdeadbeef;
149 hinf = test_file_contents( invalid_files[i].data, &err_line );
150 err = GetLastError();
151 trace( "hinf=%p err=0x%x line=%d\n", hinf, err, err_line );
152 if (invalid_files[i].error) /* should fail */
154 ok( hinf == INVALID_HANDLE_VALUE, "file %u: Open succeeded\n", i );
155 if (invalid_files[i].todo) todo_wine
157 ok( err == invalid_files[i].error, "file %u: Bad error %u/%u\n",
158 i, err, invalid_files[i].error );
159 ok( err_line == invalid_files[i].err_line, "file %u: Bad error line %d/%d\n",
160 i, err_line, invalid_files[i].err_line );
162 else
164 ok( err == invalid_files[i].error, "file %u: Bad error %u/%u\n",
165 i, err, invalid_files[i].error );
166 ok( err_line == invalid_files[i].err_line, "file %u: Bad error line %d/%d\n",
167 i, err_line, invalid_files[i].err_line );
170 else /* should succeed */
172 ok( hinf != INVALID_HANDLE_VALUE, "file %u: Open failed\n", i );
173 ok( err == 0, "file %u: Error code set to %u\n", i, err );
175 SetupCloseInfFile( hinf );
180 /* Test various section names */
182 static const struct
184 const char *data;
185 const char *section;
186 DWORD error;
187 } section_names[] =
189 /* file contents section name error code */
190 { STD_HEADER "[TestSection]", "TestSection", 0 },
191 { STD_HEADER "[TestSection]\n", "TestSection", 0 },
192 { STD_HEADER "[TESTSECTION]\r\n", "TestSection", 0 },
193 { STD_HEADER "[TestSection]\n[abc]", "testsection", 0 },
194 { STD_HEADER ";[TestSection]\n", "TestSection", ERROR_SECTION_NOT_FOUND },
195 { STD_HEADER "[TestSection]\n", "Bad name", ERROR_SECTION_NOT_FOUND },
196 /* spaces */
197 { STD_HEADER "[TestSection] \r\n", "TestSection", 0 },
198 { STD_HEADER " [TestSection]\r\n", "TestSection", 0 },
199 { STD_HEADER " [TestSection] dummy\r\n", "TestSection", 0 },
200 { STD_HEADER " [TestSection] [foo]\r\n", "TestSection", 0 },
201 { STD_HEADER " [ Test Section ] dummy\r\n", " Test Section ", 0 },
202 { STD_HEADER "[TestSection] \032\ndummy", "TestSection", 0 },
203 { STD_HEADER "[TestSection] \n\032dummy", "TestSection", 0 },
204 /* special chars in section name */
205 { STD_HEADER "[Test[Section]\r\n", "Test[Section", 0 },
206 { STD_HEADER "[Test[S]ection]\r\n", "Test[S", 0 },
207 { STD_HEADER "[Test[[[Section]\r\n", "Test[[[Section", 0 },
208 { STD_HEADER "[]\r\n", "", 0 },
209 { STD_HEADER "[[[]\n", "[[", 0 },
210 { STD_HEADER "[Test\"Section]\r\n", "Test\"Section", 0 },
211 { STD_HEADER "[Test\\Section]\r\n", "Test\\Section", 0 },
212 { STD_HEADER "[Test\\ Section]\r\n", "Test\\ Section", 0 },
213 { STD_HEADER "[Test;Section]\r\n", "Test;Section", 0 },
214 /* various control chars */
215 { STD_HEADER " [Test\r\b\tSection] \n", "Test\r\b\tSection", 0 },
216 /* nulls */
219 static void test_section_names(void)
221 unsigned int i;
222 UINT err_line;
223 HINF hinf;
224 DWORD err;
225 LONG ret;
227 for (i = 0; i < sizeof(section_names)/sizeof(section_names[0]); i++)
229 SetLastError( 0xdeadbeef );
230 hinf = test_file_contents( section_names[i].data, &err_line );
231 ok( hinf != INVALID_HANDLE_VALUE, "line %u: open failed err %u\n", i, GetLastError() );
232 if (hinf == INVALID_HANDLE_VALUE) continue;
234 ret = SetupGetLineCountA( hinf, section_names[i].section );
235 err = GetLastError();
236 trace( "hinf=%p ret=%d err=0x%x\n", hinf, ret, err );
237 if (ret != -1)
239 ok( !section_names[i].error, "line %u: section name %s found\n",
240 i, section_names[i].section );
241 ok( !err, "line %u: bad error code %u\n", i, err );
243 else
245 ok( section_names[i].error, "line %u: section name %s not found\n",
246 i, section_names[i].section );
247 ok( err == section_names[i].error, "line %u: bad error %u/%u\n",
248 i, err, section_names[i].error );
250 SetupCloseInfFile( hinf );
254 static void test_enum_sections(void)
256 static const char *contents = STD_HEADER "[s1]\nfoo=bar\n[s2]\nbar=foo\n[s3]\n[strings]\na=b\n";
258 BOOL ret;
259 DWORD len;
260 HINF hinf;
261 UINT err, index;
262 char buffer[256];
264 if (!pSetupEnumInfSectionsA)
266 win_skip( "SetupEnumInfSectionsA not available\n" );
267 return;
270 hinf = test_file_contents( contents, &err );
271 ok( hinf != NULL, "Expected valid INF file\n" );
273 for (index = 0; ; index++)
275 SetLastError( 0xdeadbeef );
276 ret = pSetupEnumInfSectionsA( hinf, index, NULL, 0, &len );
277 err = GetLastError();
278 if (!ret && GetLastError() == ERROR_NO_MORE_ITEMS) break;
279 ok( ret, "SetupEnumInfSectionsA failed\n" );
280 ok( len == 3 || len == 8, "wrong len %u\n", len );
282 SetLastError( 0xdeadbeef );
283 ret = pSetupEnumInfSectionsA( hinf, index, NULL, sizeof(buffer), &len );
284 err = GetLastError();
285 ok( !ret, "SetupEnumInfSectionsA succeeded\n" );
286 ok( err == ERROR_INVALID_USER_BUFFER, "wrong error %u\n", err );
287 ok( len == 3 || len == 8, "wrong len %u\n", len );
289 SetLastError( 0xdeadbeef );
290 ret = pSetupEnumInfSectionsA( hinf, index, buffer, sizeof(buffer), &len );
291 ok( ret, "SetupEnumInfSectionsA failed err %u\n", GetLastError() );
292 ok( len == 3 || len == 8, "wrong len %u\n", len );
293 ok( !lstrcmpiA( buffer, "version" ) || !lstrcmpiA( buffer, "s1" ) ||
294 !lstrcmpiA( buffer, "s2" ) || !lstrcmpiA( buffer, "s3" ) || !lstrcmpiA( buffer, "strings" ),
295 "bad section '%s'\n", buffer );
297 SetupCloseInfFile( hinf );
301 /* Test various key and value names */
303 static const struct
305 const char *data;
306 const char *key;
307 const char *fields[10];
308 } key_names[] =
310 /* file contents expected key expected fields */
311 { "ab=cd", "ab", { "cd" } },
312 { "ab=cd,ef,gh,ij", "ab", { "cd", "ef", "gh", "ij" } },
313 { "ab", "ab", { "ab" } },
314 { "ab,cd", NULL, { "ab", "cd" } },
315 { "ab,cd=ef", NULL, { "ab", "cd=ef" } },
316 { "=abcd,ef", "", { "abcd", "ef" } },
317 /* backslashes */
318 { "ba\\\ncd=ef", "bacd", { "ef" } },
319 { "ab \\ \ncd=ef", "abcd", { "ef" } },
320 { "ab\\\ncd,ef", NULL, { "abcd", "ef" } },
321 { "ab \\ ;cc\ncd=ef", "abcd", { "ef" } },
322 { "ab \\ \\ \ncd=ef", "abcd", { "ef" } },
323 { "ba \\ dc=xx", "ba \\ dc", { "xx" } },
324 { "ba \\\\ \nc=d", "bac", { "d" } },
325 { "a=b\\\\c", "a", { "b\\\\c" } },
326 { "ab=cd \\ ", "ab", { "cd" } },
327 { "ba=c \\ \n \\ \n a", "ba", { "ca" } },
328 { "ba=c \\ \n \\ a", "ba", { "c\\ a" } },
329 { " \\ a= \\ b", "\\ a", { "\\ b" } },
330 /* quotes */
331 { "Ab\"Cd\"=Ef", "AbCd", { "Ef" } },
332 { "Ab\"Cd=Ef\"", "AbCd=Ef", { "AbCd=Ef" } },
333 { "ab\"\"\"cd,ef=gh\"", "ab\"cd,ef=gh", { "ab\"cd,ef=gh" } },
334 { "ab\"\"cd=ef", "abcd", { "ef" } },
335 { "ab\"\"cd=ef,gh", "abcd", { "ef", "gh" } },
336 { "ab=cd\"\"ef", "ab", { "cdef" } },
337 { "ab=cd\",\"ef", "ab", { "cd,ef" } },
338 { "ab=cd\",ef", "ab", { "cd,ef" } },
339 { "ab=cd\",ef\\\nab", "ab", { "cd,ef\\" } },
341 /* single quotes (unhandled)*/
342 { "HKLM,A,B,'C',D", NULL, { "HKLM", "A","B","'C'","D" } },
343 /* spaces */
344 { " a b = c , d \n", "a b", { "c", "d" } },
345 { " a b = c ,\" d\" \n", "a b", { "c", " d" } },
346 { " a b\r = c\r\n", "a b", { "c" } },
347 /* empty fields */
348 { "a=b,,,c,,,d", "a", { "b", "", "", "c", "", "", "d" } },
349 { "a=b,\"\",c,\" \",d", "a", { "b", "", "c", " ", "d" } },
350 { "=,,b", "", { "", "", "b" } },
351 { ",=,,b", NULL, { "", "=", "", "b" } },
352 { "a=\n", "a", { "" } },
353 { "=", "", { "" } },
354 /* eof */
355 { "ab=c\032d", "ab", { "c" } },
356 { "ab\032=cd", "ab", { "ab" } },
357 /* nulls */
358 { "abcd=ef\x0gh", "abcd", { "ef" } },
359 /* multiple sections with same name */
360 { "[Test2]\nab\n[Test]\nee=ff\n", "ee", { "ff" } },
361 /* string substitution */
362 { "%foo%=%bar%\n" STR_SECTION, "aaa", { "bbb" } },
363 { "%foo%xx=%bar%yy\n" STR_SECTION, "aaaxx", { "bbbyy" } },
364 { "%% %foo%=%bar%\n" STR_SECTION, "% aaa", { "bbb" } },
365 { "%f\"o\"o%=ccc\n" STR_SECTION, "aaa", { "ccc" } },
366 { "abc=%bar;bla%\n" STR_SECTION, "abc", { "%bar" } },
367 { "loop=%loop%\n" STR_SECTION, "loop", { "%loop2%" } },
368 { "%per%%cent%=100\n" STR_SECTION, "12", { "100" } },
369 { "a=%big%\n" STR_SECTION, "a", { A400 } },
370 { "a=%verybig%\n" STR_SECTION, "a", { A511 } }, /* truncated to 511, not on Vista/W2K8 */
371 { "a=%big%%big%%big%%big%\n" STR_SECTION, "a", { A400 A400 A400 A400 } },
372 { "a=%big%%big%%big%%big%%big%%big%%big%%big%%big%\n" STR_SECTION, "a", { A400 A400 A400 A400 A400 A400 A400 A400 A400 } },
373 { "a=%big%%big%%big%%big%%big%%big%%big%%big%%big%%big%%big%\n" STR_SECTION, "a", { A4097 /*MAX_INF_STRING_LENGTH+1*/ } },
375 /* Prove expansion of system entries removes extra \'s and string
376 replacements doesn't */
377 { "ab=\"%24%\"\n" STR_SECTION, "ab", { "C:\\" } },
378 { "ab=\"%mydrive%\"\n" STR_SECTION, "ab", { "C:\\" } },
379 { "ab=\"%24%\\fred\"\n" STR_SECTION, "ab", { "C:\\fred" } },
380 { "ab=\"%mydrive%\\fred\"\n" STR_SECTION,"ab", { "C:\\\\fred" } },
381 /* Confirm duplicate \'s kept */
382 { "ab=\"%24%\\\\fred\"", "ab", { "C:\\\\fred" } },
383 { "ab=C:\\\\FRED", "ab", { "C:\\\\FRED" } },
386 /* check the key of a certain line */
387 static const char *check_key( INFCONTEXT *context, const char *wanted )
389 const char *key = get_string_field( context, 0 );
390 DWORD err = GetLastError();
392 if (!key)
394 ok( !wanted, "missing key %s\n", wanted );
395 ok( err == 0 || err == ERROR_INVALID_PARAMETER, "last error set to %u\n", err );
397 else
399 ok( !strcmp( key, wanted ), "bad key %s/%s\n", key, wanted );
400 ok( err == 0, "last error set to %u\n", err );
402 return key;
405 static void test_key_names(void)
407 char buffer[MAX_INF_STRING_LENGTH+32];
408 const char *line;
409 unsigned int i, index, count;
410 UINT err_line;
411 HINF hinf;
412 DWORD err;
413 BOOL ret;
414 INFCONTEXT context;
416 for (i = 0; i < sizeof(key_names)/sizeof(key_names[0]); i++)
418 strcpy( buffer, STD_HEADER "[Test]\n" );
419 strcat( buffer, key_names[i].data );
420 SetLastError( 0xdeadbeef );
421 hinf = test_file_contents( buffer, &err_line );
422 ok( hinf != INVALID_HANDLE_VALUE, "line %u: open failed err %u\n", i, GetLastError() );
423 if (hinf == INVALID_HANDLE_VALUE) continue;
425 ret = SetupFindFirstLineA( hinf, "Test", 0, &context );
426 ok(ret, "SetupFindFirstLineA failed: le=%u\n", GetLastError());
427 if (!ret)
429 SetupCloseInfFile( hinf );
430 continue;
433 check_key( &context, key_names[i].key );
435 buffer[0] = buffer[1] = 0; /* build the full line */
436 for (index = 0; ; index++)
438 const char *field = get_string_field( &context, index + 1 );
439 err = GetLastError();
440 if (field)
442 ok( err == 0, "line %u: bad error %u\n", i, err );
443 if (key_names[i].fields[index])
445 if (i == 49)
446 ok( !strcmp( field, key_names[i].fields[index] ) ||
447 !strcmp( field, A1200), /* Vista, W2K8 */
448 "line %u: bad field %s/%s\n",
449 i, field, key_names[i].fields[index] );
450 else /* don't compare drive letter of paths */
451 if (field[0] && field[1] == ':' && field[2] == '\\')
452 ok( !strcmp( field + 1, key_names[i].fields[index] + 1 ),
453 "line %u: bad field %s/%s\n",
454 i, field, key_names[i].fields[index] );
455 else
456 ok( !strcmp( field, key_names[i].fields[index] ), "line %u: bad field %s/%s\n",
457 i, field, key_names[i].fields[index] );
459 else
460 ok( 0, "line %u: got extra field %s\n", i, field );
461 strcat( buffer, "," );
462 strcat( buffer, field );
464 else
466 ok( err == 0 || err == ERROR_INVALID_PARAMETER,
467 "line %u: bad error %u\n", i, err );
468 if (key_names[i].fields[index])
469 ok( 0, "line %u: missing field %s\n", i, key_names[i].fields[index] );
471 if (!key_names[i].fields[index]) break;
473 count = SetupGetFieldCount( &context );
474 ok( count == index, "line %u: bad count %d/%d\n", i, index, count );
476 line = get_line_text( &context );
477 ok( line != NULL, "line %u: SetupGetLineText failed\n", i );
478 if (line) ok( !strcmp( line, buffer+1 ), "line %u: bad text %s/%s\n", i, line, buffer+1 );
480 SetupCloseInfFile( hinf );
485 static void test_close_inf_file(void)
487 SetLastError(0xdeadbeef);
488 SetupCloseInfFile(NULL);
489 ok(GetLastError() == 0xdeadbeef ||
490 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x, WinMe */
491 "Expected 0xdeadbeef, got %u\n", GetLastError());
493 SetLastError(0xdeadbeef);
494 SetupCloseInfFile(INVALID_HANDLE_VALUE);
495 ok(GetLastError() == 0xdeadbeef ||
496 GetLastError() == ERROR_INVALID_PARAMETER, /* Win9x, WinMe */
497 "Expected 0xdeadbeef, got %u\n", GetLastError());
500 static const char *contents = "[Version]\n"
501 "Signature=\"$Windows NT$\"\n"
502 "FileVersion=5.1.1.2\n"
503 "[FileBranchInfo]\n"
504 "RTMQFE=\"%RTMGFE_NAME%\",SP1RTM,"A4097"\n"
505 "[Strings]\n"
506 "RTMQFE_NAME = \"RTMQFE\"\n";
508 static const CHAR getfield_resA[][20] =
510 "RTMQFE",
511 "%RTMGFE_NAME%",
512 "SP1RTM",
515 static const WCHAR getfield_resW[][20] =
517 {'R','T','M','Q','F','E',0},
518 {'%','R','T','M','G','F','E','_','N','A','M','E','%',0},
519 {'S','P','1','R','T','M',0},
522 static void test_pSetupGetField(void)
524 UINT err;
525 BOOL ret;
526 HINF hinf;
527 LPCSTR fieldA;
528 LPCWSTR fieldW;
529 INFCONTEXT context;
530 int i;
531 int len;
532 BOOL unicode = TRUE;
534 SetLastError(0xdeadbeef);
535 lstrcmpW(NULL, NULL);
536 if (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
538 win_skip("Using A-functions instead of W\n");
539 unicode = FALSE;
542 hinf = test_file_contents( contents, &err );
543 ok( hinf != NULL, "Expected valid INF file\n" );
545 ret = SetupFindFirstLineA( hinf, "FileBranchInfo", NULL, &context );
546 ok( ret, "Failed to find first line\n" );
548 /* native Windows crashes if a NULL context is sent in */
550 for ( i = 0; i < 3; i++ )
552 if (unicode)
554 fieldW = pSetupGetFieldW( &context, i );
555 ok( fieldW != NULL, "Failed to get field %i\n", i );
556 ok( !lstrcmpW( getfield_resW[i], fieldW ), "Wrong string returned\n" );
558 else
560 fieldA = pSetupGetFieldA( &context, i );
561 ok( fieldA != NULL, "Failed to get field %i\n", i );
562 ok( !lstrcmpA( getfield_resA[i], fieldA ), "Wrong string returned\n" );
566 if (unicode)
568 fieldW = pSetupGetFieldW( &context, 3 );
569 ok( fieldW != NULL, "Failed to get field 3\n" );
570 len = lstrlenW( fieldW );
571 ok( len == 511 || /* NT4, W2K, XP and W2K3 */
572 len == 4096, /* Vista */
573 "Unexpected length, got %d\n", len );
575 fieldW = pSetupGetFieldW( &context, 4 );
576 ok( fieldW == NULL, "Expected NULL, got %p\n", fieldW );
577 ok( GetLastError() == ERROR_INVALID_PARAMETER,
578 "Expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError() );
580 else
582 fieldA = pSetupGetFieldA( &context, 3 );
583 ok( fieldA != NULL, "Failed to get field 3\n" );
584 len = lstrlenA( fieldA );
585 ok( len == 511, /* Win9x, WinME */
586 "Unexpected length, got %d\n", len );
588 fieldA = pSetupGetFieldA( &context, 4 );
589 ok( fieldA == NULL, "Expected NULL, got %p\n", fieldA );
590 ok( GetLastError() == ERROR_INVALID_PARAMETER,
591 "Expected ERROR_INVALID_PARAMETER, got %u\n", GetLastError() );
594 SetupCloseInfFile( hinf );
597 static void test_SetupGetIntField(void)
599 static const struct
601 const char *key;
602 const char *fields;
603 DWORD index;
604 INT value;
605 DWORD err;
606 } keys[] =
608 /* key fields index expected int errorcode */
609 { "Key", "48", 1, 48, ERROR_SUCCESS },
610 { "Key", "48", 0, -1, ERROR_INVALID_DATA },
611 { "123", "48", 0, 123, ERROR_SUCCESS },
612 { "Key", "0x4", 1, 4, ERROR_SUCCESS },
613 { "Key", "Field1", 1, -1, ERROR_INVALID_DATA },
614 { "Key", "Field1,34", 2, 34, ERROR_SUCCESS },
615 { "Key", "Field1,,Field3", 2, 0, ERROR_SUCCESS },
616 { "Key", "Field1,", 2, 0, ERROR_SUCCESS }
618 unsigned int i;
620 for (i = 0; i < sizeof(keys)/sizeof(keys[0]); i++)
622 HINF hinf;
623 char buffer[MAX_INF_STRING_LENGTH];
624 INFCONTEXT context;
625 UINT err;
626 BOOL retb;
627 INT intfield;
629 strcpy( buffer, STD_HEADER "[TestSection]\n" );
630 strcat( buffer, keys[i].key );
631 strcat( buffer, "=" );
632 strcat( buffer, keys[i].fields );
633 hinf = test_file_contents( buffer, &err);
634 ok( hinf != NULL, "Expected valid INF file\n" );
636 SetupFindFirstLineA( hinf, "TestSection", keys[i].key, &context );
637 SetLastError( 0xdeadbeef );
638 intfield = -1;
639 retb = SetupGetIntField( &context, keys[i].index, &intfield );
640 if ( keys[i].err == ERROR_SUCCESS )
642 ok( retb, "%u: Expected success\n", i );
643 ok( GetLastError() == ERROR_SUCCESS ||
644 GetLastError() == 0xdeadbeef /* win9x, NT4 */,
645 "%u: Expected ERROR_SUCCESS or 0xdeadbeef, got %u\n", i, GetLastError() );
647 else
649 ok( !retb, "%u: Expected failure\n", i );
650 ok( GetLastError() == keys[i].err,
651 "%u: Expected %d, got %u\n", i, keys[i].err, GetLastError() );
653 ok( intfield == keys[i].value, "%u: Expected %d, got %d\n", i, keys[i].value, intfield );
655 SetupCloseInfFile( hinf );
659 static void test_GLE(void)
661 static const char *inf =
662 "[Version]\n"
663 "Signature=\"$Windows NT$\"\n"
664 "[Sectionname]\n"
665 "Keyname1=Field1,Field2,Field3\n"
666 "\n"
667 "Keyname2=Field4,Field5\n";
668 HINF hinf;
669 UINT err;
670 INFCONTEXT context;
671 BOOL retb;
672 LONG retl;
673 char buf[MAX_INF_STRING_LENGTH];
674 int bufsize = MAX_INF_STRING_LENGTH;
675 DWORD retsize;
677 hinf = test_file_contents( inf, &err );
678 ok( hinf != NULL, "Expected valid INF file\n" );
680 SetLastError(0xdeadbeef);
681 retb = SetupFindFirstLineA( hinf, "ImNotThere", NULL, &context );
682 ok(!retb, "Expected failure\n");
683 ok(GetLastError() == ERROR_LINE_NOT_FOUND,
684 "Expected ERROR_LINE_NOT_FOUND, got %08x\n", GetLastError());
686 SetLastError(0xdeadbeef);
687 retb = SetupFindFirstLineA( hinf, "ImNotThere", "ImNotThere", &context );
688 ok(!retb, "Expected failure\n");
689 ok(GetLastError() == ERROR_LINE_NOT_FOUND,
690 "Expected ERROR_LINE_NOT_FOUND, got %08x\n", GetLastError());
692 SetLastError(0xdeadbeef);
693 retb = SetupFindFirstLineA( hinf, "Sectionname", NULL, &context );
694 ok(retb, "Expected success\n");
695 ok(GetLastError() == ERROR_SUCCESS,
696 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
698 SetLastError(0xdeadbeef);
699 retb = SetupFindFirstLineA( hinf, "Sectionname", "ImNotThere", &context );
700 ok(!retb, "Expected failure\n");
701 ok(GetLastError() == ERROR_LINE_NOT_FOUND,
702 "Expected ERROR_LINE_NOT_FOUND, got %08x\n", GetLastError());
704 SetLastError(0xdeadbeef);
705 retb = SetupFindFirstLineA( hinf, "Sectionname", "Keyname1", &context );
706 ok(retb, "Expected success\n");
707 ok(GetLastError() == ERROR_SUCCESS,
708 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
710 SetLastError(0xdeadbeef);
711 retb = SetupFindNextMatchLineA( &context, "ImNotThere", &context );
712 ok(!retb, "Expected failure\n");
713 ok(GetLastError() == ERROR_LINE_NOT_FOUND,
714 "Expected ERROR_LINE_NOT_FOUND, got %08x\n", GetLastError());
716 SetLastError(0xdeadbeef);
717 retb = SetupFindNextMatchLineA( &context, "Keyname2", &context );
718 ok(retb, "Expected success\n");
719 ok(GetLastError() == ERROR_SUCCESS,
720 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
722 SetLastError(0xdeadbeef);
723 retl = SetupGetLineCountA( hinf, "ImNotThere");
724 ok(retl == -1, "Expected -1, got %d\n", retl);
725 ok(GetLastError() == ERROR_SECTION_NOT_FOUND,
726 "Expected ERROR_SECTION_NOT_FOUND, got %08x\n", GetLastError());
728 SetLastError(0xdeadbeef);
729 retl = SetupGetLineCountA( hinf, "Sectionname");
730 ok(retl == 2, "Expected 2, got %d\n", retl);
731 ok(GetLastError() == ERROR_SUCCESS,
732 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
734 SetLastError(0xdeadbeef);
735 retb = SetupGetLineTextA( NULL, hinf, "ImNotThere", "ImNotThere", buf, bufsize, &retsize);
736 ok(!retb, "Expected failure\n");
737 ok(GetLastError() == ERROR_LINE_NOT_FOUND,
738 "Expected ERROR_LINE_NOT_FOUND, got %08x\n", GetLastError());
740 SetLastError(0xdeadbeef);
741 retb = SetupGetLineTextA( NULL, hinf, "Sectionname", "ImNotThere", buf, bufsize, &retsize);
742 ok(!retb, "Expected failure\n");
743 ok(GetLastError() == ERROR_LINE_NOT_FOUND,
744 "Expected ERROR_LINE_NOT_FOUND, got %08x\n", GetLastError());
746 SetLastError(0xdeadbeef);
747 retb = SetupGetLineTextA( NULL, hinf, "Sectionname", "Keyname1", buf, bufsize, &retsize);
748 ok(retb, "Expected success\n");
749 ok(GetLastError() == ERROR_SUCCESS,
750 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
752 SetLastError(0xdeadbeef);
753 retb = SetupGetLineByIndexA( hinf, "ImNotThere", 1, &context );
754 ok(!retb, "Expected failure\n");
755 ok(GetLastError() == ERROR_LINE_NOT_FOUND,
756 "Expected ERROR_LINE_NOT_FOUND, got %08x\n", GetLastError());
758 SetLastError(0xdeadbeef);
759 retb = SetupGetLineByIndexA( hinf, "Sectionname", 1, &context );
760 ok(retb, "Expected success\n");
761 ok(GetLastError() == ERROR_SUCCESS,
762 "Expected ERROR_SUCCESS, got %08x\n", GetLastError());
764 SetLastError(0xdeadbeef);
765 retb = SetupGetLineByIndexA( hinf, "Sectionname", 3, &context );
766 ok(!retb, "Expected failure\n");
767 ok(GetLastError() == ERROR_LINE_NOT_FOUND,
768 "Expected ERROR_LINE_NOT_FOUND, got %08x\n", GetLastError());
770 SetupCloseInfFile( hinf );
773 START_TEST(parser)
775 init_function_pointers();
776 test_invalid_files();
777 test_section_names();
778 test_enum_sections();
779 test_key_names();
780 test_close_inf_file();
781 test_pSetupGetField();
782 test_SetupGetIntField();
783 test_GLE();
784 DeleteFileA( tmpfilename );