push decde5eed3d79f9d889b4d757f73e86ce6ff9241
[wine/hacks.git] / dlls / ntdll / tests / env.c
blob3363e73484eacc6775f50ba2566bcf14dd387400
1 /*
2 * Unit test suite for ntdll path functions
4 * Copyright 2003 Eric Pouech
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>
23 #include "ntdll_test.h"
25 static NTSTATUS (WINAPI *pRtlMultiByteToUnicodeN)( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
26 LPCSTR src, DWORD srclen );
27 static NTSTATUS (WINAPI *pRtlCreateEnvironment)(BOOLEAN, PWSTR*);
28 static NTSTATUS (WINAPI *pRtlDestroyEnvironment)(PWSTR);
29 static NTSTATUS (WINAPI *pRtlQueryEnvironmentVariable_U)(PWSTR, PUNICODE_STRING, PUNICODE_STRING);
30 static void (WINAPI *pRtlSetCurrentEnvironment)(PWSTR, PWSTR*);
31 static NTSTATUS (WINAPI *pRtlSetEnvironmentVariable)(PWSTR*, PUNICODE_STRING, PUNICODE_STRING);
32 static NTSTATUS (WINAPI *pRtlExpandEnvironmentStrings_U)(LPWSTR, PUNICODE_STRING, PUNICODE_STRING, PULONG);
34 static WCHAR small_env[] = {'f','o','o','=','t','o','t','o',0,
35 'f','o','=','t','i','t','i',0,
36 'f','o','o','o','=','t','u','t','u',0,
37 's','r','=','a','n','=','o','u','o',0,
38 'g','=','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
39 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
40 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
41 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
42 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
43 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
44 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
45 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
46 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',
47 'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a',0,
48 '=','o','O','H','=','I','I','I',0,
49 'n','u','l','=',0,
50 0};
52 static void testQuery(void)
54 struct test
56 const char *var;
57 int len;
58 NTSTATUS status;
59 const char *val;
62 static const struct test tests[] =
64 {"foo", 256, STATUS_SUCCESS, "toto"},
65 {"FoO", 256, STATUS_SUCCESS, "toto"},
66 {"foo=", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
67 {"foo ", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
68 {"foo", 1, STATUS_BUFFER_TOO_SMALL, "toto"},
69 {"foo", 3, STATUS_BUFFER_TOO_SMALL, "toto"},
70 {"foo", 4, STATUS_SUCCESS, "toto"},
71 {"fooo", 256, STATUS_SUCCESS, "tutu"},
72 {"f", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
73 {"g", 256, STATUS_SUCCESS, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
74 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
75 {"sr=an", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
76 {"sr", 256, STATUS_SUCCESS, "an=ouo"},
77 {"=oOH", 256, STATUS_SUCCESS, "III"},
78 {"", 256, STATUS_VARIABLE_NOT_FOUND, NULL},
79 {"nul", 256, STATUS_SUCCESS, ""},
80 {NULL, 0, 0, NULL}
83 WCHAR bn[257];
84 WCHAR bv[257];
85 UNICODE_STRING name;
86 UNICODE_STRING value;
87 const struct test* test;
88 NTSTATUS nts;
90 for (test = tests; test->var; test++)
92 name.Length = strlen(test->var) * 2;
93 name.MaximumLength = name.Length + 2;
94 name.Buffer = bn;
95 value.Length = 0;
96 value.MaximumLength = test->len * 2;
97 value.Buffer = bv;
98 bv[test->len] = '@';
100 pRtlMultiByteToUnicodeN( bn, sizeof(bn), NULL, test->var, strlen(test->var)+1 );
101 nts = pRtlQueryEnvironmentVariable_U(small_env, &name, &value);
102 ok( nts == test->status, "[%d]: Wrong status for '%s', expecting %x got %x\n",
103 test - tests, test->var, test->status, nts );
104 if (nts == test->status) switch (nts)
106 case STATUS_SUCCESS:
107 pRtlMultiByteToUnicodeN( bn, sizeof(bn), NULL, test->val, strlen(test->val)+1 );
108 ok( value.Length == strlen(test->val) * sizeof(WCHAR), "Wrong length %d for %s\n",
109 value.Length, test->var );
110 ok((value.Length == strlen(test->val) * sizeof(WCHAR) && memcmp(bv, bn, test->len*sizeof(WCHAR)) == 0) ||
111 lstrcmpW(bv, bn) == 0,
112 "Wrong result for %s/%d\n", test->var, test->len);
113 ok(bv[test->len] == '@', "Writing too far away in the buffer for %s/%d\n", test->var, test->len);
114 break;
115 case STATUS_BUFFER_TOO_SMALL:
116 ok( value.Length == strlen(test->val) * sizeof(WCHAR),
117 "Wrong returned length %d (too small buffer) for %s\n", value.Length, test->var );
118 break;
123 static void testSetHelper(LPWSTR* env, const char* var, const char* val, NTSTATUS ret)
125 WCHAR bvar[256], bval1[256], bval2[256];
126 UNICODE_STRING uvar;
127 UNICODE_STRING uval;
128 NTSTATUS nts;
130 uvar.Length = strlen(var) * sizeof(WCHAR);
131 uvar.MaximumLength = uvar.Length + sizeof(WCHAR);
132 uvar.Buffer = bvar;
133 pRtlMultiByteToUnicodeN( bvar, sizeof(bvar), NULL, var, strlen(var)+1 );
134 if (val)
136 uval.Length = strlen(val) * sizeof(WCHAR);
137 uval.MaximumLength = uval.Length + sizeof(WCHAR);
138 uval.Buffer = bval1;
139 pRtlMultiByteToUnicodeN( bval1, sizeof(bval1), NULL, val, strlen(val)+1 );
141 nts = pRtlSetEnvironmentVariable(env, &uvar, val ? &uval : NULL);
142 ok(nts == ret, "Setting var %s=%s (%x/%x)\n", var, val, nts, ret);
143 if (nts == STATUS_SUCCESS)
145 uval.Length = 0;
146 uval.MaximumLength = sizeof(bval2);
147 uval.Buffer = bval2;
148 nts = pRtlQueryEnvironmentVariable_U(*env, &uvar, &uval);
149 switch (nts)
151 case STATUS_SUCCESS:
152 ok(lstrcmpW(bval1, bval2) == 0, "Cannot get value written to environment\n");
153 break;
154 case STATUS_VARIABLE_NOT_FOUND:
155 ok(val == NULL, "Couldn't find variable, but didn't delete it. val = %s\n", val);
156 break;
157 default:
158 ok(0, "Wrong ret %u for %s\n", nts, var);
159 break;
164 static void testSet(void)
166 LPWSTR env;
167 char tmp[16];
168 int i;
170 ok(pRtlCreateEnvironment(FALSE, &env) == STATUS_SUCCESS, "Creating environment\n");
171 memmove(env, small_env, sizeof(small_env));
173 testSetHelper(&env, "cat", "dog", STATUS_SUCCESS);
174 testSetHelper(&env, "cat", "horse", STATUS_SUCCESS);
175 testSetHelper(&env, "cat", "zz", STATUS_SUCCESS);
176 testSetHelper(&env, "cat", NULL, STATUS_SUCCESS);
177 testSetHelper(&env, "cat", NULL, STATUS_VARIABLE_NOT_FOUND);
178 testSetHelper(&env, "foo", "meouw", STATUS_SUCCESS);
179 testSetHelper(&env, "me=too", "also", STATUS_INVALID_PARAMETER);
180 testSetHelper(&env, "me", "too=also", STATUS_SUCCESS);
181 testSetHelper(&env, "=too", "also", STATUS_SUCCESS);
182 testSetHelper(&env, "=", "also", STATUS_SUCCESS);
184 for (i = 0; i < 128; i++)
186 sprintf(tmp, "zork%03d", i);
187 testSetHelper(&env, tmp, "is alive", STATUS_SUCCESS);
190 for (i = 0; i < 128; i++)
192 sprintf(tmp, "zork%03d", i);
193 testSetHelper(&env, tmp, NULL, STATUS_SUCCESS);
195 testSetHelper(&env, "fOo", NULL, STATUS_SUCCESS);
197 ok(pRtlDestroyEnvironment(env) == STATUS_SUCCESS, "Destroying environment\n");
200 static void testExpand(void)
202 static const struct test
204 const char *src;
205 const char *dst;
206 } tests[] =
208 {"hello%foo%world", "hellototoworld"},
209 {"hello%=oOH%world", "helloIIIworld"},
210 {"hello%foo", "hello%foo"},
211 {"hello%bar%world", "hello%bar%world"},
213 * {"hello%foo%world%=oOH%eeck", "hellototoworldIIIeeck"},
214 * Interestingly enough, with a 8 WCHAR buffers, we get on 2k:
215 * helloIII
216 * so it seems like strings overflowing the buffer are written
217 * (truncated) but the write cursor is not advanced :-/
219 {NULL, NULL}
222 const struct test* test;
223 NTSTATUS nts;
224 UNICODE_STRING us_src, us_dst;
225 WCHAR src[256], dst[256], rst[256];
226 ULONG ul;
228 for (test = tests; test->src; test++)
230 pRtlMultiByteToUnicodeN(src, sizeof(src), NULL, test->src, strlen(test->src)+1);
231 pRtlMultiByteToUnicodeN(rst, sizeof(rst), NULL, test->dst, strlen(test->dst)+1);
233 us_src.Length = strlen(test->src) * sizeof(WCHAR);
234 us_src.MaximumLength = us_src.Length + 2;
235 us_src.Buffer = src;
237 us_dst.Length = 0;
238 us_dst.MaximumLength = 0;
239 us_dst.Buffer = NULL;
241 nts = pRtlExpandEnvironmentStrings_U(small_env, &us_src, &us_dst, &ul);
242 ok(ul == strlen(test->dst) * sizeof(WCHAR) + sizeof(WCHAR),
243 "Wrong returned length for %s: %u\n", test->src, ul );
245 us_dst.Length = 0;
246 us_dst.MaximumLength = sizeof(dst);
247 us_dst.Buffer = dst;
249 nts = pRtlExpandEnvironmentStrings_U(small_env, &us_src, &us_dst, &ul);
250 ok(nts == STATUS_SUCCESS, "Call failed (%u)\n", nts);
251 ok(ul == us_dst.Length + sizeof(WCHAR),
252 "Wrong returned length for %s: %u\n", test->src, ul);
253 ok(ul == strlen(test->dst) * sizeof(WCHAR) + sizeof(WCHAR),
254 "Wrong returned length for %s: %u\n", test->src, ul);
255 ok(lstrcmpW(dst, rst) == 0, "Wrong result for %s: expecting %s\n",
256 test->src, test->dst);
258 us_dst.Length = 0;
259 us_dst.MaximumLength = 8 * sizeof(WCHAR);
260 us_dst.Buffer = dst;
261 dst[8] = '-';
262 nts = pRtlExpandEnvironmentStrings_U(small_env, &us_src, &us_dst, &ul);
263 ok(nts == STATUS_BUFFER_TOO_SMALL, "Call failed (%u)\n", nts);
264 ok(ul == strlen(test->dst) * sizeof(WCHAR) + sizeof(WCHAR),
265 "Wrong returned length for %s (with buffer too small): %u\n", test->src, ul);
266 ok(memcmp(dst, rst, 8*sizeof(WCHAR)) == 0,
267 "Wrong result for %s (with buffer too small): expecting %s\n",
268 test->src, test->dst);
269 ok(dst[8] == '-', "Writing too far in buffer (got %c/%d)\n", dst[8], dst[8]);
274 START_TEST(env)
276 HMODULE mod = GetModuleHandleA("ntdll.dll");
278 pRtlMultiByteToUnicodeN = (void *)GetProcAddress(mod,"RtlMultiByteToUnicodeN");
279 pRtlCreateEnvironment = (void*)GetProcAddress(mod, "RtlCreateEnvironment");
280 pRtlDestroyEnvironment = (void*)GetProcAddress(mod, "RtlDestroyEnvironment");
281 pRtlQueryEnvironmentVariable_U = (void*)GetProcAddress(mod, "RtlQueryEnvironmentVariable_U");
282 pRtlSetCurrentEnvironment = (void*)GetProcAddress(mod, "RtlSetCurrentEnvironment");
283 pRtlSetEnvironmentVariable = (void*)GetProcAddress(mod, "RtlSetEnvironmentVariable");
284 pRtlExpandEnvironmentStrings_U = (void*)GetProcAddress(mod, "RtlExpandEnvironmentStrings_U");
286 if (pRtlQueryEnvironmentVariable_U)
287 testQuery();
288 if (pRtlSetEnvironmentVariable)
289 testSet();
290 if (pRtlExpandEnvironmentStrings_U)
291 testExpand();