kernel32/tests/profile: Enable compilation with long types.
[wine.git] / dlls / kernel32 / tests / profile.c
blob08e419544cc035c5fb3d2c50fd43a2e6fb171b07
1 /*
2 * Unit tests for profile functions
4 * Copyright (c) 2003 Stefan Leichter
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #undef WINE_NO_LONG_TYPES /* temporary for migration */
22 #include <stdarg.h>
23 #include <stdio.h>
25 #include "wine/test.h"
26 #include "windef.h"
27 #include "winbase.h"
28 #include "windows.h"
29 #include "sddl.h"
31 #define KEY "ProfileInt"
32 #define SECTION "Test"
33 #define TESTFILE ".\\testwine.ini"
34 #define TESTFILE2 ".\\testwine2.ini"
36 static void check_profile_string_(int line, const char *section, const char *name, const char *file, const char *expect)
38 char value[200] = {0};
39 DWORD ret = GetPrivateProfileStringA(section, name, "default", value, sizeof(value), file);
40 ok_(__FILE__, line)(ret == strlen(expect), "expected len %Iu, got %lu\n", strlen(expect), ret);
41 ok_(__FILE__, line)(!strcmp(value, expect), "expected %s, got %s\n", debugstr_a(expect), debugstr_a(value));
43 #define check_profile_string(a, b, c, d) check_profile_string_(__LINE__, a, b, c, d);
45 struct _profileInt {
46 LPCSTR section;
47 LPCSTR key;
48 LPCSTR value;
49 LPCSTR iniFile;
50 INT defaultVal;
51 UINT result;
54 static void test_profile_int(void)
56 struct _profileInt profileInt[]={
57 { NULL, NULL, NULL, NULL, 70, 0}, /* 0 */
58 { NULL, NULL, NULL, TESTFILE, -1, 4294967295U},
59 { NULL, NULL, NULL, TESTFILE, 1, 1},
60 { SECTION, NULL, NULL, TESTFILE, -1, 4294967295U},
61 { SECTION, NULL, NULL, TESTFILE, 1, 1},
62 { NULL, KEY, NULL, TESTFILE, -1, 4294967295U}, /* 5 */
63 { NULL, KEY, NULL, TESTFILE, 1, 1},
64 { SECTION, KEY, NULL, TESTFILE, -1, 4294967295U},
65 { SECTION, KEY, NULL, TESTFILE, 1, 1},
66 { SECTION, KEY, "-1", TESTFILE, -1, 4294967295U},
67 { SECTION, KEY, "-1", TESTFILE, 1, 4294967295U}, /* 10 */
68 { SECTION, KEY, "1", TESTFILE, -1, 1},
69 { SECTION, KEY, "1", TESTFILE, 1, 1},
70 { SECTION, KEY, "+1", TESTFILE, -1, 1},
71 { SECTION, KEY, "+1", TESTFILE, 1, 1},
72 { SECTION, KEY, "4294967296", TESTFILE, -1, 0}, /* 15 */
73 { SECTION, KEY, "4294967296", TESTFILE, 1, 0},
74 { SECTION, KEY, "4294967297", TESTFILE, -1, 1},
75 { SECTION, KEY, "4294967297", TESTFILE, 1, 1},
76 { SECTION, KEY, "-4294967297", TESTFILE, -1, 4294967295U},
77 { SECTION, KEY, "-4294967297", TESTFILE, 1, 4294967295U}, /* 20 */
78 { SECTION, KEY, "42A94967297", TESTFILE, -1, 42},
79 { SECTION, KEY, "42A94967297", TESTFILE, 1, 42},
80 { SECTION, KEY, "B4294967297", TESTFILE, -1, 0},
81 { SECTION, KEY, "B4294967297", TESTFILE, 1, 0},
83 int i, num_test = ARRAY_SIZE(profileInt);
84 UINT res;
86 DeleteFileA( TESTFILE);
88 for (i=0; i < num_test; i++) {
89 if (profileInt[i].value)
90 WritePrivateProfileStringA(SECTION, KEY, profileInt[i].value,
91 profileInt[i].iniFile);
93 res = GetPrivateProfileIntA(profileInt[i].section, profileInt[i].key,
94 profileInt[i].defaultVal, profileInt[i].iniFile);
95 ok((res == profileInt[i].result), "test<%02d>: ret<%010u> exp<%010u>\n",
96 i, res, profileInt[i].result);
99 DeleteFileA( TESTFILE);
102 static void test_profile_string(void)
104 static WCHAR emptyW[] = { 0 }; /* if "const", GetPrivateProfileStringW(emptyW, ...) crashes on win2k */
105 static const WCHAR keyW[] = { 'k','e','y',0 };
106 static const WCHAR sW[] = { 's',0 };
107 static const WCHAR TESTFILE2W[] = {'.','\\','t','e','s','t','w','i','n','e','2','.','i','n','i',0};
108 static const WCHAR valsectionW[] = {'v','a','l','_','e','_','s','e','c','t','i','o','n',0 };
109 static const WCHAR valnokeyW[] = {'v','a','l','_','n','o','_','k','e','y',0};
110 HANDLE h;
111 int ret;
112 DWORD count;
113 char buf[100];
114 WCHAR bufW[100];
115 char *p;
116 /* test that lines without an '=' will not be enumerated */
117 /* in the case below, name2 is a key while name3 is not. */
118 char content[]="[s]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n";
119 char content2[]="\r\nkey=val_no_section\r\n[]\r\nkey=val_e_section\r\n"
120 "[s]\r\n=val_no_key\r\n[t]\r\n";
121 DeleteFileA( TESTFILE2);
122 h = CreateFileA( TESTFILE2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
123 FILE_ATTRIBUTE_NORMAL, NULL);
124 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", TESTFILE2);
125 if( h == INVALID_HANDLE_VALUE) return;
126 WriteFile( h, content, sizeof(content), &count, NULL);
127 CloseHandle( h);
129 /* enumerate the keys */
130 ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
131 TESTFILE2);
132 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
133 p[-1] = ',';
134 /* and test */
135 ok( ret == 18 && !strcmp( buf, "name1,name2,name4"), "wrong keys returned(%d): %s\n", ret,
136 buf);
138 /* add a new key to test that the file is quite usable */
139 WritePrivateProfileStringA( "s", "name5", "val5", TESTFILE2);
140 ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
141 TESTFILE2);
142 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
143 p[-1] = ',';
144 ok( ret == 24 && !strcmp( buf, "name1,name2,name4,name5"), "wrong keys returned(%d): %s\n",
145 ret, buf);
147 h = CreateFileA( TESTFILE2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
148 FILE_ATTRIBUTE_NORMAL, NULL);
149 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", TESTFILE2);
150 if( h == INVALID_HANDLE_VALUE) return;
151 WriteFile( h, content2, sizeof(content2), &count, NULL);
152 CloseHandle( h);
154 /* works only in unicode, ansi crashes */
155 ret=GetPrivateProfileStringW(emptyW, keyW, emptyW, bufW, ARRAY_SIZE(bufW), TESTFILE2W);
156 todo_wine
157 ok(ret == 13, "expected 13, got %u\n", ret);
158 todo_wine
159 ok(!lstrcmpW(valsectionW,bufW), "expected %s, got %s\n",
160 wine_dbgstr_w(valsectionW), wine_dbgstr_w(bufW) );
162 /* works only in unicode, ansi crashes */
163 ret=GetPrivateProfileStringW(sW, emptyW, emptyW, bufW, ARRAY_SIZE(bufW), TESTFILE2W);
164 ok(ret == 10, "expected 10, got %u\n", ret);
165 ok(!lstrcmpW(valnokeyW,bufW), "expected %s, got %s\n",
166 wine_dbgstr_w(valnokeyW), wine_dbgstr_w(bufW) );
168 DeleteFileA( TESTFILE2);
171 static void test_profile_sections(void)
173 HANDLE h;
174 int ret;
175 DWORD count;
176 char buf[100];
177 char *p;
178 static const char content[]="[section1]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n[section2]\r\n[section3]\r\n=val5\r\n";
179 static const char testfile4[]=".\\testwine4.ini";
180 BOOL on_win98 = FALSE;
182 DeleteFileA( testfile4 );
183 h = CreateFileA( testfile4, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
184 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile4);
185 if( h == INVALID_HANDLE_VALUE) return;
186 WriteFile( h, content, sizeof(content), &count, NULL);
187 CloseHandle( h);
189 /* Some parameter checking */
190 SetLastError(0xdeadbeef);
191 ret = GetPrivateProfileSectionA( NULL, NULL, 0, NULL );
192 ok( ret == 0, "expected return size 0, got %d\n", ret );
193 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
194 GetLastError() == 0xdeadbeef /* Win98 */,
195 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
196 if (GetLastError() == 0xdeadbeef) on_win98 = TRUE;
198 SetLastError(0xdeadbeef);
199 ret = GetPrivateProfileSectionA( NULL, NULL, 0, testfile4 );
200 ok( ret == 0, "expected return size 0, got %d\n", ret );
201 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
202 GetLastError() == 0xdeadbeef /* Win98 */,
203 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
205 if (!on_win98)
207 SetLastError(0xdeadbeef);
208 ret = GetPrivateProfileSectionA( "section1", NULL, 0, testfile4 );
209 ok( ret == 0, "expected return size 0, got %d\n", ret );
210 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
213 SetLastError(0xdeadbeef);
214 ret = GetPrivateProfileSectionA( NULL, buf, sizeof(buf), testfile4 );
215 ok( ret == 0, "expected return size 0, got %d\n", ret );
216 ok( GetLastError() == ERROR_INVALID_PARAMETER,
217 "expected ERROR_INVALID_PARAMETER, got %ld\n", GetLastError());
219 SetLastError(0xdeadbeef);
220 ret = GetPrivateProfileSectionA( "section1", buf, sizeof(buf), NULL );
221 ok( ret == 0, "expected return size 0, got %d\n", ret );
222 todo_wine
223 ok( GetLastError() == ERROR_FILE_NOT_FOUND,
224 "expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
226 /* Existing empty section with no keys */
227 SetLastError(0xdeadbeef);
228 ret=GetPrivateProfileSectionA("section2", buf, sizeof(buf), testfile4);
229 ok( ret == 0, "expected return size 0, got %d\n", ret );
230 ok( GetLastError() == ERROR_SUCCESS,
231 "expected ERROR_SUCCESS, got %ld\n", GetLastError());
233 /* Existing section with keys and values*/
234 SetLastError(0xdeadbeef);
235 ret=GetPrivateProfileSectionA("section1", buf, sizeof(buf), testfile4);
236 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
237 p[-1] = ',';
238 ok( ret == 35 && !strcmp( buf, "name1=val1,name2=,name3,name4=val4"), "wrong section returned(%d): %s\n",
239 ret, buf);
240 ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
241 ok( GetLastError() == ERROR_SUCCESS,
242 "expected ERROR_SUCCESS, got %ld\n", GetLastError());
244 /* Existing section with no keys but has values */
245 SetLastError(0xdeadbeef);
246 ret=GetPrivateProfileSectionA("section3", buf, sizeof(buf), testfile4);
247 trace("section3 return: %s\n", buf);
248 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
249 p[-1] = ',';
250 ok( ret == 6 && !strcmp( buf, "=val5"), "wrong section returned(%d): %s\n",
251 ret, buf);
252 ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
253 ok( GetLastError() == ERROR_SUCCESS,
254 "expected ERROR_SUCCESS, got %ld\n", GetLastError());
256 /* Overflow*/
257 ret=GetPrivateProfileSectionA("section1", buf, 24, testfile4);
258 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
259 p[-1] = ',';
260 ok( ret == 22 && !strcmp( buf, "name1=val1,name2=,name"), "wrong section returned(%d): %s\n",
261 ret, buf);
262 ok( buf[ret] == 0 && buf[ret+1] == 0, "returned buffer not terminated with double-null\n" );
264 DeleteFileA( testfile4 );
267 static void test_profile_sections_names(void)
269 HANDLE h;
270 int ret;
271 DWORD count;
272 char buf[100];
273 WCHAR bufW[100];
274 static const char content[]="[section1]\r\n[section2]\r\n[section3]\r\n";
275 static const char testfile3[]=".\\testwine3.ini";
276 static const WCHAR testfile3W[]={ '.','\\','t','e','s','t','w','i','n','e','3','.','i','n','i',0 };
277 static const WCHAR not_here[] = {'.','\\','n','o','t','_','h','e','r','e','.','i','n','i',0};
278 DeleteFileA( testfile3 );
279 h = CreateFileA( testfile3, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
280 FILE_ATTRIBUTE_NORMAL, NULL);
281 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile3);
282 if( h == INVALID_HANDLE_VALUE) return;
283 WriteFile( h, content, sizeof(content), &count, NULL);
284 CloseHandle( h);
286 /* Test with sufficiently large buffer */
287 memset(buf, 0xc, sizeof(buf));
288 ret = GetPrivateProfileSectionNamesA( buf, 29, testfile3 );
289 ok( ret == 27, "expected return size 27, got %d\n", ret );
290 ok( (buf[ret-1] == 0 && buf[ret] == 0),
291 "returned buffer not terminated with double-null\n" );
293 /* Test with exactly fitting buffer */
294 memset(buf, 0xc, sizeof(buf));
295 ret = GetPrivateProfileSectionNamesA( buf, 28, testfile3 );
296 ok( ret == 26, "expected return size 26, got %d\n", ret );
297 todo_wine
298 ok( (buf[ret+1] == 0 && buf[ret] == 0) || /* W2K3 and higher */
299 broken(buf[ret+1] == 0xc && buf[ret] == 0), /* NT4, W2K, WinXP */
300 "returned buffer not terminated with double-null\n" );
302 /* Test with a buffer too small */
303 memset(buf, 0xc, sizeof(buf));
304 ret = GetPrivateProfileSectionNamesA( buf, 27, testfile3 );
305 ok( ret == 25, "expected return size 25, got %d\n", ret );
306 count = strlen("section1") + sizeof(CHAR) + strlen("section2");
307 todo_wine
308 ok( buf[ret+1] == 0 && buf[ret] == 0,
309 "returned buffer not terminated with double-null\n" );
311 /* Tests on nonexistent file */
312 memset(buf, 0xc, sizeof(buf));
313 ret = GetPrivateProfileSectionNamesA( buf, 10, ".\\not_here.ini" );
314 ok( ret == 0, "expected return size 0, got %d\n", ret );
315 ok( buf[0] == 0, "returned buffer not terminated with null\n" );
316 ok( buf[1] != 0, "returned buffer terminated with double-null\n" );
318 /* Test with sufficiently large buffer */
319 SetLastError(0xdeadbeef);
320 memset(bufW, 0xcc, sizeof(bufW));
321 ret = GetPrivateProfileSectionNamesW( bufW, 29, testfile3W );
322 if (ret == 0 && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
324 win_skip("GetPrivateProfileSectionNamesW is not implemented\n");
325 DeleteFileA( testfile3 );
326 return;
328 ok( ret == 27, "expected return size 27, got %d\n", ret );
329 ok( bufW[ret-1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
331 /* Test with exactly fitting buffer */
332 memset(bufW, 0xcc, sizeof(bufW));
333 ret = GetPrivateProfileSectionNamesW( bufW, 28, testfile3W );
334 ok( ret == 26, "expected return size 26, got %d\n", ret );
335 ok( (bufW[ret+1] == 0 && bufW[ret] == 0) || /* W2K3 and higher */
336 broken(bufW[ret+1] == 0xcccc && bufW[ret] == 0), /* NT4, W2K, WinXP */
337 "returned buffer not terminated with double-null\n" );
339 /* Test with a buffer too small */
340 memset(bufW, 0xcc, sizeof(bufW));
341 ret = GetPrivateProfileSectionNamesW( bufW, 27, testfile3W );
342 ok( ret == 25, "expected return size 25, got %d\n", ret );
343 ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
345 DeleteFileA( testfile3 );
347 /* Tests on nonexistent file */
348 memset(bufW, 0xcc, sizeof(bufW));
349 ret = GetPrivateProfileSectionNamesW( bufW, 10, not_here );
350 ok( ret == 0, "expected return size 0, got %d\n", ret );
351 ok( bufW[0] == 0, "returned buffer not terminated with null\n" );
352 ok( bufW[1] != 0, "returned buffer terminated with double-null\n" );
355 /* If the ini-file has already been opened with CreateFile, WritePrivateProfileString failed in wine with an error ERROR_SHARING_VIOLATION, some testing here */
356 static void test_profile_existing(void)
358 static const char *testfile1 = ".\\winesharing1.ini";
359 static const char *testfile2 = ".\\winesharing2.ini";
361 static const struct {
362 DWORD dwDesiredAccess;
363 DWORD dwShareMode;
364 DWORD write_error;
365 BOOL read_error;
366 DWORD broken_error;
367 } pe[] = {
368 {GENERIC_READ, FILE_SHARE_READ, ERROR_SHARING_VIOLATION, FALSE },
369 {GENERIC_READ, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
370 {GENERIC_WRITE, FILE_SHARE_READ, ERROR_SHARING_VIOLATION, FALSE },
371 {GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
372 {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, ERROR_SHARING_VIOLATION, FALSE },
373 {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_WRITE, ERROR_SHARING_VIOLATION, TRUE },
374 {GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
375 {GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */},
376 /*Thief demo (bug 5024) opens .ini file like this*/
377 {GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, 0, FALSE, ERROR_SHARING_VIOLATION /* nt4 */}
380 int i;
381 BOOL ret;
382 DWORD size;
383 HANDLE h = 0;
384 char buffer[MAX_PATH];
386 for (i=0; i < ARRAY_SIZE(pe); i++)
388 h = CreateFileA(testfile1, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
389 CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
390 ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
391 SetLastError(0xdeadbeef);
393 ret = WritePrivateProfileStringA(SECTION, KEY, "12345", testfile1);
394 if (!pe[i].write_error)
396 if (!ret)
397 ok( broken(GetLastError() == pe[i].broken_error),
398 "%d: WritePrivateProfileString failed with error %lu\n", i, GetLastError() );
399 CloseHandle(h);
400 size = GetPrivateProfileStringA(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
401 if (ret)
402 ok( size == 5, "%d: test failed, number of characters copied: %ld instead of 5\n", i, size );
403 else
404 ok( !size, "%d: test failed, number of characters copied: %ld instead of 0\n", i, size );
406 else
408 DWORD err = GetLastError();
409 ok( !ret, "%d: WritePrivateProfileString succeeded\n", i );
410 if (!ret)
411 ok( err == pe[i].write_error, "%d: WritePrivateProfileString failed with error %lu/%lu\n",
412 i, err, pe[i].write_error );
413 CloseHandle(h);
414 size = GetPrivateProfileStringA(SECTION, KEY, 0, buffer, MAX_PATH, testfile1);
415 ok( !size, "%d: test failed, number of characters copied: %ld instead of 0\n", i, size );
418 ok( DeleteFileA(testfile1), "delete failed\n" );
421 h = CreateFileA(testfile2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
422 sprintf( buffer, "[%s]\r\n%s=123\r\n", SECTION, KEY );
423 ok( WriteFile( h, buffer, strlen(buffer), &size, NULL ), "failed to write\n" );
424 CloseHandle( h );
426 for (i=0; i < ARRAY_SIZE(pe); i++)
428 h = CreateFileA(testfile2, pe[i].dwDesiredAccess, pe[i].dwShareMode, NULL,
429 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
430 ok(INVALID_HANDLE_VALUE != h, "%d: CreateFile failed\n",i);
431 SetLastError(0xdeadbeef);
432 ret = GetPrivateProfileStringA(SECTION, KEY, NULL, buffer, MAX_PATH, testfile2);
433 if (!pe[i].read_error)
434 ok( ret, "%d: GetPrivateProfileString failed with error %lu\n", i, GetLastError() );
435 else
436 ok( !ret, "%d: GetPrivateProfileString succeeded\n", i );
437 CloseHandle(h);
439 ok( DeleteFileA(testfile2), "delete failed\n" );
442 static void test_profile_delete_on_close(void)
444 HANDLE h;
445 DWORD size, res;
446 static const CHAR testfile[] = ".\\testwine5.ini";
447 static const char contents[] = "[" SECTION "]\n" KEY "=123\n";
449 h = CreateFileA(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: %lx\n", GetLastError() );
453 ok( size == sizeof contents - 1, "Test file: partial write\n");
455 SetLastError(0xdeadbeef);
456 res = GetPrivateProfileIntA(SECTION, KEY, 0, testfile);
457 ok( res == 123, "Got %ld instead of 123\n", res);
459 /* This also deletes the file */
460 CloseHandle(h);
463 static void test_profile_refresh(void)
465 static const CHAR testfile[] = ".\\winetest4.ini";
466 HANDLE h;
467 DWORD size, res;
468 static const char contents1[] = "[" SECTION "]\n" KEY "=123\n";
469 static const char contents2[] = "[" SECTION "]\n" KEY "=124\n";
471 h = CreateFileA(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
472 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
473 res = WriteFile( h, contents1, sizeof contents1 - 1, &size, NULL );
474 ok( res, "Cannot write test file: %lx\n", GetLastError() );
475 ok( size == sizeof contents1 - 1, "Test file: partial write\n");
477 SetLastError(0xdeadbeef);
478 res = GetPrivateProfileIntA(SECTION, KEY, 0, testfile);
479 ok( res == 123, "Got %ld instead of 123\n", res);
481 CloseHandle(h);
483 /* Test proper invalidation of wine's profile file cache */
485 h = CreateFileA(testfile, GENERIC_WRITE, FILE_SHARE_READ, NULL,
486 CREATE_ALWAYS, FILE_FLAG_DELETE_ON_CLOSE, NULL);
487 res = WriteFile( h, contents2, sizeof contents2 - 1, &size, NULL );
488 ok( res, "Cannot write test file: %lx\n", GetLastError() );
489 ok( size == sizeof contents2 - 1, "Test file: partial write\n");
491 SetLastError(0xdeadbeef);
492 res = GetPrivateProfileIntA(SECTION, KEY, 0, testfile);
493 ok( res == 124, "Got %ld instead of 124\n", res);
495 /* This also deletes the file */
496 CloseHandle(h);
498 /* Cache must be invalidated if file no longer exists and default must be returned */
499 SetLastError(0xdeadbeef);
500 res = GetPrivateProfileIntA(SECTION, KEY, 421, testfile);
501 ok( res == 421, "Got %ld instead of 421\n", res);
504 static void create_test_file(LPCSTR name, LPCSTR data, DWORD size)
506 HANDLE hfile;
507 DWORD count;
509 hfile = CreateFileA(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
510 ok(hfile != INVALID_HANDLE_VALUE, "cannot create %s\n", name);
511 WriteFile(hfile, data, size, &count, NULL);
512 CloseHandle(hfile);
515 static BOOL emptystr_ok(CHAR emptystr[MAX_PATH])
517 int i;
519 for(i = 0;i < MAX_PATH;++i)
520 if(emptystr[i] != 0)
522 trace("emptystr[%d] = %d\n",i,emptystr[i]);
523 return FALSE;
526 return TRUE;
529 static void test_profile_directory_readonly(void)
531 BOOL ret;
532 CHAR path_folder[MAX_PATH];
533 CHAR path_file[MAX_PATH];
534 const char *sddl_string_everyone_readonly = "D:PAI(A;;0x1200a9;;;WD)";
535 SECURITY_ATTRIBUTES attributes = {0};
536 char lpStruct[] = { 's', 't', 'r', 'i', 'n', 'g' };
538 attributes.nLength = sizeof(attributes);
539 ret = ConvertStringSecurityDescriptorToSecurityDescriptorA(sddl_string_everyone_readonly, SDDL_REVISION_1, &attributes.lpSecurityDescriptor, NULL);
540 ok(ret == TRUE, "ConvertStringSecurityDescriptorToSecurityDescriptor failed: %ld\n", GetLastError());
542 GetTempPathA(MAX_PATH, path_folder);
543 lstrcatA(path_folder, "wine-test");
545 strcpy(path_file, path_folder);
546 lstrcatA(path_file, "\\tmp.ini");
548 ret = CreateDirectoryA(path_folder, &attributes);
549 ok(ret == TRUE, "CreateDirectoryA failed: %ld\n", GetLastError());
551 ret = WritePrivateProfileStringA("App", "key", "string", path_file);
552 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
554 ret = WritePrivateProfileSectionA("App", "key=string", path_file);
555 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
557 ret = WritePrivateProfileStructA("App", "key", lpStruct, sizeof(lpStruct), path_file);
558 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
560 ret = RemoveDirectoryA(path_folder);
561 ok(ret == TRUE, "RemoveDirectoryA failed: %ld\n", GetLastError());
563 LocalFree(attributes.lpSecurityDescriptor);
566 static void test_GetPrivateProfileString(const char *content, const char *descript)
568 DWORD ret, len;
569 CHAR buf[MAX_PATH];
570 CHAR def_val[MAX_PATH];
571 CHAR path[MAX_PATH];
572 CHAR windir[MAX_PATH];
573 /* NT series crashes on r/o empty strings, so pass an r/w
574 empty string and check for modification */
575 CHAR emptystr[MAX_PATH] = "";
576 LPSTR tempfile;
578 static const char filename[] = ".\\winetest.ini";
580 trace("test_GetPrivateProfileStringA: %s\n", descript);
582 create_test_file(filename, content, lstrlenA(content));
584 /* Run this test series with caching. Wine won't cache profile
585 files younger than 2.1 seconds. */
586 Sleep(2500);
588 /* lpAppName is NULL */
589 memset(buf, 0xc, sizeof(buf));
590 lstrcpyA(buf, "kumquat");
591 ret = GetPrivateProfileStringA(NULL, "name1", "default",
592 buf, MAX_PATH, filename);
593 ok(ret == 18, "Expected 18, got %ld\n", ret);
594 len = lstrlenA("section1") + sizeof(CHAR) + lstrlenA("section2") + 2 * sizeof(CHAR);
596 ok(!memcmp(buf, "section1\0section2\0", len),
597 "Expected \"section1\\x00section2\\x00\\x00\", got %s\n",
598 debugstr_an(buf, (ret + 2 >= MAX_PATH ? MAX_PATH : ret + 1)));
600 /* lpAppName is empty */
601 memset(buf, 0xc, sizeof(buf));
602 lstrcpyA(buf, "kumquat");
603 ret = GetPrivateProfileStringA(emptystr, "name1", "default",
604 buf, MAX_PATH, filename);
605 ok(ret == 7, "Expected 7, got %ld\n", ret);
606 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
607 ok(emptystr_ok(emptystr), "AppName modified\n");
609 /* lpAppName is missing */
610 memset(buf, 0xc,sizeof(buf));
611 lstrcpyA(buf, "kumquat");
612 ret = GetPrivateProfileStringA("notasection", "name1", "default",
613 buf, MAX_PATH, filename);
614 ok(ret == 7, "Expected 7, got %ld\n", ret);
615 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
617 /* lpAppName is empty, lpDefault is NULL */
618 memset(buf, 0xc,sizeof(buf));
619 lstrcpyA(buf, "kumquat");
620 ret = GetPrivateProfileStringA(emptystr, "name1", NULL,
621 buf, MAX_PATH, filename);
622 ok(ret == 0, "Expected 0, got %ld\n", ret);
623 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
624 ok(emptystr_ok(emptystr), "AppName modified\n");
626 /* lpAppName is empty, lpDefault is empty */
627 memset(buf, 0xc,sizeof(buf));
628 lstrcpyA(buf, "kumquat");
629 ret = GetPrivateProfileStringA(emptystr, "name1", "",
630 buf, MAX_PATH, filename);
631 ok(ret == 0, "Expected 0, got %ld\n", ret);
632 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
633 ok(emptystr_ok(emptystr), "AppName modified\n");
635 /* lpAppName is empty, lpDefault has trailing blank characters */
636 memset(buf, 0xc,sizeof(buf));
637 lstrcpyA(buf, "kumquat");
638 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
639 lstrcpyA(def_val, "default ");
640 ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
641 buf, MAX_PATH, filename);
642 ok(ret == 7, "Expected 7, got %ld\n", ret);
643 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
644 ok(emptystr_ok(emptystr), "AppName modified\n");
646 /* lpAppName is empty, many blank characters in lpDefault */
647 memset(buf, 0xc,sizeof(buf));
648 lstrcpyA(buf, "kumquat");
649 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
650 lstrcpyA(def_val, "one two ");
651 ret = GetPrivateProfileStringA(emptystr, "name1", def_val,
652 buf, MAX_PATH, filename);
653 ok(ret == 7, "Expected 7, got %ld\n", ret);
654 ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
655 ok(emptystr_ok(emptystr), "AppName modified\n");
657 /* lpAppName is empty, blank character but not trailing in lpDefault */
658 memset(buf, 0xc,sizeof(buf));
659 lstrcpyA(buf, "kumquat");
660 ret = GetPrivateProfileStringA(emptystr, "name1", "one two",
661 buf, MAX_PATH, filename);
662 ok(ret == 7, "Expected 7, got %ld\n", ret);
663 ok(!lstrcmpA(buf, "one two"), "Expected \"one two\", got \"%s\"\n", buf);
664 ok(emptystr_ok(emptystr), "AppName modified\n");
666 /* lpKeyName is NULL */
667 memset(buf, 0xc,sizeof(buf));
668 lstrcpyA(buf, "kumquat");
669 ret = GetPrivateProfileStringA("section1", NULL, "default",
670 buf, MAX_PATH, filename);
671 ok(ret == 18, "Expected 18, got %ld\n", ret);
672 ok(!memcmp(buf, "name1\0name2\0name4\0", ret + 1),
673 "Expected \"name1\\x00name2\\x00name4\\x00\\x00\", got %s\n",
674 debugstr_an(buf, (ret + 2 >= MAX_PATH ? MAX_PATH : ret + 1)));
676 /* lpKeyName is empty */
677 memset(buf, 0xc,sizeof(buf));
678 lstrcpyA(buf, "kumquat");
679 ret = GetPrivateProfileStringA("section1", emptystr, "default",
680 buf, MAX_PATH, filename);
681 ok(ret == 7, "Expected 7, got %ld\n", ret);
682 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
683 ok(emptystr_ok(emptystr), "KeyName modified\n");
685 /* lpKeyName is missing */
686 memset(buf, 0xc,sizeof(buf));
687 lstrcpyA(buf, "kumquat");
688 ret = GetPrivateProfileStringA("section1", "notakey", "default",
689 buf, MAX_PATH, filename);
690 ok(ret == 7, "Expected 7, got %ld\n", ret);
691 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
693 /* lpKeyName is empty, lpDefault is NULL */
694 memset(buf, 0xc,sizeof(buf));
695 lstrcpyA(buf, "kumquat");
696 ret = GetPrivateProfileStringA("section1", emptystr, NULL,
697 buf, MAX_PATH, filename);
698 ok(ret == 0, "Expected 0, got %ld\n", ret);
699 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
700 ok(emptystr_ok(emptystr), "KeyName modified\n");
702 /* lpKeyName is empty, lpDefault is empty */
703 memset(buf, 0xc,sizeof(buf));
704 lstrcpyA(buf, "kumquat");
705 ret = GetPrivateProfileStringA("section1", emptystr, "",
706 buf, MAX_PATH, filename);
707 ok(ret == 0, "Expected 0, got %ld\n", ret);
708 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
709 ok(emptystr_ok(emptystr), "KeyName modified\n");
711 /* lpKeyName is empty, lpDefault has trailing blank characters */
712 memset(buf, 0xc,sizeof(buf));
713 lstrcpyA(buf, "kumquat");
714 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
715 lstrcpyA(def_val, "default ");
716 ret = GetPrivateProfileStringA("section1", emptystr, def_val,
717 buf, MAX_PATH, filename);
718 ok(ret == 7, "Expected 7, got %ld\n", ret);
719 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
720 ok(emptystr_ok(emptystr), "KeyName modified\n");
722 if (0) /* crashes */
724 /* lpReturnedString is NULL */
725 ret = GetPrivateProfileStringA("section1", "name1", "default",
726 NULL, MAX_PATH, filename);
729 /* lpFileName is NULL */
730 memset(buf, 0xc,sizeof(buf));
731 lstrcpyA(buf, "kumquat");
732 ret = GetPrivateProfileStringA("section1", "name1", "default",
733 buf, MAX_PATH, NULL);
734 ok(ret == 7, "Expected 7, got %ld\n", ret);
735 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
737 /* lpFileName is empty */
738 memset(buf, 0xc,sizeof(buf));
739 lstrcpyA(buf, "kumquat");
740 ret = GetPrivateProfileStringA("section1", "name1", "default",
741 buf, MAX_PATH, "");
742 ok(ret == 7, "Expected 7, got %ld\n", ret);
743 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
745 /* lpFileName is nonexistent */
746 memset(buf, 0xc,sizeof(buf));
747 lstrcpyA(buf, "kumquat");
748 ret = GetPrivateProfileStringA("section1", "name1", "default",
749 buf, MAX_PATH, "nonexistent");
750 ok(ret == 7, "Expected 7, got %ld\n", ret);
751 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
753 /* nSize is 0 */
754 memset(buf, 0xc,sizeof(buf));
755 lstrcpyA(buf, "kumquat");
756 ret = GetPrivateProfileStringA("section1", "name1", "default",
757 buf, 0, filename);
758 ok(ret == 0, "Expected 0, got %ld\n", ret);
759 ok(!lstrcmpA(buf, "kumquat"), "Expected buf to be unchanged, got \"%s\"\n", buf);
761 /* nSize is exact size of output */
762 memset(buf, 0xc,sizeof(buf));
763 lstrcpyA(buf, "kumquat");
764 ret = GetPrivateProfileStringA("section1", "name1", "default",
765 buf, 4, filename);
766 ok(ret == 3, "Expected 3, got %ld\n", ret);
767 ok(!lstrcmpA(buf, "val"), "Expected \"val\", got \"%s\"\n", buf);
769 /* nSize has room for NULL terminator */
770 memset(buf, 0xc,sizeof(buf));
771 lstrcpyA(buf, "kumquat");
772 ret = GetPrivateProfileStringA("section1", "name1", "default",
773 buf, 5, filename);
774 ok(ret == 4, "Expected 4, got %ld\n", ret);
775 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
777 /* output is 1 character */
778 memset(buf, 0xc,sizeof(buf));
779 lstrcpyA(buf, "kumquat");
780 ret = GetPrivateProfileStringA("section1", "name4", "default",
781 buf, MAX_PATH, filename);
782 ok(ret == 1, "Expected 1, got %ld\n", ret);
783 ok(!lstrcmpA(buf, "a"), "Expected \"a\", got \"%s\"\n", buf);
785 /* output is 1 character, no room for NULL terminator */
786 memset(buf, 0xc,sizeof(buf));
787 lstrcpyA(buf, "kumquat");
788 ret = GetPrivateProfileStringA("section1", "name4", "default",
789 buf, 1, filename);
790 ok(ret == 0, "Expected 0, got %ld\n", ret);
791 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
793 /* lpAppName is NULL, not enough room for final section name */
794 memset(buf, 0xc,sizeof(buf));
795 lstrcpyA(buf, "kumquat");
796 ret = GetPrivateProfileStringA(NULL, "name1", "default",
797 buf, 16, filename);
798 ok(ret == 14, "Expected 14, got %ld\n", ret);
799 len = lstrlenA("section1") + 2 * sizeof(CHAR);
800 todo_wine
801 ok(!memcmp(buf, "section1\0secti\0", ret + 2),
802 "Expected \"section1\\x00secti\\x00\\x00\", got %s\n",
803 debugstr_an(buf, (ret + 2 >= 16 ? 16 : ret + 1)));
805 /* lpKeyName is NULL, not enough room for final key name */
806 memset(buf, 0xc,sizeof(buf));
807 lstrcpyA(buf, "kumquat");
808 ret = GetPrivateProfileStringA("section1", NULL, "default",
809 buf, 16, filename);
810 ok(ret == 14, "Expected 14, got %ld\n", ret);
811 todo_wine
812 ok(!memcmp(buf, "name1\0name2\0na\0", ret + 2),
813 "Expected \"name1\\x00name2\\x00na\\x00\\x00\", got %s\n",
814 debugstr_an(buf, (ret + 2 >= 16 ? 16 : ret + 1)));
816 /* key value has quotation marks which are stripped */
817 memset(buf, 0xc,sizeof(buf));
818 lstrcpyA(buf, "kumquat");
819 ret = GetPrivateProfileStringA("section1", "name2", "default",
820 buf, MAX_PATH, filename);
821 ok(ret == 4, "Expected 4, got %ld\n", ret);
822 ok(!lstrcmpA(buf, "val2"), "Expected \"val2\", got \"%s\"\n", buf);
824 /* case does not match */
825 memset(buf, 0xc,sizeof(buf));
826 lstrcpyA(buf, "kumquat");
827 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
828 buf, MAX_PATH, filename);
829 ok(ret == 4, "Expected 4, got %ld\n", ret);
830 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
832 /* only filename is used */
833 memset(buf, 0xc,sizeof(buf));
834 lstrcpyA(buf, "kumquat");
835 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
836 buf, MAX_PATH, "winetest.ini");
837 ok(ret == 7, "Expected 7, got %ld\n", ret);
838 ok(!lstrcmpA(buf, "default"), "Expected \"default\", got \"%s\"\n", buf);
840 GetWindowsDirectoryA(windir, MAX_PATH);
841 SetLastError(0xdeadbeef);
842 ret = GetTempFileNameA(windir, "pre", 0, path);
843 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
845 skip("Not allowed to create a file in the Windows directory\n");
846 DeleteFileA(filename);
847 return;
849 tempfile = strrchr(path, '\\') + 1;
850 create_test_file(path, content, lstrlenA(content));
852 /* only filename is used, file exists in windows directory */
853 memset(buf, 0xc,sizeof(buf));
854 lstrcpyA(buf, "kumquat");
855 ret = GetPrivateProfileStringA("section1", "NaMe1", "default",
856 buf, MAX_PATH, tempfile);
857 ok(ret == 4, "Expected 4, got %ld\n", ret);
858 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
860 /* successful case */
861 memset(buf, 0xc,sizeof(buf));
862 lstrcpyA(buf, "kumquat");
863 ret = GetPrivateProfileStringA("section1", "name1", "default",
864 buf, MAX_PATH, filename);
865 ok(ret == 4, "Expected 4, got %ld\n", ret);
866 ok(!lstrcmpA(buf, "val1"), "Expected \"val1\", got \"%s\"\n", buf);
868 /* Existing section with no keys in an existing file */
869 memset(buf, 0xc,sizeof(buf));
870 SetLastError(0xdeadbeef);
871 ret=GetPrivateProfileStringA("section2", "DoesntExist", "",
872 buf, MAX_PATH, filename);
873 ok( ret == 0, "expected return size 0, got %ld\n", ret );
874 ok(!lstrcmpA(buf, ""), "Expected \"\", got \"%s\"\n", buf);
875 todo_wine
876 ok( GetLastError() == 0xdeadbeef ||
877 GetLastError() == ERROR_FILE_NOT_FOUND /* Win 7 */,
878 "expected 0xdeadbeef or ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
881 DeleteFileA(path);
882 DeleteFileA(filename);
885 static BOOL check_binary_file_data(LPCSTR path, const VOID *data, DWORD size)
887 HANDLE file;
888 CHAR buf[MAX_PATH];
889 BOOL ret;
891 file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
892 if (file == INVALID_HANDLE_VALUE)
893 return FALSE;
895 if(size != GetFileSize(file, NULL) )
897 CloseHandle(file);
898 return FALSE;
901 ret = ReadFile(file, buf, size, &size, NULL);
902 CloseHandle(file);
903 if (!ret)
904 return FALSE;
906 return !memcmp(buf, data, size);
909 static BOOL check_file_data(LPCSTR path, LPCSTR data)
911 return check_binary_file_data(path, data, lstrlenA(data));
914 static void test_WritePrivateProfileString(void)
916 BOOL ret;
917 LPCSTR data;
918 CHAR path[MAX_PATH];
919 CHAR temp[MAX_PATH];
920 HANDLE file;
922 GetTempPathA(MAX_PATH, temp);
923 GetTempFileNameA(temp, "wine", 0, path);
924 DeleteFileA(path);
926 /* path is not created yet */
928 /* NULL lpAppName */
929 SetLastError(0xdeadbeef);
930 ret = WritePrivateProfileStringA(NULL, "key", "string", path);
931 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
932 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
933 "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
934 ok(GetFileAttributesA(path) == INVALID_FILE_ATTRIBUTES,
935 "Expected path to not exist\n");
937 GetTempFileNameA(temp, "wine", 0, path);
939 /* NULL lpAppName, path exists */
940 data = "";
941 SetLastError(0xdeadbeef);
942 ret = WritePrivateProfileStringA(NULL, "key", "string", path);
943 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
944 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
945 "Expected ERROR_FILE_NOT_FOUND, got %ld\n", GetLastError());
946 ok(check_file_data(path, data), "File doesn't match\n");
947 DeleteFileA(path);
949 if (0)
951 /* empty lpAppName, crashes on NT4 and higher */
952 data = "[]\r\n"
953 "key=string\r\n";
954 ret = WritePrivateProfileStringA("", "key", "string", path);
955 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
956 ok(check_file_data(path, data), "File doesn't match\n");
957 DeleteFileA(path);
960 /* NULL lpKeyName */
961 data = "";
962 ret = WritePrivateProfileStringA("App", NULL, "string", path);
963 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
964 todo_wine
966 ok(check_file_data(path, data), "File doesn't match\n");
968 DeleteFileA(path);
970 if (0)
972 /* empty lpKeyName, crashes on NT4 and higher */
973 data = "[App]\r\n"
974 "=string\r\n";
975 ret = WritePrivateProfileStringA("App", "", "string", path);
976 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
977 todo_wine
979 ok(check_file_data(path, data), "File doesn't match\n");
981 DeleteFileA(path);
984 /* NULL lpString */
985 data = "";
986 ret = WritePrivateProfileStringA("App", "key", NULL, path);
987 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
988 todo_wine
990 ok(check_file_data(path, data), "File doesn't match\n");
992 DeleteFileA(path);
994 /* empty lpString */
995 data = "[App]\r\n"
996 "key=\r\n";
997 ret = WritePrivateProfileStringA("App", "key", "", path);
998 ok(ret == TRUE, "Expected TRUE, got %d\n", ret);
999 ok(check_file_data(path, data), "File doesn't match\n");
1000 DeleteFileA(path);
1002 /* empty lpFileName */
1003 SetLastError(0xdeadbeef);
1004 ret = WritePrivateProfileStringA("App", "key", "string", "");
1005 ok(ret == FALSE, "Expected FALSE, got %d\n", ret);
1006 ok(GetLastError() == ERROR_ACCESS_DENIED,
1007 "Expected ERROR_ACCESS_DENIED, got %ld\n", GetLastError());
1009 /* Relative paths are relative to X:\\%WINDIR% */
1010 GetWindowsDirectoryA(path, MAX_PATH);
1011 strcat(path, "\\win1.tmp");
1012 file = CreateFileA(path, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
1013 if (!ret && GetLastError() == ERROR_ACCESS_DENIED)
1014 skip("Not allowed to create a file in the Windows directory\n");
1015 else
1017 CloseHandle(file);
1018 DeleteFileA(path);
1020 data = "[App]\r\n"
1021 "key=string\r\n";
1022 ret = WritePrivateProfileStringA("App", "key", "string", "win1.tmp");
1023 ok(ret == TRUE, "Expected TRUE, got %d, le=%lu\n", ret, GetLastError());
1024 ok(check_file_data(path, data), "File doesn't match\n");
1025 DeleteFileA(path);
1028 GetTempPathA(MAX_PATH, temp);
1029 GetTempFileNameA(temp, "wine", 0, path);
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);
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");
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");
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");
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");
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");
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);
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), "File doesn't match\n");
1107 DeleteFileA(path);
1110 static void test_profile_struct(void)
1112 static const char expect_data[] = "[s]\r\nkey=616261637573006F\r\n";
1113 char buffer[20];
1114 BOOL ret;
1116 SetLastError(0xdeadbeef);
1117 ret = GetPrivateProfileStructA("s", "key", buffer, sizeof(buffer), "./winetest.ini");
1118 ok(!ret, "expected failure\n");
1119 todo_wine ok(GetLastError() == ERROR_BAD_LENGTH, "got error %lu\n", GetLastError());
1121 ret = WritePrivateProfileStructA("s", "key", (void *)"abacus", sizeof("abacus"), "./winetest.ini");
1122 ok(ret, "got error %lu\n", GetLastError());
1123 ok(check_file_data("./winetest.ini", expect_data), "file doesn't match\n");
1125 SetLastError(0xdeadbeef);
1126 ret = GetPrivateProfileStructA("s", "key", buffer, 6, "./winetest.ini");
1127 ok(!ret, "expected failure\n");
1128 todo_wine ok(GetLastError() == ERROR_BAD_LENGTH, "got error %lu\n", GetLastError());
1130 SetLastError(0xdeadbeef);
1131 ret = GetPrivateProfileStructA("s", "key", buffer, 8, "./winetest.ini");
1132 ok(!ret, "expected failure\n");
1133 todo_wine ok(GetLastError() == ERROR_BAD_LENGTH, "got error %lu\n", GetLastError());
1135 memset(buffer, 0xcc, sizeof(buffer));
1136 ret = GetPrivateProfileStructA("s", "key", buffer, 7, "./winetest.ini");
1137 ok(ret, "got error %lu\n", GetLastError());
1138 ok(!strcmp(buffer, "abacus"), "data didn't match\n");
1140 memset(buffer, 0xcc, sizeof(buffer));
1141 ret = GetPrivateProfileStringA("s", "key", "default", buffer, sizeof(buffer), "./winetest.ini");
1142 ok(ret == 16, "got size %u\n", ret);
1143 ok(!strcmp(buffer, "616261637573006F"), "got %s\n", debugstr_a(buffer));
1145 ret = WritePrivateProfileStringA("s", "key", "636163747573006F", "./winetest.ini");
1146 ok(ret, "got error %lu\n", GetLastError());
1148 SetLastError(0xdeadbeef);
1149 ret = GetPrivateProfileStructA("s", "key", buffer, 7, "./winetest.ini");
1150 ok(!ret, "expected failure\n");
1151 todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "got error %lu\n", GetLastError());
1153 ret = WritePrivateProfileStringA("s", "key", "6361637475730083", "./winetest.ini");
1154 ok(ret, "got error %lu\n", GetLastError());
1156 memset(buffer, 0xcc, sizeof(buffer));
1157 ret = GetPrivateProfileStructA("s", "key", buffer, 7, "./winetest.ini");
1158 ok(ret, "got error %lu\n", GetLastError());
1159 ok(!strcmp(buffer, "cactus"), "data didn't match\n");
1161 ret = WritePrivateProfileStringA("s", "key", "636163747573008Q", "./winetest.ini");
1162 ok(ret, "got error %lu\n", GetLastError());
1164 SetLastError(0xdeadbeef);
1165 ret = GetPrivateProfileStructA("s", "key", buffer, 7, "./winetest.ini");
1166 ok(!ret, "expected failure\n");
1167 todo_wine ok(GetLastError() == ERROR_INVALID_DATA, "got error %lu\n", GetLastError());
1169 ret = WritePrivateProfileStringA("s", "key", "16361637475730083", "./winetest.ini");
1170 ok(ret, "got error %lu\n", GetLastError());
1172 SetLastError(0xdeadbeef);
1173 ret = GetPrivateProfileStructA("s", "key", buffer, 7, "./winetest.ini");
1174 ok(!ret, "expected failure\n");
1175 todo_wine ok(GetLastError() == ERROR_BAD_LENGTH, "got error %lu\n", GetLastError());
1177 ret = DeleteFileA("./winetest.ini");
1178 ok(ret, "got error %lu\n", GetLastError());
1181 static void check_registry_value_(int line, HKEY key, const char *value, const char *expect)
1183 char buffer[30];
1184 DWORD type, size = sizeof(buffer);
1185 LSTATUS ret;
1187 memset(buffer, 0xcc, sizeof(buffer));
1188 ret = RegQueryValueExA(key, value, 0, &type, (BYTE *)buffer, &size);
1189 ok_(__FILE__, line)(!ret, "got error %lu\n", ret);
1190 ok_(__FILE__, line)(!strcmp(buffer, expect), "expected %s, got %s\n", debugstr_a(expect), debugstr_a(buffer));
1191 ok_(__FILE__, line)(type == REG_SZ, "got type %lu\n", type);
1193 #define check_registry_value(a, b, c) check_registry_value_(__LINE__, a, b, c)
1195 static void test_registry_mapping(void)
1197 static const DWORD ivalue = 0xabacab;
1198 HKEY mapping_key, mapped_key, mapping_subkey;
1199 char buffer[30];
1200 LSTATUS ret;
1202 /* impersonate ourselves to prevent registry virtualization */
1203 ret = ImpersonateSelf(SecurityImpersonation);
1204 ok(ret, "got error %lu\n", GetLastError());
1206 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE,
1207 "Software\\Microsoft\\Windows NT\\CurrentVersion\\IniFileMapping\\winetest_map.ini",
1208 0, NULL, 0, KEY_READ | KEY_WRITE | KEY_WOW64_64KEY, NULL, &mapping_key, NULL);
1209 if (ret == ERROR_ACCESS_DENIED)
1211 skip("Not enough permissions to write to the IniFileMapping key.\n");
1212 return;
1214 ok(!ret, "got error %lu\n", ret);
1216 ret = RegSetValueExA(mapping_key, "section1", 0, REG_SZ, (BYTE *)"USR:winetest_map", sizeof("USR:winetest_map"));
1217 ok(!ret, "got error %lu\n", ret);
1218 ret = WritePrivateProfileStringA(NULL, NULL, NULL, "winetest_map.ini");
1219 todo_wine ok(ret, "got error %lu\n", GetLastError());
1221 check_profile_string("section1", "name1", "winetest_map.ini", "default");
1223 ret = WritePrivateProfileStringA("section1", "name1", "value1", "winetest_map.ini");
1224 ok(ret, "got error %lu\n", GetLastError());
1226 check_profile_string("section1", "name1", "winetest_map.ini", "value1");
1227 check_profile_string("section1", "name1", "C:/fake/path/winetest_map.ini", "value1");
1229 ret = RegOpenKeyExA(HKEY_CURRENT_USER, "winetest_map", 0, KEY_READ | KEY_WRITE, &mapped_key);
1230 ok(!ret, "got error %lu\n", ret);
1231 check_registry_value(mapped_key, "name1", "value1");
1233 ret = RegSetValueExA(mapped_key, "name2", 0, REG_SZ, (BYTE *)"value2", sizeof("value2"));
1234 ok(!ret, "got error %lu\n", ret);
1236 check_profile_string("section1", "name2", "winetest_map.ini", "value2");
1238 ret = GetFileAttributesA("C:/windows/winetest_map.ini");
1239 ok(ret == INVALID_FILE_ATTRIBUTES, "winetest_map.ini should not exist.\n");
1241 ret = WritePrivateProfileStringA("section1", "name2", NULL, "winetest_map.ini");
1242 ok(ret, "got error %lu\n", GetLastError());
1243 ret = RegQueryValueExA(mapped_key, "name2", 0, NULL, NULL, NULL);
1244 ok(ret == ERROR_FILE_NOT_FOUND, "got error %lu\n", ret);
1246 /* Test non-string types. */
1248 ret = RegSetValueExA(mapped_key, "name3", 0, REG_DWORD, (BYTE *)&ivalue, sizeof(ivalue));
1249 ok(!ret, "got error %lu\n", ret);
1250 check_profile_string("section1", "name3", "winetest_map.ini", "default");
1252 ret = GetPrivateProfileIntA("section1", "name3", 0, "winetest_map.ini");
1253 ok(ret == 0, "got %#lx\n", ret);
1255 ret = RegSetValueExA(mapped_key, "name3", 0, REG_BINARY, (BYTE *)"value3", sizeof("value3"));
1256 ok(!ret, "got error %lu\n", ret);
1257 check_profile_string("section1", "name3", "winetest_map.ini", "default");
1259 ret = RegSetValueExA(mapped_key, "name3", 0, REG_MULTI_SZ, (BYTE *)"one\0two\0", sizeof("one\0two\0"));
1260 ok(!ret, "got error %lu\n", ret);
1261 check_profile_string("section1", "name3", "winetest_map.ini", "default");
1263 ret = RegSetValueExA(mapped_key, "name3", 0, REG_EXPAND_SZ, (BYTE *)"x%SystemRoot%", sizeof("x%SystemRoot%"));
1264 ok(!ret, "got error %lu\n", ret);
1265 check_profile_string("section1", "name3", "winetest_map.ini", "default");
1267 /* Test WritePrivateProfileSection(). Unlike with .ini files, it doesn't
1268 * remove existing entries. */
1270 ret = WritePrivateProfileStringA("section1", "name4", "value4", "winetest_map.ini");
1271 ok(ret, "got error %lu\n", GetLastError());
1272 ret = WritePrivateProfileStringA("section1", "name5", "value5", "winetest_map.ini");
1273 ok(ret, "got error %lu\n", GetLastError());
1274 ret = WritePrivateProfileSectionA("section1", "name4=four\0name6=six\0", "winetest_map.ini");
1275 ok(ret, "got error %lu\n", GetLastError());
1276 check_profile_string("section1", "name4", "winetest_map.ini", "four");
1277 check_profile_string("section1", "name5", "winetest_map.ini", "value5");
1278 check_profile_string("section1", "name6", "winetest_map.ini", "six");
1280 /* Test deleting the section. */
1282 RegCloseKey(mapped_key);
1284 ret = RegCreateKeyExA(HKEY_CURRENT_USER, "winetest_map\\subkey", 0, NULL, 0, 0, NULL, &mapped_key, NULL);
1285 ok(!ret, "got error %lu\n", ret);
1286 RegCloseKey(mapped_key);
1288 ret = WritePrivateProfileStringA("section1", "name1", "value1", "winetest_map.ini");
1289 ok(ret, "got error %lu\n", GetLastError());
1290 ret = WritePrivateProfileStringA("section1", NULL, NULL, "winetest_map.ini");
1291 ok(ret, "got error %lu\n", GetLastError());
1292 check_profile_string("section1", "name1", "winetest_map.ini", "default");
1294 ret = WritePrivateProfileStringA("section1", "name1", "value1", "winetest_map.ini");
1295 ok(ret, "got error %lu\n", GetLastError());
1296 ret = WritePrivateProfileSectionA("section1", NULL, "winetest_map.ini");
1297 ok(ret, "got error %lu\n", GetLastError());
1298 check_profile_string("section1", "name1", "winetest_map.ini", "default");
1300 ret = RegDeleteKeyA(HKEY_CURRENT_USER, "winetest_map\\subkey");
1301 ok(!ret, "got error %lu\n", ret);
1302 ret = RegDeleteKeyA(HKEY_CURRENT_USER, "winetest_map");
1303 ok(!ret, "got error %lu\n", ret);
1305 /* Test GetPrivateProfileSectionNames(). */
1307 memset(buffer, 0xcc, sizeof(buffer));
1308 ret = GetPrivateProfileSectionNamesA(buffer, 5, "winetest_map.ini");
1309 ok(ret == 3, "got %lu\n", ret);
1310 ok(!memcmp(buffer, "sec\0", 5), "got %s\n", debugstr_an(buffer, ret));
1312 memset(buffer, 0xcc, sizeof(buffer));
1313 ret = GetPrivateProfileSectionNamesA(buffer, sizeof(buffer), "winetest_map.ini");
1314 ok(ret == 9, "got %lu\n", ret);
1315 ok(!memcmp(buffer, "section1\0", 10), "got %s\n", debugstr_an(buffer, ret));
1317 ret = WritePrivateProfileStringA("file_section", "name1", "value1", "winetest_map.ini");
1318 ok(ret, "got error %lu\n", GetLastError());
1320 memset(buffer, 0xcc, sizeof(buffer));
1321 ret = GetPrivateProfileSectionNamesA(buffer, 5, "winetest_map.ini");
1322 ok(ret == 3, "got %lu\n", ret);
1323 ok(!memcmp(buffer, "sec\0", 5), "got %s\n", debugstr_an(buffer, ret));
1325 memset(buffer, 0xcc, sizeof(buffer));
1326 ret = GetPrivateProfileSectionNamesA(buffer, sizeof(buffer), "winetest_map.ini");
1327 ok(ret == 22, "got %lu\n", ret);
1328 ok(!memcmp(buffer, "section1\0file_section\0", 23), "got %s\n", debugstr_an(buffer, ret));
1330 ret = DeleteFileA("C:/windows/winetest_map.ini");
1331 ok(ret, "got error %lu\n", GetLastError());
1333 /* Test the SYS: prefix. */
1335 ret = RegSetValueExA(mapping_key, "section2", 0, REG_SZ, (BYTE *)"SYS:winetest_map", sizeof("SYS:winetest_map"));
1336 ok(!ret, "got error %lu\n", ret);
1337 ret = WritePrivateProfileStringA(NULL, NULL, NULL, "winetest_map.ini");
1338 todo_wine ok(ret, "got error %lu\n", GetLastError());
1340 check_profile_string("section2", "name1", "winetest_map.ini", "default");
1342 ret = WritePrivateProfileStringA("section2", "name1", "value1", "winetest_map.ini");
1343 ok(ret, "got error %lu\n", GetLastError());
1345 check_profile_string("section2", "name1", "winetest_map.ini", "value1");
1347 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\winetest_map", 0, KEY_READ | KEY_WRITE, &mapped_key);
1348 ok(!ret, "got error %lu\n", ret);
1349 check_registry_value(mapped_key, "name1", "value1");
1351 ret = RegSetValueExA(mapped_key, "name2", 0, REG_SZ, (BYTE *)"value2", sizeof("value2"));
1352 ok(!ret, "got error %lu\n", ret);
1354 check_profile_string("section2", "name2", "winetest_map.ini", "value2");
1356 ret = GetFileAttributesA("C:/windows/winetest_map.ini");
1357 ok(ret == INVALID_FILE_ATTRIBUTES, "winetest_map.ini should not exist.\n");
1359 ret = RegDeleteKeyA(mapped_key, "");
1360 ok(!ret, "got error %lu\n", ret);
1361 RegCloseKey(mapped_key);
1363 /* Try writing directly to the .ini file on disk instead. */
1365 ret = WritePrivateProfileStringA("section3", "name1", "value1", "winetest_map.ini");
1366 ok(ret, "got error %lu\n", GetLastError());
1367 check_profile_string("section3", "name1", "winetest_map.ini", "value1");
1369 ret = RegSetValueExA(mapping_key, "section3", 0, REG_SZ, (BYTE *)"USR:winetest_map", sizeof("USR:winetest_map"));
1370 ok(!ret, "got error %lu\n", ret);
1371 ret = WritePrivateProfileStringA(NULL, NULL, NULL, "winetest_map.ini");
1372 todo_wine ok(ret, "got error %lu\n", GetLastError());
1374 check_profile_string("section3", "name1", "winetest_map.ini", "default");
1376 ret = RegOpenKeyExA(HKEY_CURRENT_USER, "winetest_section3", 0, KEY_READ | KEY_WRITE, &mapped_key);
1377 ok(ret == ERROR_FILE_NOT_FOUND, "got error %lu\n", ret);
1379 ret = WritePrivateProfileStringA("section3", "name1", "value2", "winetest_map.ini");
1380 ok(ret, "got error %lu\n", GetLastError());
1382 check_profile_string("section3", "name1", "winetest_map.ini", "value2");
1384 ret = RegOpenKeyExA(HKEY_CURRENT_USER, "winetest_map", 0, KEY_READ | KEY_WRITE, &mapped_key);
1385 ok(!ret, "got error %lu\n", ret);
1387 ret = RegDeleteKeyA(mapped_key, "");
1388 ok(!ret, "got error %lu\n", ret);
1389 RegCloseKey(mapped_key);
1391 ret = RegDeleteValueA(mapping_key, "section3");
1392 ok(!ret, "got error %lu\n", ret);
1393 ret = WritePrivateProfileStringA(NULL, NULL, NULL, "winetest_map.ini");
1394 todo_wine ok(ret, "got error %lu\n", GetLastError());
1396 check_profile_string("section3", "name1", "winetest_map.ini", "value1");
1398 ret = DeleteFileA("C:/windows/winetest_map.ini");
1399 ok(ret, "got error %lu\n", GetLastError());
1401 /* Test default keys. */
1403 ret = WritePrivateProfileStringA("section4", "name1", "value1", "winetest_map.ini");
1404 ok(ret, "got error %lu\n", GetLastError());
1406 check_profile_string("section4", "name1", "winetest_map.ini", "value1");
1408 ret = DeleteFileA("C:/windows/winetest_map.ini");
1409 ok(ret, "got error %lu\n", GetLastError());
1411 ret = RegSetValueExA(mapping_key, NULL, 0, REG_SZ, (BYTE *)"SYS:winetest_default", sizeof("SYS:winetest_default"));
1412 ok(!ret, "got error %lu\n", ret);
1413 ret = WritePrivateProfileStringA(NULL, NULL, NULL, "winetest_map.ini");
1414 todo_wine ok(ret, "got error %lu\n", GetLastError());
1416 ret = WritePrivateProfileStringA("section4", "name1", "value1", "winetest_map.ini");
1417 ok(ret, "got error %lu\n", GetLastError());
1419 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\winetest_default\\section4", 0, KEY_READ, &mapped_key);
1420 ok(!ret, "got error %lu\n", ret);
1421 check_registry_value(mapped_key, "name1", "value1");
1422 RegCloseKey(mapped_key);
1424 ret = RegCreateKeyExA(HKEY_LOCAL_MACHINE, "Software\\winetest_default\\section5",
1425 0, NULL, 0, KEY_WRITE, NULL, &mapped_key, NULL);
1426 ok(!ret, "got error %lu\n", ret);
1427 ret = RegSetValueExA(mapped_key, "name2", 0, REG_SZ, (BYTE *)"value2", sizeof("value2"));
1428 ok(!ret, "got error %lu\n", ret);
1429 RegCloseKey(mapped_key);
1431 check_profile_string("section5", "name2", "winetest_map.ini", "value2");
1433 ret = GetFileAttributesA("C:/windows/winetest_map.ini");
1434 ok(ret == INVALID_FILE_ATTRIBUTES, "winetest_map.ini should not exist.\n");
1436 ret = RegDeleteKeyA(HKEY_LOCAL_MACHINE, "Software\\winetest_default\\Section4");
1437 ret = RegDeleteKeyA(HKEY_LOCAL_MACHINE, "Software\\winetest_default\\Section5");
1438 ret = RegDeleteKeyA(HKEY_LOCAL_MACHINE, "Software\\winetest_default");
1439 ok(!ret, "got error %lu\n", ret);
1440 ret = RegDeleteValueA(mapping_key, NULL);
1441 ok(!ret, "got error %lu\n", ret);
1443 /* Test name-specific mapping. */
1445 ret = RegCreateKeyExA(mapping_key, "section6", 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &mapping_subkey, NULL);
1446 ok(!ret, "got error %lu\n", ret);
1447 ret = RegSetValueExA(mapping_subkey, "name1", 0, REG_SZ, (BYTE *)"USR:winetest_name1", sizeof("USR:winetest_name1"));
1448 ok(!ret, "got error %lu\n", ret);
1449 ret = RegSetValueExA(mapping_subkey, "name2", 0, REG_SZ, (BYTE *)"SYS:winetest_name2", sizeof("SYS:winetest_name2"));
1450 ok(!ret, "got error %lu\n", ret);
1451 ret = WritePrivateProfileStringA(NULL, NULL, NULL, "winetest_map.ini");
1452 todo_wine ok(ret, "got error %lu\n", GetLastError());
1454 ret = WritePrivateProfileStringA("section6", "name1", "value1", "winetest_map.ini");
1455 ok(ret, "got error %lu\n", GetLastError());
1456 check_profile_string("section6", "name1", "winetest_map.ini", "value1");
1458 ret = RegOpenKeyExA(HKEY_CURRENT_USER, "winetest_name1", 0, KEY_READ | KEY_WRITE, &mapped_key);
1459 ok(!ret, "got error %lu\n", ret);
1460 check_registry_value(mapped_key, "name1", "value1");
1462 ret = RegSetValueExA(mapped_key, "name1", 0, REG_SZ, (BYTE *)"one", sizeof("one"));
1463 ok(!ret, "got error %lu\n", ret);
1464 check_profile_string("section6", "name1", "winetest_map.ini", "one");
1466 ret = RegDeleteKeyA(mapped_key, "");
1467 ok(!ret, "got error %lu\n", ret);
1468 RegCloseKey(mapped_key);
1470 ret = WritePrivateProfileStringA("section6", "name2", "value2", "winetest_map.ini");
1471 ok(ret, "got error %lu\n", GetLastError());
1473 ret = RegOpenKeyExA(HKEY_LOCAL_MACHINE, "Software\\winetest_name2", 0, KEY_READ | KEY_WRITE, &mapped_key);
1474 ok(!ret, "got error %lu\n", ret);
1475 check_registry_value(mapped_key, "name2", "value2");
1477 ret = RegSetValueExA(mapped_key, "name2", 0, REG_SZ, (BYTE *)"two", sizeof("two"));
1478 ok(!ret, "got error %lu\n", ret);
1479 check_profile_string("section6", "name2", "winetest_map.ini", "two");
1481 ret = RegDeleteKeyA(mapped_key, "");
1482 ok(!ret, "got error %lu\n", ret);
1483 RegCloseKey(mapped_key);
1485 ret = WritePrivateProfileStringA("section6", "name3", "value3", "winetest_map.ini");
1486 ok(ret, "got error %lu\n", GetLastError());
1487 check_profile_string("section6", "name3", "winetest_map.ini", "value3");
1488 ret = DeleteFileA("C:/windows/winetest_map.ini");
1489 ok(ret, "got error %lu\n", GetLastError());
1491 /* Test name-specific mapping with Get/WritePrivateProfileSection(). */
1493 ret = WritePrivateProfileStringA("section6", "name2", "value2", "winetest_map.ini");
1494 ok(ret, "got error %lu\n", GetLastError());
1496 ret = WritePrivateProfileStringA("section6", "name3", "value3", "winetest_map.ini");
1497 ok(ret, "got error %lu\n", GetLastError());
1499 ret = WritePrivateProfileSectionA("section6", "name1=one\0name3=three\0", "winetest_map.ini");
1500 ok(ret, "got error %lu\n", GetLastError());
1501 check_profile_string("section6", "name1", "winetest_map.ini", "one");
1502 check_profile_string("section6", "name2", "winetest_map.ini", "value2");
1503 check_profile_string("section6", "name3", "winetest_map.ini", "value3");
1505 ret = RegOpenKeyExA(HKEY_CURRENT_USER, "winetest_name1", 0, KEY_READ | KEY_WRITE, &mapped_key);
1506 ok(!ret, "got error %lu\n", ret);
1507 ret = RegDeleteValueA(mapped_key, "name1");
1508 ok(!ret, "got error %lu\n", ret);
1509 RegCloseKey(mapped_key);
1511 memset(buffer, 0xcc, sizeof(buffer));
1512 ret = GetPrivateProfileSectionA("section6", buffer, 5, "winetest_map.ini");
1513 ok(ret == 3, "got %lu\n", ret);
1514 ok(!memcmp(buffer, "nam\0", 5), "got %s\n", debugstr_an(buffer, ret));
1516 memset(buffer, 0xcc, sizeof(buffer));
1517 ret = GetPrivateProfileSectionA("section6", buffer, sizeof(buffer), "winetest_map.ini");
1518 ok(ret == 26, "got %lu\n", ret);
1519 ok(!memcmp(buffer, "name2=value2\0name3=value3\0", 27), "got %s\n", debugstr_an(buffer, ret));
1521 ret = WritePrivateProfileStringA("section6", NULL, NULL, "winetest_map.ini");
1522 ok(ret, "got error %lu\n", GetLastError());
1523 check_profile_string("section6", "name1", "winetest_map.ini", "default");
1524 check_profile_string("section6", "name2", "winetest_map.ini", "default");
1525 check_profile_string("section6", "name3", "winetest_map.ini", "default");
1527 ret = RegDeleteKeyA(HKEY_CURRENT_USER, "winetest_name1");
1528 ok(!ret, "got error %lu\n", ret);
1529 ret = RegDeleteKeyA(HKEY_LOCAL_MACHINE, "Software\\winetest_name2");
1530 ok(!ret, "got error %lu\n", ret);
1531 ret = DeleteFileA("C:/windows/winetest_map.ini");
1532 ok(ret, "got error %lu\n", GetLastError());
1534 /* Test name-specific mapping with a default value. */
1536 ret = RegSetValueExA(mapping_subkey, NULL, 0, REG_SZ, (BYTE *)"USR:winetest_default", sizeof("USR:winetest_default"));
1537 ok(!ret, "got error %lu\n", ret);
1538 ret = WritePrivateProfileStringA(NULL, NULL, NULL, "winetest_map.ini");
1539 todo_wine ok(ret, "got error %lu\n", GetLastError());
1541 ret = WritePrivateProfileStringA("section6", "name2", "value2", "winetest_map.ini");
1542 ok(ret, "got error %lu\n", GetLastError());
1543 ret = WritePrivateProfileStringA("section6", "name3", "value3", "winetest_map.ini");
1544 ok(ret, "got error %lu\n", GetLastError());
1546 ret = RegOpenKeyExA(HKEY_CURRENT_USER, "winetest_default", 0, KEY_READ | KEY_WRITE, &mapped_key);
1547 ok(!ret, "got error %lu\n", ret);
1548 check_registry_value(mapped_key, "name3", "value3");
1550 ret = RegSetValueExA(mapped_key, "name3", 0, REG_SZ, (BYTE *)"three", sizeof("three"));
1551 ok(!ret, "got error %lu\n", ret);
1552 check_profile_string("section6", "name3", "winetest_map.ini", "three");
1554 memset(buffer, 0xcc, sizeof(buffer));
1555 ret = GetPrivateProfileSectionA("section6", buffer, sizeof(buffer), "winetest_map.ini");
1556 ok(ret == 25, "got %lu\n", ret);
1557 todo_wine ok(!memcmp(buffer, "name2=value2\0name3=three\0", 26), "got %s\n", debugstr_an(buffer, ret));
1559 ret = WritePrivateProfileSectionA("section6", "name2=duo\0name3=treis\0", "winetest_map.ini");
1560 ok(ret, "got error %lu\n", GetLastError());
1561 check_profile_string("section6", "name2", "winetest_map.ini", "duo");
1562 check_profile_string("section6", "name3", "winetest_map.ini", "treis");
1564 ret = WritePrivateProfileStringA("section6", NULL, NULL, "winetest_map.ini");
1565 ok(ret, "got error %lu\n", GetLastError());
1566 check_profile_string("section6", "name2", "winetest_map.ini", "default");
1567 check_profile_string("section6", "name3", "winetest_map.ini", "default");
1569 ret = RegDeleteKeyA(HKEY_LOCAL_MACHINE, "Software\\winetest_name2");
1570 ok(!ret, "got error %lu\n", ret);
1571 ret = RegDeleteKeyA(HKEY_CURRENT_USER, "winetest_name1");
1572 ok(!ret, "got error %lu\n", ret);
1573 ret = RegDeleteKeyA(mapped_key, "");
1574 ok(!ret, "got error %lu\n", ret);
1575 RegCloseKey(mapped_key);
1577 ret = RegDeleteKeyA(mapping_subkey, "");
1578 ok(!ret, "got error %lu\n", ret);
1579 RegCloseKey(mapping_subkey);
1581 ret = RegDeleteKeyA(mapping_key, "");
1582 ok(!ret, "got error %lu\n", ret);
1583 RegCloseKey(mapping_key);
1585 ret = DeleteFileA("C:/windows/winetest_map.ini");
1586 ok(!ret, "expected failure\n");
1587 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "got error %lu\n", GetLastError());
1588 ret = RevertToSelf();
1589 ok(ret, "got error %lu\n", GetLastError());
1592 START_TEST(profile)
1594 test_profile_int();
1595 test_profile_string();
1596 test_profile_sections();
1597 test_profile_sections_names();
1598 test_profile_existing();
1599 test_profile_delete_on_close();
1600 test_profile_refresh();
1601 test_profile_directory_readonly();
1602 test_GetPrivateProfileString(
1603 "[section1]\r\n"
1604 "name1=val1\r\n"
1605 "name2=\"val2\"\r\n"
1606 "name3\r\n"
1607 "name4=a\r\n"
1608 "[section2]\r\n",
1609 "CR+LF");
1610 test_GetPrivateProfileString(
1611 "[section1]\r"
1612 "name1=val1\r"
1613 "name2=\"val2\"\r"
1614 "name3\r"
1615 "name4=a\r"
1616 "[section2]\r",
1617 "CR only");
1618 test_WritePrivateProfileString();
1619 test_profile_struct();
1620 test_registry_mapping();