sync rosapps to r44454
[reactos.git] / rostests / winetests / kernel32 / profile.c
1 /*
2 * Unit tests for profile functions
3 *
4 * Copyright (c) 2003 Stefan Leichter
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdarg.h>
22
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "windows.h"
27
28 #define KEY "ProfileInt"
29 #define SECTION "Test"
30 #define TESTFILE ".\\testwine.ini"
31 #define TESTFILE2 ".\\testwine2.ini"
32
33 struct _profileInt {
34 LPCSTR section;
35 LPCSTR key;
36 LPCSTR value;
37 LPCSTR iniFile;
38 INT defaultVal;
39 UINT result;
40 UINT result9x;
41 };
42
43 static void test_profile_int(void)
44 {
45 struct _profileInt profileInt[]={
46 { NULL, NULL, NULL, NULL, 70, 0 , 0}, /* 0 */
47 { NULL, NULL, NULL, TESTFILE, -1, 4294967295U, 0},
48 { NULL, NULL, NULL, TESTFILE, 1, 1 , 0},
49 { SECTION, NULL, NULL, TESTFILE, -1, 4294967295U, 0},
50 { SECTION, NULL, NULL, TESTFILE, 1, 1 , 0},
51 { NULL, KEY, NULL, TESTFILE, -1, 4294967295U, 0}, /* 5 */
52 { NULL, KEY, NULL, TESTFILE, 1, 1 , 0},
53 { SECTION, KEY, NULL, TESTFILE, -1, 4294967295U, 4294967295U},
54 { SECTION, KEY, NULL, TESTFILE, 1, 1 , 1},
55 { SECTION, KEY, "-1", TESTFILE, -1, 4294967295U, 4294967295U},
56 { SECTION, KEY, "-1", TESTFILE, 1, 4294967295U, 4294967295U}, /* 10 */
57 { SECTION, KEY, "1", TESTFILE, -1, 1 , 1},
58 { SECTION, KEY, "1", TESTFILE, 1, 1 , 1},
59 { SECTION, KEY, "+1", TESTFILE, -1, 1 , 0},
60 { SECTION, KEY, "+1", TESTFILE, 1, 1 , 0},
61 { SECTION, KEY, "4294967296", TESTFILE, -1, 0 , 0}, /* 15 */
62 { SECTION, KEY, "4294967296", TESTFILE, 1, 0 , 0},
63 { SECTION, KEY, "4294967297", TESTFILE, -1, 1 , 1},
64 { SECTION, KEY, "4294967297", TESTFILE, 1, 1 , 1},
65 { SECTION, KEY, "-4294967297", TESTFILE, -1, 4294967295U, 4294967295U},
66 { SECTION, KEY, "-4294967297", TESTFILE, 1, 4294967295U, 4294967295U}, /* 20 */
67 { SECTION, KEY, "42A94967297", TESTFILE, -1, 42 , 42},
68 { SECTION, KEY, "42A94967297", TESTFILE, 1, 42 , 42},
69 { SECTION, KEY, "B4294967297", TESTFILE, -1, 0 , 0},
70 { SECTION, KEY, "B4294967297", TESTFILE, 1, 0 , 0},
71 };
72 int i, num_test = (sizeof(profileInt)/sizeof(struct _profileInt));
73 UINT res;
74
75 DeleteFileA( TESTFILE);
76
77 for (i=0; i < num_test; i++) {
78 if (profileInt[i].value)
79 WritePrivateProfileStringA(SECTION, KEY, profileInt[i].value,
80 profileInt[i].iniFile);
81
82 res = GetPrivateProfileIntA(profileInt[i].section, profileInt[i].key,
83 profileInt[i].defaultVal, profileInt[i].iniFile);
84 ok((res == profileInt[i].result) || (res == profileInt[i].result9x),
85 "test<%02d>: ret<%010u> exp<%010u><%010u>\n",
86 i, res, profileInt[i].result, profileInt[i].result9x);
87 }
88
89 DeleteFileA( TESTFILE);
90 }
91
92 static void test_profile_string(void)
93 {
94 HANDLE h;
95 int ret;
96 DWORD count;
97 char buf[100];
98 char *p;
99 /* test that lines without an '=' will not be enumerated */
100 /* in the case below, name2 is a key while name3 is not. */
101 char content[]="[s]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n";
102 DeleteFileA( TESTFILE2);
103 h = CreateFileA( TESTFILE2, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
104 FILE_ATTRIBUTE_NORMAL, NULL);
105 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", TESTFILE2);
106 if( h == INVALID_HANDLE_VALUE) return;
107 WriteFile( h, content, sizeof(content), &count, NULL);
108 CloseHandle( h);
109
110 /* enumerate the keys */
111 ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
112 TESTFILE2);
113 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
114 p[-1] = ',';
115 /* and test */
116 ok( ret == 18 && !strcmp( buf, "name1,name2,name4"), "wrong keys returned(%d): %s\n", ret,
117 buf);
118
119 /* add a new key to test that the file is quite usable */
120 WritePrivateProfileStringA( "s", "name5", "val5", TESTFILE2);
121 ret=GetPrivateProfileStringA( "s", NULL, "", buf, sizeof(buf),
122 TESTFILE2);
123 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
124 p[-1] = ',';
125 ok( ret == 24 && !strcmp( buf, "name1,name2,name4,name5"), "wrong keys returned(%d): %s\n",
126 ret, buf);
127
128 DeleteFileA( TESTFILE2);
129 }
130
131 static void test_profile_sections(void)
132 {
133 HANDLE h;
134 int ret;
135 DWORD count;
136 char buf[100];
137 char *p;
138 static const char content[]="[section1]\r\nname1=val1\r\nname2=\r\nname3\r\nname4=val4\r\n[section2]\r\n";
139 static const char testfile4[]=".\\testwine4.ini";
140 BOOL on_win98 = FALSE;
141
142 DeleteFileA( testfile4 );
143 h = CreateFileA( testfile4, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
144 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile4);
145 if( h == INVALID_HANDLE_VALUE) return;
146 WriteFile( h, content, sizeof(content), &count, NULL);
147 CloseHandle( h);
148
149 /* Some parameter checking */
150 SetLastError(0xdeadbeef);
151 ret = GetPrivateProfileSectionA( NULL, NULL, 0, NULL );
152 ok( ret == 0, "expected return size 0, got %d\n", ret );
153 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
154 GetLastError() == 0xdeadbeef /* Win98 */,
155 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
156 if (GetLastError() == 0xdeadbeef) on_win98 = TRUE;
157
158 SetLastError(0xdeadbeef);
159 ret = GetPrivateProfileSectionA( NULL, NULL, 0, testfile4 );
160 ok( ret == 0, "expected return size 0, got %d\n", ret );
161 ok( GetLastError() == ERROR_INVALID_PARAMETER ||
162 GetLastError() == 0xdeadbeef /* Win98 */,
163 "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
164
165 if (!on_win98)
166 {
167 SetLastError(0xdeadbeef);
168 ret = GetPrivateProfileSectionA( "section1", NULL, 0, testfile4 );
169 ok( ret == 0, "expected return size 0, got %d\n", ret );
170 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
171 }
172
173 SetLastError(0xdeadbeef);
174 ret = GetPrivateProfileSectionA( NULL, buf, sizeof(buf), testfile4 );
175 ok( ret == 0, "expected return size 0, got %d\n", ret );
176 ok( GetLastError() == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %d\n", GetLastError());
177
178 SetLastError(0xdeadbeef);
179 ret = GetPrivateProfileSectionA( "section1", buf, sizeof(buf), NULL );
180 ok( ret == 0, "expected return size 0, got %d\n", ret );
181 todo_wine
182 ok( GetLastError() == ERROR_FILE_NOT_FOUND, "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
183
184 /* And a real one */
185 ret=GetPrivateProfileSectionA("section1", buf, sizeof(buf), testfile4);
186 for( p = buf + strlen(buf) + 1; *p;p += strlen(p)+1)
187 p[-1] = ',';
188 ok( ret == 35 && !strcmp( buf, "name1=val1,name2=,name3,name4=val4"), "wrong section returned(%d): %s\n",
189 ret, buf);
190 ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
191 ok( GetLastError() == S_OK, "expected S_OK, got %d\n", GetLastError());
192
193 DeleteFileA( testfile4 );
194 }
195
196 static void test_profile_sections_names(void)
197 {
198 HANDLE h;
199 int ret;
200 DWORD count;
201 char buf[100];
202 WCHAR bufW[100];
203 static const char content[]="[section1]\r\n[section2]\r\n[section3]\r\n";
204 static const char testfile3[]=".\\testwine3.ini";
205 static const WCHAR testfile3W[]={ '.','\\','t','e','s','t','w','i','n','e','3','.','i','n','i',0 };
206 static const WCHAR not_here[] = {'.','\\','n','o','t','_','h','e','r','e','.','i','n','i',0};
207 DeleteFileA( testfile3 );
208 h = CreateFileA( testfile3, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
209 FILE_ATTRIBUTE_NORMAL, NULL);
210 ok( h != INVALID_HANDLE_VALUE, " cannot create %s\n", testfile3);
211 if( h == INVALID_HANDLE_VALUE) return;
212 WriteFile( h, content, sizeof(content), &count, NULL);
213 CloseHandle( h);
214
215 /* Test with sufficiently large buffer */
216 ret = GetPrivateProfileSectionNamesA( buf, 29, testfile3 );
217 ok( ret == 27, "expected return size 27, got %d\n", ret );
218 ok( buf[ret-1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
219
220 /* Test with exactly fitting buffer */
221 ret = GetPrivateProfileSectionNamesA( buf, 28, testfile3 );
222 ok( ret == 26, "expected return size 26, got %d\n", ret );
223 ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
224
225 /* Test with a buffer too small */
226 ret = GetPrivateProfileSectionNamesA( buf, 27, testfile3 );
227 ok( ret == 25, "expected return size 25, got %d\n", ret );
228 ok( buf[ret+1] == 0 && buf[ret] == 0, "returned buffer not terminated with double-null\n" );
229
230 /* Tests on nonexistent file */
231 memset(buf, 0xcc, sizeof(buf));
232 ret = GetPrivateProfileSectionNamesA( buf, 10, ".\\not_here.ini" );
233 ok( ret == 0, "expected return size 0, got %d\n", ret );
234 ok( buf[0] == 0, "returned buffer not terminated with null\n" );
235 ok( buf[1] != 0, "returned buffer terminated with double-null\n" );
236
237 /* Test with sufficiently large buffer */
238 SetLastError(0xdeadbeef);
239 ret = GetPrivateProfileSectionNamesW( bufW, 29, testfile3W );
240 if (ret == 0 && (GetLastError() == ERROR_CALL_NOT_IMPLEMENTED))
241 {
242 skip("GetPrivateProfileSectionNamesW is not implemented\n");
243 DeleteFileA( testfile3 );
244 return;
245 }
246 ok( ret == 27, "expected return size 27, got %d\n", ret );
247 ok( bufW[ret-1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
248
249 /* Test with exactly fitting buffer */
250 ret = GetPrivateProfileSectionNamesW( bufW, 28, testfile3W );
251 ok( ret == 26, "expected return size 26, got %d\n", ret );
252 ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
253
254 /* Test with a buffer too small */
255 ret = GetPrivateProfileSectionNamesW( bufW, 27, testfile3W );
256 ok( ret == 25, "expected return size 25, got %d\n", ret );
257 ok( bufW[ret+1] == 0 && bufW[ret] == 0, "returned buffer not terminated with double-null\n" );
258
259 DeleteFileA( testfile3 );
260
261 /* Tests on nonexistent file */
262 memset(bufW, 0xcc, sizeof(bufW));
263 ret = GetPrivateProfileSectionNamesW( bufW, 10, not_here );
264 ok( ret == 0, "expected return size 0, got %d\n", ret );
265 ok( bufW[0] == 0, "returned buffer not terminated with null\n" );
266 ok( bufW[1] != 0, "returned buffer terminated with double-null\n" );
267 }
268
269 START_TEST(profile)
270 {
271 test_profile_int();
272 test_profile_string();
273 test_profile_sections();
274 test_profile_sections_names();
275 }