kernel32: Add a test for CreateFile when using OPEN_ALWAYS on directories.
[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, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
178
179     SetLastError(0xdeadbeef);
180     ret = GetPrivateProfileSectionA( "section1", buf, sizeof(buf), NULL );
181     ok( ret == 0, "expected return size 0, got %d\n", ret );
182     todo_wine
183     ok( GetLastError() == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
184
185     /* And a real one */
186     SetLastError(0xdeadbeef);
187     ret=GetPrivateProfileSectionA("section1", buf, sizeof(buf), testfile4);
188     for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
189         p[-1] = ',';
190     ok( ret == 35 && !strcmp( buf, "name1=val1,name2=,name3,name4=val4"), "wrong section returned(%d): %s\n",
191             ret, buf);
192     ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
193     ok( GetLastError() == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %d\n", GetLastError());
194
195     DeleteFileA( testfile4 );
196 }
197
198 static void test_profile_sections_names(void)
199 {
200     HANDLE h;
201     int ret;
202     DWORD count;
203     char buf[100];
204     WCHAR bufW[100];
205     static const char content[]="[section1]\r\n[section2]\r\n[section3]\r\n";
206     static const char testfile3[]=".\\testwine3.ini";
207     static const WCHAR testfile3W[]={ '.','\\','t','e','s','t','w','i','n','e','3','.','i','n','i',0 };
208     static const WCHAR not_here[] = {'.','\\','n','o','t','_','h','e','r','e','.','i','n','i',0};
209     DeleteFileA( testfile3 );
210     h = CreateFileA( testfile3, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
211         FILE_ATTRIBUTE_NORMAL, NULL);
212     ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile3);
213     if( h == INVALID_HANDLE_VALUE) return;
214     WriteFile( h, content, sizeof(content), &count, NULL);
215     CloseHandle( h);
216
217     /* Test with sufficiently large buffer */
218     ret = GetPrivateProfileSectionNamesA( buf, 29, testfile3 );
219     ok( ret == 27, "expected return size 27, got %d\n", ret );
220     ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
221     
222     /* Test with exactly fitting buffer */
223     ret = GetPrivateProfileSectionNamesA( buf, 28, testfile3 );
224     ok( ret == 26, "expected return size 26, got %d\n", ret );
225     ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
226     
227     /* Test with a buffer too small */
228     ret = GetPrivateProfileSectionNamesA( buf, 27, testfile3 );
229     ok( ret == 25, "expected return size 25, got %d\n", ret );
230     ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
231     
232     /* Tests on nonexistent file */
233     memset(buf, 0xcc, sizeof(buf));
234     ret = GetPrivateProfileSectionNamesA( buf, 10, ".\\not_here.ini" );
235     ok( ret == 0, "expected return size 0, got %d\n", ret );
236     ok( buf[0] == 0, "returned buffer not terminated with null\n" );
237     ok( buf[1] != 0, "returned buffer terminated with double-null\n" );
238     
239     /* Test with sufficiently large buffer */
240     SetLastError(0xdeadbeef);
241     ret = GetPrivateProfileSectionNamesW( bufW, 29, testfile3W );
242     if (ret == 0 && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
243     {
244         skip("GetPrivateProfileSectionNamesW is not implemented\n");
245         DeleteFileA( testfile3 );
246         return;
247     }
248     ok( ret == 27, "expected return size 27, got %d\n", ret );
249     ok( bufW[ret-1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
250     
251     /* Test with exactly fitting buffer */
252     ret = GetPrivateProfileSectionNamesW( bufW, 28, testfile3W );
253     ok( ret == 26, "expected return size 26, got %d\n", ret );
254     ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
255     
256     /* Test with a buffer too small */
257     ret = GetPrivateProfileSectionNamesW( bufW, 27, testfile3W );
258     ok( ret == 25, "expected return size 25, got %d\n", ret );
259     ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
260     
261     DeleteFileA( testfile3 );
262
263     /* Tests on nonexistent file */
264     memset(bufW, 0xcc, sizeof(bufW));
265     ret = GetPrivateProfileSectionNamesW( bufW, 10, not_here );
266     ok( ret == 0, "expected return size 0, got %d\n", ret );
267     ok( bufW[0] == 0, "returned buffer not terminated with null\n" );
268     ok( bufW[1] != 0, "returned buffer terminated with double-null\n" );
269 }
270
271 /* If the ini-file has already been opened with CreateFile, WritePrivateProfileString failed in wine with an error ERROR_SHARING_VIOLATION,  some testing here */
272 static void test_profile_existing(void)
273 {
274     static const char *testfile1 = ".\\winesharing1.ini";
275     static const char *testfile2 = ".\\winesharing2.ini";
276
277     static const struct {
278         DWORD dwDesiredAccess;
279         DWORD dwShareMode;
280         DWORD write_error;
281         BOOL read_error;
282         DWORD broken_error;
283     } pe[] = {
284         {GENERIC_READ,  FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
285         {GENERIC_READ,  FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
286         {GENERIC_WRITE, FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
287         {GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
288         {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ,  ERROR_SHARING_VIOLATION, FALSE },
289         {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
290         {GENERIC_READ,  FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
291         {GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
292         /*Thief demo (bug 5024) opens .ini file like this*/
293         {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */}
294     };
295
296     int i;
297     BOOL ret;
298     DWORD size;
299     HANDLE h = 0;
300     char buffer[MAX_PATH];
301
302     for (i=0; i < sizeof(pe)/sizeof(pe[0]); i++)
303     {
304         h = CreateFile(testfile1, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
305                        CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
306         ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
307         SetLastError(0xdeadbeef);
308
309         ret = WritePrivateProfileString(SECTION, KEY, "12345", testfile1);
310         if (!pe[i].write_error)
311         {
312             if (!ret)
313                 ok( broken(GetLastError() == pe[i].broken_error),
314                     "%d: WritePrivateProfileString failed with error %u\n", i, GetLastError() );
315             CloseHandle(h);
316             size = GetPrivateProfileString(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
317             if (ret)
318                 ok( size == 5, "%d: test failed, number of characters copied: %d instead of 5\n", i, size );
319             else
320                 ok( !size, "%d: test failed, number of characters copied: %d instead of 0\n", i, size );
321         }
322         else
323         {
324             DWORD err = GetLastError();
325             ok( !ret, "%d: WritePrivateProfileString succeeded\n", i );
326             if (!ret)
327                 ok( err == pe[i].write_error, "%d: WritePrivateProfileString failed with error %u/%u\n",
328                     i, err, pe[i].write_error );
329             CloseHandle(h);
330             size = GetPrivateProfileString(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
331             ok( !size, "%d: test failed, number of characters copied: %d instead of 0\n", i, size );
332         }
333
334         ok( DeleteFile(testfile1), "delete failed\n" );
335     }
336
337     h = CreateFile(testfile2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
338     sprintf( buffer, "[%s]\r\n%s=123\r\n", SECTION, KEY );
339     ok( WriteFile( h, buffer, strlen(buffer), &size, NULL ), "failed to write\n" );
340     CloseHandle( h );
341
342     for (i=0; i < sizeof(pe)/sizeof(pe[0]); i++)
343     {
344         h = CreateFile(testfile2, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
345                        OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
346         ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
347         SetLastError(0xdeadbeef);
348         ret = GetPrivateProfileStringA(SECTION, KEY, NULL, buffer, MAX_PATH, testfile2);
349         if (!pe[i].read_error)
350             ok( ret, "%d: GetPrivateProfileString failed with error %u\n", i, GetLastError() );
351         else
352             ok( !ret, "%d: GetPrivateProfileString succeeded\n", i );
353         CloseHandle(h);
354     }
355     ok( DeleteFile(testfile2), "delete failed\n" );
356 }
357
358 static void test_profile_delete_on_close()
359 {
360     static CHAR testfile[] = ".\\testwine5.ini";
361     HANDLE h;
362     DWORD size, res;
363     static const char contents[] = "[" SECTION "]\n" KEY "=123\n";
364
365     h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
366                     CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
367     ok( WriteFile( h, contents, sizeof contents - 1, &size, NULL ),
368                     "Cannot write test file: %x\n", GetLastError() );
369     ok( size == sizeof contents - 1, "Test file: partial write\n");
370
371     res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
372     ok( res == 123, "Got %d instead of 123\n", res);
373
374     /* This also deletes the file */
375     CloseHandle(h);
376 }
377
378 static void test_profile_refresh(void)
379 {
380     static CHAR testfile[] = ".\\winetest4.ini";
381     HANDLE h;
382     DWORD size, res;
383     static const char contents1[] = "[" SECTION "]\n" KEY "=123\n";
384     static const char contents2[] = "[" SECTION "]\n" KEY "=124\n";
385
386     h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
387                     CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
388     ok( WriteFile( h, contents1, sizeof contents1 - 1, &size, NULL ),
389                     "Cannot write test file: %x\n", GetLastError() );
390     ok( size == sizeof contents1 - 1, "Test file: partial write\n");
391
392     res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
393     ok( res == 123, "Got %d instead of 123\n", res);
394
395     CloseHandle(h);
396
397     /* Test proper invalidation of wine's profile file cache */
398
399     h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
400                     CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
401     ok( WriteFile( h, contents2, sizeof contents2 - 1, &size, NULL ),
402                     "Cannot write test file: %x\n", GetLastError() );
403     ok( size == sizeof contents2 - 1, "Test file: partial write\n");
404
405     res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
406     ok( res == 124, "Got %d instead of 124\n", res);
407
408     /* This also deletes the file */
409     CloseHandle(h);
410 }
411
412 static void create_test_file(LPCSTR name, LPCSTR data, DWORD size)
413 {
414     HANDLE hfile;
415     DWORD count;
416
417     hfile = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
418     ok(hfile != INVALID_HANDLE_VALUE, "cannot create %s\n", name);
419     WriteFile(hfile, data, size, &count, NULL);
420     CloseHandle(hfile);
421 }
422
423 static BOOL emptystr_ok(CHAR emptystr[MAX_PATH])
424 {
425     int i;
426
427     for(i = 0;i < MAX_PATH;++i)
428         if(emptystr[i] != 0)
429         {
430             trace("emptystr[%d] = %d\n",i,emptystr[i]);
431             return FALSE;
432         }
433
434     return TRUE;
435 }
436
437 static void test_GetPrivateProfileString(const char *content, const char *descript)
438 {
439     DWORD ret;
440     CHAR buf[MAX_PATH];
441     CHAR def_val[MAX_PATH];
442     CHAR path[MAX_PATH];
443     CHAR windir[MAX_PATH];
444     /* NT series crashes on r/o empty strings, so pass an r/w
445        empty string and check for modification */
446     CHAR emptystr[MAX_PATH] = "";
447     LPSTR tempfile;
448
449     static const char filename[] = ".\\winetest.ini";
450
451     trace("test_GetPrivateProfileStringA: %s\n", descript);
452
453     create_test_file(filename, content, lstrlenA(content));
454
455     /* Run this test series with caching. Wine won't cache profile
456        files younger than 2.1 seconds. */
457     Sleep(2500);
458
459     /* lpAppName is NULL */
460     lstrcpyA(buf, "kumquat");
461     ret = GetPrivateProfileStringA(NULL, "name1", "default",
462                                    buf, MAX_PATH, filename);
463     ok(ret == 18, "Expected 18, got %d\n", ret);
464     ok(!memcmp(buf, "section1\0section2\0", ret + 1),
465        "Expected \"section1\\0section2\\0\", got \"%s\"\n", buf);
466
467     /* lpAppName is empty */
468     lstrcpyA(buf, "kumquat");
469     ret = GetPrivateProfileStringA(emptystr, "name1", "default",
470                                    buf, MAX_PATH, filename);
471     ok(ret == 7, "Expected 7, got %d\n", ret);
472     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
473     ok(emptystr_ok(emptystr), "AppName modified\n");
474
475     /* lpAppName is missing */
476     lstrcpyA(buf, "kumquat");
477     ret = GetPrivateProfileStringA("notasection", "name1", "default",
478                                    buf, MAX_PATH, filename);
479     ok(ret == 7, "Expected 7, got %d\n", ret);
480     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
481
482     /* lpAppName is empty, lpDefault is NULL */
483     lstrcpyA(buf, "kumquat");
484     ret = GetPrivateProfileStringA(emptystr, "name1", NULL,
485                                    buf, MAX_PATH, filename);
486     ok(ret == 0, "Expected 0, got %d\n", ret);
487     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
488     ok(emptystr_ok(emptystr), "AppName modified\n");
489
490     /* lpAppName is empty, lpDefault is empty */
491     lstrcpyA(buf, "kumquat");
492     ret = GetPrivateProfileStringA(emptystr, "name1", "",
493                                    buf, MAX_PATH, filename);
494     ok(ret == 0, "Expected 0, got %d\n", ret);
495     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
496     ok(emptystr_ok(emptystr), "AppName modified\n");
497
498     /* lpAppName is empty, lpDefault has trailing blank characters */
499     lstrcpyA(buf, "kumquat");
500     /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
501     lstrcpyA(def_val, "default  ");
502     ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
503                                    buf, MAX_PATH, filename);
504     ok(ret == 7, "Expected 7, got %d\n", ret);
505     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
506     ok(emptystr_ok(emptystr), "AppName modified\n");
507
508     /* lpAppName is empty, many blank characters in lpDefault */
509     lstrcpyA(buf, "kumquat");
510     /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
511     lstrcpyA(def_val, "one two  ");
512     ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
513                                    buf, MAX_PATH, filename);
514     ok(ret == 7, "Expected 7, got %d\n", ret);
515     ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
516     ok(emptystr_ok(emptystr), "AppName modified\n");
517
518     /* lpAppName is empty, blank character but not trailing in lpDefault */
519     lstrcpyA(buf, "kumquat");
520     ret = GetPrivateProfileStringA(emptystr, "name1", "one two",
521                                    buf, MAX_PATH, filename);
522     ok(ret == 7, "Expected 7, got %d\n", ret);
523     ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
524     ok(emptystr_ok(emptystr), "AppName modified\n");
525
526     /* lpKeyName is NULL */
527     lstrcpyA(buf, "kumquat");
528     ret = GetPrivateProfileStringA("section1", NULL, "default",
529                                    buf, MAX_PATH, filename);
530     ok(ret == 18, "Expected 18, got %d\n", ret);
531     ok(!memcmp(buf, "name1\0name2\0name4\0", ret + 1),
532        "Expected \"name1\\0name2\\0name4\\0\", got \"%s\"\n", buf);
533
534     /* lpKeyName is empty */
535     lstrcpyA(buf, "kumquat");
536     ret = GetPrivateProfileStringA("section1", emptystr, "default",
537                                    buf, MAX_PATH, filename);
538     ok(ret == 7, "Expected 7, got %d\n", ret);
539     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
540     ok(emptystr_ok(emptystr), "KeyName modified\n");
541
542     /* lpKeyName is missing */
543     lstrcpyA(buf, "kumquat");
544     ret = GetPrivateProfileStringA("section1", "notakey", "default",
545                                    buf, MAX_PATH, filename);
546     ok(ret == 7, "Expected 7, got %d\n", ret);
547     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
548
549     /* lpKeyName is empty, lpDefault is NULL */
550     lstrcpyA(buf, "kumquat");
551     ret = GetPrivateProfileStringA("section1", emptystr, NULL,
552                                    buf, MAX_PATH, filename);
553     ok(ret == 0, "Expected 0, got %d\n", ret);
554     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
555     ok(emptystr_ok(emptystr), "KeyName modified\n");
556
557     /* lpKeyName is empty, lpDefault is empty */
558     lstrcpyA(buf, "kumquat");
559     ret = GetPrivateProfileStringA("section1", emptystr, "",
560                                    buf, MAX_PATH, filename);
561     ok(ret == 0, "Expected 0, got %d\n", ret);
562     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
563     ok(emptystr_ok(emptystr), "KeyName modified\n");
564
565     /* lpKeyName is empty, lpDefault has trailing blank characters */
566     lstrcpyA(buf, "kumquat");
567     /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
568     lstrcpyA(def_val, "default  ");
569     ret = GetPrivateProfileStringA("section1", emptystr, def_val,
570                                    buf, MAX_PATH, filename);
571     ok(ret == 7, "Expected 7, got %d\n", ret);
572     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
573     ok(emptystr_ok(emptystr), "KeyName modified\n");
574
575     if (0) /* crashes */
576     {
577         /* lpReturnedString is NULL */
578         ret = GetPrivateProfileStringA("section1", "name1", "default",
579                                        NULL, MAX_PATH, filename);
580     }
581
582     /* lpFileName is NULL */
583     lstrcpyA(buf, "kumquat");
584     ret = GetPrivateProfileStringA("section1", "name1", "default",
585                                    buf, MAX_PATH, NULL);
586     ok(ret == 7, "Expected 7, got %d\n", ret);
587     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
588
589     /* lpFileName is empty */
590     lstrcpyA(buf, "kumquat");
591     ret = GetPrivateProfileStringA("section1", "name1", "default",
592                                    buf, MAX_PATH, "");
593     ok(ret == 7, "Expected 7, got %d\n", ret);
594     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
595
596     /* lpFileName is nonexistent */
597     lstrcpyA(buf, "kumquat");
598     ret = GetPrivateProfileStringA("section1", "name1", "default",
599                                    buf, MAX_PATH, "nonexistent");
600     ok(ret == 7, "Expected 7, got %d\n", ret);
601     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
602
603     /* nSize is 0 */
604     lstrcpyA(buf, "kumquat");
605     ret = GetPrivateProfileStringA("section1", "name1", "default",
606                                    buf, 0, filename);
607     ok(ret == 0, "Expected 0, got %d\n", ret);
608     ok(!lstrcmpA(buf, "kumquat"), "Expected buf to be unchanged, got \"%s\"\n", buf);
609
610     /* nSize is exact size of output */
611     lstrcpyA(buf, "kumquat");
612     ret = GetPrivateProfileStringA("section1", "name1", "default",
613                                    buf, 4, filename);
614     ok(ret == 3, "Expected 3, got %d\n", ret);
615     ok(!lstrcmpA(buf, "val"), "Expected \"val\", got \"%s\"\n", buf);
616
617     /* nSize has room for NULL terminator */
618     lstrcpyA(buf, "kumquat");
619     ret = GetPrivateProfileStringA("section1", "name1", "default",
620                                    buf, 5, filename);
621     ok(ret == 4, "Expected 4, got %d\n", ret);
622     ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
623
624     /* output is 1 character */
625     lstrcpyA(buf, "kumquat");
626     ret = GetPrivateProfileStringA("section1", "name4", "default",
627                                    buf, MAX_PATH, filename);
628     ok(ret == 1, "Expected 1, got %d\n", ret);
629     ok(!lstrcmpA(buf, "a"), "Expected \"a\", got \"%s\"\n", buf);
630
631     /* output is 1 character, no room for NULL terminator */
632     lstrcpyA(buf, "kumquat");
633     ret = GetPrivateProfileStringA("section1", "name4", "default",
634                                    buf, 1, filename);
635     ok(ret == 0, "Expected 0, got %d\n", ret);
636     ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
637
638     /* lpAppName is NULL, not enough room for final section name */
639     lstrcpyA(buf, "kumquat");
640     ret = GetPrivateProfileStringA(NULL, "name1", "default",
641                                    buf, 16, filename);
642     ok(ret == 14, "Expected 14, got %d\n", ret);
643     ok(!memcmp(buf, "section1\0secti\0", ret + 1),
644        "Expected \"section1\\0secti\\0\", got \"%s\"\n", buf);
645
646     /* lpKeyName is NULL, not enough room for final key name */
647     lstrcpyA(buf, "kumquat");
648     ret = GetPrivateProfileStringA("section1", NULL, "default",
649                                    buf, 16, filename);
650     ok(ret == 14, "Expected 14, got %d\n", ret);
651     ok(!memcmp(buf, "name1\0name2\0na\0", ret + 1),
652        "Expected \"name1\\0name2\\0na\\0\", got \"%s\"\n", buf);
653
654     /* key value has quotation marks which are stripped */
655     lstrcpyA(buf, "kumquat");
656     ret = GetPrivateProfileStringA("section1", "name2", "default",
657                                    buf, MAX_PATH, filename);
658     ok(ret == 4, "Expected 4, got %d\n", ret);
659     ok(!lstrcmpA(buf, "val2"), "Expected \"val2\", got \"%s\"\n", buf);
660
661     /* case does not match */
662     lstrcpyA(buf, "kumquat");
663     ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
664                                    buf, MAX_PATH, filename);
665     ok(ret == 4, "Expected 4, got %d\n", ret);
666     ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
667
668     /* only filename is used */
669     lstrcpyA(buf, "kumquat");
670     ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
671                                    buf, MAX_PATH, "winetest.ini");
672     ok(ret == 7, "Expected 7, got %d\n", ret);
673     ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
674
675     GetWindowsDirectoryA(windir, MAX_PATH);
676     GetTempFileNameA(windir, "pre", 0, path);
677     tempfile = strrchr(path, '\\') + 1;
678     create_test_file(path, content, lstrlenA(content));
679
680     /* only filename is used, file exists in windows directory */
681     lstrcpyA(buf, "kumquat");
682     ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
683                                    buf, MAX_PATH, tempfile);
684     ok(ret == 4, "Expected 4, got %d\n", ret);
685     ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
686
687     /* successful case */
688     lstrcpyA(buf, "kumquat");
689     ret = GetPrivateProfileStringA("section1", "name1", "default",
690                                    buf, MAX_PATH, filename);
691     ok(ret == 4, "Expected 4, got %d\n", ret);
692     ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
693
694     DeleteFileA(path);
695     DeleteFileA(filename);
696 }
697
698 START_TEST(profile)
699 {
700     test_profile_int();
701     test_profile_string();
702     test_profile_sections();
703     test_profile_sections_names();
704     test_profile_existing();
705     test_profile_delete_on_close();
706     test_profile_refresh();
707     test_GetPrivateProfileString(
708         "[section1]\r\n"
709         "name1=val1\r\n"
710         "name2=\"val2\"\r\n"
711         "name3\r\n"
712         "name4=a\r\n"
713         "[section2]\r\n",
714         "CR+LF");
715     test_GetPrivateProfileString(
716         "[section1]\r"
717         "name1=val1\r"
718         "name2=\"val2\"\r"
719         "name3\r"
720         "name4=a\r"
721         "[section2]\r",
722         "CR only");
723 }