2 * Unit tests for profile functions
4 * Copyright (c) 2003 Stefan Leichter
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.
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.
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
24 #include "wine/test.h"
29 #define KEY "ProfileInt"
30 #define SECTION "Test"
31 #define TESTFILE ".\\testwine.ini"
32 #define TESTFILE2 ".\\testwine2.ini"
44 static void test_profile_int(void)
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},
73 int i, num_test = (sizeof(profileInt)/sizeof(struct _profileInt));
76 DeleteFileA( TESTFILE);
78 for (i=0; i < num_test; i++) {
79 if (profileInt[i].value)
80 WritePrivateProfileStringA(SECTION, KEY, profileInt[i].value,
81 profileInt[i].iniFile);
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);
90 DeleteFileA( TESTFILE);
93 static void test_profile_string(void)
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);
111 /* enumerate the keys */
112 ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
114 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
117 ok( ret == 18 && !strcmp( buf, "name1,name2,name4"), "wrong keys returned(%d): %s\n", ret,
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),
124 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
126 ok( ret == 24 && !strcmp( buf, "name1,name2,name4,name5"), "wrong keys returned(%d): %s\n",
129 DeleteFileA( TESTFILE2);
132 static void test_profile_sections(void)
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;
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);
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;
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());
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());
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());
179 SetLastError(0xdeadbeef);
180 ret = GetPrivateProfileSectionA( "section1", buf, sizeof(buf), NULL );
181 ok( ret == 0, "expected return size 0, got %d\n", ret );
183 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
186 SetLastError(0xdeadbeef);
187 ret=GetPrivateProfileSectionA("section1", buf, sizeof(buf), testfile4);
188 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
190 ok( ret == 35 && !strcmp( buf, "name1=val1,name2=,name3,name4=val4"), "wrong section returned(%d): %s\n",
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());
195 DeleteFileA( testfile4 );
198 static void test_profile_sections_names(void)
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);
217 /* Test with sufficiently large buffer */
218 memset(buf, 0xc, sizeof(buf));
219 ret = GetPrivateProfileSectionNamesA( buf, 29, testfile3 );
221 broken(ret == 28), /* Win9x, WinME */
222 "expected return size 27, got %d\n", ret );
223 ok( (buf[ret-1] == 0 && buf[ret] == 0) ||
224 broken(buf[ret-1] == 0 && buf[ret-2] == 0), /* Win9x, WinME */
225 "returned buffer not terminated with double-null\n" );
227 /* Test with exactly fitting buffer */
228 memset(buf, 0xc, sizeof(buf));
229 ret = GetPrivateProfileSectionNamesA( buf, 28, testfile3 );
231 broken(ret == 28), /* Win9x, WinME */
232 "expected return size 26, got %d\n", ret );
234 ok( (buf[ret+1] == 0 && buf[ret] == 0) || /* W2K3 and higher */
235 broken(buf[ret+1] == 0xc && buf[ret] == 0) || /* NT4, W2K, WinXP */
236 broken(buf[ret-1] == 0 && buf[ret-2] == 0), /* Win9x, WinME */
237 "returned buffer not terminated with double-null\n" );
239 /* Test with a buffer too small */
240 memset(buf, 0xc, sizeof(buf));
241 ret = GetPrivateProfileSectionNamesA( buf, 27, testfile3 );
242 ok( ret == 25, "expected return size 25, got %d\n", ret );
243 /* Win9x and WinME only fills the buffer with complete section names (double-null terminated) */
244 count = strlen("section1") + sizeof(CHAR) + strlen("section2");
246 ok( (buf[ret+1] == 0 && buf[ret] == 0) ||
247 broken(buf[count] == 0 && buf[count+1] == 0), /* Win9x, WinME */
248 "returned buffer not terminated with double-null\n" );
250 /* Tests on nonexistent file */
251 memset(buf, 0xc, sizeof(buf));
252 ret = GetPrivateProfileSectionNamesA( buf, 10, ".\\not_here.ini" );
254 broken(ret == 1), /* Win9x, WinME */
255 "expected return size 0, got %d\n", ret );
256 ok( buf[0] == 0, "returned buffer not terminated with null\n" );
257 ok( buf[1] != 0, "returned buffer terminated with double-null\n" );
259 /* Test with sufficiently large buffer */
260 SetLastError(0xdeadbeef);
261 memset(bufW, 0xcc, sizeof(bufW));
262 ret = GetPrivateProfileSectionNamesW( bufW, 29, testfile3W );
263 if (ret == 0 && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
265 win_skip("GetPrivateProfileSectionNamesW is not implemented\n");
266 DeleteFileA( testfile3 );
269 ok( ret == 27, "expected return size 27, got %d\n", ret );
270 ok( bufW[ret-1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
272 /* Test with exactly fitting buffer */
273 memset(bufW, 0xcc, sizeof(bufW));
274 ret = GetPrivateProfileSectionNamesW( bufW, 28, testfile3W );
275 ok( ret == 26, "expected return size 26, got %d\n", ret );
276 ok( (bufW[ret+1] == 0 && bufW[ret] == 0) || /* W2K3 and higher */
277 broken(bufW[ret+1] == 0xcccc && bufW[ret] == 0), /* NT4, W2K, WinXP */
278 "returned buffer not terminated with double-null\n" );
280 /* Test with a buffer too small */
281 memset(bufW, 0xcc, sizeof(bufW));
282 ret = GetPrivateProfileSectionNamesW( bufW, 27, testfile3W );
283 ok( ret == 25, "expected return size 25, got %d\n", ret );
284 ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
286 DeleteFileA( testfile3 );
288 /* Tests on nonexistent file */
289 memset(bufW, 0xcc, sizeof(bufW));
290 ret = GetPrivateProfileSectionNamesW( bufW, 10, not_here );
291 ok( ret == 0, "expected return size 0, got %d\n", ret );
292 ok( bufW[0] == 0, "returned buffer not terminated with null\n" );
293 ok( bufW[1] != 0, "returned buffer terminated with double-null\n" );
296 /* If the ini-file has already been opened with CreateFile, WritePrivateProfileString failed in wine with an error ERROR_SHARING_VIOLATION, some testing here */
297 static void test_profile_existing(void)
299 static const char *testfile1 = ".\\winesharing1.ini";
300 static const char *testfile2 = ".\\winesharing2.ini";
302 static const struct {
303 DWORD dwDesiredAccess;
309 {GENERIC_READ, FILE_SHARE_READ, ERROR_SHARING_VIOLATION, FALSE },
310 {GENERIC_READ, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
311 {GENERIC_WRITE, FILE_SHARE_READ, ERROR_SHARING_VIOLATION, FALSE },
312 {GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
313 {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, ERROR_SHARING_VIOLATION, FALSE },
314 {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
315 {GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
316 {GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
317 /*Thief demo (bug 5024) opens .ini file like this*/
318 {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */}
325 char buffer[MAX_PATH];
327 for (i=0; i < sizeof(pe)/sizeof(pe[0]); i++)
329 h = CreateFile(testfile1, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
330 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
331 ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
332 SetLastError(0xdeadbeef);
334 ret = WritePrivateProfileString(SECTION, KEY, "12345", testfile1);
335 if (!pe[i].write_error)
338 ok( broken(GetLastError() == pe[i].broken_error),
339 "%d: WritePrivateProfileString failed with error %u\n", i, GetLastError() );
341 size = GetPrivateProfileString(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
343 ok( size == 5, "%d: test failed, number of characters copied: %d instead of 5\n", i, size );
345 ok( !size, "%d: test failed, number of characters copied: %d instead of 0\n", i, size );
349 DWORD err = GetLastError();
350 ok( !ret, "%d: WritePrivateProfileString succeeded\n", i );
352 ok( err == pe[i].write_error, "%d: WritePrivateProfileString failed with error %u/%u\n",
353 i, err, pe[i].write_error );
355 size = GetPrivateProfileString(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
356 ok( !size, "%d: test failed, number of characters copied: %d instead of 0\n", i, size );
359 ok( DeleteFile(testfile1), "delete failed\n" );
362 h = CreateFile(testfile2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
363 sprintf( buffer, "[%s]\r\n%s=123\r\n", SECTION, KEY );
364 ok( WriteFile( h, buffer, strlen(buffer), &size, NULL ), "failed to write\n" );
367 for (i=0; i < sizeof(pe)/sizeof(pe[0]); i++)
369 h = CreateFile(testfile2, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
370 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
371 ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
372 SetLastError(0xdeadbeef);
373 ret = GetPrivateProfileStringA(SECTION, KEY, NULL, buffer, MAX_PATH, testfile2);
374 if (!pe[i].read_error)
375 ok( ret, "%d: GetPrivateProfileString failed with error %u\n", i, GetLastError() );
377 ok( !ret, "%d: GetPrivateProfileString succeeded\n", i );
380 ok( DeleteFile(testfile2), "delete failed\n" );
383 static void test_profile_delete_on_close(void)
385 static CHAR testfile[] = ".\\testwine5.ini";
388 static const char contents[] = "[" SECTION "]\n" KEY "=123\n";
390 h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
391 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
392 ok( WriteFile( h, contents, sizeof contents - 1, &size, NULL ),
393 "Cannot write test file: %x\n", GetLastError() );
394 ok( size == sizeof contents - 1, "Test file: partial write\n");
396 res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
397 ok( res == 123, "Got %d instead of 123\n", res);
399 /* This also deletes the file */
403 static void test_profile_refresh(void)
405 static CHAR testfile[] = ".\\winetest4.ini";
408 static const char contents1[] = "[" SECTION "]\n" KEY "=123\n";
409 static const char contents2[] = "[" SECTION "]\n" KEY "=124\n";
411 h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
412 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
413 ok( WriteFile( h, contents1, sizeof contents1 - 1, &size, NULL ),
414 "Cannot write test file: %x\n", GetLastError() );
415 ok( size == sizeof contents1 - 1, "Test file: partial write\n");
417 res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
418 ok( res == 123, "Got %d instead of 123\n", res);
422 /* Test proper invalidation of wine's profile file cache */
424 h = CreateFile(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
425 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
426 ok( WriteFile( h, contents2, sizeof contents2 - 1, &size, NULL ),
427 "Cannot write test file: %x\n", GetLastError() );
428 ok( size == sizeof contents2 - 1, "Test file: partial write\n");
430 res = GetPrivateProfileInt(SECTION, KEY, 0, testfile);
431 ok( res == 124, "Got %d instead of 124\n", res);
433 /* This also deletes the file */
437 static void create_test_file(LPCSTR name, LPCSTR data, DWORD size)
442 hfile = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
443 ok(hfile != INVALID_HANDLE_VALUE, "cannot create %s\n", name);
444 WriteFile(hfile, data, size, &count, NULL);
448 static BOOL emptystr_ok(CHAR emptystr[MAX_PATH])
452 for(i = 0;i < MAX_PATH;++i)
455 trace("emptystr[%d] = %d\n",i,emptystr[i]);
462 static void test_GetPrivateProfileString(const char *content, const char *descript)
466 CHAR def_val[MAX_PATH];
468 CHAR windir[MAX_PATH];
469 /* NT series crashes on r/o empty strings, so pass an r/w
470 empty string and check for modification */
471 CHAR emptystr[MAX_PATH] = "";
474 static const char filename[] = ".\\winetest.ini";
476 trace("test_GetPrivateProfileStringA: %s\n", descript);
478 if(!lstrcmpA(descript, "CR only"))
480 SetLastError(0xdeadbeef);
481 ret = GetPrivateProfileStringW(NULL, NULL, NULL,
483 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
485 win_skip("Win9x and WinME don't handle 'CR only' correctly\n");
490 create_test_file(filename, content, lstrlenA(content));
492 /* Run this test series with caching. Wine won't cache profile
493 files younger than 2.1 seconds. */
496 /* lpAppName is NULL */
497 lstrcpyA(buf, "kumquat");
498 ret = GetPrivateProfileStringA(NULL, "name1", "default",
499 buf, MAX_PATH, filename);
501 broken(ret == 19), /* Win9x and WinME */
502 "Expected 18, got %d\n", ret);
503 len = lstrlenA("section1") + sizeof(CHAR) + lstrlenA("section2") + 2 * sizeof(CHAR);
504 ok(!memcmp(buf, "section1\0section2\0", len),
505 "Expected \"section1\\0section2\\0\", got \"%s\"\n", buf);
507 /* lpAppName is empty */
508 lstrcpyA(buf, "kumquat");
509 ret = GetPrivateProfileStringA(emptystr, "name1", "default",
510 buf, MAX_PATH, filename);
511 ok(ret == 7, "Expected 7, got %d\n", ret);
512 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
513 ok(emptystr_ok(emptystr), "AppName modified\n");
515 /* lpAppName is missing */
516 lstrcpyA(buf, "kumquat");
517 ret = GetPrivateProfileStringA("notasection", "name1", "default",
518 buf, MAX_PATH, filename);
519 ok(ret == 7, "Expected 7, got %d\n", ret);
520 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
522 /* lpAppName is empty, lpDefault is NULL */
523 lstrcpyA(buf, "kumquat");
524 ret = GetPrivateProfileStringA(emptystr, "name1", NULL,
525 buf, MAX_PATH, filename);
526 ok(ret == 0, "Expected 0, got %d\n", ret);
527 ok(!lstrcmpA(buf, "") ||
528 broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
529 "Expected \"\", got \"%s\"\n", buf);
530 ok(emptystr_ok(emptystr), "AppName modified\n");
532 /* lpAppName is empty, lpDefault is empty */
533 lstrcpyA(buf, "kumquat");
534 ret = GetPrivateProfileStringA(emptystr, "name1", "",
535 buf, MAX_PATH, filename);
536 ok(ret == 0, "Expected 0, got %d\n", ret);
537 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
538 ok(emptystr_ok(emptystr), "AppName modified\n");
540 /* lpAppName is empty, lpDefault has trailing blank characters */
541 lstrcpyA(buf, "kumquat");
542 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
543 lstrcpyA(def_val, "default ");
544 ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
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 ok(emptystr_ok(emptystr), "AppName modified\n");
550 /* lpAppName is empty, many blank characters in lpDefault */
551 lstrcpyA(buf, "kumquat");
552 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
553 lstrcpyA(def_val, "one two ");
554 ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
555 buf, MAX_PATH, filename);
556 ok(ret == 7, "Expected 7, got %d\n", ret);
557 ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
558 ok(emptystr_ok(emptystr), "AppName modified\n");
560 /* lpAppName is empty, blank character but not trailing in lpDefault */
561 lstrcpyA(buf, "kumquat");
562 ret = GetPrivateProfileStringA(emptystr, "name1", "one two",
563 buf, MAX_PATH, filename);
564 ok(ret == 7, "Expected 7, got %d\n", ret);
565 ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
566 ok(emptystr_ok(emptystr), "AppName modified\n");
568 /* lpKeyName is NULL */
569 lstrcpyA(buf, "kumquat");
570 ret = GetPrivateProfileStringA("section1", NULL, "default",
571 buf, MAX_PATH, filename);
572 ok(ret == 18, "Expected 18, got %d\n", ret);
573 ok(!memcmp(buf, "name1\0name2\0name4\0", ret + 1),
574 "Expected \"name1\\0name2\\0name4\\0\", got \"%s\"\n", buf);
576 /* lpKeyName is empty */
577 lstrcpyA(buf, "kumquat");
578 ret = GetPrivateProfileStringA("section1", emptystr, "default",
579 buf, MAX_PATH, filename);
580 ok(ret == 7, "Expected 7, got %d\n", ret);
581 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
582 ok(emptystr_ok(emptystr), "KeyName modified\n");
584 /* lpKeyName is missing */
585 lstrcpyA(buf, "kumquat");
586 ret = GetPrivateProfileStringA("section1", "notakey", "default",
587 buf, MAX_PATH, filename);
588 ok(ret == 7, "Expected 7, got %d\n", ret);
589 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
591 /* lpKeyName is empty, lpDefault is NULL */
592 lstrcpyA(buf, "kumquat");
593 ret = GetPrivateProfileStringA("section1", emptystr, NULL,
594 buf, MAX_PATH, filename);
595 ok(ret == 0, "Expected 0, got %d\n", ret);
596 ok(!lstrcmpA(buf, "") ||
597 broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
598 "Expected \"\", got \"%s\"\n", buf);
599 ok(emptystr_ok(emptystr), "KeyName modified\n");
601 /* lpKeyName is empty, lpDefault is empty */
602 lstrcpyA(buf, "kumquat");
603 ret = GetPrivateProfileStringA("section1", emptystr, "",
604 buf, MAX_PATH, filename);
605 ok(ret == 0, "Expected 0, got %d\n", ret);
606 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
607 ok(emptystr_ok(emptystr), "KeyName modified\n");
609 /* lpKeyName is empty, lpDefault has trailing blank characters */
610 lstrcpyA(buf, "kumquat");
611 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
612 lstrcpyA(def_val, "default ");
613 ret = GetPrivateProfileStringA("section1", emptystr, def_val,
614 buf, MAX_PATH, filename);
615 ok(ret == 7, "Expected 7, got %d\n", ret);
616 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
617 ok(emptystr_ok(emptystr), "KeyName modified\n");
621 /* lpReturnedString is NULL */
622 ret = GetPrivateProfileStringA("section1", "name1", "default",
623 NULL, MAX_PATH, filename);
626 /* lpFileName is NULL */
627 lstrcpyA(buf, "kumquat");
628 ret = GetPrivateProfileStringA("section1", "name1", "default",
629 buf, MAX_PATH, NULL);
631 broken(ret == 0), /* Win9x, WinME */
632 "Expected 7, got %d\n", ret);
633 ok(!lstrcmpA(buf, "default") ||
634 broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
635 "Expected \"default\", got \"%s\"\n", buf);
637 /* lpFileName is empty */
638 lstrcpyA(buf, "kumquat");
639 ret = GetPrivateProfileStringA("section1", "name1", "default",
641 ok(ret == 7, "Expected 7, got %d\n", ret);
642 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
644 /* lpFileName is nonexistent */
645 lstrcpyA(buf, "kumquat");
646 ret = GetPrivateProfileStringA("section1", "name1", "default",
647 buf, MAX_PATH, "nonexistent");
648 ok(ret == 7, "Expected 7, got %d\n", ret);
649 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
652 lstrcpyA(buf, "kumquat");
653 ret = GetPrivateProfileStringA("section1", "name1", "default",
655 ok(ret == 0, "Expected 0, got %d\n", ret);
656 ok(!lstrcmpA(buf, "kumquat"), "Expected buf to be unchanged, got \"%s\"\n", buf);
658 /* nSize is exact size of output */
659 lstrcpyA(buf, "kumquat");
660 ret = GetPrivateProfileStringA("section1", "name1", "default",
662 ok(ret == 3, "Expected 3, got %d\n", ret);
663 ok(!lstrcmpA(buf, "val"), "Expected \"val\", got \"%s\"\n", buf);
665 /* nSize has room for NULL terminator */
666 lstrcpyA(buf, "kumquat");
667 ret = GetPrivateProfileStringA("section1", "name1", "default",
669 ok(ret == 4, "Expected 4, got %d\n", ret);
670 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
672 /* output is 1 character */
673 lstrcpyA(buf, "kumquat");
674 ret = GetPrivateProfileStringA("section1", "name4", "default",
675 buf, MAX_PATH, filename);
676 ok(ret == 1, "Expected 1, got %d\n", ret);
677 ok(!lstrcmpA(buf, "a"), "Expected \"a\", got \"%s\"\n", buf);
679 /* output is 1 character, no room for NULL terminator */
680 lstrcpyA(buf, "kumquat");
681 ret = GetPrivateProfileStringA("section1", "name4", "default",
683 ok(ret == 0, "Expected 0, got %d\n", ret);
684 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
686 /* lpAppName is NULL, not enough room for final section name */
687 lstrcpyA(buf, "kumquat");
688 ret = GetPrivateProfileStringA(NULL, "name1", "default",
690 ok(ret == 14, "Expected 14, got %d\n", ret);
691 len = lstrlenA("section1") + 2 * sizeof(CHAR);
692 ok(!memcmp(buf, "section1\0secti\0", ret + 1) ||
693 broken(!memcmp(buf, "section1\0\0", len)), /* Win9x, WinME */
694 "Expected \"section1\\0secti\\0\", got \"%s\"\n", buf);
696 /* lpKeyName is NULL, not enough room for final key name */
697 lstrcpyA(buf, "kumquat");
698 ret = GetPrivateProfileStringA("section1", NULL, "default",
700 ok(ret == 14, "Expected 14, got %d\n", ret);
701 ok(!memcmp(buf, "name1\0name2\0na\0", ret + 1) ||
702 broken(!memcmp(buf, "name1\0name2\0n\0\0", ret + 1)), /* Win9x, WinME */
703 "Expected \"name1\\0name2\\0na\\0\", got \"%s\"\n", buf);
705 /* key value has quotation marks which are stripped */
706 lstrcpyA(buf, "kumquat");
707 ret = GetPrivateProfileStringA("section1", "name2", "default",
708 buf, MAX_PATH, filename);
709 ok(ret == 4, "Expected 4, got %d\n", ret);
710 ok(!lstrcmpA(buf, "val2"), "Expected \"val2\", got \"%s\"\n", buf);
712 /* case does not match */
713 lstrcpyA(buf, "kumquat");
714 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
715 buf, MAX_PATH, filename);
716 ok(ret == 4, "Expected 4, got %d\n", ret);
717 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
719 /* only filename is used */
720 lstrcpyA(buf, "kumquat");
721 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
722 buf, MAX_PATH, "winetest.ini");
723 ok(ret == 7, "Expected 7, got %d\n", ret);
724 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
726 GetWindowsDirectoryA(windir, MAX_PATH);
727 SetLastError(0xdeadbeef);
728 ret = GetTempFileNameA(windir, "pre", 0, path);
729 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
731 skip("Not allowed to create a file in the Windows directory\n");
732 DeleteFileA(filename);
735 tempfile = strrchr(path, '\\') + 1;
736 create_test_file(path, content, lstrlenA(content));
738 /* only filename is used, file exists in windows directory */
739 lstrcpyA(buf, "kumquat");
740 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
741 buf, MAX_PATH, tempfile);
742 ok(ret == 4, "Expected 4, got %d\n", ret);
743 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
745 /* successful case */
746 lstrcpyA(buf, "kumquat");
747 ret = GetPrivateProfileStringA("section1", "name1", "default",
748 buf, MAX_PATH, filename);
749 ok(ret == 4, "Expected 4, got %d\n", ret);
750 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
753 DeleteFileA(filename);
759 test_profile_string();
760 test_profile_sections();
761 test_profile_sections_names();
762 test_profile_existing();
763 test_profile_delete_on_close();
764 test_profile_refresh();
765 test_GetPrivateProfileString(
773 test_GetPrivateProfileString(