2 * Unit test suite for userenv functions
4 * Copyright 2008 Google (Lei Zhang)
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
32 #include "wine/test.h"
34 #define expect(EXPECTED,GOT) ok((GOT)==(EXPECTED), "Expected %d, got %d\n", (EXPECTED), (GOT))
35 #define expect_env(EXPECTED,GOT,VAR) ok((GOT)==(EXPECTED), "Expected %d, got %d for %s (%d)\n", (EXPECTED), (GOT), (VAR), j)
36 #define expect_gle(EXPECTED) ok(GetLastError() == (EXPECTED), "Expected %d, got %d\n", (EXPECTED), GetLastError())
44 /* Helper function for retrieving environment variables */
45 static BOOL get_env(const WCHAR * env, const char * var, char ** result)
47 const WCHAR * p = env;
48 int envlen, varlen, buflen;
51 if (!env || !var || !result) return FALSE;
56 if (!WideCharToMultiByte( CP_ACP, 0, p, -1, buf, sizeof(buf), NULL, NULL )) buf[sizeof(buf)-1] = 0;
58 if (CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, buf, min(envlen, varlen), var, varlen) == CSTR_EQUAL)
60 if (buf[varlen] == '=')
63 *result = HeapAlloc(GetProcessHeap(), 0, buflen + 1);
64 if (!*result) return FALSE;
65 memcpy(*result, buf, buflen + 1);
75 static void test_create_env(void)
83 static const struct profile_item common_vars[] = {
84 { "ComSpec", { 1, 1, 0, 0 } },
85 { "COMPUTERNAME", { 1, 1, 1, 1 } },
86 { "NUMBER_OF_PROCESSORS", { 1, 1, 0, 0 } },
87 { "OS", { 1, 1, 0, 0 } },
88 { "PROCESSOR_ARCHITECTURE", { 1, 1, 0, 0 } },
89 { "PROCESSOR_IDENTIFIER", { 1, 1, 0, 0 } },
90 { "PROCESSOR_LEVEL", { 1, 1, 0, 0 } },
91 { "PROCESSOR_REVISION", { 1, 1, 0, 0 } },
92 { "SystemDrive", { 1, 1, 0, 0 } },
93 { "SystemRoot", { 1, 1, 0, 0 } },
94 { "windir", { 1, 1, 0, 0 } }
96 static const struct profile_item common_post_nt4_vars[] = {
97 { "ALLUSERSPROFILE", { 1, 1, 0, 0 } },
98 { "TEMP", { 1, 1, 0, 0 } },
99 { "TMP", { 1, 1, 0, 0 } },
100 { "CommonProgramFiles", { 1, 1, 0, 0 } },
101 { "ProgramFiles", { 1, 1, 0, 0 } }
103 static const struct profile_item htok_vars[] = {
104 { "PATH", { 1, 1, 0, 0 } },
105 { "USERPROFILE", { 1, 1, 0, 0 } }
108 r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
114 r = CreateEnvironmentBlock(NULL, NULL, FALSE);
118 r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
124 r = CreateEnvironmentBlock(NULL, htok, FALSE);
128 r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
131 r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
134 r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
137 r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
140 /* Test for common environment variables (NT4 and higher) */
141 for (i = 0; i < sizeof(common_vars)/sizeof(common_vars[0]); i++)
143 for (j = 0; j < 4; j++)
145 r = get_env(env[j], common_vars[i].name, &st);
146 if (common_vars[i].todo[j])
147 todo_wine expect_env(TRUE, r, common_vars[i].name);
149 expect_env(TRUE, r, common_vars[i].name);
150 if (r) HeapFree(GetProcessHeap(), 0, st);
154 /* Test for common environment variables (post NT4) */
155 if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL, 0))
157 win_skip("Some environment variables are not present on NT4\n");
161 for (i = 0; i < sizeof(common_post_nt4_vars)/sizeof(common_post_nt4_vars[0]); i++)
163 for (j = 0; j < 4; j++)
165 r = get_env(env[j], common_post_nt4_vars[i].name, &st);
166 if (common_post_nt4_vars[i].todo[j])
167 todo_wine expect_env(TRUE, r, common_post_nt4_vars[i].name);
169 expect_env(TRUE, r, common_post_nt4_vars[i].name);
170 if (r) HeapFree(GetProcessHeap(), 0, st);
175 /* Test for environment variables with values that depends on htok */
176 for (i = 0; i < sizeof(htok_vars)/sizeof(htok_vars[0]); i++)
178 for (j = 0; j < 4; j++)
180 r = get_env(env[j], htok_vars[i].name, &st);
181 if (htok_vars[i].todo[j])
182 todo_wine expect_env(TRUE, r, htok_vars[i].name);
184 expect_env(TRUE, r, htok_vars[i].name);
185 if (r) HeapFree(GetProcessHeap(), 0, st);
189 r = get_env(env[0], "WINE_XYZZY", &st);
192 r = get_env(env[1], "WINE_XYZZY", &st);
195 r = get_env(env[2], "WINE_XYZZY", &st);
197 if (r) HeapFree(GetProcessHeap(), 0, st);
199 r = get_env(env[3], "WINE_XYZZY", &st);
201 if (r) HeapFree(GetProcessHeap(), 0, st);
203 for (i = 0; i < sizeof(env) / sizeof(env[0]); i++)
205 r = DestroyEnvironmentBlock(env[i]);
210 static void test_get_profiles_dir(void)
212 static const char ProfileListA[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
213 static const char ProfilesDirectory[] = "ProfilesDirectory";
215 DWORD cch, profiles_len;
218 char *profiles_dir, *buf, small_buf[1];
220 l = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ProfileListA, 0, KEY_READ, &key);
221 ok(!l, "RegOpenKeyExA failed: %d\n", GetLastError());
223 l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, NULL, &cch);
226 win_skip("No ProfilesDirectory value (NT4), skipping tests\n");
229 buf = HeapAlloc(GetProcessHeap(), 0, cch);
230 RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, (BYTE *)buf, &cch);
232 profiles_len = ExpandEnvironmentStringsA(buf, NULL, 0);
233 profiles_dir = HeapAlloc(GetProcessHeap(), 0, profiles_len);
234 ExpandEnvironmentStringsA(buf, profiles_dir, profiles_len);
235 HeapFree(GetProcessHeap(), 0, buf);
237 SetLastError(0xdeadbeef);
238 r = GetProfilesDirectoryA(NULL, NULL);
240 expect_gle(ERROR_INVALID_PARAMETER);
241 SetLastError(0xdeadbeef);
242 r = GetProfilesDirectoryA(NULL, &cch);
244 expect_gle(ERROR_INVALID_PARAMETER);
245 SetLastError(0xdeadbeef);
247 r = GetProfilesDirectoryA(small_buf, &cch);
249 expect_gle(ERROR_INSUFFICIENT_BUFFER);
250 /* MSDN claims the returned character count includes the NULL terminator
251 * when the buffer is too small, but that's not in fact what gets returned.
253 ok(cch == profiles_len - 1, "expected %d, got %d\n", profiles_len - 1, cch);
254 /* Allocate one more character than the return value to prevent a buffer
257 buf = HeapAlloc(GetProcessHeap(), 0, cch + 1);
258 r = GetProfilesDirectoryA(buf, &cch);
259 /* Rather than a BOOL, the return value is also the number of characters
260 * stored in the buffer.
262 expect(profiles_len - 1, r);
263 ok(!strcmp(buf, profiles_dir), "expected %s, got %s\n", profiles_dir, buf);
265 HeapFree(GetProcessHeap(), 0, buf);
266 HeapFree(GetProcessHeap(), 0, profiles_dir);
268 SetLastError(0xdeadbeef);
269 r = GetProfilesDirectoryW(NULL, NULL);
271 expect_gle(ERROR_INVALID_PARAMETER);
274 SetLastError(0xdeadbeef);
275 r = GetProfilesDirectoryW(NULL, &cch);
277 expect_gle(ERROR_INSUFFICIENT_BUFFER);
278 ok(cch, "expected cch > 0\n");
280 SetLastError(0xdeadbeef);
281 r = GetProfilesDirectoryW(NULL, &cch);
283 expect_gle(ERROR_INSUFFICIENT_BUFFER);
286 static void test_get_user_profile_dir(void)
294 if (!GetEnvironmentVariableA( "ALLUSERSPROFILE", NULL, 0 ))
296 win_skip("Skipping tests on NT4\n");
300 ret = OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token );
301 ok(ret, "expected success %u\n", GetLastError());
303 SetLastError( 0xdeadbeef );
304 ret = GetUserProfileDirectoryA( NULL, NULL, NULL );
305 error = GetLastError();
306 ok(!ret, "expected failure\n");
307 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
309 SetLastError( 0xdeadbeef );
310 ret = GetUserProfileDirectoryA( token, NULL, NULL );
311 error = GetLastError();
312 ok(!ret, "expected failure\n");
313 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
315 dirA = HeapAlloc( GetProcessHeap(), 0, 32 );
316 SetLastError( 0xdeadbeef );
317 ret = GetUserProfileDirectoryA( token, dirA, NULL );
318 error = GetLastError();
319 ok(!ret, "expected failure\n");
320 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
321 HeapFree( GetProcessHeap(), 0, dirA );
324 SetLastError( 0xdeadbeef );
325 ret = GetUserProfileDirectoryA( token, NULL, &len );
326 error = GetLastError();
327 ok(!ret, "expected failure\n");
328 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
329 ok(!len, "expected 0, got %u\n", len);
332 dirA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 32 );
333 SetLastError( 0xdeadbeef );
334 ret = GetUserProfileDirectoryA( token, dirA, &len );
335 error = GetLastError();
336 ok(!ret, "expected failure\n");
337 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
338 ok(len, "expected len > 0\n");
339 HeapFree( GetProcessHeap(), 0, dirA );
341 dirA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len );
342 SetLastError( 0xdeadbeef );
343 ret = GetUserProfileDirectoryA( token, dirA, &len );
344 ok(ret, "expected success %u\n", GetLastError());
345 ok(len, "expected len > 0\n");
346 ok(lstrlenA( dirA ) == len - 1, "length mismatch %d != %d - 1\n", lstrlenA( dirA ), len );
348 HeapFree( GetProcessHeap(), 0, dirA );
350 SetLastError( 0xdeadbeef );
351 ret = GetUserProfileDirectoryW( NULL, NULL, NULL );
352 error = GetLastError();
353 ok(!ret, "expected failure\n");
354 todo_wine ok(error == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %u\n", error);
356 SetLastError( 0xdeadbeef );
357 ret = GetUserProfileDirectoryW( token, NULL, NULL );
358 error = GetLastError();
359 ok(!ret, "expected failure\n");
360 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
362 dirW = HeapAlloc( GetProcessHeap(), 0, 32 );
363 SetLastError( 0xdeadbeef );
364 ret = GetUserProfileDirectoryW( token, dirW, NULL );
365 error = GetLastError();
366 ok(!ret, "expected failure\n");
367 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
368 HeapFree( GetProcessHeap(), 0, dirW );
371 SetLastError( 0xdeadbeef );
372 ret = GetUserProfileDirectoryW( token, NULL, &len );
373 error = GetLastError();
374 ok(!ret, "expected failure\n");
375 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
376 ok(len, "expected len > 0\n");
378 dirW = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR) );
379 SetLastError( 0xdeadbeef );
380 ret = GetUserProfileDirectoryW( token, dirW, &len );
381 ok(ret, "expected success %u\n", GetLastError());
382 ok(len, "expected len > 0\n");
383 ok(lstrlenW( dirW ) == len - 1, "length mismatch %d != %d - 1\n", lstrlenW( dirW ), len );
384 HeapFree( GetProcessHeap(), 0, dirW );
386 CloseHandle( token );
392 test_get_profiles_dir();
393 test_get_user_profile_dir();