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