push a6a7df41ae272b3b1bf5fcfb544267c1e96afa99
[wine/hacks.git] / dlls / userenv / tests / userenv.c
blob49491f7efdd74657cbd404765c970c8abfb699fa
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"
29 #include "userenv.h"
31 #include "wine/test.h"
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)
36 struct profile_item
38 const char * name;
39 const int todo[4];
42 /* Debugging functions from wine/libs/wine/debug.c, slightly modified */
44 /* allocate some tmp string space */
45 /* FIXME: this is not 100% thread-safe */
46 static char *get_tmp_space( int size )
48 static char *list[32];
49 static long pos;
50 char *ret;
51 int idx;
53 idx = ++pos % (sizeof(list)/sizeof(list[0]));
54 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
55 return ret;
58 /* default implementation of wine_dbgstr_wn */
59 static const char *default_dbgstr_wn( const WCHAR *str, int n, BOOL quotes )
61 char *dst, *res;
63 if (!HIWORD(str))
65 if (!str) return "(null)";
66 res = get_tmp_space( 6 );
67 sprintf( res, "#%04x", LOWORD(str) );
68 return res;
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)
76 *dst++ = 'L';
77 *dst++ = '"';
79 while (n-- > 0)
81 WCHAR c = *str++;
82 switch (c)
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
94 *dst++ = '\\';
95 sprintf(dst,"%04x",c);
96 dst+=4;
100 if (quotes) *dst++ = '"';
101 if (*str)
103 *dst++ = '.';
104 *dst++ = '.';
105 *dst++ = '.';
107 *dst = 0;
108 return res;
111 const char *wine_dbgstr_wn( const WCHAR *s, int n )
113 return default_dbgstr_wn(s, n, TRUE);
116 const char *wine_dbgstr_w( const WCHAR *s )
118 return default_dbgstr_wn( s, -1, TRUE);
121 const char *userenv_dbgstr_w( const WCHAR *s )
123 return default_dbgstr_wn( s, -1, FALSE);
126 /* Helper function for retrieving environment variables */
127 static BOOL get_env(const WCHAR * env, const char * var, char ** result)
129 const WCHAR * p = env;
130 int envlen, varlen, buflen;
131 char buf[256];
133 if (!env || !var || !result) return FALSE;
135 varlen = strlen(var);
138 envlen = lstrlenW(p);
139 sprintf(buf, "%s", userenv_dbgstr_w(p));
140 if (CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, buf, min(envlen, varlen), var, varlen) == CSTR_EQUAL)
142 if (buf[varlen] == '=')
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;
151 p = p + envlen + 1;
152 } while (*p);
153 return FALSE;
156 static void test_create_env(void)
158 BOOL r;
159 HANDLE htok;
160 WCHAR * env[4];
161 char * st;
162 int i, j;
164 static const struct profile_item common_vars[] = {
165 { "ComSpec", { 1, 1, 0, 0 } },
166 { "COMPUTERNAME", { 1, 1, 1, 1 } },
167 { "NUMBER_OF_PROCESSORS", { 1, 1, 0, 0 } },
168 { "OS", { 1, 1, 0, 0 } },
169 { "PROCESSOR_ARCHITECTURE", { 1, 1, 0, 0 } },
170 { "PROCESSOR_IDENTIFIER", { 1, 1, 0, 0 } },
171 { "PROCESSOR_LEVEL", { 1, 1, 0, 0 } },
172 { "PROCESSOR_REVISION", { 1, 1, 0, 0 } },
173 { "SystemDrive", { 1, 1, 0, 0 } },
174 { "SystemRoot", { 1, 1, 0, 0 } },
175 { "windir", { 1, 1, 0, 0 } }
177 static const struct profile_item common_post_nt4_vars[] = {
178 { "ALLUSERSPROFILE", { 1, 1, 0, 0 } },
179 { "CommonProgramFiles", { 1, 1, 1, 1 } },
180 { "ProgramFiles", { 1, 1, 0, 0 } }
182 static const struct profile_item htok_vars[] = {
183 { "PATH", { 1, 1, 0, 0 } },
184 { "TEMP", { 1, 1, 0, 0 } },
185 { "TMP", { 1, 1, 0, 0 } },
186 { "USERPROFILE", { 1, 1, 0, 0 } }
189 r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
190 expect(TRUE, r);
192 if (0)
194 /* Crashes on NT4 */
195 r = CreateEnvironmentBlock(NULL, NULL, FALSE);
196 expect(FALSE, r);
199 r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
200 expect(TRUE, r);
202 if (0)
204 /* Crashes on NT4 */
205 r = CreateEnvironmentBlock(NULL, htok, FALSE);
206 expect(FALSE, r);
209 r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
210 expect(TRUE, r);
212 r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
213 expect(TRUE, r);
215 r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
216 expect(TRUE, r);
218 r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
219 expect(TRUE, r);
221 /* Test for common environment variables (NT4 and higher) */
222 for (i = 0; i < sizeof(common_vars)/sizeof(common_vars[0]); i++)
224 for (j = 0; j < 4; j++)
226 r = get_env(env[j], common_vars[i].name, &st);
227 if (common_vars[i].todo[j])
228 todo_wine expect_env(TRUE, r, common_vars[i].name);
229 else
230 expect_env(TRUE, r, common_vars[i].name);
234 /* Test for common environment variables (post NT4) */
235 if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL, 0))
237 win_skip("Some environment variables are not present on NT4\n");
239 else
241 for (i = 0; i < sizeof(common_post_nt4_vars)/sizeof(common_post_nt4_vars[0]); i++)
243 for (j = 0; j < 4; j++)
245 r = get_env(env[j], common_post_nt4_vars[i].name, &st);
246 if (common_post_nt4_vars[i].todo[j])
247 todo_wine expect_env(TRUE, r, common_post_nt4_vars[i].name);
248 else
249 expect_env(TRUE, r, common_post_nt4_vars[i].name);
254 /* Test for environment variables with values that depends on htok */
255 for (i = 0; i < sizeof(htok_vars)/sizeof(htok_vars[0]); i++)
257 for (j = 0; j < 4; j++)
259 r = get_env(env[j], htok_vars[i].name, &st);
260 if (htok_vars[i].todo[j])
261 todo_wine expect_env(TRUE, r, htok_vars[i].name);
262 else
263 expect_env(TRUE, r, htok_vars[i].name);
267 r = get_env(env[0], "WINE_XYZZY", &st);
268 expect(FALSE, r);
269 r = get_env(env[1], "WINE_XYZZY", &st);
270 expect(FALSE, r);
271 r = get_env(env[2], "WINE_XYZZY", &st);
272 expect(TRUE, r);
273 r = get_env(env[3], "WINE_XYZZY", &st);
274 expect(TRUE, r);
277 START_TEST(userenv)
279 test_create_env();