1 /*
2  * Unit test suite for userenv functions
3  *
4  * Copyright 2008 Google (Lei Zhang)
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winnls.h"
28 #include "winreg.h"
29 
30 #include "userenv.h"
31 
32 #include "wine/test.h"
33 
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())
37 
38 static BOOL (WINAPI *pIsWow64Process)(HANDLE,PBOOL);
39 
40 struct profile_item
41 {
42     const char * name;
43 };
44 
45 /* Helper function for retrieving environment variables */
get_env(const WCHAR * env,const char * var,char ** result)46 static BOOL get_env(const WCHAR * env, const char * var, char ** result)
47 {
48     const WCHAR * p = env;
49     int envlen, varlen, buflen;
50     char buf[256];
51 
52     if (!env || !var || !result) return FALSE;
53 
54     varlen = strlen(var);
55     do
56     {
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)
60         {
61             if (buf[varlen] == '=')
62             {
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;
68             }
69         }
70         while (*p) p++;
71         p++;
72     } while (*p);
73     return FALSE;
74 }
75 
test_create_env(void)76 static void test_create_env(void)
77 {
78     BOOL r, is_wow64 = FALSE;
79     HANDLE htok;
80     WCHAR * env[4];
81     char * st, systemroot[100];
82     int i, j;
83 
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" }
96     };
97     static const struct profile_item common_post_nt4_vars[] = {
98         { "ALLUSERSPROFILE" },
99         { "TEMP" },
100         { "TMP" },
101         { "CommonProgramFiles" },
102         { "ProgramFiles" },
103         { "PATH" },
104         { "USERPROFILE" }
105     };
106     static const struct profile_item common_win64_vars[] = {
107         { "ProgramW6432" },
108         { "CommonProgramW6432" }
109     };
110 
111     r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
112     expect(TRUE, r);
113 
114     r = GetEnvironmentVariableA("SystemRoot", systemroot, sizeof(systemroot));
115     ok(r != 0, "GetEnvironmentVariable failed (%d)\n", GetLastError());
116 
117     r = SetEnvironmentVariableA("SystemRoot", "overwrite");
118     expect(TRUE, r);
119 
120     if (0)
121     {
122         /* Crashes on NT4 */
123         r = CreateEnvironmentBlock(NULL, NULL, FALSE);
124         expect(FALSE, r);
125     }
126 
127     r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
128     expect(TRUE, r);
129 
130     if (0)
131     {
132         /* Crashes on NT4 */
133         r = CreateEnvironmentBlock(NULL, htok, FALSE);
134         expect(FALSE, r);
135     }
136 
137     r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
138     expect(TRUE, r);
139 
140     r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
141     expect(TRUE, r);
142 
143     r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
144     expect(TRUE, r);
145 
146     r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
147     expect(TRUE, r);
148 
149     r = SetEnvironmentVariableA("SystemRoot", systemroot);
150     expect(TRUE, r);
151 
152     for(i=0; i<4; i++)
153     {
154         r = get_env(env[i], "SystemRoot", &st);
155 #ifdef __REACTOS__
156         ok(!stricmp(st, "SystemRoot=overwrite"), "%s\n", st);
157 #else
158         ok(!strcmp(st, "SystemRoot=overwrite"), "%s\n", st);
159 #endif
160         expect(TRUE, r);
161         HeapFree(GetProcessHeap(), 0, st);
162     }
163 
164     /* Test for common environment variables (NT4 and higher) */
165     for (i = 0; i < ARRAY_SIZE(common_vars); i++)
166     {
167         for (j = 0; j < 4; j++)
168         {
169             r = get_env(env[j], common_vars[i].name, &st);
170             expect_env(TRUE, r, common_vars[i].name);
171             if (r) HeapFree(GetProcessHeap(), 0, st);
172         }
173     }
174 
175     /* Test for common environment variables (post NT4) */
176     if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL, 0))
177     {
178         win_skip("Some environment variables are not present on NT4\n");
179     }
180     else
181     {
182         for (i = 0; i < ARRAY_SIZE(common_post_nt4_vars); i++)
183         {
184             for (j = 0; j < 4; j++)
185             {
186                 r = get_env(env[j], common_post_nt4_vars[i].name, &st);
187                 expect_env(TRUE, r, common_post_nt4_vars[i].name);
188                 if (r) HeapFree(GetProcessHeap(), 0, st);
189             }
190         }
191     }
192 
193     if(pIsWow64Process)
194         pIsWow64Process(GetCurrentProcess(), &is_wow64);
195     if (sizeof(void*)==8 || is_wow64)
196     {
197         for (i = 0; i < ARRAY_SIZE(common_win64_vars); i++)
198         {
199             for (j=0; j<4; j++)
200             {
201                 r = get_env(env[j], common_win64_vars[i].name, &st);
202                 ok(r || broken(!r)/* Vista,2k3,XP */, "Expected 1, got 0 for %s\n", common_win64_vars[i].name);
203                 if (r) HeapFree(GetProcessHeap(), 0, st);
204             }
205         }
206     }
207 
208     r = get_env(env[0], "WINE_XYZZY", &st);
209     expect(FALSE, r);
210 
211     r = get_env(env[1], "WINE_XYZZY", &st);
212     expect(FALSE, r);
213 
214     r = get_env(env[2], "WINE_XYZZY", &st);
215     expect(TRUE, r);
216     if (r) HeapFree(GetProcessHeap(), 0, st);
217 
218     r = get_env(env[3], "WINE_XYZZY", &st);
219     expect(TRUE, r);
220     if (r) HeapFree(GetProcessHeap(), 0, st);
221 
222     for (i = 0; i < ARRAY_SIZE(env); i++)
223     {
224         r = DestroyEnvironmentBlock(env[i]);
225         expect(TRUE, r);
226     }
227 }
228 
test_get_profiles_dir(void)229 static void test_get_profiles_dir(void)
230 {
231     static const char ProfileListA[] = "Software\\Microsoft\\Windows NT\\CurrentVersion\\ProfileList";
232     static const char ProfilesDirectory[] = "ProfilesDirectory";
233     BOOL r;
234     DWORD cch, profiles_len;
235     LONG l;
236     HKEY key;
237     char *profiles_dir, *buf, small_buf[1];
238 
239     l = RegOpenKeyExA(HKEY_LOCAL_MACHINE, ProfileListA, 0, KEY_READ, &key);
240     ok(!l, "RegOpenKeyExA failed: %d\n", GetLastError());
241 
242     l = RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, NULL, &cch);
243     if (l)
244     {
245         win_skip("No ProfilesDirectory value (NT4), skipping tests\n");
246         return;
247     }
248     buf = HeapAlloc(GetProcessHeap(), 0, cch);
249     RegQueryValueExA(key, ProfilesDirectory, NULL, NULL, (BYTE *)buf, &cch);
250     RegCloseKey(key);
251     profiles_len = ExpandEnvironmentStringsA(buf, NULL, 0);
252     profiles_dir = HeapAlloc(GetProcessHeap(), 0, profiles_len);
253     ExpandEnvironmentStringsA(buf, profiles_dir, profiles_len);
254     HeapFree(GetProcessHeap(), 0, buf);
255 
256     SetLastError(0xdeadbeef);
257     r = GetProfilesDirectoryA(NULL, NULL);
258     expect(FALSE, r);
259     expect_gle(ERROR_INVALID_PARAMETER);
260     SetLastError(0xdeadbeef);
261     r = GetProfilesDirectoryA(NULL, &cch);
262     expect(FALSE, r);
263     expect_gle(ERROR_INVALID_PARAMETER);
264     SetLastError(0xdeadbeef);
265     cch = 1;
266     r = GetProfilesDirectoryA(small_buf, &cch);
267     expect(FALSE, r);
268     expect_gle(ERROR_INSUFFICIENT_BUFFER);
269     /* MSDN claims the returned character count includes the NULL terminator
270      * when the buffer is too small, but that's not in fact what gets returned.
271      */
272     ok(cch == profiles_len - 1, "expected %d, got %d\n", profiles_len - 1, cch);
273     /* Allocate one more character than the return value to prevent a buffer
274      * overrun.
275      */
276     buf = HeapAlloc(GetProcessHeap(), 0, cch + 1);
277     r = GetProfilesDirectoryA(buf, &cch);
278     /* Rather than a BOOL, the return value is also the number of characters
279      * stored in the buffer.
280      */
281     expect(profiles_len - 1, r);
282     ok(!strcmp(buf, profiles_dir), "expected %s, got %s\n", profiles_dir, buf);
283 
284     HeapFree(GetProcessHeap(), 0, buf);
285     HeapFree(GetProcessHeap(), 0, profiles_dir);
286 
287     SetLastError(0xdeadbeef);
288     r = GetProfilesDirectoryW(NULL, NULL);
289     expect(FALSE, r);
290     expect_gle(ERROR_INVALID_PARAMETER);
291 
292     cch = 0;
293     SetLastError(0xdeadbeef);
294     r = GetProfilesDirectoryW(NULL, &cch);
295     expect(FALSE, r);
296     expect_gle(ERROR_INSUFFICIENT_BUFFER);
297     ok(cch, "expected cch > 0\n");
298 
299     SetLastError(0xdeadbeef);
300     r = GetProfilesDirectoryW(NULL, &cch);
301     expect(FALSE, r);
302     expect_gle(ERROR_INSUFFICIENT_BUFFER);
303 }
304 
test_get_user_profile_dir(void)305 static void test_get_user_profile_dir(void)
306 {
307     BOOL ret;
308     DWORD error, len;
309     HANDLE token;
310     char *dirA;
311     WCHAR *dirW;
312 
313     if (!GetEnvironmentVariableA( "ALLUSERSPROFILE", NULL, 0 ))
314     {
315         win_skip("Skipping tests on NT4\n");
316         return;
317     }
318 
319     ret = OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY, &token );
320     ok(ret, "expected success %u\n", GetLastError());
321 
322     SetLastError( 0xdeadbeef );
323     ret = GetUserProfileDirectoryA( NULL, NULL, NULL );
324     error = GetLastError();
325     ok(!ret, "expected failure\n");
326     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
327 
328     SetLastError( 0xdeadbeef );
329     ret = GetUserProfileDirectoryA( token, NULL, NULL );
330     error = GetLastError();
331     ok(!ret, "expected failure\n");
332     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
333 
334     dirA = HeapAlloc( GetProcessHeap(), 0, 32 );
335     SetLastError( 0xdeadbeef );
336     ret = GetUserProfileDirectoryA( token, dirA, NULL );
337     error = GetLastError();
338     ok(!ret, "expected failure\n");
339     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
340     HeapFree( GetProcessHeap(), 0, dirA );
341 
342     len = 0;
343     SetLastError( 0xdeadbeef );
344     ret = GetUserProfileDirectoryA( token, NULL, &len );
345     error = GetLastError();
346     ok(!ret, "expected failure\n");
347     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
348     ok(!len, "expected 0, got %u\n", len);
349 
350     len = 0;
351     dirA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 32 );
352     SetLastError( 0xdeadbeef );
353     ret = GetUserProfileDirectoryA( token, dirA, &len );
354     error = GetLastError();
355     ok(!ret, "expected failure\n");
356     ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
357     ok(len, "expected len > 0\n");
358     HeapFree( GetProcessHeap(), 0, dirA );
359 
360     dirA = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len );
361     SetLastError( 0xdeadbeef );
362     ret = GetUserProfileDirectoryA( token, dirA, &len );
363     ok(ret, "expected success %u\n", GetLastError());
364     ok(len, "expected len > 0\n");
365     ok(lstrlenA( dirA ) == len - 1, "length mismatch %d != %d - 1\n", lstrlenA( dirA ), len );
366     trace("%s\n", dirA);
367     HeapFree( GetProcessHeap(), 0, dirA );
368 
369     SetLastError( 0xdeadbeef );
370     ret = GetUserProfileDirectoryW( NULL, NULL, NULL );
371     error = GetLastError();
372     ok(!ret, "expected failure\n");
373     todo_wine ok(error == ERROR_INVALID_HANDLE, "expected ERROR_INVALID_HANDLE, got %u\n", error);
374 
375     SetLastError( 0xdeadbeef );
376     ret = GetUserProfileDirectoryW( token, NULL, NULL );
377     error = GetLastError();
378     ok(!ret, "expected failure\n");
379     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
380 
381     dirW = HeapAlloc( GetProcessHeap(), 0, 32 );
382     SetLastError( 0xdeadbeef );
383     ret = GetUserProfileDirectoryW( token, dirW, NULL );
384     error = GetLastError();
385     ok(!ret, "expected failure\n");
386     ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
387     HeapFree( GetProcessHeap(), 0, dirW );
388 
389     len = 0;
390     SetLastError( 0xdeadbeef );
391     ret = GetUserProfileDirectoryW( token, NULL, &len );
392     error = GetLastError();
393     ok(!ret, "expected failure\n");
394     ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
395     ok(len, "expected len > 0\n");
396 
397     dirW = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len * sizeof(WCHAR) );
398     SetLastError( 0xdeadbeef );
399     ret = GetUserProfileDirectoryW( token, dirW, &len );
400     ok(ret, "expected success %u\n", GetLastError());
401     ok(len, "expected len > 0\n");
402     ok(lstrlenW( dirW ) == len - 1, "length mismatch %d != %d - 1\n", lstrlenW( dirW ), len );
403     HeapFree( GetProcessHeap(), 0, dirW );
404 
405     CloseHandle( token );
406 }
407 
START_TEST(userenv)408 START_TEST(userenv)
409 {
410     pIsWow64Process = (void*)GetProcAddress(GetModuleHandleA("kernel32.dll"), "IsWow64Process");
411 
412     test_create_env();
413     test_get_profiles_dir();
414     test_get_user_profile_dir();
415 }
416