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