wined3d: Pass a wined3d_device_context to wined3d_cs_emit_blt_sub_resource().
[wine/zf.git] / dlls / userenv / tests / userenv.c
bloba91edeef260d3327ab5b733614c5146b23cb5da6
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 %d\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];
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 { "TEMP" },
100 { "TMP" },
101 { "CommonProgramFiles" },
102 { "ProgramFiles" },
103 { "PATH" },
104 { "USERPROFILE" }
106 static const struct profile_item common_win64_vars[] = {
107 { "ProgramFiles(x86)" },
108 { "CommonProgramFiles(x86)" },
109 { "ProgramW6432" },
110 { "CommonProgramW6432" }
113 r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
114 expect(TRUE, r);
116 r = GetEnvironmentVariableA("SystemRoot", systemroot, sizeof(systemroot));
117 ok(r != 0, "GetEnvironmentVariable failed (%d)\n", GetLastError());
119 r = SetEnvironmentVariableA("SystemRoot", "overwrite");
120 expect(TRUE, r);
122 if (0)
124 /* Crashes on NT4 */
125 r = CreateEnvironmentBlock(NULL, NULL, FALSE);
126 expect(FALSE, r);
129 r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
130 expect(TRUE, r);
132 if (0)
134 /* Crashes on NT4 */
135 r = CreateEnvironmentBlock(NULL, htok, FALSE);
136 expect(FALSE, r);
139 r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
140 expect(TRUE, r);
142 r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
143 expect(TRUE, r);
145 r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
146 expect(TRUE, r);
148 r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
149 expect(TRUE, r);
151 r = SetEnvironmentVariableA("SystemRoot", systemroot);
152 expect(TRUE, r);
154 for(i=0; i<4; i++)
156 r = get_env(env[i], "SystemRoot", &st);
157 ok(!strcmp(st, "SystemRoot=overwrite"), "%s\n", st);
158 expect(TRUE, r);
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");
178 else
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);
191 if(pIsWow64Process)
192 pIsWow64Process(GetCurrentProcess(), &is_wow64);
193 if (sizeof(void*)==8 || is_wow64)
195 for (i = 0; i < ARRAY_SIZE(common_win64_vars); i++)
197 for (j=0; j<4; j++)
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);
207 expect(FALSE, r);
209 r = get_env(env[1], "WINE_XYZZY", &st);
210 expect(FALSE, r);
212 r = get_env(env[2], "WINE_XYZZY", &st);
213 expect(TRUE, r);
214 if (r) HeapFree(GetProcessHeap(), 0, st);
216 r = get_env(env[3], "WINE_XYZZY", &st);
217 expect(TRUE, r);
218 if (r) HeapFree(GetProcessHeap(), 0, st);
220 for (i = 0; i < ARRAY_SIZE(env); i++)
222 r = DestroyEnvironmentBlock(env[i]);
223 expect(TRUE, r);
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";
231 BOOL r;
232 DWORD cch, profiles_len;
233 LONG l;
234 HKEY key;
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);
241 if (l)
243 win_skip("No ProfilesDirectory value (NT4), skipping tests\n");
244 return;
246 buf = HeapAlloc(GetProcessHeap(), 0, cch);
247 RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, (BYTE *)buf, &cch);
248 RegCloseKey(key);
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);
256 expect(FALSE, r);
257 expect_gle(ERROR_INVALID_PARAMETER);
258 SetLastError(0xdeadbeef);
259 r = GetProfilesDirectoryA(NULL, &cch);
260 expect(FALSE, r);
261 expect_gle(ERROR_INVALID_PARAMETER);
262 SetLastError(0xdeadbeef);
263 cch = 1;
264 r = GetProfilesDirectoryA(small_buf, &cch);
265 expect(FALSE, r);
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
272 * overrun.
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);
287 expect(FALSE, r);
288 expect_gle(ERROR_INVALID_PARAMETER);
290 cch = 0;
291 SetLastError(0xdeadbeef);
292 r = GetProfilesDirectoryW(NULL, &cch);
293 expect(FALSE, r);
294 expect_gle(ERROR_INSUFFICIENT_BUFFER);
295 ok(cch, "expected cch > 0\n");
297 SetLastError(0xdeadbeef);
298 r = GetProfilesDirectoryW(NULL, &cch);
299 expect(FALSE, r);
300 expect_gle(ERROR_INSUFFICIENT_BUFFER);
303 static void test_get_user_profile_dir(void)
305 BOOL ret;
306 DWORD error, len, len2;
307 HANDLE token;
308 char *dirA;
309 WCHAR *dirW;
311 if (!GetEnvironmentVariableA( "ALLUSERSPROFILE", NULL, 0 ))
313 win_skip("Skipping tests on NT4\n");
314 return;
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 );
340 len = 0;
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);
348 len = 0;
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 );
365 trace("%s\n", dirA);
366 HeapFree( GetProcessHeap(), 0, dirA );
368 else
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 );
392 len = 0;
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 );
408 len2 = 0;
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);
417 else
418 ok(broken(error == ERROR_INVALID_PARAMETER) /* win10 1809+ */,
419 "unexpected error %u\n", error);
421 CloseHandle( token );
424 START_TEST(userenv)
426 pIsWow64Process = (void*)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
428 test_create_env();
429 test_get_profiles_dir();
430 test_get_user_profile_dir();