1 /*
2  * Unit tests for profile functions
3  *
4  * Copyright (c) 2003 Stefan Leichter
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 "precomp.h"
22 
23 #define KEY      "ProfileInt"
24 #define SECTION  "Test"
25 #define TESTFILE ".\\testwine.ini"
26 #define TESTFILE2 ".\\testwine2.ini"
27 
28 struct _profileInt {
29     LPCSTR section;
30     LPCSTR key;
31     LPCSTR value;
32     LPCSTR iniFile;
33     INT defaultVal;
34     UINT result;
35     UINT result9x;
36 };
37 
38 static void test_profile_int(void)
39 {
40     struct _profileInt profileInt[]={
41          { NULL,    NULL, NULL,          NULL,     70, 0          , 0}, /*  0 */
42          { NULL,    NULL, NULL,          TESTFILE, -1, 4294967295U, 0},
43          { NULL,    NULL, NULL,          TESTFILE,  1, 1          , 0},
44          { SECTION, NULL, NULL,          TESTFILE, -1, 4294967295U, 0},
45          { SECTION, NULL, NULL,          TESTFILE,  1, 1          , 0},
46          { NULL,    KEY,  NULL,          TESTFILE, -1, 4294967295U, 0}, /*  5 */
47          { NULL,    KEY,  NULL,          TESTFILE,  1, 1          , 0},
48          { SECTION, KEY,  NULL,          TESTFILE, -1, 4294967295U, 4294967295U},
49          { SECTION, KEY,  NULL,          TESTFILE,  1, 1          , 1},
50          { SECTION, KEY,  "-1",          TESTFILE, -1, 4294967295U, 4294967295U},
51          { SECTION, KEY,  "-1",          TESTFILE,  1, 4294967295U, 4294967295U}, /* 10 */
52          { SECTION, KEY,  "1",           TESTFILE, -1, 1          , 1},
53          { SECTION, KEY,  "1",           TESTFILE,  1, 1          , 1},
54          { SECTION, KEY,  "+1",          TESTFILE, -1, 1          , 0},
55          { SECTION, KEY,  "+1",          TESTFILE,  1, 1          , 0},
56          { SECTION, KEY,  "4294967296",  TESTFILE, -1, 0          , 0}, /* 15 */
57          { SECTION, KEY,  "4294967296",  TESTFILE,  1, 0          , 0},
58          { SECTION, KEY,  "4294967297",  TESTFILE, -1, 1          , 1},
59          { SECTION, KEY,  "4294967297",  TESTFILE,  1, 1          , 1},
60          { SECTION, KEY,  "-4294967297", TESTFILE, -1, 4294967295U, 4294967295U},
61          { SECTION, KEY,  "-4294967297", TESTFILE,  1, 4294967295U, 4294967295U}, /* 20 */
62          { SECTION, KEY,  "42A94967297", TESTFILE, -1, 42         , 42},
63          { SECTION, KEY,  "42A94967297", TESTFILE,  1, 42         , 42},
64          { SECTION, KEY,  "B4294967297", TESTFILE, -1, 0          , 0},
65          { SECTION, KEY,  "B4294967297", TESTFILE,  1, 0          , 0},
66     };
67     int i, num_test = (sizeof(profileInt)/sizeof(struct _profileInt));
68     UINT res;
69 
70     DeleteFileA( TESTFILE);
71 
72     for (i=0; i < num_test; i++) {
73         if (profileInt[i].value)
74             WritePrivateProfileStringA(SECTION, KEY, profileInt[i].value,
75                                       profileInt[i].iniFile);
76 
77        res = GetPrivateProfileIntA(profileInt[i].section, profileInt[i].key,
78                  profileInt[i].defaultVal, profileInt[i].iniFile);
79        ok((res == profileInt[i].result) || (res == profileInt[i].result9x),
80 	    "test<%02d>: ret<%010u> exp<%010u><%010u>\n",
81             i, res, profileInt[i].result, profileInt[i].result9x);
82     }
83 
84     DeleteFileA( TESTFILE);
85 }
86 
87 static void test_profile_string(void)
88 {
89     static WCHAR emptyW[] = { 0 }; /* if "const", GetPrivateProfileStringW(emptyW, ...) crashes on win2k */
90     static const WCHAR keyW[] = { 'k','e','y',0 };
91     static const WCHAR sW[] = { 's',0 };
92     static const WCHAR TESTFILE2W[] = {'.','\\','t','e','s','t','w','i','n','e','2','.','i','n','i',0};
93     static const WCHAR valsectionW[] = {'v','a','l','_','e','_','s','e','c','t','i','o','n',0 };
94     static const WCHAR valnokeyW[] = {'v','a','l','_','n','o','_','k','e','y',0};
95     HANDLE h;
96     int ret;
97     DWORD count;
98     char buf[100];
99     WCHAR bufW[100];
100     char *p;
101     /* test that lines without an '=' will not be enumerated */
102     /* in the case below, name2 is a key while name3 is not. */
103     char content[]="[s]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n";
104     char content2[]="\r\nkey=val_no_section\r\n[]\r\nkey=val_e_section\r\n"
105                     "[s]\r\n=val_no_key\r\n[t]\r\n";
106     DeleteFileA( TESTFILE2);
107     h = CreateFileA( TESTFILE2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
108         FILE_ATTRIBUTE_NORMAL, NULL);
109     ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", TESTFILE2);
110     if( h == INVALID_HANDLE_VALUE) return;
111     WriteFile( h, content, sizeof(content), &count, NULL);
112     CloseHandle( h);
113 
114     /* enumerate the keys */
115     ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
116         TESTFILE2);
117     for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
118         p[-1] = ',';
119     /* and test */
120     ok( ret == 18 && !strcmp( buf, "name1,name2,name4"), "wrong keys returned(%d): %s\n", ret,
121             buf);
122 
123     /* add a new key to test that the file is quite usable */
124     WritePrivateProfileStringA( "s", "name5", "val5", TESTFILE2);
125     ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
126         TESTFILE2);
127     for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
128         p[-1] = ',';
129     ok( ret == 24 && !strcmp( buf, "name1,name2,name4,name5"), "wrong keys returned(%d): %s\n",
130             ret, buf);
131 
132     h = CreateFileA( TESTFILE2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
133         FILE_ATTRIBUTE_NORMAL, NULL);
134     ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", TESTFILE2);
135     if( h == INVALID_HANDLE_VALUE) return;
136     WriteFile( h, content2, sizeof(content2), &count, NULL);
137     CloseHandle( h);
138 
139     /* works only in unicode, ascii crashes */
140     ret=GetPrivateProfileStringW(emptyW, keyW, emptyW, bufW,
141                                  sizeof(bufW)/sizeof(bufW[0]), TESTFILE2W);
142     todo_wine
143     ok(ret == 13, "expected 13, got %u\n", ret);
144     todo_wine
145     ok(!lstrcmpW(valsectionW,bufW), "expected %s, got %s\n",
146         wine_dbgstr_w(valsectionW), wine_dbgstr_w(bufW) );
147 
148     /* works only in unicode, ascii crashes */
149     ret=GetPrivateProfileStringW(sW, emptyW, emptyW, bufW,
150                                  sizeof(bufW)/sizeof(bufW[0]), TESTFILE2W);
151     ok(ret == 10, "expected 10, got %u\n", ret);
152     ok(!lstrcmpW(valnokeyW,bufW), "expected %s, got %s\n",
153         wine_dbgstr_w(valnokeyW), wine_dbgstr_w(bufW) );
154 
155     DeleteFileA( TESTFILE2);
156 }
157 
158 static void test_profile_sections(void)
159 {
160     HANDLE h;
161     int ret;
162     DWORD count;
163     char buf[100];
164     char *p;
165     static const char content[]="[section1]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n[section2]\r\n";
166     static const char testfile4[]=".\\testwine4.ini";
167     BOOL on_win98 = FALSE;
168 
169     DeleteFileA( testfile4 );
170     h = CreateFileA( testfile4, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
171     ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile4);
172     if( h == INVALID_HANDLE_VALUE) return;
173     WriteFile( h, content, sizeof(content), &count, NULL);
174     CloseHandle( h);
175 
176     /* Some parameter checking */
177     SetLastError(0xdeadbeef);
178     ret = GetPrivateProfileSectionA( NULL, NULL, 0, NULL );
179     ok( ret == 0, "expected return size 0, got %d\n", ret );
180     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
181         GetLastError() == 0xdeadbeef /* Win98 */,
182         "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
183     if (GetLastError() == 0xdeadbeef) on_win98 = TRUE;
184 
185     SetLastError(0xdeadbeef);
186     ret = GetPrivateProfileSectionA( NULL, NULL, 0, testfile4 );
187     ok( ret == 0, "expected return size 0, got %d\n", ret );
188     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
189         GetLastError() == 0xdeadbeef /* Win98 */,
190         "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
191 
192     if (!on_win98)
193     {
194         SetLastError(0xdeadbeef);
195         ret = GetPrivateProfileSectionA( "section1", NULL, 0, testfile4 );
196         ok( ret == 0, "expected return size 0, got %d\n", ret );
197         ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
198     }
199 
200     SetLastError(0xdeadbeef);
201     ret = GetPrivateProfileSectionA( NULL, buf, sizeof(buf), testfile4 );
202     ok( ret == 0, "expected return size 0, got %d\n", ret );
203     ok( GetLastError() == ERROR_INVALID_PARAMETER ||
204         broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
205         "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
206 
207     SetLastError(0xdeadbeef);
208     ret = GetPrivateProfileSectionA( "section1", buf, sizeof(buf), NULL );
209     ok( ret == 0, "expected return size 0, got %d\n", ret );
210     todo_wine
211     ok( GetLastError() == ERROR_FILE_NOT_FOUND ||
212         broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
213         "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
214 
215     /* Existing empty section with no keys */
216     SetLastError(0xdeadbeef);
217     ret=GetPrivateProfileSectionA("section2", buf, sizeof(buf), testfile4);
218     ok( ret == 0, "expected return size 0, got %d\n", ret );
219     ok( GetLastError() == ERROR_SUCCESS ||
220         broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
221         "expected ERROR_SUCCESS, got %d\n", GetLastError());
222 
223     /* Existing section with keys and values*/
224     SetLastError(0xdeadbeef);
225     ret=GetPrivateProfileSectionA("section1", buf, sizeof(buf), testfile4);
226     for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
227         p[-1] = ',';
228     ok( ret == 35 && !strcmp( buf, "name1=val1,name2=,name3,name4=val4"), "wrong section returned(%d): %s\n",
229             ret, buf);
230     ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
231     ok( GetLastError() == ERROR_SUCCESS ||
232         broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
233         "expected ERROR_SUCCESS, got %d\n", GetLastError());
234 
235     /* Overflow*/
236     ret=GetPrivateProfileSectionA("section1", buf, 24, testfile4);
237     for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
238         p[-1] = ',';
239     ok( ret == 22 && !strcmp( buf, "name1=val1,name2=,name"), "wrong section returned(%d): %s\n",
240         ret, buf);
241     ok( buf[ret] == 0 && buf[ret+1] == 0, "returned buffer not terminated with double-null\n" );
242 
243     DeleteFileA( testfile4 );
244 }
245 
246 static void test_profile_sections_names(void)
247 {
248     HANDLE h;
249     int ret;
250     DWORD count;
251     char buf[100];
252     WCHAR bufW[100];
253     static const char content[]="[section1]\r\n[section2]\r\n[section3]\r\n";
254     static const char testfile3[]=".\\testwine3.ini";
255     static const WCHAR testfile3W[]={ '.','\\','t','e','s','t','w','i','n','e','3','.','i','n','i',0 };
256     static const WCHAR not_here[] = {'.','\\','n','o','t','_','h','e','r','e','.','i','n','i',0};
257     DeleteFileA( testfile3 );
258     h = CreateFileA( testfile3, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
259         FILE_ATTRIBUTE_NORMAL, NULL);
260     ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile3);
261     if( h == INVALID_HANDLE_VALUE) return;
262     WriteFile( h, content, sizeof(content), &count, NULL);
263     CloseHandle( h);
264 
265     /* Test with sufficiently large buffer */
266     memset(buf, 0xc, sizeof(buf));
267     ret = GetPrivateProfileSectionNamesA( buf, 29, testfile3 );
268     ok( ret == 27 ||
269         broken(ret == 28), /* Win9x, WinME */
270         "expected return size 27, got %d\n", ret );
271     ok( (buf[ret-1] == 0 && buf[ret] == 0) ||
272         broken(buf[ret-1] == 0 && buf[ret-2] == 0), /* Win9x, WinME */
273         "returned buffer not terminated with double-null\n" );
274 
275     /* Test with exactly fitting buffer */
276     memset(buf, 0xc, sizeof(buf));
277     ret = GetPrivateProfileSectionNamesA( buf, 28, testfile3 );
278     ok( ret == 26 ||
279         broken(ret == 28), /* Win9x, WinME */
280         "expected return size 26, got %d\n", ret );
281     todo_wine
282     ok( (buf[ret+1] == 0 && buf[ret] == 0) || /* W2K3 and higher */
283         broken(buf[ret+1] == 0xc && buf[ret] == 0) || /* NT4, W2K, WinXP */
284         broken(buf[ret-1] == 0 && buf[ret-2] == 0), /* Win9x, WinME */
285         "returned buffer not terminated with double-null\n" );
286 
287     /* Test with a buffer too small */
288     memset(buf, 0xc, sizeof(buf));
289     ret = GetPrivateProfileSectionNamesA( buf, 27, testfile3 );
290     ok( ret == 25, "expected return size 25, got %d\n", ret );
291     /* Win9x and WinME only fills the buffer with complete section names (double-null terminated) */
292     count = strlen("section1") + sizeof(CHAR) + strlen("section2");
293     todo_wine
294     ok( (buf[ret+1] == 0 && buf[ret] == 0) ||
295         broken(buf[count] == 0 && buf[count+1] == 0), /* Win9x, WinME */
296         "returned buffer not terminated with double-null\n" );
297 
298     /* Tests on nonexistent file */
299     memset(buf, 0xc, sizeof(buf));
300     ret = GetPrivateProfileSectionNamesA( buf, 10, ".\\not_here.ini" );
301     ok( ret == 0 ||
302         broken(ret == 1), /* Win9x, WinME */
303         "expected return size 0, got %d\n", ret );
304     ok( buf[0] == 0, "returned buffer not terminated with null\n" );
305     ok( buf[1] != 0, "returned buffer terminated with double-null\n" );
306 
307     /* Test with sufficiently large buffer */
308     SetLastError(0xdeadbeef);
309     memset(bufW, 0xcc, sizeof(bufW));
310     ret = GetPrivateProfileSectionNamesW( bufW, 29, testfile3W );
311     if (ret == 0 && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
312     {
313         win_skip("GetPrivateProfileSectionNamesW is not implemented\n");
314         DeleteFileA( testfile3 );
315         return;
316     }
317     ok( ret == 27, "expected return size 27, got %d\n", ret );
318     ok( bufW[ret-1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
319 
320     /* Test with exactly fitting buffer */
321     memset(bufW, 0xcc, sizeof(bufW));
322     ret = GetPrivateProfileSectionNamesW( bufW, 28, testfile3W );
323     ok( ret == 26, "expected return size 26, got %d\n", ret );
324     ok( (bufW[ret+1] == 0 && bufW[ret] == 0) || /* W2K3 and higher */
325         broken(bufW[ret+1] == 0xcccc && bufW[ret] == 0), /* NT4, W2K, WinXP */
326         "returned buffer not terminated with double-null\n" );
327 
328     /* Test with a buffer too small */
329     memset(bufW, 0xcc, sizeof(bufW));
330     ret = GetPrivateProfileSectionNamesW( bufW, 27, testfile3W );
331     ok( ret == 25, "expected return size 25, got %d\n", ret );
332     ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
333 
334     DeleteFileA( testfile3 );
335 
336     /* Tests on nonexistent file */
337     memset(bufW, 0xcc, sizeof(bufW));
338     ret = GetPrivateProfileSectionNamesW( bufW, 10, not_here );
339     ok( ret == 0, "expected return size 0, got %d\n", ret );
340     ok( bufW[0] == 0, "returned buffer not terminated with null\n" );
341     ok( bufW[1] != 0, "returned buffer terminated with double-null\n" );
342 }
343 
344 /* If the ini-file has already been opened with CreateFile, WritePrivateProfileString failed in wine with an error ERROR_SHARING_VIOLATION,  some testing here */
345 static void test_profile_existing(void)
346 {
347     static const char *testfile1 = ".\\winesharing1.ini";
348     static const char *testfile2 = ".\\winesharing2.ini";
349 
350     static const struct {
351         DWORD dwDesiredAccess;
352         DWORD dwShareMode;
353         DWORD write_error;
354         BOOL read_error;
355         DWORD broken_error;
356     } pe[] = {
357         {GENERIC_READ,  FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
358         {GENERIC_READ,  FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
359         {GENERIC_WRITE, FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
360         {GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
361         {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
362         {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
363         {GENERIC_READ,  FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
364         {GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
365         /*Thief demo (bug 5024) opens .ini file like this*/
366         {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */}
367     };
368 
369     int i;
370     BOOL ret;
371     DWORD size;
372     HANDLE h = 0;
373     char buffer[MAX_PATH];
374 
375     for (i=0; i < sizeof(pe)/sizeof(pe[0]); i++)
376     {
377         h = CreateFileA(testfile1, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
378                        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
379         ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
380         SetLastError(0xdeadbeef);
381 
382         ret = WritePrivateProfileStringA(SECTION, KEY, "12345", testfile1);
383         if (!pe[i].write_error)
384         {
385             if (!ret)
386                 ok( broken(GetLastError() == pe[i].broken_error),
387                     "%d: WritePrivateProfileString failed with error %u\n", i, GetLastError() );
388             CloseHandle(h);
389             size = GetPrivateProfileStringA(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
390             if (ret)
391                 ok( size == 5, "%d: test failed, number of characters copied: %d instead of 5\n", i, size );
392             else
393                 ok( !size, "%d: test failed, number of characters copied: %d instead of 0\n", i, size );
394         }
395         else
396         {
397             DWORD err = GetLastError();
398             ok( !ret, "%d: WritePrivateProfileString succeeded\n", i );
399             if (!ret)
400                 ok( err == pe[i].write_error, "%d: WritePrivateProfileString failed with error %u/%u\n",
401                     i, err, pe[i].write_error );
402             CloseHandle(h);
403             size = GetPrivateProfileStringA(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
404             ok( !size, "%d: test failed, number of characters copied: %d instead of 0\n", i, size );
405         }
406 
407         ok( DeleteFileA(testfile1), "delete failed\n" );
408     }
409 
410     h = CreateFileA(testfile2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
411     sprintf( buffer, "[%s]\r\n%s=123\r\n", SECTION, KEY );
412     ok( WriteFile( h, buffer, strlen(buffer), &size, NULL ), "failed to write\n" );
413     CloseHandle( h );
414 
415     for (i=0; i < sizeof(pe)/sizeof(pe[0]); i++)
416     {
417         h = CreateFileA(testfile2, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
418                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
419         ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
420         SetLastError(0xdeadbeef);
421         ret = GetPrivateProfileStringA(SECTION, KEY, NULL, buffer, MAX_PATH, testfile2);
422         /* Win9x and WinME returns 0 for all cases except the first one */
423         if (!pe[i].read_error)
424             ok( ret ||
425                 broken(!ret && GetLastError() == 0xdeadbeef), /* Win9x, WinME */
426                 "%d: GetPrivateProfileString failed with error %u\n", i, GetLastError() );
427         else
428             ok( !ret, "%d: GetPrivateProfileString succeeded\n", i );
429         CloseHandle(h);
430     }
431     ok( DeleteFileA(testfile2), "delete failed\n" );
432 }
433 
434 static void test_profile_delete_on_close(void)
435 {
436     HANDLE h;
437     DWORD size, res;
438     static const CHAR testfile[] = ".\\testwine5.ini";
439     static const char contents[] = "[" SECTION "]\n" KEY "=123\n";
440 
441     h = CreateFileA(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
442                     CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
443     res = WriteFile( h, contents, sizeof contents - 1, &size, NULL );
444     ok( res, "Cannot write test file: %x\n", GetLastError() );
445     ok( size == sizeof contents - 1, "Test file: partial write\n");
446 
447     SetLastError(0xdeadbeef);
448     res = GetPrivateProfileIntA(SECTION, KEY, 0, testfile);
449     ok( res == 123 ||
450         broken(res == 0 && GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x, WinME */
451         "Got %d instead of 123\n", res);
452 
453     /* This also deletes the file */
454     CloseHandle(h);
455 }
456 
457 static void test_profile_refresh(void)
458 {
459     static const CHAR testfile[] = ".\\winetest4.ini";
460     HANDLE h;
461     DWORD size, res;
462     static const char contents1[] = "[" SECTION "]\n" KEY "=123\n";
463     static const char contents2[] = "[" SECTION "]\n" KEY "=124\n";
464 
465     h = CreateFileA(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
466                     CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
467     res = WriteFile( h, contents1, sizeof contents1 - 1, &size, NULL );
468     ok( res, "Cannot write test file: %x\n", GetLastError() );
469     ok( size == sizeof contents1 - 1, "Test file: partial write\n");
470 
471     SetLastError(0xdeadbeef);
472     res = GetPrivateProfileIntA(SECTION, KEY, 0, testfile);
473     ok( res == 123 ||
474         broken(res == 0 && GetLastError() == ERROR_SHARING_VIOLATION), /* Win9x, WinME */
475         "Got %d instead of 123\n", res);
476 
477     CloseHandle(h);
478 
479     /* Test proper invalidation of wine's profile file cache */
480 
481     h = CreateFileA(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
482                     CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
483     res = WriteFile( h, contents2, sizeof contents2 - 1, &size, NULL );
484     ok( res, "Cannot write test file: %x\n", GetLastError() );
485     ok( size == sizeof contents2 - 1, "Test file: partial write\n");
486 
487     SetLastError(0xdeadbeef);
488     res = GetPrivateProfileIntA(SECTION, KEY, 0, testfile);
489     ok( res == 124 ||
490         broken(res == 0 && GetLastError() == 0xdeadbeef), /* Win9x, WinME */
491         "Got %d instead of 124\n", res);
492 
493     /* This also deletes the file */
494     CloseHandle(h);
495 
496     /* Cache must be invalidated if file no longer exists and default must be returned */
497     SetLastError(0xdeadbeef);
498     res = GetPrivateProfileIntA(SECTION, KEY, 421, testfile);
499     ok( res == 421 ||
500         broken(res == 0 && GetLastError() == 0xdeadbeef), /* Win9x, WinME */
501         "Got %d instead of 421\n", res);
502 }
503 
504 static void create_test_file(LPCSTR name, LPCSTR data, DWORD size)
505 {
506     HANDLE hfile;
507     DWORD count;
508 
509     hfile = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
510     ok(hfile != INVALID_HANDLE_VALUE, "cannot create %s\n", name);
511     WriteFile(hfile, data, size, &count, NULL);
512     CloseHandle(hfile);
513 }
514 
515 static BOOL emptystr_ok(CHAR emptystr[MAX_PATH])
516 {
517     int i;
518 
519     for(i = 0;i < MAX_PATH;++i)
520         if(emptystr[i] != 0)
521         {
522             trace("emptystr[%d] = %d\n",i,emptystr[i]);
523             return FALSE;
524         }
525 
526     return TRUE;
527 }
528 
529 static void test_GetPrivateProfileString(const char *content, const char *descript)
530 {
531     DWORD ret, len;
532     CHAR buf[MAX_PATH];
533     CHAR def_val[MAX_PATH];
534     CHAR path[MAX_PATH];
535     CHAR windir[MAX_PATH];
536     /* NT series crashes on r/o empty strings, so pass an r/w
537        empty string and check for modification */
538     CHAR emptystr[MAX_PATH] = "";
539     LPSTR tempfile;
540 
541     static const char filename[] = ".\\winetest.ini";
542 
543     trace("test_GetPrivateProfileStringA: %s\n", descript);
544 
545     if(!lstrcmpA(descript, "CR only"))
546     {
547         SetLastError(0xdeadbeef);
548         ret = GetPrivateProfileStringW(NULL, NULL, NULL,
549                                        NULL, 0, NULL);
550         if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
551         {
552             win_skip("Win9x and WinME don't handle 'CR only' correctly\n");
553             return;
554         }
555     }
556 
557     create_test_file(filename, content, lstrlenA(content));
558 
559     /* Run this test series with caching. Wine won't cache profile
560        files younger than 2.1 seconds. */
561     Sleep(2500);
562 
563     /* lpAppName is NULL */
564     memset(buf, 0xc, sizeof(buf));
565     lstrcpyA(buf, "kumquat");
566     ret = GetPrivateProfileStringA(NULL, "name1", "default",
567                                    buf, MAX_PATH, filename);
568     ok(ret == 18 ||
569        broken(ret == 19), /* Win9x and WinME */
570        "Expected 18, got %d\n", ret);
571     len = lstrlenA("section1") + sizeof(CHAR) + lstrlenA("section2") + 2 * sizeof(CHAR);
572     ok(!memcmp(buf, "section1\0section2\0\0", len),
573        "Expected \"section1\\0section2\\0\\0\", got \"%s\"\n", buf);
574 
575     /* lpAppName is empty */
576     memset(buf, 0xc, sizeof(buf));
577     lstrcpyA(buf, "kumquat");
578     ret = GetPrivateProfileStringA(emptystr, "name1", "default",
579                                    buf, MAX_PATH, filename);
580     ok(ret == 7, "Expected 7, got %d\n", ret);
581     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
582     ok(emptystr_ok(emptystr), "AppName modified\n");
583 
584     /* lpAppName is missing */
585     memset(buf, 0xc,sizeof(buf));
586     lstrcpyA(buf, "kumquat");
587     ret = GetPrivateProfileStringA("notasection", "name1", "default",
588                                    buf, MAX_PATH, filename);
589     ok(ret == 7, "Expected 7, got %d\n", ret);
590     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
591 
592     /* lpAppName is empty, lpDefault is NULL */
593     memset(buf, 0xc,sizeof(buf));
594     lstrcpyA(buf, "kumquat");
595     ret = GetPrivateProfileStringA(emptystr, "name1", NULL,
596                                    buf, MAX_PATH, filename);
597     ok(ret == 0, "Expected 0, got %d\n", ret);
598     ok(!lstrcmpA(buf, "") ||
599        broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
600        "Expected \"\", got \"%s\"\n", buf);
601     ok(emptystr_ok(emptystr), "AppName modified\n");
602 
603     /* lpAppName is empty, lpDefault is empty */
604     memset(buf, 0xc,sizeof(buf));
605     lstrcpyA(buf, "kumquat");
606     ret = GetPrivateProfileStringA(emptystr, "name1", "",
607                                    buf, MAX_PATH, filename);
608     ok(ret == 0, "Expected 0, got %d\n", ret);
609     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
610     ok(emptystr_ok(emptystr), "AppName modified\n");
611 
612     /* lpAppName is empty, lpDefault has trailing blank characters */
613     memset(buf, 0xc,sizeof(buf));
614     lstrcpyA(buf, "kumquat");
615     /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
616     lstrcpyA(def_val, "default  ");
617     ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
618                                    buf, MAX_PATH, filename);
619     ok(ret == 7, "Expected 7, got %d\n", ret);
620     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
621     ok(emptystr_ok(emptystr), "AppName modified\n");
622 
623     /* lpAppName is empty, many blank characters in lpDefault */
624     memset(buf, 0xc,sizeof(buf));
625     lstrcpyA(buf, "kumquat");
626     /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
627     lstrcpyA(def_val, "one two  ");
628     ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
629                                    buf, MAX_PATH, filename);
630     ok(ret == 7, "Expected 7, got %d\n", ret);
631     ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
632     ok(emptystr_ok(emptystr), "AppName modified\n");
633 
634     /* lpAppName is empty, blank character but not trailing in lpDefault */
635     memset(buf, 0xc,sizeof(buf));
636     lstrcpyA(buf, "kumquat");
637     ret = GetPrivateProfileStringA(emptystr, "name1", "one two",
638                                    buf, MAX_PATH, filename);
639     ok(ret == 7, "Expected 7, got %d\n", ret);
640     ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
641     ok(emptystr_ok(emptystr), "AppName modified\n");
642 
643     /* lpKeyName is NULL */
644     memset(buf, 0xc,sizeof(buf));
645     lstrcpyA(buf, "kumquat");
646     ret = GetPrivateProfileStringA("section1", NULL, "default",
647                                    buf, MAX_PATH, filename);
648     ok(ret == 18, "Expected 18, got %d\n", ret);
649     ok(!memcmp(buf, "name1\0name2\0name4\0", ret + 1),
650        "Expected \"name1\\0name2\\0name4\\0\", got \"%s\"\n", buf);
651 
652     /* lpKeyName is empty */
653     memset(buf, 0xc,sizeof(buf));
654     lstrcpyA(buf, "kumquat");
655     ret = GetPrivateProfileStringA("section1", emptystr, "default",
656                                    buf, MAX_PATH, filename);
657     ok(ret == 7, "Expected 7, got %d\n", ret);
658     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
659     ok(emptystr_ok(emptystr), "KeyName modified\n");
660 
661     /* lpKeyName is missing */
662     memset(buf, 0xc,sizeof(buf));
663     lstrcpyA(buf, "kumquat");
664     ret = GetPrivateProfileStringA("section1", "notakey", "default",
665                                    buf, MAX_PATH, filename);
666     ok(ret == 7, "Expected 7, got %d\n", ret);
667     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
668 
669     /* lpKeyName is empty, lpDefault is NULL */
670     memset(buf, 0xc,sizeof(buf));
671     lstrcpyA(buf, "kumquat");
672     ret = GetPrivateProfileStringA("section1", emptystr, NULL,
673                                    buf, MAX_PATH, filename);
674     ok(ret == 0, "Expected 0, got %d\n", ret);
675     ok(!lstrcmpA(buf, "") ||
676        broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
677        "Expected \"\", got \"%s\"\n", buf);
678     ok(emptystr_ok(emptystr), "KeyName modified\n");
679 
680     /* lpKeyName is empty, lpDefault is empty */
681     memset(buf, 0xc,sizeof(buf));
682     lstrcpyA(buf, "kumquat");
683     ret = GetPrivateProfileStringA("section1", emptystr, "",
684                                    buf, MAX_PATH, filename);
685     ok(ret == 0, "Expected 0, got %d\n", ret);
686     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
687     ok(emptystr_ok(emptystr), "KeyName modified\n");
688 
689     /* lpKeyName is empty, lpDefault has trailing blank characters */
690     memset(buf, 0xc,sizeof(buf));
691     lstrcpyA(buf, "kumquat");
692     /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
693     lstrcpyA(def_val, "default  ");
694     ret = GetPrivateProfileStringA("section1", emptystr, def_val,
695                                    buf, MAX_PATH, filename);
696     ok(ret == 7, "Expected 7, got %d\n", ret);
697     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
698     ok(emptystr_ok(emptystr), "KeyName modified\n");
699 
700     if (0) /* crashes */
701     {
702         /* lpReturnedString is NULL */
703         ret = GetPrivateProfileStringA("section1", "name1", "default",
704                                        NULL, MAX_PATH, filename);
705     }
706 
707     /* lpFileName is NULL */
708     memset(buf, 0xc,sizeof(buf));
709     lstrcpyA(buf, "kumquat");
710     ret = GetPrivateProfileStringA("section1", "name1", "default",
711                                    buf, MAX_PATH, NULL);
712     ok(ret == 7 ||
713        broken(ret == 0), /* Win9x, WinME */
714        "Expected 7, got %d\n", ret);
715     ok(!lstrcmpA(buf, "default") ||
716        broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
717        "Expected \"default\", got \"%s\"\n", buf);
718 
719     /* lpFileName is empty */
720     memset(buf, 0xc,sizeof(buf));
721     lstrcpyA(buf, "kumquat");
722     ret = GetPrivateProfileStringA("section1", "name1", "default",
723                                    buf, MAX_PATH, "");
724     ok(ret == 7, "Expected 7, got %d\n", ret);
725     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
726 
727     /* lpFileName is nonexistent */
728     memset(buf, 0xc,sizeof(buf));
729     lstrcpyA(buf, "kumquat");
730     ret = GetPrivateProfileStringA("section1", "name1", "default",
731                                    buf, MAX_PATH, "nonexistent");
732     ok(ret == 7, "Expected 7, got %d\n", ret);
733     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
734 
735     /* nSize is 0 */
736     memset(buf, 0xc,sizeof(buf));
737     lstrcpyA(buf, "kumquat");
738     ret = GetPrivateProfileStringA("section1", "name1", "default",
739                                    buf, 0, filename);
740     ok(ret == 0, "Expected 0, got %d\n", ret);
741     ok(!lstrcmpA(buf, "kumquat"), "Expected buf to be unchanged, got \"%s\"\n", buf);
742 
743     /* nSize is exact size of output */
744     memset(buf, 0xc,sizeof(buf));
745     lstrcpyA(buf, "kumquat");
746     ret = GetPrivateProfileStringA("section1", "name1", "default",
747                                    buf, 4, filename);
748     ok(ret == 3, "Expected 3, got %d\n", ret);
749     ok(!lstrcmpA(buf, "val"), "Expected \"val\", got \"%s\"\n", buf);
750 
751     /* nSize has room for NULL terminator */
752     memset(buf, 0xc,sizeof(buf));
753     lstrcpyA(buf, "kumquat");
754     ret = GetPrivateProfileStringA("section1", "name1", "default",
755                                    buf, 5, filename);
756     ok(ret == 4, "Expected 4, got %d\n", ret);
757     ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
758 
759     /* output is 1 character */
760     memset(buf, 0xc,sizeof(buf));
761     lstrcpyA(buf, "kumquat");
762     ret = GetPrivateProfileStringA("section1", "name4", "default",
763                                    buf, MAX_PATH, filename);
764     ok(ret == 1, "Expected 1, got %d\n", ret);
765     ok(!lstrcmpA(buf, "a"), "Expected \"a\", got \"%s\"\n", buf);
766 
767     /* output is 1 character, no room for NULL terminator */
768     memset(buf, 0xc,sizeof(buf));
769     lstrcpyA(buf, "kumquat");
770     ret = GetPrivateProfileStringA("section1", "name4", "default",
771                                    buf, 1, filename);
772     ok(ret == 0, "Expected 0, got %d\n", ret);
773     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
774 
775     /* lpAppName is NULL, not enough room for final section name */
776     memset(buf, 0xc,sizeof(buf));
777     lstrcpyA(buf, "kumquat");
778     ret = GetPrivateProfileStringA(NULL, "name1", "default",
779                                    buf, 16, filename);
780     ok(ret == 14, "Expected 14, got %d\n", ret);
781     len = lstrlenA("section1") + 2 * sizeof(CHAR);
782     todo_wine
783     ok(!memcmp(buf, "section1\0secti\0\0", ret + 2) ||
784        broken(!memcmp(buf, "section1\0\0", len)), /* Win9x, WinME */
785        "Expected \"section1\\0secti\\0\\0\", got \"%s\"\n", buf);
786 
787     /* lpKeyName is NULL, not enough room for final key name */
788     memset(buf, 0xc,sizeof(buf));
789     lstrcpyA(buf, "kumquat");
790     ret = GetPrivateProfileStringA("section1", NULL, "default",
791                                    buf, 16, filename);
792     ok(ret == 14, "Expected 14, got %d\n", ret);
793     todo_wine
794     ok(!memcmp(buf, "name1\0name2\0na\0\0", ret + 2) ||
795        broken(!memcmp(buf, "name1\0name2\0n\0\0", ret + 1)), /* Win9x, WinME */
796        "Expected \"name1\\0name2\\0na\\0\\0\", got \"%s\"\n", buf);
797 
798     /* key value has quotation marks which are stripped */
799     memset(buf, 0xc,sizeof(buf));
800     lstrcpyA(buf, "kumquat");
801     ret = GetPrivateProfileStringA("section1", "name2", "default",
802                                    buf, MAX_PATH, filename);
803     ok(ret == 4, "Expected 4, got %d\n", ret);
804     ok(!lstrcmpA(buf, "val2"), "Expected \"val2\", got \"%s\"\n", buf);
805 
806     /* case does not match */
807     memset(buf, 0xc,sizeof(buf));
808     lstrcpyA(buf, "kumquat");
809     ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
810                                    buf, MAX_PATH, filename);
811     ok(ret == 4, "Expected 4, got %d\n", ret);
812     ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
813 
814     /* only filename is used */
815     memset(buf, 0xc,sizeof(buf));
816     lstrcpyA(buf, "kumquat");
817     ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
818                                    buf, MAX_PATH, "winetest.ini");
819     ok(ret == 7, "Expected 7, got %d\n", ret);
820     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
821 
822     GetWindowsDirectoryA(windir, MAX_PATH);
823     SetLastError(0xdeadbeef);
824     ret = GetTempFileNameA(windir, "pre", 0, path);
825     if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
826     {
827         skip("Not allowed to create a file in the Windows directory\n");
828         DeleteFileA(filename);
829         return;
830     }
831     tempfile = strrchr(path, '\\') + 1;
832     create_test_file(path, content, lstrlenA(content));
833 
834     /* only filename is used, file exists in windows directory */
835     memset(buf, 0xc,sizeof(buf));
836     lstrcpyA(buf, "kumquat");
837     ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
838                                    buf, MAX_PATH, tempfile);
839     ok(ret == 4, "Expected 4, got %d\n", ret);
840     ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
841 
842     /* successful case */
843     memset(buf, 0xc,sizeof(buf));
844     lstrcpyA(buf, "kumquat");
845     ret = GetPrivateProfileStringA("section1", "name1", "default",
846                                    buf, MAX_PATH, filename);
847     ok(ret == 4, "Expected 4, got %d\n", ret);
848     ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
849 
850  /* Existing section with no keys in an existing file */
851     memset(buf, 0xc,sizeof(buf));
852     SetLastError(0xdeadbeef);
853     ret=GetPrivateProfileStringA("section2", "DoesntExist", "",
854                                  buf, MAX_PATH, filename);
855     ok( ret == 0, "expected return size 0, got %d\n", ret );
856     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
857     todo_wine
858    ok( GetLastError() == 0xdeadbeef ||
859        GetLastError() == ERROR_FILE_NOT_FOUND /* Win 7 */,
860        "expected 0xdeadbeef or ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
861 
862 
863     DeleteFileA(path);
864     DeleteFileA(filename);
865 }
866 
867 static BOOL check_binary_file_data(LPCSTR path, const VOID *data, DWORD size)
868 {
869     HANDLE file;
870     CHAR buf[MAX_PATH];
871     BOOL ret;
872 
873     file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
874     if (file == INVALID_HANDLE_VALUE)
875       return FALSE;
876 
877     if(size != GetFileSize(file, NULL) )
878     {
879         CloseHandle(file);
880         return FALSE;
881     }
882 
883     ret = ReadFile(file, buf, size, &size, NULL);
884     CloseHandle(file);
885     if (!ret)
886       return FALSE;
887 
888     return !memcmp(buf, data, size);
889 }
890 
891 static BOOL check_file_data(LPCSTR path, LPCSTR data)
892 {
893     return check_binary_file_data(path, data, lstrlenA(data));
894 }
895 
896 static void test_WritePrivateProfileString(void)
897 {
898     BOOL ret;
899     LPCSTR data;
900     CHAR path[MAX_PATH];
901     CHAR temp[MAX_PATH];
902 
903     SetLastError(0xdeadbeef);
904     ret = WritePrivateProfileStringW(NULL, NULL, NULL, NULL);
905     if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
906     {
907         /* Win9x/WinME needs (variable) timeouts between tests and even long timeouts don't
908          * guarantee a correct result.
909          * Win9x/WinMe also produces different ini files where there is always a newline before
910          * a section start (except for the first one).
911          */
912         win_skip("WritePrivateProfileString on Win9x/WinME is hard to test reliably\n");
913         return;
914     }
915 
916     GetTempPathA(MAX_PATH, temp);
917     GetTempFileNameA(temp, "wine", 0, path);
918     DeleteFileA(path);
919 
920     /* path is not created yet */
921 
922     /* NULL lpAppName */
923     SetLastError(0xdeadbeef);
924     ret = WritePrivateProfileStringA(NULL, "key", "string", path);
925     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
926     ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
927        broken(GetLastError() == ERROR_INVALID_PARAMETER) || /* NT4 */
928        broken(GetLastError() == 0xdeadbeef), /* Win9x and WinME */
929        "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
930     ok(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES,
931        "Expected path to not exist\n");
932 
933     GetTempFileNameA(temp, "wine", 0, path);
934 
935     /* NULL lpAppName, path exists */
936     data = "";
937     SetLastError(0xdeadbeef);
938     ret = WritePrivateProfileStringA(NULL, "key", "string", path);
939     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
940     ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
941        broken(GetLastError() == ERROR_INVALID_PARAMETER) || /* NT4 */
942        broken(GetLastError() == 0xdeadbeef), /* Win9x and WinME */
943        "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
944     ok(check_file_data(path, data), "File doesn't match\n");
945     DeleteFileA(path);
946 
947     if (0)
948     {
949     /* empty lpAppName, crashes on NT4 and higher */
950     data = "[]\r\n"
951            "key=string\r\n";
952     ret = WritePrivateProfileStringA("", "key", "string", path);
953     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
954     ok(check_file_data(path, data), "File doesn't match\n");
955     DeleteFileA(path);
956     }
957 
958     /* NULL lpKeyName */
959     data = "";
960     ret = WritePrivateProfileStringA("App", NULL, "string", path);
961     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
962     todo_wine
963     {
964         ok(check_file_data(path, data), "File doesn't match\n");
965     }
966     DeleteFileA(path);
967 
968     if (0)
969     {
970     /* empty lpKeyName, crashes on NT4 and higher */
971     data = "[App]\r\n"
972            "=string\r\n";
973     ret = WritePrivateProfileStringA("App", "", "string", path);
974     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
975     todo_wine
976     {
977         ok(check_file_data(path, data), "File doesn't match\n");
978     }
979     DeleteFileA(path);
980     }
981 
982     /* NULL lpString */
983     data = "";
984     ret = WritePrivateProfileStringA("App", "key", NULL, path);
985     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
986     todo_wine
987     {
988         ok(check_file_data(path, data) ||
989            (broken(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)), /* Win9x and WinME */
990            "File doesn't match\n");
991     }
992     DeleteFileA(path);
993 
994     /* empty lpString */
995     data = "[App]\r\n"
996            "key=\r\n";
997     ret = WritePrivateProfileStringA("App", "key", "", path);
998     ok(ret == TRUE ||
999        broken(!ret), /* Win9x and WinME */
1000        "Expected TRUE, got %d\n", ret);
1001     ok(check_file_data(path, data) ||
1002        (broken(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)), /* Win9x and WinME */
1003        "File doesn't match\n");
1004     DeleteFileA(path);
1005 
1006     /* empty lpFileName */
1007     SetLastError(0xdeadbeef);
1008     ret = WritePrivateProfileStringA("App", "key", "string", "");
1009     ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1010     ok(GetLastError() == ERROR_ACCESS_DENIED ||
1011        broken(GetLastError() == ERROR_PATH_NOT_FOUND), /* Win9x and WinME */
1012        "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1013 
1014     /* Relative paths are relative to X:\\%WINDIR% */
1015     GetWindowsDirectoryA(temp, MAX_PATH);
1016     GetTempFileNameA(temp, "win", 1, path);
1017     if (GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)
1018         skip("Not allowed to create a file in the Windows directory\n");
1019     else
1020     {
1021         DeleteFileA(path);
1022 
1023         data = "[App]\r\n"
1024                "key=string\r\n";
1025         ret = WritePrivateProfileStringA("App", "key", "string", "win1.tmp");
1026         ok(ret == TRUE, "Expected TRUE, got %d, le=%u\n", ret, GetLastError());
1027         ok(check_file_data(path, data), "File doesn't match\n");
1028         DeleteFileA(path);
1029     }
1030 
1031     GetTempPathA(MAX_PATH, temp);
1032     GetTempFileNameA(temp, "wine", 0, path);
1033 
1034     /* build up an INI file */
1035     WritePrivateProfileStringA("App1", "key1", "string1", path);
1036     WritePrivateProfileStringA("App1", "key2", "string2", path);
1037     WritePrivateProfileStringA("App1", "key3", "string3", path);
1038     WritePrivateProfileStringA("App2", "key4", "string4", path);
1039 
1040     /* make an addition and verify the INI */
1041     data = "[App1]\r\n"
1042            "key1=string1\r\n"
1043            "key2=string2\r\n"
1044            "key3=string3\r\n"
1045            "[App2]\r\n"
1046            "key4=string4\r\n"
1047            "[App3]\r\n"
1048            "key5=string5\r\n";
1049     ret = WritePrivateProfileStringA("App3", "key5", "string5", path);
1050     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1051     ok(check_file_data(path, data), "File doesn't match\n");
1052 
1053     /* lpString is NULL, key2 key is deleted */
1054     data = "[App1]\r\n"
1055            "key1=string1\r\n"
1056            "key3=string3\r\n"
1057            "[App2]\r\n"
1058            "key4=string4\r\n"
1059            "[App3]\r\n"
1060            "key5=string5\r\n";
1061     ret = WritePrivateProfileStringA("App1", "key2", NULL, path);
1062     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1063     ok(check_file_data(path, data), "File doesn't match\n");
1064 
1065     /* try to delete key2 again */
1066     data = "[App1]\r\n"
1067            "key1=string1\r\n"
1068            "key3=string3\r\n"
1069            "[App2]\r\n"
1070            "key4=string4\r\n"
1071            "[App3]\r\n"
1072            "key5=string5\r\n";
1073     ret = WritePrivateProfileStringA("App1", "key2", NULL, path);
1074     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1075     ok(check_file_data(path, data), "File doesn't match\n");
1076 
1077     /* lpKeyName is NULL, App1 section is deleted */
1078     data = "[App2]\r\n"
1079            "key4=string4\r\n"
1080            "[App3]\r\n"
1081            "key5=string5\r\n";
1082     ret = WritePrivateProfileStringA("App1", NULL, "string1", path);
1083     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1084     ok(check_file_data(path, data), "File doesn't match\n");
1085 
1086     /* lpString is not needed to delete a section */
1087     data = "[App3]\r\n"
1088            "key5=string5\r\n";
1089     ret = WritePrivateProfileStringA("App2", NULL, NULL, path);
1090     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1091     ok(check_file_data(path, data), "File doesn't match\n");
1092 
1093     /* leave just the section */
1094     data = "[App3]\r\n";
1095     ret = WritePrivateProfileStringA("App3", "key5", NULL, path);
1096     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1097     ok(check_file_data(path, data), "File doesn't match\n");
1098     DeleteFileA(path);
1099 
1100     /* NULLs in file before first section. Should be preserved in output */
1101     data = "Data \0 before \0 first \0 section"    /* 31 bytes */
1102            "\r\n[section1]\r\n"                    /* 14 bytes */
1103            "key1=string1\r\n";                     /* 14 bytes */
1104     GetTempFileNameA(temp, "wine", 0, path);
1105     create_test_file(path, data, 31);
1106     ret = WritePrivateProfileStringA("section1", "key1", "string1", path);
1107     ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1108     todo_wine
1109     ok( check_binary_file_data(path, data, 59) ||
1110         broken( check_binary_file_data(path,        /* Windows 9x */
1111             "Data \0 before \0 first \0 section"    /* 31 bytes */
1112             "\r\n\r\n[section1]\r\n"                /* 14 bytes */
1113             "key1=string1"                          /* 14 bytes */
1114             , 59)), "File doesn't match\n");
1115     DeleteFileA(path);
1116 }
1117 
1118 START_TEST(profile)
1119 {
1120     test_profile_int();
1121     test_profile_string();
1122     test_profile_sections();
1123     test_profile_sections_names();
1124     test_profile_existing();
1125     test_profile_delete_on_close();
1126     test_profile_refresh();
1127     test_GetPrivateProfileString(
1128         "[section1]\r\n"
1129         "name1=val1\r\n"
1130         "name2=\"val2\"\r\n"
1131         "name3\r\n"
1132         "name4=a\r\n"
1133         "[section2]\r\n",
1134         "CR+LF");
1135     test_GetPrivateProfileString(
1136         "[section1]\r"
1137         "name1=val1\r"
1138         "name2=\"val2\"\r"
1139         "name3\r"
1140         "name4=a\r"
1141         "[section2]\r",
1142         "CR only");
1143     test_WritePrivateProfileString();
1144 }
1145