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
24 #include "wine/test.h"
29 #define KEY "ProfileInt"
30 #define SECTION "Test"
31 #define TESTFILE ".\\testwine.ini"
32 #define TESTFILE2 ".\\testwine2.ini"
44 static void test_profile_int(void)
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},
73 int i
, num_test
= (sizeof(profileInt
)/sizeof(struct _profileInt
));
76 DeleteFileA( TESTFILE
);
78 for (i
=0; i
< num_test
; i
++) {
79 if (profileInt
[i
].value
)
80 WritePrivateProfileStringA(SECTION
, KEY
, profileInt
[i
].value
,
81 profileInt
[i
].iniFile
);
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
);
90 DeleteFileA( TESTFILE
);
93 static void test_profile_string(void)
100 /* test that lines without an '=' will not be enumerated */
101 /* in the case below, name2 is a key while name3 is not. */
102 char content
[]="[s]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n";
103 DeleteFileA( TESTFILE2
);
104 h
= CreateFileA( TESTFILE2
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
105 FILE_ATTRIBUTE_NORMAL
, NULL
);
106 ok( h
!= INVALID_HANDLE_VALUE
, " cannot create %s\n", TESTFILE2
);
107 if( h
== INVALID_HANDLE_VALUE
) return;
108 WriteFile( h
, content
, sizeof(content
), &count
, NULL
);
111 /* enumerate the keys */
112 ret
=GetPrivateProfileStringA( "s", NULL
, "", buf
, sizeof(buf
),
114 for( p
= buf
+ strlen(buf
) + 1; *p
;p
+= strlen(p
)+1)
117 ok( ret
== 18 && !strcmp( buf
, "name1,name2,name4"), "wrong keys returned(%d): %s\n", ret
,
120 /* add a new key to test that the file is quite usable */
121 WritePrivateProfileStringA( "s", "name5", "val5", TESTFILE2
);
122 ret
=GetPrivateProfileStringA( "s", NULL
, "", buf
, sizeof(buf
),
124 for( p
= buf
+ strlen(buf
) + 1; *p
;p
+= strlen(p
)+1)
126 ok( ret
== 24 && !strcmp( buf
, "name1,name2,name4,name5"), "wrong keys returned(%d): %s\n",
129 DeleteFileA( TESTFILE2
);
132 static void test_profile_sections(void)
139 static const char content
[]="[section1]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n[section2]\r\n";
140 static const char testfile4
[]=".\\testwine4.ini";
141 BOOL on_win98
= FALSE
;
143 DeleteFileA( testfile4
);
144 h
= CreateFileA( testfile4
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
145 ok( h
!= INVALID_HANDLE_VALUE
, " cannot create %s\n", testfile4
);
146 if( h
== INVALID_HANDLE_VALUE
) return;
147 WriteFile( h
, content
, sizeof(content
), &count
, NULL
);
150 /* Some parameter checking */
151 SetLastError(0xdeadbeef);
152 ret
= GetPrivateProfileSectionA( NULL
, NULL
, 0, NULL
);
153 ok( ret
== 0, "expected return size 0, got %d\n", ret
);
154 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
155 GetLastError() == 0xdeadbeef /* Win98 */,
156 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
157 if (GetLastError() == 0xdeadbeef) on_win98
= TRUE
;
159 SetLastError(0xdeadbeef);
160 ret
= GetPrivateProfileSectionA( NULL
, NULL
, 0, testfile4
);
161 ok( ret
== 0, "expected return size 0, got %d\n", ret
);
162 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
163 GetLastError() == 0xdeadbeef /* Win98 */,
164 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
168 SetLastError(0xdeadbeef);
169 ret
= GetPrivateProfileSectionA( "section1", NULL
, 0, testfile4
);
170 ok( ret
== 0, "expected return size 0, got %d\n", ret
);
171 ok( GetLastError() == ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
174 SetLastError(0xdeadbeef);
175 ret
= GetPrivateProfileSectionA( NULL
, buf
, sizeof(buf
), testfile4
);
176 ok( ret
== 0, "expected return size 0, got %d\n", ret
);
177 ok( GetLastError() == ERROR_INVALID_PARAMETER
||
178 broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
179 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
181 SetLastError(0xdeadbeef);
182 ret
= GetPrivateProfileSectionA( "section1", buf
, sizeof(buf
), NULL
);
183 ok( ret
== 0, "expected return size 0, got %d\n", ret
);
185 ok( GetLastError() == ERROR_FILE_NOT_FOUND
||
186 broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
187 "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
190 SetLastError(0xdeadbeef);
191 ret
=GetPrivateProfileSectionA("section1", buf
, sizeof(buf
), testfile4
);
192 for( p
= buf
+ strlen(buf
) + 1; *p
;p
+= strlen(p
)+1)
194 ok( ret
== 35 && !strcmp( buf
, "name1=val1,name2=,name3,name4=val4"), "wrong section returned(%d): %s\n",
196 ok( buf
[ret
-1] == 0 && buf
[ret
] == 0, "returned buffer not terminated with double-null\n" );
197 ok( GetLastError() == ERROR_SUCCESS
||
198 broken(GetLastError() == 0xdeadbeef), /* Win9x, WinME */
199 "expected ERROR_SUCCESS, got %d\n", GetLastError());
201 DeleteFileA( testfile4
);
204 static void test_profile_sections_names(void)
211 static const char content
[]="[section1]\r\n[section2]\r\n[section3]\r\n";
212 static const char testfile3
[]=".\\testwine3.ini";
213 static const WCHAR testfile3W
[]={ '.','\\','t','e','s','t','w','i','n','e','3','.','i','n','i',0 };
214 static const WCHAR not_here
[] = {'.','\\','n','o','t','_','h','e','r','e','.','i','n','i',0};
215 DeleteFileA( testfile3
);
216 h
= CreateFileA( testfile3
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
,
217 FILE_ATTRIBUTE_NORMAL
, NULL
);
218 ok( h
!= INVALID_HANDLE_VALUE
, " cannot create %s\n", testfile3
);
219 if( h
== INVALID_HANDLE_VALUE
) return;
220 WriteFile( h
, content
, sizeof(content
), &count
, NULL
);
223 /* Test with sufficiently large buffer */
224 memset(buf
, 0xc, sizeof(buf
));
225 ret
= GetPrivateProfileSectionNamesA( buf
, 29, testfile3
);
227 broken(ret
== 28), /* Win9x, WinME */
228 "expected return size 27, got %d\n", ret
);
229 ok( (buf
[ret
-1] == 0 && buf
[ret
] == 0) ||
230 broken(buf
[ret
-1] == 0 && buf
[ret
-2] == 0), /* Win9x, WinME */
231 "returned buffer not terminated with double-null\n" );
233 /* Test with exactly fitting buffer */
234 memset(buf
, 0xc, sizeof(buf
));
235 ret
= GetPrivateProfileSectionNamesA( buf
, 28, testfile3
);
237 broken(ret
== 28), /* Win9x, WinME */
238 "expected return size 26, got %d\n", ret
);
240 ok( (buf
[ret
+1] == 0 && buf
[ret
] == 0) || /* W2K3 and higher */
241 broken(buf
[ret
+1] == 0xc && buf
[ret
] == 0) || /* NT4, W2K, WinXP */
242 broken(buf
[ret
-1] == 0 && buf
[ret
-2] == 0), /* Win9x, WinME */
243 "returned buffer not terminated with double-null\n" );
245 /* Test with a buffer too small */
246 memset(buf
, 0xc, sizeof(buf
));
247 ret
= GetPrivateProfileSectionNamesA( buf
, 27, testfile3
);
248 ok( ret
== 25, "expected return size 25, got %d\n", ret
);
249 /* Win9x and WinME only fills the buffer with complete section names (double-null terminated) */
250 count
= strlen("section1") + sizeof(CHAR
) + strlen("section2");
252 ok( (buf
[ret
+1] == 0 && buf
[ret
] == 0) ||
253 broken(buf
[count
] == 0 && buf
[count
+1] == 0), /* Win9x, WinME */
254 "returned buffer not terminated with double-null\n" );
256 /* Tests on nonexistent file */
257 memset(buf
, 0xc, sizeof(buf
));
258 ret
= GetPrivateProfileSectionNamesA( buf
, 10, ".\\not_here.ini" );
260 broken(ret
== 1), /* Win9x, WinME */
261 "expected return size 0, got %d\n", ret
);
262 ok( buf
[0] == 0, "returned buffer not terminated with null\n" );
263 ok( buf
[1] != 0, "returned buffer terminated with double-null\n" );
265 /* Test with sufficiently large buffer */
266 SetLastError(0xdeadbeef);
267 memset(bufW
, 0xcc, sizeof(bufW
));
268 ret
= GetPrivateProfileSectionNamesW( bufW
, 29, testfile3W
);
269 if (ret
== 0 && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
))
271 win_skip("GetPrivateProfileSectionNamesW is not implemented\n");
272 DeleteFileA( testfile3
);
275 ok( ret
== 27, "expected return size 27, got %d\n", ret
);
276 ok( bufW
[ret
-1] == 0 && bufW
[ret
] == 0, "returned buffer not terminated with double-null\n" );
278 /* Test with exactly fitting buffer */
279 memset(bufW
, 0xcc, sizeof(bufW
));
280 ret
= GetPrivateProfileSectionNamesW( bufW
, 28, testfile3W
);
281 ok( ret
== 26, "expected return size 26, got %d\n", ret
);
282 ok( (bufW
[ret
+1] == 0 && bufW
[ret
] == 0) || /* W2K3 and higher */
283 broken(bufW
[ret
+1] == 0xcccc && bufW
[ret
] == 0), /* NT4, W2K, WinXP */
284 "returned buffer not terminated with double-null\n" );
286 /* Test with a buffer too small */
287 memset(bufW
, 0xcc, sizeof(bufW
));
288 ret
= GetPrivateProfileSectionNamesW( bufW
, 27, testfile3W
);
289 ok( ret
== 25, "expected return size 25, got %d\n", ret
);
290 ok( bufW
[ret
+1] == 0 && bufW
[ret
] == 0, "returned buffer not terminated with double-null\n" );
292 DeleteFileA( testfile3
);
294 /* Tests on nonexistent file */
295 memset(bufW
, 0xcc, sizeof(bufW
));
296 ret
= GetPrivateProfileSectionNamesW( bufW
, 10, not_here
);
297 ok( ret
== 0, "expected return size 0, got %d\n", ret
);
298 ok( bufW
[0] == 0, "returned buffer not terminated with null\n" );
299 ok( bufW
[1] != 0, "returned buffer terminated with double-null\n" );
302 /* If the ini-file has already been opened with CreateFile, WritePrivateProfileString failed in wine with an error ERROR_SHARING_VIOLATION, some testing here */
303 static void test_profile_existing(void)
305 static const char *testfile1
= ".\\winesharing1.ini";
306 static const char *testfile2
= ".\\winesharing2.ini";
308 static const struct {
309 DWORD dwDesiredAccess
;
315 {GENERIC_READ
, FILE_SHARE_READ
, ERROR_SHARING_VIOLATION
, FALSE
},
316 {GENERIC_READ
, FILE_SHARE_WRITE
, ERROR_SHARING_VIOLATION
, TRUE
},
317 {GENERIC_WRITE
, FILE_SHARE_READ
, ERROR_SHARING_VIOLATION
, FALSE
},
318 {GENERIC_WRITE
, FILE_SHARE_WRITE
, ERROR_SHARING_VIOLATION
, TRUE
},
319 {GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
, ERROR_SHARING_VIOLATION
, FALSE
},
320 {GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_WRITE
, ERROR_SHARING_VIOLATION
, TRUE
},
321 {GENERIC_READ
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, 0, FALSE
, ERROR_SHARING_VIOLATION
/* nt4 */},
322 {GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, 0, FALSE
, ERROR_SHARING_VIOLATION
/* nt4 */},
323 /*Thief demo (bug 5024) opens .ini file like this*/
324 {GENERIC_READ
|GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
, 0, FALSE
, ERROR_SHARING_VIOLATION
/* nt4 */}
331 char buffer
[MAX_PATH
];
333 for (i
=0; i
< sizeof(pe
)/sizeof(pe
[0]); i
++)
335 h
= CreateFile(testfile1
, pe
[i
].dwDesiredAccess
, pe
[i
].dwShareMode
, NULL
,
336 CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
337 ok(INVALID_HANDLE_VALUE
!= h
, "%d: CreateFile failed\n",i
);
338 SetLastError(0xdeadbeef);
340 ret
= WritePrivateProfileString(SECTION
, KEY
, "12345", testfile1
);
341 if (!pe
[i
].write_error
)
344 ok( broken(GetLastError() == pe
[i
].broken_error
),
345 "%d: WritePrivateProfileString failed with error %u\n", i
, GetLastError() );
347 size
= GetPrivateProfileString(SECTION
, KEY
, 0, buffer
, MAX_PATH
, testfile1
);
349 ok( size
== 5, "%d: test failed, number of characters copied: %d instead of 5\n", i
, size
);
351 ok( !size
, "%d: test failed, number of characters copied: %d instead of 0\n", i
, size
);
355 DWORD err
= GetLastError();
356 ok( !ret
, "%d: WritePrivateProfileString succeeded\n", i
);
358 ok( err
== pe
[i
].write_error
, "%d: WritePrivateProfileString failed with error %u/%u\n",
359 i
, err
, pe
[i
].write_error
);
361 size
= GetPrivateProfileString(SECTION
, KEY
, 0, buffer
, MAX_PATH
, testfile1
);
362 ok( !size
, "%d: test failed, number of characters copied: %d instead of 0\n", i
, size
);
365 ok( DeleteFile(testfile1
), "delete failed\n" );
368 h
= CreateFile(testfile2
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
369 sprintf( buffer
, "[%s]\r\n%s=123\r\n", SECTION
, KEY
);
370 ok( WriteFile( h
, buffer
, strlen(buffer
), &size
, NULL
), "failed to write\n" );
373 for (i
=0; i
< sizeof(pe
)/sizeof(pe
[0]); i
++)
375 h
= CreateFile(testfile2
, pe
[i
].dwDesiredAccess
, pe
[i
].dwShareMode
, NULL
,
376 OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
377 ok(INVALID_HANDLE_VALUE
!= h
, "%d: CreateFile failed\n",i
);
378 SetLastError(0xdeadbeef);
379 ret
= GetPrivateProfileStringA(SECTION
, KEY
, NULL
, buffer
, MAX_PATH
, testfile2
);
380 /* Win9x and WinME returns 0 for all cases except the first one */
381 if (!pe
[i
].read_error
)
383 broken(!ret
&& GetLastError() == 0xdeadbeef), /* Win9x, WinME */
384 "%d: GetPrivateProfileString failed with error %u\n", i
, GetLastError() );
386 ok( !ret
, "%d: GetPrivateProfileString succeeded\n", i
);
389 ok( DeleteFile(testfile2
), "delete failed\n" );
392 static void test_profile_delete_on_close(void)
394 static CHAR testfile
[] = ".\\testwine5.ini";
397 static const char contents
[] = "[" SECTION
"]\n" KEY
"=123\n";
399 h
= CreateFile(testfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
400 CREATE_ALWAYS
, FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
401 ok( WriteFile( h
, contents
, sizeof contents
- 1, &size
, NULL
),
402 "Cannot write test file: %x\n", GetLastError() );
403 ok( size
== sizeof contents
- 1, "Test file: partial write\n");
405 SetLastError(0xdeadbeef);
406 res
= GetPrivateProfileInt(SECTION
, KEY
, 0, testfile
);
408 broken(res
== 0 && GetLastError() == ERROR_SHARING_VIOLATION
), /* Win9x, WinME */
409 "Got %d instead of 123\n", res
);
411 /* This also deletes the file */
415 static void test_profile_refresh(void)
417 static CHAR testfile
[] = ".\\winetest4.ini";
420 static const char contents1
[] = "[" SECTION
"]\n" KEY
"=123\n";
421 static const char contents2
[] = "[" SECTION
"]\n" KEY
"=124\n";
423 h
= CreateFile(testfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
424 CREATE_ALWAYS
, FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
425 ok( WriteFile( h
, contents1
, sizeof contents1
- 1, &size
, NULL
),
426 "Cannot write test file: %x\n", GetLastError() );
427 ok( size
== sizeof contents1
- 1, "Test file: partial write\n");
429 SetLastError(0xdeadbeef);
430 res
= GetPrivateProfileInt(SECTION
, KEY
, 0, testfile
);
432 broken(res
== 0 && GetLastError() == ERROR_SHARING_VIOLATION
), /* Win9x, WinME */
433 "Got %d instead of 123\n", res
);
437 /* Test proper invalidation of wine's profile file cache */
439 h
= CreateFile(testfile
, GENERIC_WRITE
, FILE_SHARE_READ
, NULL
,
440 CREATE_ALWAYS
, FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
441 ok( WriteFile( h
, contents2
, sizeof contents2
- 1, &size
, NULL
),
442 "Cannot write test file: %x\n", GetLastError() );
443 ok( size
== sizeof contents2
- 1, "Test file: partial write\n");
445 SetLastError(0xdeadbeef);
446 res
= GetPrivateProfileInt(SECTION
, KEY
, 0, testfile
);
448 broken(res
== 0 && GetLastError() == 0xdeadbeef), /* Win9x, WinME */
449 "Got %d instead of 124\n", res
);
451 /* This also deletes the file */
455 static void create_test_file(LPCSTR name
, LPCSTR data
, DWORD size
)
460 hfile
= CreateFileA(name
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
461 ok(hfile
!= INVALID_HANDLE_VALUE
, "cannot create %s\n", name
);
462 WriteFile(hfile
, data
, size
, &count
, NULL
);
466 static BOOL
emptystr_ok(CHAR emptystr
[MAX_PATH
])
470 for(i
= 0;i
< MAX_PATH
;++i
)
473 trace("emptystr[%d] = %d\n",i
,emptystr
[i
]);
480 static void test_GetPrivateProfileString(const char *content
, const char *descript
)
484 CHAR def_val
[MAX_PATH
];
486 CHAR windir
[MAX_PATH
];
487 /* NT series crashes on r/o empty strings, so pass an r/w
488 empty string and check for modification */
489 CHAR emptystr
[MAX_PATH
] = "";
492 static const char filename
[] = ".\\winetest.ini";
494 trace("test_GetPrivateProfileStringA: %s\n", descript
);
496 if(!lstrcmpA(descript
, "CR only"))
498 SetLastError(0xdeadbeef);
499 ret
= GetPrivateProfileStringW(NULL
, NULL
, NULL
,
501 if (!ret
&& GetLastError() == ERROR_CALL_NOT_IMPLEMENTED
)
503 win_skip("Win9x and WinME don't handle 'CR only' correctly\n");
508 create_test_file(filename
, content
, lstrlenA(content
));
510 /* Run this test series with caching. Wine won't cache profile
511 files younger than 2.1 seconds. */
514 /* lpAppName is NULL */
515 memset(buf
, 0xc, sizeof(buf
));
516 lstrcpyA(buf
, "kumquat");
517 ret
= GetPrivateProfileStringA(NULL
, "name1", "default",
518 buf
, MAX_PATH
, filename
);
520 broken(ret
== 19), /* Win9x and WinME */
521 "Expected 18, got %d\n", ret
);
522 len
= lstrlenA("section1") + sizeof(CHAR
) + lstrlenA("section2") + 2 * sizeof(CHAR
);
523 ok(!memcmp(buf
, "section1\0section2\0\0", len
),
524 "Expected \"section1\\0section2\\0\\0\", got \"%s\"\n", buf
);
526 /* lpAppName is empty */
527 memset(buf
, 0xc, sizeof(buf
));
528 lstrcpyA(buf
, "kumquat");
529 ret
= GetPrivateProfileStringA(emptystr
, "name1", "default",
530 buf
, MAX_PATH
, filename
);
531 ok(ret
== 7, "Expected 7, got %d\n", ret
);
532 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
533 ok(emptystr_ok(emptystr
), "AppName modified\n");
535 /* lpAppName is missing */
536 memset(buf
, 0xc,sizeof(buf
));
537 lstrcpyA(buf
, "kumquat");
538 ret
= GetPrivateProfileStringA("notasection", "name1", "default",
539 buf
, MAX_PATH
, filename
);
540 ok(ret
== 7, "Expected 7, got %d\n", ret
);
541 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
543 /* lpAppName is empty, lpDefault is NULL */
544 memset(buf
, 0xc,sizeof(buf
));
545 lstrcpyA(buf
, "kumquat");
546 ret
= GetPrivateProfileStringA(emptystr
, "name1", NULL
,
547 buf
, MAX_PATH
, filename
);
548 ok(ret
== 0, "Expected 0, got %d\n", ret
);
549 ok(!lstrcmpA(buf
, "") ||
550 broken(!lstrcmpA(buf
, "kumquat")), /* Win9x, WinME */
551 "Expected \"\", got \"%s\"\n", buf
);
552 ok(emptystr_ok(emptystr
), "AppName modified\n");
554 /* lpAppName is empty, lpDefault is empty */
555 memset(buf
, 0xc,sizeof(buf
));
556 lstrcpyA(buf
, "kumquat");
557 ret
= GetPrivateProfileStringA(emptystr
, "name1", "",
558 buf
, MAX_PATH
, filename
);
559 ok(ret
== 0, "Expected 0, got %d\n", ret
);
560 ok(!lstrcmpA(buf
, ""), "Expected \"\", got \"%s\"\n", buf
);
561 ok(emptystr_ok(emptystr
), "AppName modified\n");
563 /* lpAppName is empty, lpDefault has trailing blank characters */
564 memset(buf
, 0xc,sizeof(buf
));
565 lstrcpyA(buf
, "kumquat");
566 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
567 lstrcpyA(def_val
, "default ");
568 ret
= GetPrivateProfileStringA(emptystr
, "name1", def_val
,
569 buf
, MAX_PATH
, filename
);
570 ok(ret
== 7, "Expected 7, got %d\n", ret
);
571 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
572 ok(emptystr_ok(emptystr
), "AppName modified\n");
574 /* lpAppName is empty, many blank characters in lpDefault */
575 memset(buf
, 0xc,sizeof(buf
));
576 lstrcpyA(buf
, "kumquat");
577 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
578 lstrcpyA(def_val
, "one two ");
579 ret
= GetPrivateProfileStringA(emptystr
, "name1", def_val
,
580 buf
, MAX_PATH
, filename
);
581 ok(ret
== 7, "Expected 7, got %d\n", ret
);
582 ok(!lstrcmpA(buf
, "one two"), "Expected \"one two\", got \"%s\"\n", buf
);
583 ok(emptystr_ok(emptystr
), "AppName modified\n");
585 /* lpAppName is empty, blank character but not trailing in lpDefault */
586 memset(buf
, 0xc,sizeof(buf
));
587 lstrcpyA(buf
, "kumquat");
588 ret
= GetPrivateProfileStringA(emptystr
, "name1", "one two",
589 buf
, MAX_PATH
, filename
);
590 ok(ret
== 7, "Expected 7, got %d\n", ret
);
591 ok(!lstrcmpA(buf
, "one two"), "Expected \"one two\", got \"%s\"\n", buf
);
592 ok(emptystr_ok(emptystr
), "AppName modified\n");
594 /* lpKeyName is NULL */
595 memset(buf
, 0xc,sizeof(buf
));
596 lstrcpyA(buf
, "kumquat");
597 ret
= GetPrivateProfileStringA("section1", NULL
, "default",
598 buf
, MAX_PATH
, filename
);
599 ok(ret
== 18, "Expected 18, got %d\n", ret
);
600 ok(!memcmp(buf
, "name1\0name2\0name4\0", ret
+ 1),
601 "Expected \"name1\\0name2\\0name4\\0\", got \"%s\"\n", buf
);
603 /* lpKeyName is empty */
604 memset(buf
, 0xc,sizeof(buf
));
605 lstrcpyA(buf
, "kumquat");
606 ret
= GetPrivateProfileStringA("section1", emptystr
, "default",
607 buf
, MAX_PATH
, filename
);
608 ok(ret
== 7, "Expected 7, got %d\n", ret
);
609 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
610 ok(emptystr_ok(emptystr
), "KeyName modified\n");
612 /* lpKeyName is missing */
613 memset(buf
, 0xc,sizeof(buf
));
614 lstrcpyA(buf
, "kumquat");
615 ret
= GetPrivateProfileStringA("section1", "notakey", "default",
616 buf
, MAX_PATH
, filename
);
617 ok(ret
== 7, "Expected 7, got %d\n", ret
);
618 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
620 /* lpKeyName is empty, lpDefault is NULL */
621 memset(buf
, 0xc,sizeof(buf
));
622 lstrcpyA(buf
, "kumquat");
623 ret
= GetPrivateProfileStringA("section1", emptystr
, NULL
,
624 buf
, MAX_PATH
, filename
);
625 ok(ret
== 0, "Expected 0, got %d\n", ret
);
626 ok(!lstrcmpA(buf
, "") ||
627 broken(!lstrcmpA(buf
, "kumquat")), /* Win9x, WinME */
628 "Expected \"\", got \"%s\"\n", buf
);
629 ok(emptystr_ok(emptystr
), "KeyName modified\n");
631 /* lpKeyName is empty, lpDefault is empty */
632 memset(buf
, 0xc,sizeof(buf
));
633 lstrcpyA(buf
, "kumquat");
634 ret
= GetPrivateProfileStringA("section1", emptystr
, "",
635 buf
, MAX_PATH
, filename
);
636 ok(ret
== 0, "Expected 0, got %d\n", ret
);
637 ok(!lstrcmpA(buf
, ""), "Expected \"\", got \"%s\"\n", buf
);
638 ok(emptystr_ok(emptystr
), "KeyName modified\n");
640 /* lpKeyName is empty, lpDefault has trailing blank characters */
641 memset(buf
, 0xc,sizeof(buf
));
642 lstrcpyA(buf
, "kumquat");
643 /* lpDefault must be writable (trailing blanks are removed inplace in win9x) */
644 lstrcpyA(def_val
, "default ");
645 ret
= GetPrivateProfileStringA("section1", emptystr
, def_val
,
646 buf
, MAX_PATH
, filename
);
647 ok(ret
== 7, "Expected 7, got %d\n", ret
);
648 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
649 ok(emptystr_ok(emptystr
), "KeyName modified\n");
653 /* lpReturnedString is NULL */
654 ret
= GetPrivateProfileStringA("section1", "name1", "default",
655 NULL
, MAX_PATH
, filename
);
658 /* lpFileName is NULL */
659 memset(buf
, 0xc,sizeof(buf
));
660 lstrcpyA(buf
, "kumquat");
661 ret
= GetPrivateProfileStringA("section1", "name1", "default",
662 buf
, MAX_PATH
, NULL
);
664 broken(ret
== 0), /* Win9x, WinME */
665 "Expected 7, got %d\n", ret
);
666 ok(!lstrcmpA(buf
, "default") ||
667 broken(!lstrcmpA(buf
, "kumquat")), /* Win9x, WinME */
668 "Expected \"default\", got \"%s\"\n", buf
);
670 /* lpFileName is empty */
671 memset(buf
, 0xc,sizeof(buf
));
672 lstrcpyA(buf
, "kumquat");
673 ret
= GetPrivateProfileStringA("section1", "name1", "default",
675 ok(ret
== 7, "Expected 7, got %d\n", ret
);
676 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
678 /* lpFileName is nonexistent */
679 memset(buf
, 0xc,sizeof(buf
));
680 lstrcpyA(buf
, "kumquat");
681 ret
= GetPrivateProfileStringA("section1", "name1", "default",
682 buf
, MAX_PATH
, "nonexistent");
683 ok(ret
== 7, "Expected 7, got %d\n", ret
);
684 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
687 memset(buf
, 0xc,sizeof(buf
));
688 lstrcpyA(buf
, "kumquat");
689 ret
= GetPrivateProfileStringA("section1", "name1", "default",
691 ok(ret
== 0, "Expected 0, got %d\n", ret
);
692 ok(!lstrcmpA(buf
, "kumquat"), "Expected buf to be unchanged, got \"%s\"\n", buf
);
694 /* nSize is exact size of output */
695 memset(buf
, 0xc,sizeof(buf
));
696 lstrcpyA(buf
, "kumquat");
697 ret
= GetPrivateProfileStringA("section1", "name1", "default",
699 ok(ret
== 3, "Expected 3, got %d\n", ret
);
700 ok(!lstrcmpA(buf
, "val"), "Expected \"val\", got \"%s\"\n", buf
);
702 /* nSize has room for NULL terminator */
703 memset(buf
, 0xc,sizeof(buf
));
704 lstrcpyA(buf
, "kumquat");
705 ret
= GetPrivateProfileStringA("section1", "name1", "default",
707 ok(ret
== 4, "Expected 4, got %d\n", ret
);
708 ok(!lstrcmpA(buf
, "val1"), "Expected \"val1\", got \"%s\"\n", buf
);
710 /* output is 1 character */
711 memset(buf
, 0xc,sizeof(buf
));
712 lstrcpyA(buf
, "kumquat");
713 ret
= GetPrivateProfileStringA("section1", "name4", "default",
714 buf
, MAX_PATH
, filename
);
715 ok(ret
== 1, "Expected 1, got %d\n", ret
);
716 ok(!lstrcmpA(buf
, "a"), "Expected \"a\", got \"%s\"\n", buf
);
718 /* output is 1 character, no room for NULL terminator */
719 memset(buf
, 0xc,sizeof(buf
));
720 lstrcpyA(buf
, "kumquat");
721 ret
= GetPrivateProfileStringA("section1", "name4", "default",
723 ok(ret
== 0, "Expected 0, got %d\n", ret
);
724 ok(!lstrcmpA(buf
, ""), "Expected \"\", got \"%s\"\n", buf
);
726 /* lpAppName is NULL, not enough room for final section name */
727 memset(buf
, 0xc,sizeof(buf
));
728 lstrcpyA(buf
, "kumquat");
729 ret
= GetPrivateProfileStringA(NULL
, "name1", "default",
731 ok(ret
== 14, "Expected 14, got %d\n", ret
);
732 len
= lstrlenA("section1") + 2 * sizeof(CHAR
);
734 ok(!memcmp(buf
, "section1\0secti\0\0", ret
+ 2) ||
735 broken(!memcmp(buf
, "section1\0\0", len
)), /* Win9x, WinME */
736 "Expected \"section1\\0secti\\0\\0\", got \"%s\"\n", buf
);
738 /* lpKeyName is NULL, not enough room for final key name */
739 memset(buf
, 0xc,sizeof(buf
));
740 lstrcpyA(buf
, "kumquat");
741 ret
= GetPrivateProfileStringA("section1", NULL
, "default",
743 ok(ret
== 14, "Expected 14, got %d\n", ret
);
745 ok(!memcmp(buf
, "name1\0name2\0na\0\0", ret
+ 2) ||
746 broken(!memcmp(buf
, "name1\0name2\0n\0\0", ret
+ 1)), /* Win9x, WinME */
747 "Expected \"name1\\0name2\\0na\\0\\0\", got \"%s\"\n", buf
);
749 /* key value has quotation marks which are stripped */
750 memset(buf
, 0xc,sizeof(buf
));
751 lstrcpyA(buf
, "kumquat");
752 ret
= GetPrivateProfileStringA("section1", "name2", "default",
753 buf
, MAX_PATH
, filename
);
754 ok(ret
== 4, "Expected 4, got %d\n", ret
);
755 ok(!lstrcmpA(buf
, "val2"), "Expected \"val2\", got \"%s\"\n", buf
);
757 /* case does not match */
758 memset(buf
, 0xc,sizeof(buf
));
759 lstrcpyA(buf
, "kumquat");
760 ret
= GetPrivateProfileStringA("section1", "NaMe1", "default",
761 buf
, MAX_PATH
, filename
);
762 ok(ret
== 4, "Expected 4, got %d\n", ret
);
763 ok(!lstrcmpA(buf
, "val1"), "Expected \"val1\", got \"%s\"\n", buf
);
765 /* only filename is used */
766 memset(buf
, 0xc,sizeof(buf
));
767 lstrcpyA(buf
, "kumquat");
768 ret
= GetPrivateProfileStringA("section1", "NaMe1", "default",
769 buf
, MAX_PATH
, "winetest.ini");
770 ok(ret
== 7, "Expected 7, got %d\n", ret
);
771 ok(!lstrcmpA(buf
, "default"), "Expected \"default\", got \"%s\"\n", buf
);
773 GetWindowsDirectoryA(windir
, MAX_PATH
);
774 SetLastError(0xdeadbeef);
775 ret
= GetTempFileNameA(windir
, "pre", 0, path
);
776 if (!ret
&& GetLastError() == ERROR_ACCESS_DENIED
)
778 skip("Not allowed to create a file in the Windows directory\n");
779 DeleteFileA(filename
);
782 tempfile
= strrchr(path
, '\\') + 1;
783 create_test_file(path
, content
, lstrlenA(content
));
785 /* only filename is used, file exists in windows directory */
786 memset(buf
, 0xc,sizeof(buf
));
787 lstrcpyA(buf
, "kumquat");
788 ret
= GetPrivateProfileStringA("section1", "NaMe1", "default",
789 buf
, MAX_PATH
, tempfile
);
790 ok(ret
== 4, "Expected 4, got %d\n", ret
);
791 ok(!lstrcmpA(buf
, "val1"), "Expected \"val1\", got \"%s\"\n", buf
);
793 /* successful case */
794 memset(buf
, 0xc,sizeof(buf
));
795 lstrcpyA(buf
, "kumquat");
796 ret
= GetPrivateProfileStringA("section1", "name1", "default",
797 buf
, MAX_PATH
, filename
);
798 ok(ret
== 4, "Expected 4, got %d\n", ret
);
799 ok(!lstrcmpA(buf
, "val1"), "Expected \"val1\", got \"%s\"\n", buf
);
802 DeleteFileA(filename
);
805 static BOOL
check_file_data(LPCSTR path
, LPCSTR data
)
812 file
= CreateFileA(path
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
813 if (file
== INVALID_HANDLE_VALUE
)
816 size
= GetFileSize(file
, NULL
);
818 ret
= ReadFile(file
, buf
, size
, &size
, NULL
);
823 if (!*data
&& size
!= 0)
826 return !lstrcmpA(buf
, data
);
829 static void test_WritePrivateProfileString(void)
836 GetTempPathA(MAX_PATH
, temp
);
837 GetTempFileNameA(temp
, "wine", 0, path
);
840 /* path is not created yet */
843 SetLastError(0xdeadbeef);
844 ret
= WritePrivateProfileStringA(NULL
, "key", "string", path
);
845 ok(ret
== FALSE
, "Expected FALSE, got %d\n", ret
);
846 ok(GetLastError() == ERROR_FILE_NOT_FOUND
||
847 broken(GetLastError() == ERROR_INVALID_PARAMETER
) || /* NT4 */
848 broken(GetLastError() == 0xdeadbeef), /* Win9x and WinME */
849 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
850 ok(GetFileAttributesA(path
) == INVALID_FILE_ATTRIBUTES
,
851 "Expected path to not exist\n");
853 GetTempFileNameA(temp
, "wine", 0, path
);
855 /* NULL lpAppName, path exists */
857 SetLastError(0xdeadbeef);
858 ret
= WritePrivateProfileStringA(NULL
, "key", "string", path
);
859 ok(ret
== FALSE
, "Expected FALSE, got %d\n", ret
);
860 ok(GetLastError() == ERROR_FILE_NOT_FOUND
||
861 broken(GetLastError() == ERROR_INVALID_PARAMETER
) || /* NT4 */
862 broken(GetLastError() == 0xdeadbeef), /* Win9x and WinME */
863 "Expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
864 ok(check_file_data(path
, data
), "File doesn't match\n");
869 /* empty lpAppName, crashes on NT4 and higher */
872 ret
= WritePrivateProfileStringA("", "key", "string", path
);
873 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
874 ok(check_file_data(path
, data
), "File doesn't match\n");
880 ret
= WritePrivateProfileStringA("App", NULL
, "string", path
);
881 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
884 ok(check_file_data(path
, data
), "File doesn't match\n");
890 /* empty lpKeyName, crashes on NT4 and higher */
893 ret
= WritePrivateProfileStringA("App", "", "string", path
);
894 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
897 ok(check_file_data(path
, data
), "File doesn't match\n");
904 ret
= WritePrivateProfileStringA("App", "key", NULL
, path
);
905 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
908 ok(check_file_data(path
, data
), "File doesn't match\n");
915 ret
= WritePrivateProfileStringA("App", "key", "", path
);
916 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
917 ok(check_file_data(path
, data
), "File doesn't match\n");
920 /* empty lpFileName */
921 SetLastError(0xdeadbeef);
922 ret
= WritePrivateProfileStringA("App", "key", "string", "");
923 ok(ret
== FALSE
, "Expected FALSE, got %d\n", ret
);
924 ok(GetLastError() == ERROR_ACCESS_DENIED
,
925 "Expected ERROR_ACCESS_DENIED, got %d\n", GetLastError());
927 /* The resulting file will be X:\\%WINDIR%\\win1.tmp */
928 GetWindowsDirectoryA(temp
, MAX_PATH
);
929 GetTempFileNameA(temp
, "win", 1, path
);
932 /* relative path in lpFileName */
935 ret
= WritePrivateProfileStringA("App", "key", "string", "win1.tmp");
936 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
937 ok(check_file_data(path
, data
), "File doesn't match\n");
940 GetTempPathA(MAX_PATH
, temp
);
941 GetTempFileNameA(temp
, "wine", 0, path
);
943 /* build up an INI file */
944 WritePrivateProfileStringA("App1", "key1", "string1", path
);
945 WritePrivateProfileStringA("App1", "key2", "string2", path
);
946 WritePrivateProfileStringA("App1", "key3", "string3", path
);
947 WritePrivateProfileStringA("App2", "key4", "string4", path
);
949 /* make an addition and verify the INI */
958 ret
= WritePrivateProfileStringA("App3", "key5", "string5", path
);
959 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
962 ok(check_file_data(path
, data
), "File doesn't match\n");
965 /* lpString is NULL, key2 key is deleted */
973 ret
= WritePrivateProfileStringA("App1", "key2", NULL
, path
);
974 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
977 ok(check_file_data(path
, data
), "File doesn't match\n");
980 /* try to delete key2 again */
988 ret
= WritePrivateProfileStringA("App1", "key2", NULL
, path
);
989 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
992 ok(check_file_data(path
, data
), "File doesn't match\n");
995 /* lpKeyName is NULL, App1 section is deleted */
1000 ret
= WritePrivateProfileStringA("App1", NULL
, "string1", path
);
1001 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
1004 ok(check_file_data(path
, data
), "File doesn't match\n");
1007 /* lpString is not needed to delete a section */
1010 ret
= WritePrivateProfileStringA("App2", NULL
, NULL
, path
);
1011 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
1014 ok(check_file_data(path
, data
), "File doesn't match\n");
1017 /* leave just the section */
1018 data
= "[App3]\r\n";
1019 ret
= WritePrivateProfileStringA("App3", "key5", NULL
, path
);
1020 ok(ret
== TRUE
, "Expected TRUE, got %d\n", ret
);
1023 ok(check_file_data(path
, data
), "File doesn't match\n");
1032 test_profile_string();
1033 test_profile_sections();
1034 test_profile_sections_names();
1035 test_profile_existing();
1036 test_profile_delete_on_close();
1037 test_profile_refresh();
1038 test_GetPrivateProfileString(
1041 "name2=\"val2\"\r\n"
1046 test_GetPrivateProfileString(
1054 test_WritePrivateProfileString();