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