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