mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / kernel32 / tests / environ.c
blobd19b8700c5fdf13209ec27ccb92a0019941b5b11
1 /*
2 * Unit test suite for environment functions.
4 * Copyright 2002 Dmitry Timoshkov
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 <stdarg.h>
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "winnls.h"
29 static CHAR string[MAX_PATH];
30 #define ok_w(res, format, szString) \
32 WideCharToMultiByte(CP_ACP, 0, szString, -1, string, MAX_PATH, NULL, NULL); \
33 ok(res, format, string);
35 static BOOL (WINAPI *pGetComputerNameExA)(COMPUTER_NAME_FORMAT,LPSTR,LPDWORD);
36 static BOOL (WINAPI *pGetComputerNameExW)(COMPUTER_NAME_FORMAT,LPWSTR,LPDWORD);
37 static BOOL (WINAPI *pGetUserProfileDirectoryA)(HANDLE,LPSTR,LPDWORD);
38 static BOOL (WINAPI *pSetEnvironmentStringsW)(WCHAR *);
40 static void init_functionpointers(void)
42 HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
43 HMODULE huserenv = LoadLibraryA("userenv.dll");
45 pGetComputerNameExA = (void *)GetProcAddress(hkernel32, "GetComputerNameExA");
46 pGetComputerNameExW = (void *)GetProcAddress(hkernel32, "GetComputerNameExW");
47 pSetEnvironmentStringsW = (void *)GetProcAddress(hkernel32, "SetEnvironmentStringsW");
48 pGetUserProfileDirectoryA = (void *)GetProcAddress(huserenv,
49 "GetUserProfileDirectoryA");
52 static void test_Predefined(void)
54 char Data[1024];
55 DWORD DataSize;
56 char Env[sizeof(Data)];
57 DWORD EnvSize;
58 HANDLE Token;
59 BOOL NoErr;
62 * Check value of %USERPROFILE%, should be same as GetUserProfileDirectory()
63 * If this fails, your test environment is probably not set up
65 if (pGetUserProfileDirectoryA == NULL)
67 skip("Skipping USERPROFILE check\n");
68 return;
70 NoErr = OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &Token);
71 ok(NoErr, "Failed to open token, error %u\n", GetLastError());
72 DataSize = sizeof(Data);
73 NoErr = pGetUserProfileDirectoryA(Token, Data, &DataSize);
74 ok(NoErr, "Failed to get user profile dir, error %u\n", GetLastError());
75 if (NoErr)
77 EnvSize = GetEnvironmentVariableA("USERPROFILE", Env, sizeof(Env));
78 ok(EnvSize != 0 && EnvSize <= sizeof(Env),
79 "Failed to retrieve environment variable USERPROFILE, error %u\n",
80 GetLastError());
81 ok(strcmp(Data, Env) == 0,
82 "USERPROFILE env var %s doesn't match GetUserProfileDirectory %s\n",
83 Env, Data);
85 else
86 skip("Skipping USERPROFILE check, can't get user profile dir\n");
87 NoErr = CloseHandle(Token);
88 ok(NoErr, "Failed to close token, error %u\n", GetLastError());
91 static void test_GetSetEnvironmentVariableA(void)
93 char buf[256];
94 BOOL ret;
95 DWORD ret_size;
96 static const char name[] = "SomeWildName";
97 static const char name_cased[] = "sOMEwILDnAME";
98 static const char value[] = "SomeWildValue";
100 ret = SetEnvironmentVariableA(name, value);
101 ok(ret == TRUE,
102 "unexpected error in SetEnvironmentVariableA, GetLastError=%d\n",
103 GetLastError());
105 /* Try to retrieve the environment variable we just set */
106 SetLastError(0xdeadbeef);
107 ret_size = GetEnvironmentVariableA(name, NULL, 0);
108 ok(ret_size == strlen(value) + 1,
109 "should return length with terminating 0 ret_size=%d\n", ret_size);
110 ok(GetLastError() == 0xdeadbeef,
111 "should not fail with zero size but GetLastError=%d\n", GetLastError());
113 lstrcpyA(buf, "foo");
114 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value));
115 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
116 ok(ret_size == strlen(value) + 1,
117 "should return length with terminating 0 ret_size=%d\n", ret_size);
119 lstrcpyA(buf, "foo");
120 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
121 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
122 ok(ret_size == strlen(value),
123 "should return length without terminating 0 ret_size=%d\n", ret_size);
125 lstrcpyA(buf, "foo");
126 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
127 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
128 ok(ret_size == strlen(value),
129 "should return length without terminating 0 ret_size=%d\n", ret_size);
131 /* Remove that environment variable */
132 ret = SetEnvironmentVariableA(name_cased, NULL);
133 ok(ret == TRUE, "should erase existing variable\n");
135 lstrcpyA(buf, "foo");
136 SetLastError(0xdeadbeef);
137 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
138 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
139 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
140 "should not find variable but ret_size=%d GetLastError=%d\n",
141 ret_size, GetLastError());
143 /* Check behavior of SetEnvironmentVariableA(name, "") */
144 ret = SetEnvironmentVariableA(name, value);
145 ok(ret == TRUE,
146 "unexpected error in SetEnvironmentVariableA, GetLastError=%d\n",
147 GetLastError());
149 lstrcpyA(buf, "foo");
150 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
151 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
152 ok(ret_size == strlen(value),
153 "should return length without terminating 0 ret_size=%d\n", ret_size);
155 ret = SetEnvironmentVariableA(name_cased, "");
156 ok(ret == TRUE,
157 "should not fail with empty value but GetLastError=%d\n", GetLastError());
159 lstrcpyA(buf, "foo");
160 SetLastError(0xdeadbeef);
161 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
162 ok(ret_size == 0 &&
163 ((GetLastError() == 0xdeadbeef && lstrcmpA(buf, "") == 0) ||
164 (GetLastError() == ERROR_ENVVAR_NOT_FOUND)),
165 "%s should be set to \"\" (NT) or removed (Win9x) but ret_size=%d GetLastError=%d and buf=%s\n",
166 name, ret_size, GetLastError(), buf);
168 SetLastError(0xdeadbeef);
169 ret_size = GetEnvironmentVariableA(name, NULL, 0);
170 ok(ret_size == 1 ||
171 broken(ret_size == 0), /* XP */
172 "should return 1 for empty string but ret_size=%d GetLastError=%d\n",
173 ret_size, GetLastError());
174 ok(GetLastError() == 0xdeadbeef ||
175 broken(GetLastError() == ERROR_MORE_DATA), /* XP */
176 "should not fail with zero size but GetLastError=%d\n", GetLastError());
178 /* Test the limits */
179 SetLastError(0xdeadbeef);
180 ret_size = GetEnvironmentVariableA(NULL, NULL, 0);
181 ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
182 "should not find variable but ret_size=%d GetLastError=%d\n",
183 ret_size, GetLastError());
185 SetLastError(0xdeadbeef);
186 ret_size = GetEnvironmentVariableA(NULL, buf, lstrlenA(value) + 1);
187 ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
188 "should not find variable but ret_size=%d GetLastError=%d\n",
189 ret_size, GetLastError());
191 SetLastError(0xdeadbeef);
192 ret_size = GetEnvironmentVariableA("", buf, lstrlenA(value) + 1);
193 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
194 "should not find variable but ret_size=%d GetLastError=%d\n",
195 ret_size, GetLastError());
198 static void test_GetSetEnvironmentVariableW(void)
200 WCHAR buf[256];
201 BOOL ret;
202 DWORD ret_size;
203 static const WCHAR name[] = {'S','o','m','e','W','i','l','d','N','a','m','e',0};
204 static const WCHAR value[] = {'S','o','m','e','W','i','l','d','V','a','l','u','e',0};
205 static const WCHAR name_cased[] = {'s','O','M','E','w','I','L','D','n','A','M','E',0};
206 static const WCHAR empty_strW[] = { 0 };
207 static const WCHAR fooW[] = {'f','o','o',0};
209 ret = SetEnvironmentVariableW(name, value);
210 if (ret == FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
212 /* Must be Win9x which doesn't support the Unicode functions */
213 win_skip("SetEnvironmentVariableW is not implemented\n");
214 return;
216 ok(ret == TRUE,
217 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
218 GetLastError());
220 /* Try to retrieve the environment variable we just set */
221 SetLastError(0xdeadbeef);
222 ret_size = GetEnvironmentVariableW(name, NULL, 0);
223 ok(ret_size == lstrlenW(value) + 1,
224 "should return length with terminating 0 ret_size=%d\n",
225 ret_size);
226 ok(GetLastError() == 0xdeadbeef,
227 "should not fail with zero size but GetLastError=%d\n", GetLastError());
229 lstrcpyW(buf, fooW);
230 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value));
231 ok_w(lstrcmpW(buf, fooW) == 0 ||
232 lstrlenW(buf) == 0, /* Vista */
233 "Expected untouched or empty buffer, got \"%s\"\n", buf);
235 ok(ret_size == lstrlenW(value) + 1,
236 "should return length with terminating 0 ret_size=%d\n", ret_size);
238 lstrcpyW(buf, fooW);
239 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
240 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
241 ok(ret_size == lstrlenW(value),
242 "should return length without terminating 0 ret_size=%d\n", ret_size);
244 lstrcpyW(buf, fooW);
245 ret_size = GetEnvironmentVariableW(name_cased, buf, lstrlenW(value) + 1);
246 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
247 ok(ret_size == lstrlenW(value),
248 "should return length without terminating 0 ret_size=%d\n", ret_size);
250 /* Remove that environment variable */
251 ret = SetEnvironmentVariableW(name_cased, NULL);
252 ok(ret == TRUE, "should erase existing variable\n");
254 lstrcpyW(buf, fooW);
255 SetLastError(0xdeadbeef);
256 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
257 ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
258 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
259 "should not find variable but ret_size=%d GetLastError=%d\n",
260 ret_size, GetLastError());
262 /* Check behavior of SetEnvironmentVariableW(name, "") */
263 ret = SetEnvironmentVariableW(name, value);
264 ok(ret == TRUE,
265 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
266 GetLastError());
268 lstrcpyW(buf, fooW);
269 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
270 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
271 ok(ret_size == lstrlenW(value),
272 "should return length without terminating 0 ret_size=%d\n", ret_size);
274 ret = SetEnvironmentVariableW(name_cased, empty_strW);
275 ok(ret == TRUE, "should not fail with empty value but GetLastError=%d\n", GetLastError());
277 lstrcpyW(buf, fooW);
278 SetLastError(0xdeadbeef);
279 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
280 ok(ret_size == 0 &&
281 ((GetLastError() == 0xdeadbeef && lstrcmpW(buf, empty_strW) == 0) ||
282 (GetLastError() == ERROR_ENVVAR_NOT_FOUND)),
283 "should be set to \"\" (NT) or removed (Win9x) but ret_size=%d GetLastError=%d\n",
284 ret_size, GetLastError());
285 ok(lstrcmpW(buf, empty_strW) == 0, "should copy an empty string\n");
287 SetLastError(0xdeadbeef);
288 ret_size = GetEnvironmentVariableW(name, NULL, 0);
289 ok(ret_size == 1 ||
290 broken(ret_size == 0), /* XP */
291 "should return 1 for empty string but ret_size=%d GetLastError=%d\n",
292 ret_size, GetLastError());
293 ok(GetLastError() == 0xdeadbeef ||
294 broken(GetLastError() == ERROR_MORE_DATA), /* XP */
295 "should not fail with zero size but GetLastError=%d\n", GetLastError());
297 /* Test the limits */
298 SetLastError(0xdeadbeef);
299 ret_size = GetEnvironmentVariableW(NULL, NULL, 0);
300 ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
301 "should not find variable but ret_size=%d GetLastError=%d\n",
302 ret_size, GetLastError());
304 if (0) /* Both tests crash on Vista */
306 SetLastError(0xdeadbeef);
307 ret_size = GetEnvironmentVariableW(NULL, buf, lstrlenW(value) + 1);
308 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
309 "should not find variable but ret_size=%d GetLastError=%d\n",
310 ret_size, GetLastError());
312 SetLastError(0xdeadbeef);
313 ret = SetEnvironmentVariableW(NULL, NULL);
314 ok(ret == FALSE && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
315 "should fail with NULL, NULL but ret=%d and GetLastError=%d\n",
316 ret, GetLastError());
320 static void test_GetSetEnvironmentVariableAW(void)
322 static const WCHAR nameW[] = {0x540D, 0x524D, 0};
323 static const char name[] = "\x96\xBC\x91\x4F";
324 static const WCHAR valueW[] = {0x5024, 0};
325 static const char value[] = "\x92\x6C";
326 WCHAR bufW[256];
327 char buf[256];
328 DWORD ret_size;
329 BOOL ret;
331 if (GetACP() != 932)
333 skip("GetACP() == %d, need 932 for A/W tests\n", GetACP());
334 return;
337 /* Write W, read A */
338 ret = SetEnvironmentVariableW(nameW, valueW);
339 ok(ret == TRUE, "SetEnvironmentVariableW failed, last error %d\n", GetLastError());
341 SetLastError(0xdeadbeef);
342 ret_size = GetEnvironmentVariableA(name, NULL, 0);
343 todo_wine ok(ret_size == lstrlenA(value) + 1, "expected ret_size %d, got %d\n", lstrlenA(value) + 1, ret_size);
344 ok(GetLastError() == 0xdeadbeef, "expected last error 0xdeadbeef, got %d\n", GetLastError());
346 lstrcpyA(buf, "foo");
347 SetLastError(0xdeadbeef);
348 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
349 todo_wine ok(lstrcmpA(buf, value) == 0, "expected %s, got %s\n", debugstr_a(value), debugstr_a(buf));
350 todo_wine ok(ret_size == lstrlenA(value), "expected ret_size %d, got %d\n", lstrlenA(value), ret_size);
351 ok(GetLastError() == 0xdeadbeef, "expected last error 0xdeadbeef, got %d\n", GetLastError());
353 /* Write A, read A/W */
354 ret = SetEnvironmentVariableA(name, value);
355 ok(ret == TRUE, "SetEnvironmentVariableW failed, last error %d\n", GetLastError());
357 SetLastError(0xdeadbeef);
358 ret_size = GetEnvironmentVariableA(name, NULL, 0);
359 todo_wine ok(ret_size == lstrlenA(value) + 1, "expected ret_size %d, got %d\n", lstrlenA(value) + 1, ret_size);
360 ok(GetLastError() == 0xdeadbeef, "expected last error 0xdeadbeef, got %d\n", GetLastError());
362 lstrcpyA(buf, "foo");
363 SetLastError(0xdeadbeef);
364 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
365 todo_wine ok(lstrcmpA(buf, value) == 0, "expected %s, got %s\n", debugstr_a(value), debugstr_a(buf));
366 todo_wine ok(ret_size == lstrlenA(value), "expected ret_size %d, got %d\n", lstrlenA(value), ret_size);
367 ok(GetLastError() == 0xdeadbeef, "expected last error 0xdeadbeef, got %d\n", GetLastError());
369 SetLastError(0xdeadbeef);
370 ret_size = GetEnvironmentVariableW(nameW, NULL, 0);
371 ok(ret_size == lstrlenW(valueW) + 1, "expected ret_size %d, got %d\n", lstrlenW(valueW) + 1, ret_size);
372 ok(GetLastError() == 0xdeadbeef, "expected last error 0xdeadbeef, got %d\n", GetLastError());
374 lstrcpyW(bufW, L"foo");
375 SetLastError(0xdeadbeef);
376 ret_size = GetEnvironmentVariableW(nameW, bufW, lstrlenW(valueW) + 1);
377 ok(ret_size == lstrlenW(valueW), "expected ret_size %d, got %d\n", lstrlenW(valueW), ret_size);
378 ok(GetLastError() == 0xdeadbeef, "expected last error 0xdeadbeef, got %d\n", GetLastError());
379 ok(lstrcmpW(bufW, valueW) == 0, "expected %s, got %s\n", debugstr_w(valueW), debugstr_w(bufW));
382 static void test_ExpandEnvironmentStringsA(void)
384 const char* value="Long long value";
385 const char* not_an_env_var="%NotAnEnvVar%";
386 char buf[256], buf1[256], buf2[0x8000];
387 DWORD ret_size, ret_size1;
389 SetEnvironmentVariableA("EnvVar", value);
391 ret_size = ExpandEnvironmentStringsA(NULL, buf1, sizeof(buf1));
392 ok(ret_size == 1 || ret_size == 0 /* Win9x */ || ret_size == 2 /* NT4 */,
393 "ExpandEnvironmentStrings returned %d\n", ret_size);
395 /* Try to get the required buffer size 'the natural way' */
396 strcpy(buf, "%EnvVar%");
397 ret_size = ExpandEnvironmentStringsA(buf, NULL, 0);
398 ok(ret_size == strlen(value)+1 || /* win98 */
399 ret_size == (strlen(value)+1)*2 || /* NT4 */
400 ret_size == strlen(value)+2 || /* win2k, XP, win2k3 */
401 ret_size == 0 /* Win95 */,
402 "ExpandEnvironmentStrings returned %d instead of %d, %d or %d\n",
403 ret_size, lstrlenA(value)+1, lstrlenA(value)+2, 0);
405 /* Again, side-stepping the Win95 bug */
406 ret_size = ExpandEnvironmentStringsA(buf, buf1, 0);
407 /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */
408 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2 ||
409 ret_size == (strlen(value)+1)*2 /* NT4 */,
410 "ExpandEnvironmentStrings returned %d instead of %d\n",
411 ret_size, lstrlenA(value)+1);
413 /* Try with a buffer that's too small */
414 ret_size = ExpandEnvironmentStringsA(buf, buf1, 12);
415 /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */
416 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2 ||
417 ret_size == (strlen(value)+1)*2 /* NT4 */,
418 "ExpandEnvironmentStrings returned %d instead of %d\n",
419 ret_size, lstrlenA(value)+1);
421 /* Try with a buffer of just the right size */
422 /* v5.1.2600.2945 (XP SP2) needs and returns len + 2 here! */
423 ret_size = ExpandEnvironmentStringsA(buf, buf1, ret_size);
424 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2 ||
425 ret_size == (strlen(value)+1)*2 /* NT4 */,
426 "ExpandEnvironmentStrings returned %d instead of %d\n",
427 ret_size, lstrlenA(value)+1);
428 ok(!strcmp(buf1, value), "ExpandEnvironmentStrings returned [%s]\n", buf1);
430 /* Try with an unset environment variable */
431 strcpy(buf, not_an_env_var);
432 ret_size = ExpandEnvironmentStringsA(buf, buf1, sizeof(buf1));
433 ok(ret_size == strlen(not_an_env_var)+1 ||
434 ret_size == (strlen(not_an_env_var)+1)*2 /* NT4 */,
435 "ExpandEnvironmentStrings returned %d instead of %d\n", ret_size, lstrlenA(not_an_env_var)+1);
436 ok(!strcmp(buf1, not_an_env_var), "ExpandEnvironmentStrings returned [%s]\n", buf1);
438 /* test a large destination size */
439 strcpy(buf, "12345");
440 ret_size = ExpandEnvironmentStringsA(buf, buf2, sizeof(buf2));
441 ok(!strcmp(buf, buf2), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf2, ret_size);
443 SetLastError(0xdeadbeef);
444 ret_size1 = GetWindowsDirectoryA(buf1,256);
445 ok ((ret_size1 >0) && (ret_size1<256), "GetWindowsDirectory Failed\n");
446 ret_size = ExpandEnvironmentStringsA("%SystemRoot%",buf,sizeof(buf));
447 if (ERROR_ENVVAR_NOT_FOUND != GetLastError())
449 ok(!strcmp(buf, buf1), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf1, ret_size);
452 /* Try with a variable that references another */
453 SetEnvironmentVariableA("IndirectVar", "Foo%EnvVar%Bar");
454 strcpy(buf, "Indirect-%IndirectVar%-Indirect");
455 strcpy(buf2, "Indirect-Foo%EnvVar%Bar-Indirect");
456 ret_size = ExpandEnvironmentStringsA(buf, buf1, sizeof(buf1));
457 ok(ret_size == strlen(buf2)+1 ||
458 ret_size == (strlen(buf2)+1)*2 /* NT4 */,
459 "ExpandEnvironmentStrings returned %d instead of %d\n", ret_size, lstrlenA(buf2)+1);
460 ok(!strcmp(buf1, buf2), "ExpandEnvironmentStrings returned [%s]\n", buf1);
461 SetEnvironmentVariableA("IndirectVar", NULL);
463 SetEnvironmentVariableA("EnvVar", NULL);
466 static void test_GetComputerName(void)
468 DWORD size;
469 BOOL ret;
470 LPSTR name;
471 LPWSTR nameW;
472 DWORD error;
473 int name_len;
475 size = 0;
476 SetLastError(0xdeadbeef);
477 ret = GetComputerNameA((LPSTR)0xdeadbeef, &size);
478 error = GetLastError();
479 ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameA should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error);
481 /* Only Vista returns the computer name length as documented in the MSDN */
482 if (size != 0)
484 size++; /* nul terminating character */
485 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
486 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
487 ret = GetComputerNameA(name, &size);
488 ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
489 HeapFree(GetProcessHeap(), 0, name);
492 size = MAX_COMPUTERNAME_LENGTH + 1;
493 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
494 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
495 ret = GetComputerNameA(name, &size);
496 ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
497 trace("computer name is \"%s\"\n", name);
498 name_len = strlen(name);
499 ok(size == name_len, "size should be same as length, name_len=%d, size=%d\n", name_len, size);
500 HeapFree(GetProcessHeap(), 0, name);
502 size = 0;
503 SetLastError(0xdeadbeef);
504 ret = GetComputerNameW((LPWSTR)0xdeadbeef, &size);
505 error = GetLastError();
506 if (error == ERROR_CALL_NOT_IMPLEMENTED)
507 win_skip("GetComputerNameW is not implemented\n");
508 else
510 ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameW should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error);
511 size++; /* nul terminating character */
512 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
513 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
514 ret = GetComputerNameW(nameW, &size);
515 ok(ret, "GetComputerNameW failed with error %d\n", GetLastError());
516 HeapFree(GetProcessHeap(), 0, nameW);
520 static void test_GetComputerNameExA(void)
522 DWORD size;
523 BOOL ret;
524 LPSTR name;
525 DWORD error;
527 static const int MAX_COMP_NAME = 32767;
529 if (!pGetComputerNameExA)
531 win_skip("GetComputerNameExA function not implemented\n");
532 return;
535 size = 0;
536 SetLastError(0xdeadbeef);
537 ret = pGetComputerNameExA(ComputerNameDnsDomain, (LPSTR)0xdeadbeef, &size);
538 error = GetLastError();
539 ok(ret == 0, "Expected 0, got %d\n", ret);
540 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
542 /* size is not set in win2k */
543 if (size == 0)
545 win_skip("Win2k doesn't set the size\n");
546 size = MAX_COMP_NAME;
548 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
549 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
550 ret = pGetComputerNameExA(ComputerNameDnsDomain, name, &size);
551 ok(ret, "GetComputerNameExA(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
552 trace("domain name is \"%s\"\n", name);
553 HeapFree(GetProcessHeap(), 0, name);
555 size = 0;
556 SetLastError(0xdeadbeef);
557 ret = pGetComputerNameExA(ComputerNameDnsFullyQualified, (LPSTR)0xdeadbeef, &size);
558 error = GetLastError();
559 ok(ret == 0, "Expected 0, got %d\n", ret);
560 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
562 /* size is not set in win2k */
563 if (size == 0)
564 size = MAX_COMP_NAME;
565 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
566 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
567 ret = pGetComputerNameExA(ComputerNameDnsFullyQualified, name, &size);
568 ok(ret, "GetComputerNameExA(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
569 trace("fully qualified hostname is \"%s\"\n", name);
570 HeapFree(GetProcessHeap(), 0, name);
572 size = 0;
573 SetLastError(0xdeadbeef);
574 ret = pGetComputerNameExA(ComputerNameDnsHostname, (LPSTR)0xdeadbeef, &size);
575 error = GetLastError();
576 ok(ret == 0, "Expected 0, got %d\n", ret);
577 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
579 /* size is not set in win2k */
580 if (size == 0)
581 size = MAX_COMP_NAME;
582 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
583 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
584 ret = pGetComputerNameExA(ComputerNameDnsHostname, name, &size);
585 ok(ret, "GetComputerNameExA(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
586 trace("hostname is \"%s\"\n", name);
587 HeapFree(GetProcessHeap(), 0, name);
589 size = 0;
590 SetLastError(0xdeadbeef);
591 ret = pGetComputerNameExA(ComputerNameNetBIOS, (LPSTR)0xdeadbeef, &size);
592 error = GetLastError();
593 ok(ret == 0, "Expected 0, got %d\n", ret);
594 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
596 size = 0;
597 SetLastError(0xdeadbeef);
598 ret = pGetComputerNameExA(ComputerNameNetBIOS, NULL, &size);
599 error = GetLastError();
600 ok(ret == 0, "Expected 0, got %d\n", ret);
601 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
603 /* size is not set in win2k */
604 if (size == 0)
605 size = MAX_COMP_NAME;
606 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
607 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
608 ret = pGetComputerNameExA(ComputerNameNetBIOS, name, &size);
609 ok(ret, "GetComputerNameExA(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
610 trace("NetBIOS name is \"%s\"\n", name);
611 HeapFree(GetProcessHeap(), 0, name);
614 static void test_GetComputerNameExW(void)
616 DWORD size;
617 BOOL ret;
618 LPWSTR nameW;
619 DWORD error;
621 if (!pGetComputerNameExW)
623 win_skip("GetComputerNameExW function not implemented\n");
624 return;
627 size = 0;
628 SetLastError(0xdeadbeef);
629 ret = pGetComputerNameExW(ComputerNameDnsDomain, (LPWSTR)0xdeadbeef, &size);
630 error = GetLastError();
631 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
632 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
633 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
634 ret = pGetComputerNameExW(ComputerNameDnsDomain, nameW, &size);
635 ok(ret, "GetComputerNameExW(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
636 HeapFree(GetProcessHeap(), 0, nameW);
638 size = 0;
639 SetLastError(0xdeadbeef);
640 ret = pGetComputerNameExW(ComputerNameDnsFullyQualified, (LPWSTR)0xdeadbeef, &size);
641 error = GetLastError();
642 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
643 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
644 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
645 ret = pGetComputerNameExW(ComputerNameDnsFullyQualified, nameW, &size);
646 ok(ret, "GetComputerNameExW(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
647 HeapFree(GetProcessHeap(), 0, nameW);
649 size = 0;
650 SetLastError(0xdeadbeef);
651 ret = pGetComputerNameExW(ComputerNameDnsHostname, (LPWSTR)0xdeadbeef, &size);
652 error = GetLastError();
653 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
654 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
655 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
656 ret = pGetComputerNameExW(ComputerNameDnsHostname, nameW, &size);
657 ok(ret, "GetComputerNameExW(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
658 HeapFree(GetProcessHeap(), 0, nameW);
660 size = 0;
661 SetLastError(0xdeadbeef);
662 ret = pGetComputerNameExW(ComputerNameNetBIOS, (LPWSTR)0xdeadbeef, &size);
663 error = GetLastError();
664 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
665 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
666 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
667 ret = pGetComputerNameExW(ComputerNameNetBIOS, nameW, &size);
668 ok(ret, "GetComputerNameExW(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
669 HeapFree(GetProcessHeap(), 0, nameW);
671 size = 0;
672 SetLastError(0xdeadbeef);
673 ret = pGetComputerNameExW(ComputerNameNetBIOS, NULL, &size);
674 error = GetLastError();
675 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
678 static void test_GetEnvironmentStringsW(void)
680 PWCHAR env1;
681 PWCHAR env2;
683 env1 = GetEnvironmentStringsW();
684 env2 = GetEnvironmentStringsW();
685 ok(env1 != env2 ||
686 broken(env1 == env2), /* NT <= 5.1 */
687 "should return different copies\n");
688 FreeEnvironmentStringsW(env1);
689 FreeEnvironmentStringsW(env2);
692 #define copy_string(dst, src) memcpy(dst, src, sizeof(src))
694 static void check_env_var_(int line, const char *var, const char *value)
696 char buffer[20];
697 DWORD size = GetEnvironmentVariableA(var, buffer, sizeof(buffer));
698 if (value)
700 ok_(__FILE__, line)(size == strlen(value), "wrong size %u\n", size);
701 ok_(__FILE__, line)(!strcmp(buffer, value), "wrong value %s\n", debugstr_a(buffer));
703 else
705 ok_(__FILE__, line)(!size, "wrong size %u\n", size);
706 ok_(__FILE__, line)(GetLastError() == ERROR_ENVVAR_NOT_FOUND, "got error %u\n", GetLastError());
709 #define check_env_var(a, b) check_env_var_(__LINE__, a, b)
711 static void test_SetEnvironmentStrings(void)
713 static const WCHAR testenv[] = L"testenv1=unus\0testenv3=tres\0";
714 WCHAR env[200];
715 WCHAR *old_env;
716 BOOL ret;
718 if (!pSetEnvironmentStringsW)
720 win_skip("SetEnvironmentStringsW() is not available\n");
721 return;
724 ret = SetEnvironmentVariableA("testenv1", "heis");
725 ok(ret, "got error %u\n", GetLastError());
726 ret = SetEnvironmentVariableA("testenv2", "dyo");
727 ok(ret, "got error %u\n", GetLastError());
729 old_env = GetEnvironmentStringsW();
731 memcpy(env, testenv, sizeof(testenv));
732 ret = pSetEnvironmentStringsW(env);
733 ok(ret, "got error %u\n", GetLastError());
734 ok(!memcmp(env, testenv, sizeof(testenv)), "input parameter should not be changed\n");
736 check_env_var("testenv1", "unus");
737 check_env_var("testenv2", NULL);
738 check_env_var("testenv3", "tres");
739 check_env_var("PATH", NULL);
741 ret = pSetEnvironmentStringsW(old_env);
742 ok(ret, "got error %u\n", GetLastError());
744 check_env_var("testenv1", "heis");
745 check_env_var("testenv2", "dyo");
746 check_env_var("testenv3", NULL);
748 SetEnvironmentVariableA("testenv1", NULL);
749 SetEnvironmentVariableA("testenv2", NULL);
751 copy_string(env, L"testenv\0");
752 SetLastError(0xdeadbeef);
753 ret = pSetEnvironmentStringsW(env);
754 ok(!ret, "expected failure\n");
755 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %u\n", GetLastError());
757 copy_string(env, L"=unus\0");
758 SetLastError(0xdeadbeef);
759 ret = pSetEnvironmentStringsW(env);
760 ok(!ret, "expected failure\n");
761 ok(GetLastError() == ERROR_INVALID_PARAMETER, "got error %u\n", GetLastError());
763 copy_string(env, L"one=two=three four=five\0");
764 ret = pSetEnvironmentStringsW(env);
765 ok(ret, "got error %u\n", GetLastError());
767 check_env_var("one", "two=three four=five");
769 ret = pSetEnvironmentStringsW(old_env);
770 ok(ret, "got error %u\n", GetLastError());
771 ret = FreeEnvironmentStringsW(old_env);
772 ok(ret, "got error %u\n", GetLastError());
775 START_TEST(environ)
777 init_functionpointers();
779 test_Predefined();
780 test_GetSetEnvironmentVariableA();
781 test_GetSetEnvironmentVariableW();
782 test_GetSetEnvironmentVariableAW();
783 test_ExpandEnvironmentStringsA();
784 test_GetComputerName();
785 test_GetComputerNameExA();
786 test_GetComputerNameExW();
787 test_GetEnvironmentStringsW();
788 test_SetEnvironmentStrings();