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