include/mscvpdb.h: Use flexible array members for the rest of structures.
[wine.git] / dlls / userenv / tests / userenv.c
bloba440f53872381511105c13538a985888e3e5a849
1 /*
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
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winreg.h"
30 #include "userenv.h"
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);
40 struct profile_item
42 const char * name;
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;
50 char buf[256];
52 if (!env || !var || !result) return FALSE;
54 varlen = strlen(var);
57 if (!WideCharToMultiByte( CP_ACP, 0, p, -1, buf, sizeof(buf), NULL, NULL )) buf[sizeof(buf)-1] = 0;
58 envlen = strlen(buf);
59 if (CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, buf, min(envlen, varlen), var, varlen) == CSTR_EQUAL)
61 if (buf[varlen] == '=')
63 buflen = strlen(buf);
64 *result = HeapAlloc(GetProcessHeap(), 0, buflen + 1);
65 if (!*result) return FALSE;
66 memcpy(*result, buf, buflen + 1);
67 return TRUE;
70 while (*p) p++;
71 p++;
72 } while (*p);
73 return FALSE;
76 static void test_create_env(void)
78 BOOL r, is_wow64 = FALSE;
79 HANDLE htok;
80 WCHAR * env[4];
81 char * st, systemroot[100], programdata[100], allusersprofile[100];
82 int i, j;
84 static const struct profile_item common_vars[] = {
85 { "ComSpec" },
86 { "COMPUTERNAME" },
87 { "NUMBER_OF_PROCESSORS" },
88 { "OS" },
89 { "PROCESSOR_ARCHITECTURE" },
90 { "PROCESSOR_IDENTIFIER" },
91 { "PROCESSOR_LEVEL" },
92 { "PROCESSOR_REVISION" },
93 { "SystemDrive" },
94 { "SystemRoot" },
95 { "windir" }
97 static const struct profile_item common_post_nt4_vars[] = {
98 { "ALLUSERSPROFILE" },
99 { "ProgramData" },
100 { "TEMP" },
101 { "TMP" },
102 { "CommonProgramFiles" },
103 { "ProgramFiles" },
104 { "PATH" },
105 { "USERPROFILE" }
107 static const struct profile_item common_win64_vars[] = {
108 { "ProgramFiles(x86)" },
109 { "CommonProgramFiles(x86)" },
110 { "ProgramW6432" },
111 { "CommonProgramW6432" }
114 r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
115 expect(TRUE, r);
117 r = GetEnvironmentVariableA("SystemRoot", systemroot, sizeof(systemroot));
118 ok(r != 0, "GetEnvironmentVariable failed (%ld)\n", GetLastError());
120 r = SetEnvironmentVariableA("SystemRoot", "overwrite");
121 expect(TRUE, r);
123 r = GetEnvironmentVariableA("ProgramData", programdata, sizeof(programdata));
124 ok(r != 0, "GetEnvironmentVariable failed (%ld)\n", GetLastError());
126 r = SetEnvironmentVariableA("ProgramData", "overwrite");
127 expect(TRUE, r);
129 if (0)
131 /* Crashes on NT4 */
132 r = CreateEnvironmentBlock(NULL, NULL, FALSE);
133 expect(FALSE, r);
136 r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
137 expect(TRUE, r);
139 if (0)
141 /* Crashes on NT4 */
142 r = CreateEnvironmentBlock(NULL, htok, FALSE);
143 expect(FALSE, r);
146 r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
147 expect(TRUE, r);
149 r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
150 expect(TRUE, r);
152 r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
153 expect(TRUE, r);
155 r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
156 expect(TRUE, r);
158 r = SetEnvironmentVariableA("SystemRoot", systemroot);
159 expect(TRUE, r);
161 r = SetEnvironmentVariableA("ProgramData", programdata);
162 expect(TRUE, r);
164 for(i=0; i<4; i++)
166 r = get_env(env[i], "SystemRoot", &st);
167 ok(!strcmp(st, "SystemRoot=overwrite"), "%s\n", st);
168 expect(TRUE, r);
169 HeapFree(GetProcessHeap(), 0, st);
171 r = get_env(env[i], "ProgramData", &st);
172 ok(strcmp(st, "ProgramData=overwrite"), "%s\n", st);
173 expect(TRUE, r);
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");
193 else
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);
209 if(pIsWow64Process)
210 pIsWow64Process(GetCurrentProcess(), &is_wow64);
211 if (sizeof(void*)==8 || is_wow64)
213 for (i = 0; i < ARRAY_SIZE(common_win64_vars); i++)
215 for (j=0; j<4; j++)
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);
225 expect(FALSE, r);
227 r = get_env(env[1], "WINE_XYZZY", &st);
228 expect(FALSE, r);
230 r = get_env(env[2], "WINE_XYZZY", &st);
231 expect(TRUE, r);
232 if (r) HeapFree(GetProcessHeap(), 0, st);
234 r = get_env(env[3], "WINE_XYZZY", &st);
235 expect(TRUE, r);
236 if (r) HeapFree(GetProcessHeap(), 0, st);
238 for (i = 0; i < ARRAY_SIZE(env); i++)
240 r = DestroyEnvironmentBlock(env[i]);
241 expect(TRUE, r);
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";
249 BOOL r;
250 DWORD cch, profiles_len;
251 LONG l;
252 HKEY key;
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);
259 if (l)
261 win_skip("No ProfilesDirectory value (NT4), skipping tests\n");
262 return;
264 buf = HeapAlloc(GetProcessHeap(), 0, cch);
265 RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, (BYTE *)buf, &cch);
266 RegCloseKey(key);
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);
274 expect(FALSE, r);
275 expect_gle(ERROR_INVALID_PARAMETER);
276 SetLastError(0xdeadbeef);
277 r = GetProfilesDirectoryA(NULL, &cch);
278 expect(FALSE, r);
279 expect_gle(ERROR_INVALID_PARAMETER);
280 SetLastError(0xdeadbeef);
281 cch = 1;
282 r = GetProfilesDirectoryA(small_buf, &cch);
283 expect(FALSE, r);
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
290 * overrun.
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);
305 expect(FALSE, r);
306 expect_gle(ERROR_INVALID_PARAMETER);
308 cch = 0;
309 SetLastError(0xdeadbeef);
310 r = GetProfilesDirectoryW(NULL, &cch);
311 expect(FALSE, r);
312 expect_gle(ERROR_INSUFFICIENT_BUFFER);
313 ok(cch, "expected cch > 0\n");
315 SetLastError(0xdeadbeef);
316 r = GetProfilesDirectoryW(NULL, &cch);
317 expect(FALSE, r);
318 expect_gle(ERROR_INSUFFICIENT_BUFFER);
321 static void test_get_user_profile_dir(void)
323 BOOL ret;
324 DWORD error, len, len2;
325 HANDLE token;
326 char *dirA;
327 WCHAR *dirW;
329 if (!GetEnvironmentVariableA( "ALLUSERSPROFILE", NULL, 0 ))
331 win_skip("Skipping tests on NT4\n");
332 return;
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 );
358 len = 0;
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);
366 len = 0;
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 );
383 trace("%s\n", dirA);
384 HeapFree( GetProcessHeap(), 0, dirA );
386 else
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 );
410 len = 0;
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 );
426 len2 = 0;
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);
435 else
436 ok(broken(error == ERROR_INVALID_PARAMETER) /* win10 1809+ */,
437 "unexpected error %lu\n", error);
439 CloseHandle( token );
442 START_TEST(userenv)
444 pIsWow64Process = (void*)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
446 test_create_env();
447 test_get_profiles_dir();
448 test_get_user_profile_dir();