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