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())
44 /* Helper function for retrieving environment variables */
45 static BOOL get_env(const WCHAR * env, const char * var, char ** result)
47 const WCHAR * p = env;
48 int envlen, varlen, buflen;
51 if (!env || !var || !result) return FALSE;
56 if (!WideCharToMultiByte( CP_ACP, 0, p, -1, buf, sizeof(buf), NULL, NULL )) buf[sizeof(buf)-1] = 0;
58 if (CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, buf, min(envlen, varlen), var, varlen) == CSTR_EQUAL)
60 if (buf[varlen] == '=')
63 *result = HeapAlloc(GetProcessHeap(), 0, buflen + 1);
64 if (!*result) return FALSE;
65 memcpy(*result, buf, buflen + 1);
75 static void test_create_env(void)
83 static const struct profile_item common_vars[] = {
84 { "ComSpec", { 1, 1, 0, 0 } },
85 { "COMPUTERNAME", { 1, 1, 1, 1 } },
86 { "NUMBER_OF_PROCESSORS", { 1, 1, 0, 0 } },
87 { "OS", { 1, 1, 0, 0 } },
88 { "PROCESSOR_ARCHITECTURE", { 1, 1, 0, 0 } },
89 { "PROCESSOR_IDENTIFIER", { 1, 1, 0, 0 } },
90 { "PROCESSOR_LEVEL", { 1, 1, 0, 0 } },
91 { "PROCESSOR_REVISION", { 1, 1, 0, 0 } },
92 { "SystemDrive", { 1, 1, 0, 0 } },
93 { "SystemRoot", { 1, 1, 0, 0 } },
94 { "windir", { 1, 1, 0, 0 } }
96 static const struct profile_item common_post_nt4_vars[] = {
97 { "ALLUSERSPROFILE", { 1, 1, 0, 0 } },
98 { "TEMP", { 1, 1, 0, 0 } },
99 { "TMP", { 1, 1, 0, 0 } },
100 { "CommonProgramFiles", { 1, 1, 0, 0 } },
101 { "ProgramFiles", { 1, 1, 0, 0 } }
103 static const struct profile_item htok_vars[] = {
104 { "PATH", { 1, 1, 0, 0 } },
105 { "USERPROFILE", { 1, 1, 0, 0 } }
108 r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
114 r = CreateEnvironmentBlock(NULL, NULL, FALSE);
118 r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
124 r = CreateEnvironmentBlock(NULL, htok, FALSE);
128 r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
131 r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
134 r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
137 r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
140 /* Test for common environment variables (NT4 and higher) */
141 for (i = 0; i < sizeof(common_vars)/sizeof(common_vars[0]); i++)
143 for (j = 0; j < 4; j++)
145 r = get_env(env[j], common_vars[i].name, &st);
146 if (common_vars[i].todo[j])
147 todo_wine expect_env(TRUE, r, common_vars[i].name);
149 expect_env(TRUE, r, common_vars[i].name);
150 if (r) HeapFree(GetProcessHeap(), 0, st);
154 /* Test for common environment variables (post NT4) */
155 if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL, 0))
157 win_skip("Some environment variables are not present on NT4\n");
161 for (i = 0; i < sizeof(common_post_nt4_vars)/sizeof(common_post_nt4_vars[0]); i++)
163 for (j = 0; j < 4; j++)
165 r = get_env(env[j], common_post_nt4_vars[i].name, &st);
166 if (common_post_nt4_vars[i].todo[j])
167 todo_wine expect_env(TRUE, r, common_post_nt4_vars[i].name);
169 expect_env(TRUE, r, common_post_nt4_vars[i].name);
170 if (r) HeapFree(GetProcessHeap(), 0, st);
175 /* Test for environment variables with values that depends on htok */
176 for (i = 0; i < sizeof(htok_vars)/sizeof(htok_vars[0]); i++)
178 for (j = 0; j < 4; j++)
180 r = get_env(env[j], htok_vars[i].name, &st);
181 if (htok_vars[i].todo[j])
182 todo_wine expect_env(TRUE, r, htok_vars[i].name);
184 expect_env(TRUE, r, htok_vars[i].name);
185 if (r) HeapFree(GetProcessHeap(), 0, st);
189 r = get_env(env[0], "WINE_XYZZY", &st);
192 r = get_env(env[1], "WINE_XYZZY", &st);
195 r = get_env(env[2], "WINE_XYZZY", &st);
197 if (r) HeapFree(GetProcessHeap(), 0, st);
199 r = get_env(env[3], "WINE_XYZZY", &st);
201 if (r) HeapFree(GetProcessHeap(), 0, st);
203 for (i = 0; i < sizeof(env) / sizeof(env[0]); i++)
205 r = DestroyEnvironmentBlock(env[i]);
210 static void test_get_profiles_dir(void)
212 static const char ProfileListA[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
213 static const char ProfilesDirectory[] = "ProfilesDirectory";
215 DWORD cch, profiles_len;
218 char *profiles_dir, *buf, small_buf[1];
220 l = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ProfileListA, 0, KEY_READ, &key);
223 win_skip("No ProfileList key (Win9x), skipping tests\n");
226 l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, NULL, &cch);
229 win_skip("No ProfilesDirectory value, skipping tests\n");
232 buf = HeapAlloc(GetProcessHeap(), 0, cch);
233 RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, (BYTE *)buf, &cch);
235 profiles_len = ExpandEnvironmentStringsA(buf, NULL, 0);
236 profiles_dir = HeapAlloc(GetProcessHeap(), 0, profiles_len);
237 ExpandEnvironmentStringsA(buf, profiles_dir, profiles_len);
238 HeapFree(GetProcessHeap(), 0, buf);
240 SetLastError(0xdeadbeef);
241 r = GetProfilesDirectoryA(NULL, NULL);
243 expect_gle(ERROR_INVALID_PARAMETER);
244 SetLastError(0xdeadbeef);
245 r = GetProfilesDirectoryA(NULL, &cch);
247 expect_gle(ERROR_INVALID_PARAMETER);
248 SetLastError(0xdeadbeef);
250 r = GetProfilesDirectoryA(small_buf, &cch);
252 expect_gle(ERROR_INSUFFICIENT_BUFFER);
253 /* MSDN claims the returned character count includes the NULL terminator
254 * when the buffer is too small, but that's not in fact what gets returned.
256 ok(cch == profiles_len - 1, "expected %d, got %d\n", profiles_len - 1, cch);
257 /* Allocate one more character than the return value to prevent a buffer
260 buf = HeapAlloc(GetProcessHeap(), 0, cch + 1);
261 r = GetProfilesDirectoryA(buf, &cch);
262 /* Rather than a BOOL, the return value is also the number of characters
263 * stored in the buffer.
265 expect(profiles_len - 1, r);
266 ok(!strcmp(buf, profiles_dir), "expected %s, got %s\n", profiles_dir, buf);
268 HeapFree(GetProcessHeap(), 0, buf);
269 HeapFree(GetProcessHeap(), 0, profiles_dir);
275 test_get_profiles_dir();