kernel32: Set the USERPROFILE and ALLUSERSPROFILE environment variables based on...
[wine] / dlls / userenv / tests / userenv.c
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
29 #include "userenv.h"
30
31 #include "wine/test.h"
32
33 #define expect(EXPECTED,GOT) ok((GOT)==(EXPECTED), "Expected %d, got %d\n", (EXPECTED), (GOT))
34 #define expect_env(EXPECTED,GOT,VAR) ok((GOT)==(EXPECTED), "Expected %d, got %d for %s (%d)\n", (EXPECTED), (GOT), (VAR), j)
35
36 struct profile_item
37 {
38     const char * name;
39     const int todo[4];
40 };
41
42 /* Debugging functions from wine/libs/wine/debug.c, slightly modified */
43
44 /* allocate some tmp string space */
45 /* FIXME: this is not 100% thread-safe */
46 static char *get_tmp_space( int size )
47 {
48     static char *list[32];
49     static long pos;
50     char *ret;
51     int idx;
52
53     idx = ++pos % (sizeof(list)/sizeof(list[0]));
54     if ((ret = realloc( list[idx], size ))) list[idx] = ret;
55     return ret;
56 }
57
58 /* default implementation of wine_dbgstr_wn */
59 static const char *default_dbgstr_wn( const WCHAR *str, int n, BOOL quotes )
60 {
61     char *dst, *res;
62
63     if (!HIWORD(str))
64     {
65         if (!str) return "(null)";
66         res = get_tmp_space( 6 );
67         sprintf( res, "#%04x", LOWORD(str) );
68         return res;
69     }
70     if (n == -1) n = lstrlenW(str);
71     if (n < 0) n = 0;
72     else if (n > 200) n = 200;
73     dst = res = get_tmp_space( n * 5 + 7 );
74     if (quotes)
75     {
76         *dst++ = 'L';
77         *dst++ = '"';
78     }
79     while (n-- > 0)
80     {
81         WCHAR c = *str++;
82         switch (c)
83         {
84         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
85         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
86         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
87         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
88         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
89         default:
90             if (c >= ' ' && c <= 126)
91                 *dst++ = (char)c;
92             else
93             {
94                 *dst++ = '\\';
95                 sprintf(dst,"%04x",c);
96                 dst+=4;
97             }
98         }
99     }
100     if (quotes) *dst++ = '"';
101     if (*str)
102     {
103         *dst++ = '.';
104         *dst++ = '.';
105         *dst++ = '.';
106     }
107     *dst = 0;
108     return res;
109 }
110
111 const char *wine_dbgstr_wn( const WCHAR *s, int n )
112 {
113     return default_dbgstr_wn(s, n, TRUE);
114 }
115
116 const char *wine_dbgstr_w( const WCHAR *s )
117 {
118     return default_dbgstr_wn( s, -1, TRUE);
119 }
120
121 const char *userenv_dbgstr_w( const WCHAR *s )
122 {
123     return default_dbgstr_wn( s, -1, FALSE);
124 }
125
126 /* Helper function for retrieving environment variables */
127 static BOOL get_env(const WCHAR * env, const char * var, char ** result)
128 {
129     const WCHAR * p = env;
130     int envlen, varlen, buflen;
131     char buf[256];
132
133     if (!env || !var || !result) return FALSE;
134
135     varlen = strlen(var);
136     do
137     {
138         envlen = lstrlenW(p);
139         sprintf(buf, "%s", userenv_dbgstr_w(p));
140         if (CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, buf, varlen, var, varlen) == CSTR_EQUAL)
141         {
142             if (buf[varlen] == '=')
143             {
144                 buflen = strlen(buf);
145                 *result = HeapAlloc(GetProcessHeap(), 0, buflen + 1);
146                 if (!*result) return FALSE;
147                 memcpy(*result, buf, buflen + 1);
148                 return TRUE;
149             }
150         }
151         p = p + envlen + 1;
152     } while (*p);
153     return FALSE;
154 }
155
156 static void test_create_env(void)
157 {
158     BOOL r;
159     HANDLE htok;
160     WCHAR * env1, * env2, * env3, * env4;
161     char * st;
162     int i, j;
163
164     static const struct profile_item common_vars[] = {
165         { "ALLUSERSPROFILE", { 1, 1, 0, 0 } },
166         { "CommonProgramFiles", { 1, 1, 1, 1 } },
167         { "ComSpec", { 1, 1, 0, 0 } },
168         { "COMPUTERNAME", { 1, 1, 1, 1 } },
169         { "NUMBER_OF_PROCESSORS", { 1, 1, 0, 0 } },
170         { "OS", { 1, 1, 0, 0 } },
171         { "PROCESSOR_ARCHITECTURE", { 1, 1, 0, 0 } },
172         { "PROCESSOR_IDENTIFIER", { 1, 1, 0, 0 } },
173         { "PROCESSOR_LEVEL", { 1, 1, 0, 0 } },
174         { "PROCESSOR_REVISION", { 1, 1, 0, 0 } },
175         { "SystemDrive", { 1, 1, 0, 0 } },
176         { "SystemRoot", { 1, 1, 0, 0 } },
177         { "windir", { 1, 1, 0, 0 } },
178         { "ProgramFiles", { 1, 1, 0, 0 } },
179         { 0, { 0, 0, 0, 0 } }
180     };
181     static const struct profile_item htok_vars[] = {
182         { "PATH", { 1, 1, 0, 0 } },
183         { "TEMP", { 1, 1, 0, 0 } },
184         { "TMP", { 1, 1, 0, 0 } },
185         { "USERPROFILE", { 1, 1, 0, 0 } },
186         { 0, { 0, 0, 0, 0 } }
187     };
188
189     r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
190     expect(TRUE, r);
191
192     r = CreateEnvironmentBlock(NULL, NULL, FALSE);
193     expect(FALSE, r);
194
195     r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
196     expect(TRUE, r);
197
198     r = CreateEnvironmentBlock(NULL, htok, FALSE);
199     expect(FALSE, r);
200
201     r = CreateEnvironmentBlock((LPVOID) &env1, NULL, FALSE);
202     expect(TRUE, r);
203
204     r = CreateEnvironmentBlock((LPVOID) &env2, htok, FALSE);
205     expect(TRUE, r);
206
207     r = CreateEnvironmentBlock((LPVOID) &env3, NULL, TRUE);
208     expect(TRUE, r);
209
210     r = CreateEnvironmentBlock((LPVOID) &env4, htok, TRUE);
211     expect(TRUE, r);
212
213     /* Test for common environment variables */
214     i = 0;
215     while (common_vars[i].name)
216     {
217         j = 0;
218         r = get_env(env1, common_vars[i].name, &st);
219         if (common_vars[i].todo[j])
220             todo_wine expect_env(TRUE, r, common_vars[i].name);
221         else
222             expect_env(TRUE, r, common_vars[i].name);
223         j++;
224         r = get_env(env2, common_vars[i].name, &st);
225         if (common_vars[i].todo[j])
226             todo_wine expect_env(TRUE, r, common_vars[i].name);
227         else
228             expect_env(TRUE, r, common_vars[i].name);
229         j++;
230         r = get_env(env3, common_vars[i].name, &st);
231         if (common_vars[i].todo[j])
232             todo_wine expect_env(TRUE, r, common_vars[i].name);
233         else
234             expect_env(TRUE, r, common_vars[i].name);
235         j++;
236         r = get_env(env4, common_vars[i].name, &st);
237         if (common_vars[i].todo[j])
238             todo_wine expect_env(TRUE, r, common_vars[i].name);
239         else
240             expect_env(TRUE, r, common_vars[i].name);
241         i++;
242     }
243
244     /* Test for environment variables with values that depends on htok */
245     i = 0;
246     while (htok_vars[i].name)
247     {
248         j = 0;
249         r = get_env(env1, htok_vars[i].name, &st);
250         if (htok_vars[i].todo[j])
251             todo_wine expect_env(TRUE, r, htok_vars[i].name);
252         else
253             expect_env(TRUE, r, htok_vars[i].name);
254         j++;
255         r = get_env(env2, htok_vars[i].name, &st);
256         if (htok_vars[i].todo[j])
257             todo_wine expect_env(TRUE, r, htok_vars[i].name);
258         else
259             expect_env(TRUE, r, htok_vars[i].name);
260         j++;
261         r = get_env(env3, htok_vars[i].name, &st);
262         if (htok_vars[i].todo[j])
263             todo_wine expect_env(TRUE, r, htok_vars[i].name);
264         else
265             expect_env(TRUE, r, htok_vars[i].name);
266         j++;
267         r = get_env(env4, htok_vars[i].name, &st);
268         if (htok_vars[i].todo[j])
269             todo_wine expect_env(TRUE, r, htok_vars[i].name);
270         else
271             expect_env(TRUE, r, htok_vars[i].name);
272         i++;
273     }
274
275     r = get_env(env1, "WINE_XYZZY", &st);
276     expect(FALSE, r);
277     r = get_env(env2, "WINE_XYZZY", &st);
278     expect(FALSE, r);
279     r = get_env(env3, "WINE_XYZZY", &st);
280     expect(TRUE, r);
281     r = get_env(env4, "WINE_XYZZY", &st);
282     expect(TRUE, r);
283 }
284
285 START_TEST(userenv)
286 {
287     test_create_env();
288 }