[PSDK]
[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 ok( WriteFile( h, contents, sizeof contents - 1, &size, NULL ),
452 "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 ok( WriteFile( h, contents1, sizeof contents1 - 1, &size, NULL ),
476 "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 ok( WriteFile( h, contents2, sizeof contents2 - 1, &size, NULL ),
492 "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
505 static void create_test_file(LPCSTR name, LPCSTR data, DWORD size)
506 {
507 HANDLE hfile;
508 DWORD count;
509
510 hfile = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
511 ok(hfile != INVALID_HANDLE_VALUE, "cannot create %s\n", name);
512 WriteFile(hfile, data, size, &count, NULL);
513 CloseHandle(hfile);
514 }
515
516 static BOOL emptystr_ok(CHAR emptystr[MAX_PATH])
517 {
518 int i;
519
520 for(i = 0;i < MAX_PATH;++i)
521 if(emptystr[i] != 0)
522 {
523 trace("emptystr[%d] = %d\n",i,emptystr[i]);
524 return FALSE;
525 }
526
527 return TRUE;
528 }
529
530 static void test_GetPrivateProfileString(const char *content, const char *descript)
531 {
532 DWORD ret, len;
533 CHAR buf[MAX_PATH];
534 CHAR def_val[MAX_PATH];
535 CHAR path[MAX_PATH];
536 CHAR windir[MAX_PATH];
537 /* NT series crashes on r/o empty strings, so pass an r/w
538 empty string and check for modification */
539 CHAR emptystr[MAX_PATH] = "";
540 LPSTR tempfile;
541
542 static const char filename[] = ".\\winetest.ini";
543
544 trace("test_GetPrivateProfileStringA: %s\n", descript);
545
546 if(!lstrcmpA(descript, "CR only"))
547 {
548 SetLastError(0xdeadbeef);
549 ret = GetPrivateProfileStringW(NULL, NULL, NULL,
550 NULL, 0, NULL);
551 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
552 {
553 win_skip("Win9x and WinME don't handle 'CR only' correctly\n");
554 return;
555 }
556 }
557
558 create_test_file(filename, content, lstrlenA(content));
559
560 /* Run this test series with caching. Wine won't cache profile
561 files younger than 2.1 seconds. */
562 Sleep(2500);
563
564 /* lpAppName is NULL */
565 memset(buf, 0xc, sizeof(buf));
566 lstrcpyA(buf, "kumquat");
567 ret = GetPrivateProfileStringA(NULL, "name1", "default",
568 buf, MAX_PATH, filename);
569 ok(ret == 18 ||
570 broken(ret == 19), /* Win9x and WinME */
571 "Expected 18, got %d\n", ret);
572 len = lstrlenA("section1") + sizeof(CHAR) + lstrlenA("section2") + 2 * sizeof(CHAR);
573 ok(!memcmp(buf, "section1\0section2\0\0", len),
574 "Expected \"section1\\0section2\\0\\0\", got \"%s\"\n", buf);
575
576 /* lpAppName is empty */
577 memset(buf, 0xc, sizeof(buf));
578 lstrcpyA(buf, "kumquat");
579 ret = GetPrivateProfileStringA(emptystr, "name1", "default",
580 buf, MAX_PATH, filename);
581 ok(ret == 7, "Expected 7, got %d\n", ret);
582 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
583 ok(emptystr_ok(emptystr), "AppName modified\n");
584
585 /* lpAppName is missing */
586 memset(buf, 0xc,sizeof(buf));
587 lstrcpyA(buf, "kumquat");
588 ret = GetPrivateProfileStringA("notasection", "name1", "default",
589 buf, MAX_PATH, filename);
590 ok(ret == 7, "Expected 7, got %d\n", ret);
591 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
592
593 /* lpAppName is empty, lpDefault is NULL */
594 memset(buf, 0xc,sizeof(buf));
595 lstrcpyA(buf, "kumquat");
596 ret = GetPrivateProfileStringA(emptystr, "name1", NULL,
597 buf, MAX_PATH, filename);
598 ok(ret == 0, "Expected 0, got %d\n", ret);
599 ok(!lstrcmpA(buf, "") ||
600 broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
601 "Expected \"\", got \"%s\"\n", buf);
602 ok(emptystr_ok(emptystr), "AppName modified\n");
603
604 /* lpAppName is empty, lpDefault is empty */
605 memset(buf, 0xc,sizeof(buf));
606 lstrcpyA(buf, "kumquat");
607 ret = GetPrivateProfileStringA(emptystr, "name1", "",
608 buf, MAX_PATH, filename);
609 ok(ret == 0, "Expected 0, got %d\n", ret);
610 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
611 ok(emptystr_ok(emptystr), "AppName modified\n");
612
613 /* lpAppName is empty, lpDefault has trailing blank characters */
614 memset(buf, 0xc,sizeof(buf));
615 lstrcpyA(buf, "kumquat");
616 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
617 lstrcpyA(def_val, "default ");
618 ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
619 buf, MAX_PATH, filename);
620 ok(ret == 7, "Expected 7, got %d\n", ret);
621 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
622 ok(emptystr_ok(emptystr), "AppName modified\n");
623
624 /* lpAppName is empty, many blank characters in lpDefault */
625 memset(buf, 0xc,sizeof(buf));
626 lstrcpyA(buf, "kumquat");
627 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
628 lstrcpyA(def_val, "one two ");
629 ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
630 buf, MAX_PATH, filename);
631 ok(ret == 7, "Expected 7, got %d\n", ret);
632 ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
633 ok(emptystr_ok(emptystr), "AppName modified\n");
634
635 /* lpAppName is empty, blank character but not trailing in lpDefault */
636 memset(buf, 0xc,sizeof(buf));
637 lstrcpyA(buf, "kumquat");
638 ret = GetPrivateProfileStringA(emptystr, "name1", "one two",
639 buf, MAX_PATH, filename);
640 ok(ret == 7, "Expected 7, got %d\n", ret);
641 ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
642 ok(emptystr_ok(emptystr), "AppName modified\n");
643
644 /* lpKeyName is NULL */
645 memset(buf, 0xc,sizeof(buf));
646 lstrcpyA(buf, "kumquat");
647 ret = GetPrivateProfileStringA("section1", NULL, "default",
648 buf, MAX_PATH, filename);
649 ok(ret == 18, "Expected 18, got %d\n", ret);
650 ok(!memcmp(buf, "name1\0name2\0name4\0", ret + 1),
651 "Expected \"name1\\0name2\\0name4\\0\", got \"%s\"\n", buf);
652
653 /* lpKeyName is empty */
654 memset(buf, 0xc,sizeof(buf));
655 lstrcpyA(buf, "kumquat");
656 ret = GetPrivateProfileStringA("section1", emptystr, "default",
657 buf, MAX_PATH, filename);
658 ok(ret == 7, "Expected 7, got %d\n", ret);
659 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
660 ok(emptystr_ok(emptystr), "KeyName modified\n");
661
662 /* lpKeyName is missing */
663 memset(buf, 0xc,sizeof(buf));
664 lstrcpyA(buf, "kumquat");
665 ret = GetPrivateProfileStringA("section1", "notakey", "default",
666 buf, MAX_PATH, filename);
667 ok(ret == 7, "Expected 7, got %d\n", ret);
668 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
669
670 /* lpKeyName is empty, lpDefault is NULL */
671 memset(buf, 0xc,sizeof(buf));
672 lstrcpyA(buf, "kumquat");
673 ret = GetPrivateProfileStringA("section1", emptystr, NULL,
674 buf, MAX_PATH, filename);
675 ok(ret == 0, "Expected 0, got %d\n", ret);
676 ok(!lstrcmpA(buf, "") ||
677 broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
678 "Expected \"\", got \"%s\"\n", buf);
679 ok(emptystr_ok(emptystr), "KeyName modified\n");
680
681 /* lpKeyName is empty, lpDefault is empty */
682 memset(buf, 0xc,sizeof(buf));
683 lstrcpyA(buf, "kumquat");
684 ret = GetPrivateProfileStringA("section1", emptystr, "",
685 buf, MAX_PATH, filename);
686 ok(ret == 0, "Expected 0, got %d\n", ret);
687 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
688 ok(emptystr_ok(emptystr), "KeyName modified\n");
689
690 /* lpKeyName is empty, lpDefault has trailing blank characters */
691 memset(buf, 0xc,sizeof(buf));
692 lstrcpyA(buf, "kumquat");
693 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
694 lstrcpyA(def_val, "default ");
695 ret = GetPrivateProfileStringA("section1", emptystr, def_val,
696 buf, MAX_PATH, filename);
697 ok(ret == 7, "Expected 7, got %d\n", ret);
698 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
699 ok(emptystr_ok(emptystr), "KeyName modified\n");
700
701 if (0) /* crashes */
702 {
703 /* lpReturnedString is NULL */
704 ret = GetPrivateProfileStringA("section1", "name1", "default",
705 NULL, MAX_PATH, filename);
706 }
707
708 /* lpFileName is NULL */
709 memset(buf, 0xc,sizeof(buf));
710 lstrcpyA(buf, "kumquat");
711 ret = GetPrivateProfileStringA("section1", "name1", "default",
712 buf, MAX_PATH, NULL);
713 ok(ret == 7 ||
714 broken(ret == 0), /* Win9x, WinME */
715 "Expected 7, got %d\n", ret);
716 ok(!lstrcmpA(buf, "default") ||
717 broken(!lstrcmpA(buf, "kumquat")), /* Win9x, WinME */
718 "Expected \"default\", got \"%s\"\n", buf);
719
720 /* lpFileName is empty */
721 memset(buf, 0xc,sizeof(buf));
722 lstrcpyA(buf, "kumquat");
723 ret = GetPrivateProfileStringA("section1", "name1", "default",
724 buf, MAX_PATH, "");
725 ok(ret == 7, "Expected 7, got %d\n", ret);
726 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
727
728 /* lpFileName is nonexistent */
729 memset(buf, 0xc,sizeof(buf));
730 lstrcpyA(buf, "kumquat");
731 ret = GetPrivateProfileStringA("section1", "name1", "default",
732 buf, MAX_PATH, "nonexistent");
733 ok(ret == 7, "Expected 7, got %d\n", ret);
734 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
735
736 /* nSize is 0 */
737 memset(buf, 0xc,sizeof(buf));
738 lstrcpyA(buf, "kumquat");
739 ret = GetPrivateProfileStringA("section1", "name1", "default",
740 buf, 0, filename);
741 ok(ret == 0, "Expected 0, got %d\n", ret);
742 ok(!lstrcmpA(buf, "kumquat"), "Expected buf to be unchanged, got \"%s\"\n", buf);
743
744 /* nSize is exact size of output */
745 memset(buf, 0xc,sizeof(buf));
746 lstrcpyA(buf, "kumquat");
747 ret = GetPrivateProfileStringA("section1", "name1", "default",
748 buf, 4, filename);
749 ok(ret == 3, "Expected 3, got %d\n", ret);
750 ok(!lstrcmpA(buf, "val"), "Expected \"val\", got \"%s\"\n", buf);
751
752 /* nSize has room for NULL terminator */
753 memset(buf, 0xc,sizeof(buf));
754 lstrcpyA(buf, "kumquat");
755 ret = GetPrivateProfileStringA("section1", "name1", "default",
756 buf, 5, filename);
757 ok(ret == 4, "Expected 4, got %d\n", ret);
758 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
759
760 /* output is 1 character */
761 memset(buf, 0xc,sizeof(buf));
762 lstrcpyA(buf, "kumquat");
763 ret = GetPrivateProfileStringA("section1", "name4", "default",
764 buf, MAX_PATH, filename);
765 ok(ret == 1, "Expected 1, got %d\n", ret);
766 ok(!lstrcmpA(buf, "a"), "Expected \"a\", got \"%s\"\n", buf);
767
768 /* output is 1 character, no room for NULL terminator */
769 memset(buf, 0xc,sizeof(buf));
770 lstrcpyA(buf, "kumquat");
771 ret = GetPrivateProfileStringA("section1", "name4", "default",
772 buf, 1, filename);
773 ok(ret == 0, "Expected 0, got %d\n", ret);
774 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
775
776 /* lpAppName is NULL, not enough room for final section name */
777 memset(buf, 0xc,sizeof(buf));
778 lstrcpyA(buf, "kumquat");
779 ret = GetPrivateProfileStringA(NULL, "name1", "default",
780 buf, 16, filename);
781 ok(ret == 14, "Expected 14, got %d\n", ret);
782 len = lstrlenA("section1") + 2 * sizeof(CHAR);
783 todo_wine
784 ok(!memcmp(buf, "section1\0secti\0\0", ret + 2) ||
785 broken(!memcmp(buf, "section1\0\0", len)), /* Win9x, WinME */
786 "Expected \"section1\\0secti\\0\\0\", got \"%s\"\n", buf);
787
788 /* lpKeyName is NULL, not enough room for final key name */
789 memset(buf, 0xc,sizeof(buf));
790 lstrcpyA(buf, "kumquat");
791 ret = GetPrivateProfileStringA("section1", NULL, "default",
792 buf, 16, filename);
793 ok(ret == 14, "Expected 14, got %d\n", ret);
794 todo_wine
795 ok(!memcmp(buf, "name1\0name2\0na\0\0", ret + 2) ||
796 broken(!memcmp(buf, "name1\0name2\0n\0\0", ret + 1)), /* Win9x, WinME */
797 "Expected \"name1\\0name2\\0na\\0\\0\", got \"%s\"\n", buf);
798
799 /* key value has quotation marks which are stripped */
800 memset(buf, 0xc,sizeof(buf));
801 lstrcpyA(buf, "kumquat");
802 ret = GetPrivateProfileStringA("section1", "name2", "default",
803 buf, MAX_PATH, filename);
804 ok(ret == 4, "Expected 4, got %d\n", ret);
805 ok(!lstrcmpA(buf, "val2"), "Expected \"val2\", got \"%s\"\n", buf);
806
807 /* case does not match */
808 memset(buf, 0xc,sizeof(buf));
809 lstrcpyA(buf, "kumquat");
810 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
811 buf, MAX_PATH, filename);
812 ok(ret == 4, "Expected 4, got %d\n", ret);
813 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
814
815 /* only filename is used */
816 memset(buf, 0xc,sizeof(buf));
817 lstrcpyA(buf, "kumquat");
818 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
819 buf, MAX_PATH, "winetest.ini");
820 ok(ret == 7, "Expected 7, got %d\n", ret);
821 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
822
823 GetWindowsDirectoryA(windir, MAX_PATH);
824 SetLastError(0xdeadbeef);
825 ret = GetTempFileNameA(windir, "pre", 0, path);
826 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
827 {
828 skip("Not allowed to create a file in the Windows directory\n");
829 DeleteFileA(filename);
830 return;
831 }
832 tempfile = strrchr(path, '\\') + 1;
833 create_test_file(path, content, lstrlenA(content));
834
835 /* only filename is used, file exists in windows directory */
836 memset(buf, 0xc,sizeof(buf));
837 lstrcpyA(buf, "kumquat");
838 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
839 buf, MAX_PATH, tempfile);
840 ok(ret == 4, "Expected 4, got %d\n", ret);
841 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
842
843 /* successful case */
844 memset(buf, 0xc,sizeof(buf));
845 lstrcpyA(buf, "kumquat");
846 ret = GetPrivateProfileStringA("section1", "name1", "default",
847 buf, MAX_PATH, filename);
848 ok(ret == 4, "Expected 4, got %d\n", ret);
849 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
850
851 /* Existing section with no keys in an existing file */
852 memset(buf, 0xc,sizeof(buf));
853 SetLastError(0xdeadbeef);
854 ret=GetPrivateProfileStringA("section2", "DoesntExist", "",
855 buf, MAX_PATH, filename);
856 ok( ret == 0, "expected return size 0, got %d\n", ret );
857 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
858 todo_wine
859 ok( GetLastError() == 0xdeadbeef ||
860 GetLastError() == ERROR_FILE_NOT_FOUND /* Win 7 */,
861 "expected 0xdeadbeef or ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
862
863
864 DeleteFileA(path);
865 DeleteFileA(filename);
866 }
867
868 static BOOL check_binary_file_data(LPCSTR path, const VOID *data, DWORD size)
869 {
870 HANDLE file;
871 CHAR buf[MAX_PATH];
872 BOOL ret;
873
874 file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
875 if (file == INVALID_HANDLE_VALUE)
876 return FALSE;
877
878 if(size != GetFileSize(file, NULL) )
879 {
880 CloseHandle(file);
881 return FALSE;
882 }
883
884 ret = ReadFile(file, buf, size, &size, NULL);
885 CloseHandle(file);
886 if (!ret)
887 return FALSE;
888
889 return !memcmp(buf, data, size);
890 }
891
892 static BOOL check_file_data(LPCSTR path, LPCSTR data)
893 {
894 return check_binary_file_data(path, data, lstrlenA(data));
895 }
896
897 static void test_WritePrivateProfileString(void)
898 {
899 BOOL ret;
900 LPCSTR data;
901 CHAR path[MAX_PATH];
902 CHAR temp[MAX_PATH];
903
904 SetLastError(0xdeadbeef);
905 ret = WritePrivateProfileStringW(NULL, NULL, NULL, NULL);
906 if (!ret && GetLastError() == ERROR_CALL_NOT_IMPLEMENTED)
907 {
908 /* Win9x/WinME needs (variable) timeouts between tests and even long timeouts don't
909 * guarantee a correct result.
910 * Win9x/WinMe also produces different ini files where there is always a newline before
911 * a section start (except for the first one).
912 */
913 win_skip("WritePrivateProfileString on Win9x/WinME is hard to test reliably\n");
914 return;
915 }
916
917 GetTempPathA(MAX_PATH, temp);
918 GetTempFileNameA(temp, "wine", 0, path);
919 DeleteFileA(path);
920
921 /* path is not created yet */
922
923 /* NULL lpAppName */
924 SetLastError(0xdeadbeef);
925 ret = WritePrivateProfileStringA(NULL, "key", "string", path);
926 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
927 ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
928 broken(GetLastError() == ERROR_INVALID_PARAMETER) || /* NT4 */
929 broken(GetLastError() == 0xdeadbeef), /* Win9x and WinME */
930 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
931 ok(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES,
932 "Expected path to not exist\n");
933
934 GetTempFileNameA(temp, "wine", 0, path);
935
936 /* NULL lpAppName, path exists */
937 data = "";
938 SetLastError(0xdeadbeef);
939 ret = WritePrivateProfileStringA(NULL, "key", "string", path);
940 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
941 ok(GetLastError() == ERROR_FILE_NOT_FOUND ||
942 broken(GetLastError() == ERROR_INVALID_PARAMETER) || /* NT4 */
943 broken(GetLastError() == 0xdeadbeef), /* Win9x and WinME */
944 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
945 ok(check_file_data(path, data), "File doesn't match\n");
946 DeleteFileA(path);
947
948 if (0)
949 {
950 /* empty lpAppName, crashes on NT4 and higher */
951 data = "[]\r\n"
952 "key=string\r\n";
953 ret = WritePrivateProfileStringA("", "key", "string", path);
954 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
955 ok(check_file_data(path, data), "File doesn't match\n");
956 DeleteFileA(path);
957 }
958
959 /* NULL lpKeyName */
960 data = "";
961 ret = WritePrivateProfileStringA("App", NULL, "string", path);
962 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
963 todo_wine
964 {
965 ok(check_file_data(path, data), "File doesn't match\n");
966 }
967 DeleteFileA(path);
968
969 if (0)
970 {
971 /* empty lpKeyName, crashes on NT4 and higher */
972 data = "[App]\r\n"
973 "=string\r\n";
974 ret = WritePrivateProfileStringA("App", "", "string", path);
975 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
976 todo_wine
977 {
978 ok(check_file_data(path, data), "File doesn't match\n");
979 }
980 DeleteFileA(path);
981 }
982
983 /* NULL lpString */
984 data = "";
985 ret = WritePrivateProfileStringA("App", "key", NULL, path);
986 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
987 todo_wine
988 {
989 ok(check_file_data(path, data) ||
990 (broken(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)), /* Win9x and WinME */
991 "File doesn't match\n");
992 }
993 DeleteFileA(path);
994
995 /* empty lpString */
996 data = "[App]\r\n"
997 "key=\r\n";
998 ret = WritePrivateProfileStringA("App", "key", "", path);
999 ok(ret == TRUE ||
1000 broken(!ret), /* Win9x and WinME */
1001 "Expected TRUE, got %d\n", ret);
1002 ok(check_file_data(path, data) ||
1003 (broken(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES)), /* Win9x and WinME */
1004 "File doesn't match\n");
1005 DeleteFileA(path);
1006
1007 /* empty lpFileName */
1008 SetLastError(0xdeadbeef);
1009 ret = WritePrivateProfileStringA("App", "key", "string", "");
1010 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1011 ok(GetLastError() == ERROR_ACCESS_DENIED ||
1012 broken(GetLastError() == ERROR_PATH_NOT_FOUND), /* Win9x and WinME */
1013 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
1014
1015 /* The resulting file will be X:\\%WINDIR%\\win1.tmp */
1016 GetWindowsDirectoryA(temp, MAX_PATH);
1017 GetTempFileNameA(temp, "win", 1, path);
1018 DeleteFileA(path);
1019
1020 /* relative path in lpFileName */
1021 data = "[App]\r\n"
1022 "key=string\r\n";
1023 ret = WritePrivateProfileStringA("App", "key", "string", "win1.tmp");
1024 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1025 ok(check_file_data(path, data), "File doesn't match\n");
1026 DeleteFileA(path);
1027
1028 GetTempPathA(MAX_PATH, temp);
1029 GetTempFileNameA(temp, "wine", 0, path);
1030
1031 /* build up an INI file */
1032 WritePrivateProfileStringA("App1", "key1", "string1", path);
1033 WritePrivateProfileStringA("App1", "key2", "string2", path);
1034 WritePrivateProfileStringA("App1", "key3", "string3", path);
1035 WritePrivateProfileStringA("App2", "key4", "string4", path);
1036
1037 /* make an addition and verify the INI */
1038 data = "[App1]\r\n"
1039 "key1=string1\r\n"
1040 "key2=string2\r\n"
1041 "key3=string3\r\n"
1042 "[App2]\r\n"
1043 "key4=string4\r\n"
1044 "[App3]\r\n"
1045 "key5=string5\r\n";
1046 ret = WritePrivateProfileStringA("App3", "key5", "string5", path);
1047 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1048 ok(check_file_data(path, data), "File doesn't match\n");
1049
1050 /* lpString is NULL, key2 key is deleted */
1051 data = "[App1]\r\n"
1052 "key1=string1\r\n"
1053 "key3=string3\r\n"
1054 "[App2]\r\n"
1055 "key4=string4\r\n"
1056 "[App3]\r\n"
1057 "key5=string5\r\n";
1058 ret = WritePrivateProfileStringA("App1", "key2", NULL, path);
1059 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1060 ok(check_file_data(path, data), "File doesn't match\n");
1061
1062 /* try to delete key2 again */
1063 data = "[App1]\r\n"
1064 "key1=string1\r\n"
1065 "key3=string3\r\n"
1066 "[App2]\r\n"
1067 "key4=string4\r\n"
1068 "[App3]\r\n"
1069 "key5=string5\r\n";
1070 ret = WritePrivateProfileStringA("App1", "key2", NULL, path);
1071 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1072 ok(check_file_data(path, data), "File doesn't match\n");
1073
1074 /* lpKeyName is NULL, App1 section is deleted */
1075 data = "[App2]\r\n"
1076 "key4=string4\r\n"
1077 "[App3]\r\n"
1078 "key5=string5\r\n";
1079 ret = WritePrivateProfileStringA("App1", NULL, "string1", path);
1080 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1081 ok(check_file_data(path, data), "File doesn't match\n");
1082
1083 /* lpString is not needed to delete a section */
1084 data = "[App3]\r\n"
1085 "key5=string5\r\n";
1086 ret = WritePrivateProfileStringA("App2", NULL, NULL, 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 /* leave just the section */
1091 data = "[App3]\r\n";
1092 ret = WritePrivateProfileStringA("App3", "key5", NULL, path);
1093 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1094 ok(check_file_data(path, data), "File doesn't match\n");
1095 DeleteFileA(path);
1096
1097 /* NULLs in file before first section. Should be preserved in output */
1098 data = "Data \0 before \0 first \0 section" /* 31 bytes */
1099 "\r\n[section1]\r\n" /* 14 bytes */
1100 "key1=string1\r\n"; /* 14 bytes */
1101 GetTempFileNameA(temp, "wine", 0, path);
1102 create_test_file(path, data, 31);
1103 ret = WritePrivateProfileStringA("section1", "key1", "string1", path);
1104 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
1105 todo_wine
1106 ok( check_binary_file_data(path, data, 59) ||
1107 broken( check_binary_file_data(path, /* Windows 9x */
1108 "Data \0 before \0 first \0 section" /* 31 bytes */
1109 "\r\n\r\n[section1]\r\n" /* 14 bytes */
1110 "key1=string1" /* 14 bytes */
1111 , 59)), "File doesn't match\n");
1112 DeleteFileA(path);
1113 }
1114
1115 START_TEST(profile)
1116 {
1117 test_profile_int();
1118 test_profile_string();
1119 test_profile_sections();
1120 test_profile_sections_names();
1121 test_profile_existing();
1122 test_profile_delete_on_close();
1123 test_profile_refresh();
1124 test_GetPrivateProfileString(
1125 "[section1]\r\n"
1126 "name1=val1\r\n"
1127 "name2=\"val2\"\r\n"
1128 "name3\r\n"
1129 "name4=a\r\n"
1130 "[section2]\r\n",
1131 "CR+LF");
1132 test_GetPrivateProfileString(
1133 "[section1]\r"
1134 "name1=val1\r"
1135 "name2=\"val2\"\r"
1136 "name3\r"
1137 "name4=a\r"
1138 "[section2]\r",
1139 "CR only");
1140 test_WritePrivateProfileString();
1141 }