userenv/tests: Fix some memory leaks.
[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 /* Helper function for retrieving environment variables */
43 static BOOL get_env(const WCHAR * env, const char * var, char ** result)
44 {
45     const WCHAR * p = env;
46     int envlen, varlen, buflen;
47     char buf[256];
48
49     if (!env || !var || !result) return FALSE;
50
51     varlen = strlen(var);
52     do
53     {
54         if (!WideCharToMultiByte( CP_ACP, 0, p, -1, buf, sizeof(buf), NULL, NULL )) buf[sizeof(buf)-1] = 0;
55         envlen = strlen(buf);
56         if (CompareStringA(GetThreadLocale(), NORM_IGNORECASE|LOCALE_USE_CP_ACP, buf, min(envlen, varlen), var, varlen) == CSTR_EQUAL)
57         {
58             if (buf[varlen] == '=')
59             {
60                 buflen = strlen(buf);
61                 *result = HeapAlloc(GetProcessHeap(), 0, buflen + 1);
62                 if (!*result) return FALSE;
63                 memcpy(*result, buf, buflen + 1);
64                 return TRUE;
65             }
66         }
67         while (*p) p++;
68         p++;
69     } while (*p);
70     return FALSE;
71 }
72
73 static void test_create_env(void)
74 {
75     BOOL r;
76     HANDLE htok;
77     WCHAR * env[4];
78     char * st;
79     int i, j;
80
81     static const struct profile_item common_vars[] = {
82         { "ComSpec", { 1, 1, 0, 0 } },
83         { "COMPUTERNAME", { 1, 1, 1, 1 } },
84         { "NUMBER_OF_PROCESSORS", { 1, 1, 0, 0 } },
85         { "OS", { 1, 1, 0, 0 } },
86         { "PROCESSOR_ARCHITECTURE", { 1, 1, 0, 0 } },
87         { "PROCESSOR_IDENTIFIER", { 1, 1, 0, 0 } },
88         { "PROCESSOR_LEVEL", { 1, 1, 0, 0 } },
89         { "PROCESSOR_REVISION", { 1, 1, 0, 0 } },
90         { "SystemDrive", { 1, 1, 0, 0 } },
91         { "SystemRoot", { 1, 1, 0, 0 } },
92         { "windir", { 1, 1, 0, 0 } }
93     };
94     static const struct profile_item common_post_nt4_vars[] = {
95         { "ALLUSERSPROFILE", { 1, 1, 0, 0 } },
96         { "TEMP", { 1, 1, 0, 0 } },
97         { "TMP", { 1, 1, 0, 0 } },
98         { "CommonProgramFiles", { 1, 1, 1, 1 } },
99         { "ProgramFiles", { 1, 1, 0, 0 } }
100     };
101     static const struct profile_item htok_vars[] = {
102         { "PATH", { 1, 1, 0, 0 } },
103         { "USERPROFILE", { 1, 1, 0, 0 } }
104     };
105
106     r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
107     expect(TRUE, r);
108
109     if (0)
110     {
111         /* Crashes on NT4 */
112         r = CreateEnvironmentBlock(NULL, NULL, FALSE);
113         expect(FALSE, r);
114     }
115
116     r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
117     expect(TRUE, r);
118
119     if (0)
120     {
121         /* Crashes on NT4 */
122         r = CreateEnvironmentBlock(NULL, htok, FALSE);
123         expect(FALSE, r);
124     }
125
126     r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
127     expect(TRUE, r);
128
129     r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
130     expect(TRUE, r);
131
132     r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
133     expect(TRUE, r);
134
135     r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
136     expect(TRUE, r);
137
138     /* Test for common environment variables (NT4 and higher) */
139     for (i = 0; i < sizeof(common_vars)/sizeof(common_vars[0]); i++)
140     {
141         for (j = 0; j < 4; j++)
142         {
143             r = get_env(env[j], common_vars[i].name, &st);
144             if (common_vars[i].todo[j])
145                 todo_wine expect_env(TRUE, r, common_vars[i].name);
146             else
147                 expect_env(TRUE, r, common_vars[i].name);
148             if (r) HeapFree(GetProcessHeap(), 0, st);
149         }
150     }
151
152     /* Test for common environment variables (post NT4) */
153     if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL, 0))
154     {
155         win_skip("Some environment variables are not present on NT4\n");
156     }
157     else
158     {
159         for (i = 0; i < sizeof(common_post_nt4_vars)/sizeof(common_post_nt4_vars[0]); i++)
160         {
161             for (j = 0; j < 4; j++)
162             {
163                 r = get_env(env[j], common_post_nt4_vars[i].name, &st);
164                 if (common_post_nt4_vars[i].todo[j])
165                     todo_wine expect_env(TRUE, r, common_post_nt4_vars[i].name);
166                 else
167                     expect_env(TRUE, r, common_post_nt4_vars[i].name);
168                 if (r) HeapFree(GetProcessHeap(), 0, st);
169             }
170         }
171     }
172
173     /* Test for environment variables with values that depends on htok */
174     for (i = 0; i < sizeof(htok_vars)/sizeof(htok_vars[0]); i++)
175     {
176         for (j = 0; j < 4; j++)
177         {
178             r = get_env(env[j], htok_vars[i].name, &st);
179             if (htok_vars[i].todo[j])
180                 todo_wine expect_env(TRUE, r, htok_vars[i].name);
181             else
182                 expect_env(TRUE, r, htok_vars[i].name);
183             if (r) HeapFree(GetProcessHeap(), 0, st);
184         }
185     }
186
187     r = get_env(env[0], "WINE_XYZZY", &st);
188     expect(FALSE, r);
189
190     r = get_env(env[1], "WINE_XYZZY", &st);
191     expect(FALSE, r);
192
193     r = get_env(env[2], "WINE_XYZZY", &st);
194     expect(TRUE, r);
195     if (r) HeapFree(GetProcessHeap(), 0, st);
196
197     r = get_env(env[3], "WINE_XYZZY", &st);
198     expect(TRUE, r);
199     if (r) HeapFree(GetProcessHeap(), 0, st);
200 }
201
202 START_TEST(userenv)
203 {
204     test_create_env();
205 }