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 %ld\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], programdata
[100], allusersprofile
[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" },
102 { "CommonProgramFiles" },
107 static const struct profile_item common_win64_vars
[] = {
108 { "ProgramFiles(x86)" },
109 { "CommonProgramFiles(x86)" },
111 { "CommonProgramW6432" }
114 r
= SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
117 r
= GetEnvironmentVariableA("SystemRoot", systemroot
, sizeof(systemroot
));
118 ok(r
!= 0, "GetEnvironmentVariable failed (%ld)\n", GetLastError());
120 r
= SetEnvironmentVariableA("SystemRoot", "overwrite");
123 r
= GetEnvironmentVariableA("ProgramData", programdata
, sizeof(programdata
));
124 ok(r
!= 0, "GetEnvironmentVariable failed (%ld)\n", GetLastError());
126 r
= SetEnvironmentVariableA("ProgramData", "overwrite");
132 r
= CreateEnvironmentBlock(NULL
, NULL
, FALSE
);
136 r
= OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY
|TOKEN_DUPLICATE
, &htok
);
142 r
= CreateEnvironmentBlock(NULL
, htok
, FALSE
);
146 r
= CreateEnvironmentBlock((LPVOID
) &env
[0], NULL
, FALSE
);
149 r
= CreateEnvironmentBlock((LPVOID
) &env
[1], htok
, FALSE
);
152 r
= CreateEnvironmentBlock((LPVOID
) &env
[2], NULL
, TRUE
);
155 r
= CreateEnvironmentBlock((LPVOID
) &env
[3], htok
, TRUE
);
158 r
= SetEnvironmentVariableA("SystemRoot", systemroot
);
161 r
= SetEnvironmentVariableA("ProgramData", programdata
);
166 r
= get_env(env
[i
], "SystemRoot", &st
);
167 ok(!strcmp(st
, "SystemRoot=overwrite"), "%s\n", st
);
169 HeapFree(GetProcessHeap(), 0, st
);
171 r
= get_env(env
[i
], "ProgramData", &st
);
172 ok(strcmp(st
, "ProgramData=overwrite"), "%s\n", st
);
174 HeapFree(GetProcessHeap(), 0, st
);
177 /* Test for common environment variables (NT4 and higher) */
178 for (i
= 0; i
< ARRAY_SIZE(common_vars
); i
++)
180 for (j
= 0; j
< 4; j
++)
182 r
= get_env(env
[j
], common_vars
[i
].name
, &st
);
183 expect_env(TRUE
, r
, common_vars
[i
].name
);
184 if (r
) HeapFree(GetProcessHeap(), 0, st
);
188 /* Test for common environment variables (post NT4) */
189 if (!GetEnvironmentVariableA("ALLUSERSPROFILE", allusersprofile
, sizeof(allusersprofile
)))
191 win_skip("Some environment variables are not present on NT4\n");
195 for (i
= 0; i
< ARRAY_SIZE(common_post_nt4_vars
); i
++)
197 for (j
= 0; j
< 4; j
++)
199 r
= get_env(env
[j
], common_post_nt4_vars
[i
].name
, &st
);
200 expect_env(TRUE
, r
, common_post_nt4_vars
[i
].name
);
201 if (!strcmp(common_post_nt4_vars
[i
].name
, "ALLUSERSPROFILE") ||
202 !strcmp(common_post_nt4_vars
[i
].name
, "ProgramData"))
203 ok(!strcmp(st
+ strlen(common_post_nt4_vars
[i
].name
) + 1, allusersprofile
), "%s\n", st
);
204 if (r
) HeapFree(GetProcessHeap(), 0, st
);
210 pIsWow64Process(GetCurrentProcess(), &is_wow64
);
211 if (sizeof(void*)==8 || is_wow64
)
213 for (i
= 0; i
< ARRAY_SIZE(common_win64_vars
); i
++)
217 r
= get_env(env
[j
], common_win64_vars
[i
].name
, &st
);
218 ok(r
|| broken(!r
)/* Vista,2k3,XP */, "Expected 1, got 0 for %s\n", common_win64_vars
[i
].name
);
219 if (r
) HeapFree(GetProcessHeap(), 0, st
);
224 r
= get_env(env
[0], "WINE_XYZZY", &st
);
227 r
= get_env(env
[1], "WINE_XYZZY", &st
);
230 r
= get_env(env
[2], "WINE_XYZZY", &st
);
232 if (r
) HeapFree(GetProcessHeap(), 0, st
);
234 r
= get_env(env
[3], "WINE_XYZZY", &st
);
236 if (r
) HeapFree(GetProcessHeap(), 0, st
);
238 for (i
= 0; i
< ARRAY_SIZE(env
); i
++)
240 r
= DestroyEnvironmentBlock(env
[i
]);
245 static void test_get_profiles_dir(void)
247 static const char ProfileListA
[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
248 static const char ProfilesDirectory
[] = "ProfilesDirectory";
250 DWORD cch
, profiles_len
;
253 char *profiles_dir
, *buf
, small_buf
[1];
255 l
= RegOpenKeyExA(HKEY_LOCAL_MACHINE
, ProfileListA
, 0, KEY_READ
, &key
);
256 ok(!l
, "RegOpenKeyExA failed: %ld\n", GetLastError());
258 l
= RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
, NULL
, &cch
);
261 win_skip("No ProfilesDirectory value (NT4), skipping tests\n");
264 buf
= HeapAlloc(GetProcessHeap(), 0, cch
);
265 RegQueryValueExA(key
, ProfilesDirectory
, NULL
, NULL
, (BYTE
*)buf
, &cch
);
267 profiles_len
= ExpandEnvironmentStringsA(buf
, NULL
, 0);
268 profiles_dir
= HeapAlloc(GetProcessHeap(), 0, profiles_len
);
269 ExpandEnvironmentStringsA(buf
, profiles_dir
, profiles_len
);
270 HeapFree(GetProcessHeap(), 0, buf
);
272 SetLastError(0xdeadbeef);
273 r
= GetProfilesDirectoryA(NULL
, NULL
);
275 expect_gle(ERROR_INVALID_PARAMETER
);
276 SetLastError(0xdeadbeef);
277 r
= GetProfilesDirectoryA(NULL
, &cch
);
279 expect_gle(ERROR_INVALID_PARAMETER
);
280 SetLastError(0xdeadbeef);
282 r
= GetProfilesDirectoryA(small_buf
, &cch
);
284 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
285 /* MSDN claims the returned character count includes the NULL terminator
286 * when the buffer is too small, but that's not in fact what gets returned.
288 ok(cch
== profiles_len
- 1, "expected %ld, got %ld\n", profiles_len
- 1, cch
);
289 /* Allocate one more character than the return value to prevent a buffer
292 buf
= HeapAlloc(GetProcessHeap(), 0, cch
+ 1);
293 r
= GetProfilesDirectoryA(buf
, &cch
);
294 /* Rather than a BOOL, the return value is also the number of characters
295 * stored in the buffer.
297 ok(profiles_len
- 1 == r
, "expected %ld, got %d\n", profiles_len
- 1, r
);
298 ok(!strcmp(buf
, profiles_dir
), "expected %s, got %s\n", profiles_dir
, buf
);
300 HeapFree(GetProcessHeap(), 0, buf
);
301 HeapFree(GetProcessHeap(), 0, profiles_dir
);
303 SetLastError(0xdeadbeef);
304 r
= GetProfilesDirectoryW(NULL
, NULL
);
306 expect_gle(ERROR_INVALID_PARAMETER
);
309 SetLastError(0xdeadbeef);
310 r
= GetProfilesDirectoryW(NULL
, &cch
);
312 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
313 ok(cch
, "expected cch > 0\n");
315 SetLastError(0xdeadbeef);
316 r
= GetProfilesDirectoryW(NULL
, &cch
);
318 expect_gle(ERROR_INSUFFICIENT_BUFFER
);
321 static void test_get_user_profile_dir(void)
324 DWORD error
, len
, len2
;
329 if (!GetEnvironmentVariableA( "ALLUSERSPROFILE", NULL
, 0 ))
331 win_skip("Skipping tests on NT4\n");
335 ret
= OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY
, &token
);
336 ok(ret
, "expected success %lu\n", GetLastError());
338 SetLastError( 0xdeadbeef );
339 ret
= GetUserProfileDirectoryA( NULL
, NULL
, NULL
);
340 error
= GetLastError();
341 ok(!ret
, "expected failure\n");
342 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %lu\n", error
);
344 SetLastError( 0xdeadbeef );
345 ret
= GetUserProfileDirectoryA( token
, NULL
, NULL
);
346 error
= GetLastError();
347 ok(!ret
, "expected failure\n");
348 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %lu\n", error
);
350 dirA
= HeapAlloc( GetProcessHeap(), 0, 32 );
351 SetLastError( 0xdeadbeef );
352 ret
= GetUserProfileDirectoryA( token
, dirA
, NULL
);
353 error
= GetLastError();
354 ok(!ret
, "expected failure\n");
355 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %lu\n", error
);
356 HeapFree( GetProcessHeap(), 0, dirA
);
359 SetLastError( 0xdeadbeef );
360 ret
= GetUserProfileDirectoryA( token
, NULL
, &len
);
361 error
= GetLastError();
362 ok(!ret
, "expected failure\n");
363 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %lu\n", error
);
364 ok(!len
, "expected 0, got %lu\n", len
);
367 dirA
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 32 );
368 SetLastError( 0xdeadbeef );
369 ret
= GetUserProfileDirectoryA( token
, dirA
, &len
);
370 error
= GetLastError();
371 ok(!ret
, "expected failure\n");
372 HeapFree( GetProcessHeap(), 0, dirA
);
373 if (error
== ERROR_INSUFFICIENT_BUFFER
)
375 ok(len
, "expected len > 0\n");
377 dirA
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, len
);
378 SetLastError( 0xdeadbeef );
379 ret
= GetUserProfileDirectoryA( token
, dirA
, &len
);
380 ok(ret
, "expected success %lu\n", GetLastError());
381 ok(len
, "expected len > 0\n");
382 ok(lstrlenA( dirA
) == len
- 1, "length mismatch %d != %ld - 1\n", lstrlenA( dirA
), len
);
384 HeapFree( GetProcessHeap(), 0, dirA
);
387 ok(broken(error
== ERROR_INVALID_PARAMETER
) /* win10 1809+ */,
388 "unexpected error %lu\n", error
);
390 SetLastError( 0xdeadbeef );
391 ret
= GetUserProfileDirectoryW( NULL
, NULL
, NULL
);
392 error
= GetLastError();
393 ok(!ret
, "expected failure\n");
394 todo_wine
ok(error
== ERROR_INVALID_HANDLE
, "expected ERROR_INVALID_HANDLE, got %lu\n", error
);
396 SetLastError( 0xdeadbeef );
397 ret
= GetUserProfileDirectoryW( token
, NULL
, NULL
);
398 error
= GetLastError();
399 ok(!ret
, "expected failure\n");
400 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %lu\n", error
);
402 dirW
= HeapAlloc( GetProcessHeap(), 0, 32 );
403 SetLastError( 0xdeadbeef );
404 ret
= GetUserProfileDirectoryW( token
, dirW
, NULL
);
405 error
= GetLastError();
406 ok(!ret
, "expected failure\n");
407 ok(error
== ERROR_INVALID_PARAMETER
, "expected ERROR_INVALID_PARAMETER, got %lu\n", error
);
408 HeapFree( GetProcessHeap(), 0, dirW
);
411 SetLastError( 0xdeadbeef );
412 ret
= GetUserProfileDirectoryW( token
, NULL
, &len
);
413 error
= GetLastError();
414 ok(!ret
, "expected failure\n");
415 ok(error
== ERROR_INSUFFICIENT_BUFFER
, "expected ERROR_INSUFFICIENT_BUFFER, got %lu\n", error
);
416 ok(len
, "expected len > 0\n");
418 dirW
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, len
* sizeof(WCHAR
) );
419 SetLastError( 0xdeadbeef );
420 ret
= GetUserProfileDirectoryW( token
, dirW
, &len
);
421 ok(ret
, "expected success %lu\n", GetLastError());
422 ok(len
, "expected len > 0\n");
423 ok(lstrlenW( dirW
) == len
- 1, "length mismatch %d != %ld - 1\n", lstrlenW( dirW
), len
);
424 HeapFree( GetProcessHeap(), 0, dirW
);
427 dirW
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, 32 * sizeof(WCHAR
) );
428 SetLastError( 0xdeadbeef );
429 ret
= GetUserProfileDirectoryW( token
, dirW
, &len2
);
430 error
= GetLastError();
431 ok(!ret
, "expected failure\n");
432 HeapFree( GetProcessHeap(), 0, dirW
);
433 if (error
== ERROR_INSUFFICIENT_BUFFER
)
434 ok(len2
== len
, "expected %ld, got %ld\n", len
, len2
);
436 ok(broken(error
== ERROR_INVALID_PARAMETER
) /* win10 1809+ */,
437 "unexpected error %lu\n", error
);
439 CloseHandle( token
);
444 pIsWow64Process
= (void*)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
447 test_get_profiles_dir();
448 test_get_user_profile_dir();