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