push 87b6981010d7405c33b14cddcceec21b47729eba
[wine/hacks.git] / dlls / userenv / tests / userenv.c
blobc61958f143d82c8c73dbfc9a90cf4adaf9b37e13
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 /* Helper function for retrieving environment variables */
43 static BOOL get_env(const WCHAR * env, const char * var, char ** result)
45 const WCHAR * p = env;
46 int envlen, varlen, buflen;
47 char buf[256];
49 if (!env || !var || !result) return FALSE;
51 varlen = strlen(var);
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)
58 if (buf[varlen] == '=')
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;
67 while (*p) p++;
68 p++;
69 } while (*p);
70 return FALSE;
73 static void test_create_env(void)
75 BOOL r;
76 HANDLE htok;
77 WCHAR * env[4];
78 char * st;
79 int i, j;
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 } }
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, 0, 0 } },
99 { "ProgramFiles", { 1, 1, 0, 0 } }
101 static const struct profile_item htok_vars[] = {
102 { "PATH", { 1, 1, 0, 0 } },
103 { "USERPROFILE", { 1, 1, 0, 0 } }
106 r = SetEnvironmentVariableA("WINE_XYZZY", "ZZYZX");
107 expect(TRUE, r);
109 if (0)
111 /* Crashes on NT4 */
112 r = CreateEnvironmentBlock(NULL, NULL, FALSE);
113 expect(FALSE, r);
116 r = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY|TOKEN_DUPLICATE, &htok);
117 expect(TRUE, r);
119 if (0)
121 /* Crashes on NT4 */
122 r = CreateEnvironmentBlock(NULL, htok, FALSE);
123 expect(FALSE, r);
126 r = CreateEnvironmentBlock((LPVOID) &env[0], NULL, FALSE);
127 expect(TRUE, r);
129 r = CreateEnvironmentBlock((LPVOID) &env[1], htok, FALSE);
130 expect(TRUE, r);
132 r = CreateEnvironmentBlock((LPVOID) &env[2], NULL, TRUE);
133 expect(TRUE, r);
135 r = CreateEnvironmentBlock((LPVOID) &env[3], htok, TRUE);
136 expect(TRUE, r);
138 /* Test for common environment variables (NT4 and higher) */
139 for (i = 0; i < sizeof(common_vars)/sizeof(common_vars[0]); i++)
141 for (j = 0; j < 4; j++)
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);
152 /* Test for common environment variables (post NT4) */
153 if (!GetEnvironmentVariableA("ALLUSERSPROFILE", NULL, 0))
155 win_skip("Some environment variables are not present on NT4\n");
157 else
159 for (i = 0; i < sizeof(common_post_nt4_vars)/sizeof(common_post_nt4_vars[0]); i++)
161 for (j = 0; j < 4; j++)
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);
173 /* Test for environment variables with values that depends on htok */
174 for (i = 0; i < sizeof(htok_vars)/sizeof(htok_vars[0]); i++)
176 for (j = 0; j < 4; j++)
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);
187 r = get_env(env[0], "WINE_XYZZY", &st);
188 expect(FALSE, r);
190 r = get_env(env[1], "WINE_XYZZY", &st);
191 expect(FALSE, r);
193 r = get_env(env[2], "WINE_XYZZY", &st);
194 expect(TRUE, r);
195 if (r) HeapFree(GetProcessHeap(), 0, st);
197 r = get_env(env[3], "WINE_XYZZY", &st);
198 expect(TRUE, r);
199 if (r) HeapFree(GetProcessHeap(), 0, st);
201 for (i = 0; i < sizeof(env) / sizeof(env[0]); i++)
203 r = DestroyEnvironmentBlock(env[i]);
204 expect(TRUE, r);
208 START_TEST(userenv)
210 test_create_env();