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
[] = {
107 { "ProgramFiles(x86)" },
108 { "CommonProgramFiles(x86)" },
110 { "CommonProgramW6432" }
113 r
= SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
116 r
= GetEnvironmentVariableA("SystemRoot", systemroot
, sizeof(systemroot
));
117 ok(r
!= 0, "GetEnvironmentVariable failed (%d)\n", GetLastError());
119 r
= SetEnvironmentVariableA("SystemRoot", "overwrite");
125 r
= CreateEnvironmentBlock(NULL
, NULL
, FALSE
);
129 r
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &htok
);
135 r
= CreateEnvironmentBlock(NULL
, htok
, FALSE
);
139 r
= CreateEnvironmentBlock((LPVOID
) &env
[0], NULL
, FALSE
);
142 r
= CreateEnvironmentBlock((LPVOID
) &env
[1], htok
, FALSE
);
145 r
= CreateEnvironmentBlock((LPVOID
) &env
[2], NULL
, TRUE
);
148 r
= CreateEnvironmentBlock((LPVOID
) &env
[3], htok
, TRUE
);
151 r
= SetEnvironmentVariableA("SystemRoot", systemroot
);
156 r
= get_env(env
[i
], "SystemRoot", &st
);
157 ok(!strcmp(st
, "SystemRoot=overwrite"), "%s\n", st
);
159 HeapFree(GetProcessHeap(), 0, st
);
162 /* Test for common environment variables (NT4 and higher) */
163 for (i
= 0; i
< ARRAY_SIZE(common_vars
); i
++)
165 for (j
= 0; j
< 4; j
++)
167 r
= get_env(env
[j
], common_vars
[i
].name
, &st
);
168 expect_env(TRUE
, r
, common_vars
[i
].name
);
169 if (r
) HeapFree(GetProcessHeap(), 0, st
);
173 /* Test for common environment variables (post NT4) */
174 if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL
, 0))
176 win_skip("Some environment variables are not present on NT4\n");
180 for (i
= 0; i
< ARRAY_SIZE(common_post_nt4_vars
); i
++)
182 for (j
= 0; j
< 4; j
++)
184 r
= get_env(env
[j
], common_post_nt4_vars
[i
].name
, &st
);
185 expect_env(TRUE
, r
, common_post_nt4_vars
[i
].name
);
186 if (r
) HeapFree(GetProcessHeap(), 0, st
);
192 pIsWow64Process(GetCurrentProcess(), &is_wow64
);
193 if (sizeof(void*)==8 || is_wow64
)
195 for (i
= 0; i
< ARRAY_SIZE(common_win64_vars
); i
++)
199 r
= get_env(env
[j
], common_win64_vars
[i
].name
, &st
);
200 ok(r
|| broken(!r
)/* Vista,2k3,XP */, "Expected 1, got 0 for %s\n", common_win64_vars
[i
].name
);
201 if (r
) HeapFree(GetProcessHeap(), 0, st
);
206 r
= get_env(env
[0], "WINE_XYZZY", &st
);
209 r
= get_env(env
[1], "WINE_XYZZY", &st
);
212 r
= get_env(env
[2], "WINE_XYZZY", &st
);
214 if (r
) HeapFree(GetProcessHeap(), 0, st
);
216 r
= get_env(env
[3], "WINE_XYZZY", &st
);
218 if (r
) HeapFree(GetProcessHeap(), 0, st
);
220 for (i
= 0; i
< ARRAY_SIZE(env
); i
++)
222 r
= DestroyEnvironmentBlock(env
[i
]);
227 static void test_get_profiles_dir(void)
229 static const char ProfileListA
[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
230 static const char ProfilesDirectory
[] = "ProfilesDirectory";
232 DWORD cch
, profiles_len
;
235 char *profiles_dir
, *buf
, small_buf
[1];
237 l
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, ProfileListA
, 0, KEY_READ
, &key
);
238 ok(!l
, "RegOpenKeyExA failed: %d\n", GetLastError());
240 l
= RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
, NULL
, &cch
);
243 win_skip("No ProfilesDirectory value (NT4), skipping tests\n");
246 buf
= HeapAlloc(GetProcessHeap(), 0, cch
);
247 RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
, (BYTE
*)buf
, &cch
);
249 profiles_len
= ExpandEnvironmentStringsA(buf
, NULL
, 0);
250 profiles_dir
= HeapAlloc(GetProcessHeap(), 0, profiles_len
);
251 ExpandEnvironmentStringsA(buf
, profiles_dir
, profiles_len
);
252 HeapFree(GetProcessHeap(), 0, buf
);
254 SetLastError(0xdeadbeef);
255 r
= GetProfilesDirectoryA(NULL
, NULL
);
257 expect_gle(ERROR_INVALID_PARAMETER
);
258 SetLastError(0xdeadbeef);
259 r
= GetProfilesDirectoryA(NULL
, &cch
);
261 expect_gle(ERROR_INVALID_PARAMETER
);
262 SetLastError(0xdeadbeef);
264 r
= GetProfilesDirectoryA(small_buf
, &cch
);
266 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
267 /* MSDN claims the returned character count includes the NULL terminator
268 * when the buffer is too small, but that's not in fact what gets returned.
270 ok(cch
== profiles_len
- 1, "expected %d, got %d\n", profiles_len
- 1, cch
);
271 /* Allocate one more character than the return value to prevent a buffer
274 buf
= HeapAlloc(GetProcessHeap(), 0, cch
+ 1);
275 r
= GetProfilesDirectoryA(buf
, &cch
);
276 /* Rather than a BOOL, the return value is also the number of characters
277 * stored in the buffer.
279 expect(profiles_len
- 1, r
);
280 ok(!strcmp(buf
, profiles_dir
), "expected %s, got %s\n", profiles_dir
, buf
);
282 HeapFree(GetProcessHeap(), 0, buf
);
283 HeapFree(GetProcessHeap(), 0, profiles_dir
);
285 SetLastError(0xdeadbeef);
286 r
= GetProfilesDirectoryW(NULL
, NULL
);
288 expect_gle(ERROR_INVALID_PARAMETER
);
291 SetLastError(0xdeadbeef);
292 r
= GetProfilesDirectoryW(NULL
, &cch
);
294 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
295 ok(cch
, "expected cch > 0\n");
297 SetLastError(0xdeadbeef);
298 r
= GetProfilesDirectoryW(NULL
, &cch
);
300 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
303 static void test_get_user_profile_dir(void)
306 DWORD error
, len
, len2
;
311 if (!GetEnvironmentVariableA( "ALLUSERSPROFILE", NULL
, 0 ))
313 win_skip("Skipping tests on NT4\n");
317 ret
= OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY
, &token
);
318 ok(ret
, "expected success %u\n", GetLastError());
320 SetLastError( 0xdeadbeef );
321 ret
= GetUserProfileDirectoryA( NULL
, NULL
, NULL
);
322 error
= GetLastError();
323 ok(!ret
, "expected failure\n");
324 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
326 SetLastError( 0xdeadbeef );
327 ret
= GetUserProfileDirectoryA( token
, NULL
, NULL
);
328 error
= GetLastError();
329 ok(!ret
, "expected failure\n");
330 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
332 dirA
= HeapAlloc( GetProcessHeap(), 0, 32 );
333 SetLastError( 0xdeadbeef );
334 ret
= GetUserProfileDirectoryA( token
, dirA
, NULL
);
335 error
= GetLastError();
336 ok(!ret
, "expected failure\n");
337 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
338 HeapFree( GetProcessHeap(), 0, dirA
);
341 SetLastError( 0xdeadbeef );
342 ret
= GetUserProfileDirectoryA( token
, NULL
, &len
);
343 error
= GetLastError();
344 ok(!ret
, "expected failure\n");
345 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
346 ok(!len
, "expected 0, got %u\n", len
);
349 dirA
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 32 );
350 SetLastError( 0xdeadbeef );
351 ret
= GetUserProfileDirectoryA( token
, dirA
, &len
);
352 error
= GetLastError();
353 ok(!ret
, "expected failure\n");
354 HeapFree( GetProcessHeap(), 0, dirA
);
355 if (error
== ERROR_INSUFFICIENT_BUFFER
)
357 ok(len
, "expected len > 0\n");
359 dirA
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
360 SetLastError( 0xdeadbeef );
361 ret
= GetUserProfileDirectoryA( token
, dirA
, &len
);
362 ok(ret
, "expected success %u\n", GetLastError());
363 ok(len
, "expected len > 0\n");
364 ok(lstrlenA( dirA
) == len
- 1, "length mismatch %d != %d - 1\n", lstrlenA( dirA
), len
);
366 HeapFree( GetProcessHeap(), 0, dirA
);
369 ok(broken(error
== ERROR_INVALID_PARAMETER
) /* win10 1809+ */,
370 "unexpected error %u\n", error
);
372 SetLastError( 0xdeadbeef );
373 ret
= GetUserProfileDirectoryW( NULL
, NULL
, NULL
);
374 error
= GetLastError();
375 ok(!ret
, "expected failure\n");
376 todo_wine
ok(error
== ERROR_INVALID_HANDLE
, "expected ERROR_INVALID_HANDLE, got %u\n", error
);
378 SetLastError( 0xdeadbeef );
379 ret
= GetUserProfileDirectoryW( token
, NULL
, NULL
);
380 error
= GetLastError();
381 ok(!ret
, "expected failure\n");
382 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
384 dirW
= HeapAlloc( GetProcessHeap(), 0, 32 );
385 SetLastError( 0xdeadbeef );
386 ret
= GetUserProfileDirectoryW( token
, dirW
, NULL
);
387 error
= GetLastError();
388 ok(!ret
, "expected failure\n");
389 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %u\n", error
);
390 HeapFree( GetProcessHeap(), 0, dirW
);
393 SetLastError( 0xdeadbeef );
394 ret
= GetUserProfileDirectoryW( token
, NULL
, &len
);
395 error
= GetLastError();
396 ok(!ret
, "expected failure\n");
397 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error
);
398 ok(len
, "expected len > 0\n");
400 dirW
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
) );
401 SetLastError( 0xdeadbeef );
402 ret
= GetUserProfileDirectoryW( token
, dirW
, &len
);
403 ok(ret
, "expected success %u\n", GetLastError());
404 ok(len
, "expected len > 0\n");
405 ok(lstrlenW( dirW
) == len
- 1, "length mismatch %d != %d - 1\n", lstrlenW( dirW
), len
);
406 HeapFree( GetProcessHeap(), 0, dirW
);
409 dirW
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 32 * sizeof(WCHAR
) );
410 SetLastError( 0xdeadbeef );
411 ret
= GetUserProfileDirectoryW( token
, dirW
, &len2
);
412 error
= GetLastError();
413 ok(!ret
, "expected failure\n");
414 HeapFree( GetProcessHeap(), 0, dirW
);
415 if (error
== ERROR_INSUFFICIENT_BUFFER
)
416 ok(len2
== len
, "expected %d, got %d\n", len
, len2
);
418 ok(broken(error
== ERROR_INVALID_PARAMETER
) /* win10 1809+ */,
419 "unexpected error %u\n", error
);
421 CloseHandle( token
);
426 pIsWow64Process
= (void*)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
429 test_get_profiles_dir();
430 test_get_user_profile_dir();