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())
38 static BOOL (WINAPI
*pIsWow64Process
)(HANDLE
,PBOOL
);
45 /* Helper function for retrieving environment variables */
46 static BOOL
get_env(const WCHAR
* env
, const char * var
, char ** result
)
48 const WCHAR
* p
= env
;
49 int envlen
, varlen
, buflen
;
52 if (!env
|| !var
|| !result
) return FALSE
;
57 if (!WideCharToMultiByte( CP_ACP
, 0, p
, -1, buf
, sizeof(buf
), NULL
, NULL
)) buf
[sizeof(buf
)-1] = 0;
59 if (CompareStringA(GetThreadLocale(), NORM_IGNORECASE
|LOCALE_USE_CP_ACP
, buf
, min(envlen
, varlen
), var
, varlen
) == CSTR_EQUAL
)
61 if (buf
[varlen
] == '=')
64 *result
= HeapAlloc(GetProcessHeap(), 0, buflen
+ 1);
65 if (!*result
) return FALSE
;
66 memcpy(*result
, buf
, buflen
+ 1);
76 static void test_create_env(void)
78 BOOL r
, is_wow64
= FALSE
;
81 char * st
, systemroot
[100];
84 static const struct profile_item common_vars
[] = {
87 { "NUMBER_OF_PROCESSORS" },
89 { "PROCESSOR_ARCHITECTURE" },
90 { "PROCESSOR_IDENTIFIER" },
91 { "PROCESSOR_LEVEL" },
92 { "PROCESSOR_REVISION" },
97 static const struct profile_item common_post_nt4_vars
[] = {
98 { "ALLUSERSPROFILE" },
101 { "CommonProgramFiles" },
106 static const struct profile_item common_win64_vars
[] = {
108 { "CommonProgramW6432" }
111 r
= SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
114 r
= GetEnvironmentVariableA("SystemRoot", systemroot
, sizeof(systemroot
));
115 ok(r
!= 0, "GetEnvironmentVariable failed (%d)\n", GetLastError());
117 r
= SetEnvironmentVariableA("SystemRoot", "overwrite");
123 r
= CreateEnvironmentBlock(NULL
, NULL
, FALSE
);
127 r
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &htok
);
133 r
= CreateEnvironmentBlock(NULL
, htok
, FALSE
);
137 r
= CreateEnvironmentBlock((LPVOID
) &env
[0], NULL
, FALSE
);
140 r
= CreateEnvironmentBlock((LPVOID
) &env
[1], htok
, FALSE
);
143 r
= CreateEnvironmentBlock((LPVOID
) &env
[2], NULL
, TRUE
);
146 r
= CreateEnvironmentBlock((LPVOID
) &env
[3], htok
, TRUE
);
149 r
= SetEnvironmentVariableA("SystemRoot", systemroot
);
154 r
= get_env(env
[i
], "SystemRoot", &st
);
155 ok(!strcmp(st
, "SystemRoot=overwrite"), "%s\n", st
);
157 HeapFree(GetProcessHeap(), 0, st
);
160 /* Test for common environment variables (NT4 and higher) */
161 for (i
= 0; i
< sizeof(common_vars
)/sizeof(common_vars
[0]); i
++)
163 for (j
= 0; j
< 4; j
++)
165 r
= get_env(env
[j
], common_vars
[i
].name
, &st
);
166 expect_env(TRUE
, r
, common_vars
[i
].name
);
167 if (r
) HeapFree(GetProcessHeap(), 0, st
);
171 /* Test for common environment variables (post NT4) */
172 if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL
, 0))
174 win_skip("Some environment variables are not present on NT4\n");
178 for (i
= 0; i
< sizeof(common_post_nt4_vars
)/sizeof(common_post_nt4_vars
[0]); i
++)
180 for (j
= 0; j
< 4; j
++)
182 r
= get_env(env
[j
], common_post_nt4_vars
[i
].name
, &st
);
183 expect_env(TRUE
, r
, common_post_nt4_vars
[i
].name
);
184 if (r
) HeapFree(GetProcessHeap(), 0, st
);
190 pIsWow64Process(GetCurrentProcess(), &is_wow64
);
191 if (sizeof(void*)==8 || is_wow64
)
193 for (i
= 0; i
< sizeof(common_win64_vars
)/sizeof(common_win64_vars
[0]); i
++)
197 r
= get_env(env
[j
], common_win64_vars
[i
].name
, &st
);
198 ok(r
|| broken(!r
)/* Vista,2k3,XP */, "Expected 1, got 0 for %s\n", common_win64_vars
[i
].name
);
199 if (r
) HeapFree(GetProcessHeap(), 0, st
);
204 r
= get_env(env
[0], "WINE_XYZZY", &st
);
207 r
= get_env(env
[1], "WINE_XYZZY", &st
);
210 r
= get_env(env
[2], "WINE_XYZZY", &st
);
212 if (r
) HeapFree(GetProcessHeap(), 0, st
);
214 r
= get_env(env
[3], "WINE_XYZZY", &st
);
216 if (r
) HeapFree(GetProcessHeap(), 0, st
);
218 for (i
= 0; i
< sizeof(env
) / sizeof(env
[0]); i
++)
220 r
= DestroyEnvironmentBlock(env
[i
]);
225 static void test_get_profiles_dir(void)
227 static const char ProfileListA
[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
228 static const char ProfilesDirectory
[] = "ProfilesDirectory";
230 DWORD cch
, profiles_len
;
233 char *profiles_dir
, *buf
, small_buf
[1];
235 l
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, ProfileListA
, 0, KEY_READ
, &key
);
236 ok(!l
, "RegOpenKeyExA failed: %d\n", GetLastError());
238 l
= RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
, NULL
, &cch
);
241 win_skip("No ProfilesDirectory value (NT4), skipping tests\n");
244 buf
= HeapAlloc(GetProcessHeap(), 0, cch
);
245 RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
, (BYTE
*)buf
, &cch
);
247 profiles_len
= ExpandEnvironmentStringsA(buf
, NULL
, 0);
248 profiles_dir
= HeapAlloc(GetProcessHeap(), 0, profiles_len
);
249 ExpandEnvironmentStringsA(buf
, profiles_dir
, profiles_len
);
250 HeapFree(GetProcessHeap(), 0, buf
);
252 SetLastError(0xdeadbeef);
253 r
= GetProfilesDirectoryA(NULL
, NULL
);
255 expect_gle(ERROR_INVALID_PARAMETER
);
256 SetLastError(0xdeadbeef);
257 r
= GetProfilesDirectoryA(NULL
, &cch
);
259 expect_gle(ERROR_INVALID_PARAMETER
);
260 SetLastError(0xdeadbeef);
262 r
= GetProfilesDirectoryA(small_buf
, &cch
);
264 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
265 /* MSDN claims the returned character count includes the NULL terminator
266 * when the buffer is too small, but that's not in fact what gets returned.
268 ok(cch
== profiles_len
- 1, "expected %d, got %d\n", profiles_len
- 1, cch
);
269 /* Allocate one more character than the return value to prevent a buffer
272 buf
= HeapAlloc(GetProcessHeap(), 0, cch
+ 1);
273 r
= GetProfilesDirectoryA(buf
, &cch
);
274 /* Rather than a BOOL, the return value is also the number of characters
275 * stored in the buffer.
277 expect(profiles_len
- 1, r
);
278 ok(!strcmp(buf
, profiles_dir
), "expected %s, got %s\n", profiles_dir
, buf
);
280 HeapFree(GetProcessHeap(), 0, buf
);
281 HeapFree(GetProcessHeap(), 0, profiles_dir
);
283 SetLastError(0xdeadbeef);
284 r
= GetProfilesDirectoryW(NULL
, NULL
);
286 expect_gle(ERROR_INVALID_PARAMETER
);
289 SetLastError(0xdeadbeef);
290 r
= GetProfilesDirectoryW(NULL
, &cch
);
292 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
293 ok(cch
, "expected cch > 0\n");
295 SetLastError(0xdeadbeef);
296 r
= GetProfilesDirectoryW(NULL
, &cch
);
298 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
301 static void test_get_user_profile_dir(void)
309 if (!GetEnvironmentVariableA( "ALLUSERSPROFILE", NULL
, 0 ))
311 win_skip("Skipping tests on NT4\n");
315 ret
= OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY
, &token
);
316 ok(ret
, "expected success %u\n", GetLastError());
318 SetLastError( 0xdeadbeef );
319 ret
= GetUserProfileDirectoryA( NULL
, NULL
, NULL
);
320 error
= GetLastError();
321 ok(!ret
, "expected failure\n");
322 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
324 SetLastError( 0xdeadbeef );
325 ret
= GetUserProfileDirectoryA( token
, NULL
, NULL
);
326 error
= GetLastError();
327 ok(!ret
, "expected failure\n");
328 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
330 dirA
= HeapAlloc( GetProcessHeap(), 0, 32 );
331 SetLastError( 0xdeadbeef );
332 ret
= GetUserProfileDirectoryA( token
, dirA
, NULL
);
333 error
= GetLastError();
334 ok(!ret
, "expected failure\n");
335 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
336 HeapFree( GetProcessHeap(), 0, dirA
);
339 SetLastError( 0xdeadbeef );
340 ret
= GetUserProfileDirectoryA( token
, NULL
, &len
);
341 error
= GetLastError();
342 ok(!ret
, "expected failure\n");
343 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
344 ok(!len
, "expected 0, got %u\n", len
);
347 dirA
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 32 );
348 SetLastError( 0xdeadbeef );
349 ret
= GetUserProfileDirectoryA( token
, dirA
, &len
);
350 error
= GetLastError();
351 ok(!ret
, "expected failure\n");
352 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
353 ok(len
, "expected len > 0\n");
354 HeapFree( GetProcessHeap(), 0, dirA
);
356 dirA
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
357 SetLastError( 0xdeadbeef );
358 ret
= GetUserProfileDirectoryA( token
, dirA
, &len
);
359 ok(ret
, "expected success %u\n", GetLastError());
360 ok(len
, "expected len > 0\n");
361 ok(lstrlenA( dirA
) == len
- 1, "length mismatch %d != %d - 1\n", lstrlenA( dirA
), len
);
363 HeapFree( GetProcessHeap(), 0, dirA
);
365 SetLastError( 0xdeadbeef );
366 ret
= GetUserProfileDirectoryW( NULL
, NULL
, NULL
);
367 error
= GetLastError();
368 ok(!ret
, "expected failure\n");
369 todo_wine
ok(error
== ERROR_INVALID_HANDLE
, "expected ERROR_INVALID_HANDLE, got %u\n", error
);
371 SetLastError( 0xdeadbeef );
372 ret
= GetUserProfileDirectoryW( token
, NULL
, NULL
);
373 error
= GetLastError();
374 ok(!ret
, "expected failure\n");
375 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
377 dirW
= HeapAlloc( GetProcessHeap(), 0, 32 );
378 SetLastError( 0xdeadbeef );
379 ret
= GetUserProfileDirectoryW( token
, dirW
, NULL
);
380 error
= GetLastError();
381 ok(!ret
, "expected failure\n");
382 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
383 HeapFree( GetProcessHeap(), 0, dirW
);
386 SetLastError( 0xdeadbeef );
387 ret
= GetUserProfileDirectoryW( token
, NULL
, &len
);
388 error
= GetLastError();
389 ok(!ret
, "expected failure\n");
390 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
391 ok(len
, "expected len > 0\n");
393 dirW
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
) );
394 SetLastError( 0xdeadbeef );
395 ret
= GetUserProfileDirectoryW( token
, dirW
, &len
);
396 ok(ret
, "expected success %u\n", GetLastError());
397 ok(len
, "expected len > 0\n");
398 ok(lstrlenW( dirW
) == len
- 1, "length mismatch %d != %d - 1\n", lstrlenW( dirW
), len
);
399 HeapFree( GetProcessHeap(), 0, dirW
);
401 CloseHandle( token
);
406 pIsWow64Process
= (void*)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
409 test_get_profiles_dir();
410 test_get_user_profile_dir();