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
23 #include "wine/test.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);
38 static void init_functionpointers(void)
40 HMODULE hkernel32 = GetModuleHandleA("kernel32.dll");
42 pGetComputerNameExA = (void *)GetProcAddress(hkernel32, "GetComputerNameExA");
43 pGetComputerNameExW = (void *)GetProcAddress(hkernel32, "GetComputerNameExW");
46 static void test_GetSetEnvironmentVariableA(void)
51 static const char name[] = "SomeWildName";
52 static const char name_cased[] = "sOMEwILDnAME";
53 static const char value[] = "SomeWildValue";
55 ret = SetEnvironmentVariableA(name, value);
57 "unexpected error in SetEnvironmentVariableA, GetLastError=%d\n",
60 /* Try to retrieve the environment variable we just set */
61 ret_size = GetEnvironmentVariableA(name, NULL, 0);
62 ok(ret_size == strlen(value) + 1,
63 "should return length with terminating 0 ret_size=%d\n", ret_size);
66 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value));
67 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
68 ok(ret_size == strlen(value) + 1,
69 "should return length with terminating 0 ret_size=%d\n", ret_size);
72 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
73 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
74 ok(ret_size == strlen(value),
75 "should return length without terminating 0 ret_size=%d\n", ret_size);
78 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
79 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
80 ok(ret_size == strlen(value),
81 "should return length without terminating 0 ret_size=%d\n", ret_size);
83 /* Remove that environment variable */
84 ret = SetEnvironmentVariableA(name_cased, NULL);
85 ok(ret == TRUE, "should erase existing variable\n");
88 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
89 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
90 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
91 "should not find variable but ret_size=%d GetLastError=%d\n",
92 ret_size, GetLastError());
94 /* Check behavior of SetEnvironmentVariableA(name, "") */
95 ret = SetEnvironmentVariableA(name, value);
97 "unexpected error in SetEnvironmentVariableA, GetLastError=%d\n",
100 lstrcpyA(buf, "foo");
101 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
102 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
103 ok(ret_size == strlen(value),
104 "should return length without terminating 0 ret_size=%d\n", ret_size);
106 ret = SetEnvironmentVariableA(name_cased, "");
108 "should not fail with empty value but GetLastError=%d\n", GetLastError());
110 lstrcpyA(buf, "foo");
112 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
114 ((GetLastError() == 0 && lstrcmpA(buf, "") == 0) ||
115 (GetLastError() == ERROR_ENVVAR_NOT_FOUND)),
116 "%s should be set to \"\" (NT) or removed (Win9x) but ret_size=%d GetLastError=%d and buf=%s\n",
117 name, ret_size, GetLastError(), buf);
119 /* Test the limits */
120 ret_size = GetEnvironmentVariableA(NULL, NULL, 0);
121 ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
122 "should not find variable but ret_size=%d GetLastError=%d\n",
123 ret_size, GetLastError());
125 ret_size = GetEnvironmentVariableA(NULL, buf, lstrlenA(value) + 1);
126 ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
127 "should not find variable but ret_size=%d GetLastError=%d\n",
128 ret_size, GetLastError());
130 ret_size = GetEnvironmentVariableA("", buf, lstrlenA(value) + 1);
131 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
132 "should not find variable but ret_size=%d GetLastError=%d\n",
133 ret_size, GetLastError());
136 static void test_GetSetEnvironmentVariableW(void)
141 static const WCHAR name[] = {'S','o','m','e','W','i','l','d','N','a','m','e',0};
142 static const WCHAR value[] = {'S','o','m','e','W','i','l','d','V','a','l','u','e',0};
143 static const WCHAR name_cased[] = {'s','O','M','E','w','I','L','D','n','A','M','E',0};
144 static const WCHAR empty_strW[] = { 0 };
145 static const WCHAR fooW[] = {'f','o','o',0};
147 ret = SetEnvironmentVariableW(name, value);
148 if (ret == FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
150 /* Must be Win9x which doesn't support the Unicode functions */
151 skip("SetEnvironmentVariableW is not implemented\n");
155 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
158 /* Try to retrieve the environment variable we just set */
159 ret_size = GetEnvironmentVariableW(name, NULL, 0);
160 ok(ret_size == lstrlenW(value) + 1,
161 "should return length with terminating 0 ret_size=%d\n",
165 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value));
166 ok_w(lstrcmpW(buf, fooW) == 0 ||
167 lstrlenW(buf) == 0, /* Vista */
168 "Expected untouched or empty buffer, got \"%s\"\n", buf);
170 ok(ret_size == lstrlenW(value) + 1,
171 "should return length with terminating 0 ret_size=%d\n", ret_size);
174 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
175 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
176 ok(ret_size == lstrlenW(value),
177 "should return length without terminating 0 ret_size=%d\n", ret_size);
180 ret_size = GetEnvironmentVariableW(name_cased, buf, lstrlenW(value) + 1);
181 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
182 ok(ret_size == lstrlenW(value),
183 "should return length without terminating 0 ret_size=%d\n", ret_size);
185 /* Remove that environment variable */
186 ret = SetEnvironmentVariableW(name_cased, NULL);
187 ok(ret == TRUE, "should erase existing variable\n");
190 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
191 ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
192 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
193 "should not find variable but ret_size=%d GetLastError=%d\n",
194 ret_size, GetLastError());
196 /* Check behavior of SetEnvironmentVariableW(name, "") */
197 ret = SetEnvironmentVariableW(name, value);
199 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
203 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
204 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
205 ok(ret_size == lstrlenW(value),
206 "should return length without terminating 0 ret_size=%d\n", ret_size);
208 ret = SetEnvironmentVariableW(name_cased, empty_strW);
209 ok(ret == TRUE, "should not fail with empty value but GetLastError=%d\n", GetLastError());
212 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
213 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
214 "should not find variable but ret_size=%d GetLastError=%d\n",
215 ret_size, GetLastError());
216 ok(lstrcmpW(buf, empty_strW) == 0, "should copy an empty string\n");
218 /* Test the limits */
219 ret_size = GetEnvironmentVariableW(NULL, NULL, 0);
220 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
221 "should not find variable but ret_size=%d GetLastError=%d\n",
222 ret_size, GetLastError());
224 if (0) /* Both tests crash on Vista */
226 ret_size = GetEnvironmentVariableW(NULL, buf, lstrlenW(value) + 1);
227 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
228 "should not find variable but ret_size=%d GetLastError=%d\n",
229 ret_size, GetLastError());
231 ret = SetEnvironmentVariableW(NULL, NULL);
232 ok(ret == FALSE && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
233 "should fail with NULL, NULL but ret=%d and GetLastError=%d\n",
234 ret, GetLastError());
238 static void test_ExpandEnvironmentStringsA(void)
240 const char* value="Long long value";
241 const char* not_an_env_var="%NotAnEnvVar%";
242 char buf[256], buf1[256], buf2[0x8000];
243 DWORD ret_size, ret_size1;
245 SetEnvironmentVariableA("EnvVar", value);
247 ret_size = ExpandEnvironmentStringsA(NULL, buf1, sizeof(buf1));
248 ok(ret_size == 1 || ret_size == 0 /* Win9x */,
249 "ExpandEnvironmentStrings returned %d\n", ret_size);
251 /* Try to get the required buffer size 'the natural way' */
252 strcpy(buf, "%EnvVar%");
253 ret_size = ExpandEnvironmentStringsA(buf, NULL, 0);
254 ok(ret_size == strlen(value)+1 || /* win98 */
255 ret_size == strlen(value)+2 || /* win2k, XP, win2k3 */
256 ret_size == 0 /* Win95 */,
257 "ExpandEnvironmentStrings returned %d instead of %d, %d or %d\n",
258 ret_size, lstrlenA(value)+1, lstrlenA(value)+2, 0);
260 /* Again, side-stepping the Win95 bug */
261 ret_size = ExpandEnvironmentStringsA(buf, buf1, 0);
262 /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */
263 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2,
264 "ExpandEnvironmentStrings returned %d instead of %d\n",
265 ret_size, lstrlenA(value)+1);
267 /* Try with a buffer that's too small */
268 ret_size = ExpandEnvironmentStringsA(buf, buf1, 12);
269 /* v5.1.2600.2945 (XP SP2) returns len + 2 here! */
270 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2,
271 "ExpandEnvironmentStrings returned %d instead of %d\n",
272 ret_size, lstrlenA(value)+1);
274 /* Try with a buffer of just the right size */
275 /* v5.1.2600.2945 (XP SP2) needs and returns len + 2 here! */
276 ret_size = ExpandEnvironmentStringsA(buf, buf1, ret_size);
277 ok(ret_size == strlen(value)+1 || ret_size == strlen(value)+2,
278 "ExpandEnvironmentStrings returned %d instead of %d\n",
279 ret_size, lstrlenA(value)+1);
280 ok(!strcmp(buf1, value), "ExpandEnvironmentStrings returned [%s]\n", buf1);
282 /* Try with an unset environment variable */
283 strcpy(buf, not_an_env_var);
284 ret_size = ExpandEnvironmentStringsA(buf, buf1, sizeof(buf1));
285 ok(ret_size == strlen(not_an_env_var)+1, "ExpandEnvironmentStrings returned %d instead of %d\n", ret_size, lstrlenA(value)+1);
286 ok(!strcmp(buf1, not_an_env_var), "ExpandEnvironmentStrings returned [%s]\n", buf1);
288 /* test a large destination size */
289 strcpy(buf, "12345");
290 ret_size = ExpandEnvironmentStringsA(buf, buf2, sizeof(buf2));
291 ok(!strcmp(buf, buf2), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf2, ret_size);
293 ret_size1 = GetWindowsDirectoryA(buf1,256);
294 ok ((ret_size1 >0) && (ret_size1<256), "GetWindowsDirectory Failed\n");
295 ret_size = ExpandEnvironmentStringsA("%SystemRoot%",buf,sizeof(buf));
296 if (ERROR_ENVVAR_NOT_FOUND != GetLastError())
298 ok(!strcmp(buf, buf1), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf1, ret_size);
301 /* Try with a variable that references another */
302 SetEnvironmentVariableA("IndirectVar", "Foo%EnvVar%Bar");
303 strcpy(buf, "Indirect-%IndirectVar%-Indirect");
304 strcpy(buf2, "Indirect-Foo%EnvVar%Bar-Indirect");
305 ret_size = ExpandEnvironmentStringsA(buf, buf1, sizeof(buf1));
306 ok(ret_size == strlen(buf2)+1, "ExpandEnvironmentStrings returned %d instead of %d\n", ret_size, lstrlen(buf2)+1);
307 ok(!strcmp(buf1, buf2), "ExpandEnvironmentStrings returned [%s]\n", buf1);
308 SetEnvironmentVariableA("IndirectVar", NULL);
310 SetEnvironmentVariableA("EnvVar", NULL);
313 static void test_GetComputerName(void)
323 ret = GetComputerNameA((LPSTR)0xdeadbeef, &size);
324 error = GetLastError();
326 ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameA should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error);
328 /* Only Vista returns the computer name length as documented in the MSDN */
331 size++; /* nul terminating character */
332 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
333 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
334 ret = GetComputerNameA(name, &size);
335 ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
336 HeapFree(GetProcessHeap(), 0, name);
339 size = MAX_COMPUTERNAME_LENGTH + 1;
340 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
341 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
342 ret = GetComputerNameA(name, &size);
343 ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
344 trace("computer name is \"%s\"\n", name);
345 name_len = strlen(name);
346 ok(size == name_len, "size should be same as length, name_len=%d, size=%d\n", name_len, size);
347 HeapFree(GetProcessHeap(), 0, name);
350 SetLastError(0xdeadbeef);
351 ret = GetComputerNameW((LPWSTR)0xdeadbeef, &size);
352 error = GetLastError();
353 if (error == ERROR_CALL_NOT_IMPLEMENTED)
354 skip("GetComputerNameW is not implemented\n");
358 ok(!ret && error == ERROR_BUFFER_OVERFLOW, "GetComputerNameW should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error);
359 size++; /* nul terminating character */
360 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
361 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
362 ret = GetComputerNameW(nameW, &size);
363 ok(ret, "GetComputerNameW failed with error %d\n", GetLastError());
364 HeapFree(GetProcessHeap(), 0, nameW);
368 static void test_GetComputerNameExA(void)
375 static const int MAX_COMP_NAME = 32767;
377 if (!pGetComputerNameExA)
379 skip("GetComputerNameExA function not implemented\n");
384 ret = pGetComputerNameExA(ComputerNameDnsDomain, (LPSTR)0xdeadbeef, &size);
385 error = GetLastError();
386 ok(ret == 0, "Expected 0, got %d\n", ret);
387 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
389 /* size is not set in win2k */
390 size = MAX_COMP_NAME;
391 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
392 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
393 ret = pGetComputerNameExA(ComputerNameDnsDomain, name, &size);
394 ok(ret, "GetComputerNameExA(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
395 trace("domain name is \"%s\"\n", name);
396 HeapFree(GetProcessHeap(), 0, name);
399 ret = pGetComputerNameExA(ComputerNameDnsFullyQualified, (LPSTR)0xdeadbeef, &size);
400 error = GetLastError();
401 ok(ret == 0, "Expected 0, got %d\n", ret);
402 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
404 /* size is not set in win2k */
405 size = MAX_COMP_NAME;
406 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
407 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
408 ret = pGetComputerNameExA(ComputerNameDnsFullyQualified, name, &size);
409 ok(ret, "GetComputerNameExA(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
410 trace("fully qualified hostname is \"%s\"\n", name);
411 HeapFree(GetProcessHeap(), 0, name);
414 ret = pGetComputerNameExA(ComputerNameDnsHostname, (LPSTR)0xdeadbeef, &size);
415 error = GetLastError();
416 ok(ret == 0, "Expected 0, got %d\n", ret);
417 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
419 /* size is not set in win2k */
420 size = MAX_COMP_NAME;
421 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
422 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
423 ret = pGetComputerNameExA(ComputerNameDnsHostname, name, &size);
424 ok(ret, "GetComputerNameExA(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
425 trace("hostname is \"%s\"\n", name);
426 HeapFree(GetProcessHeap(), 0, name);
429 ret = pGetComputerNameExA(ComputerNameNetBIOS, (LPSTR)0xdeadbeef, &size);
430 error = GetLastError();
431 ok(ret == 0, "Expected 0, got %d\n", ret);
432 ok(error == ERROR_MORE_DATA, "Expected ERROR_MORE_DATA, got %d\n", error);
434 /* size is not set in win2k */
435 size = MAX_COMP_NAME;
436 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
437 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
438 ret = pGetComputerNameExA(ComputerNameNetBIOS, name, &size);
439 ok(ret, "GetComputerNameExA(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
440 trace("NetBIOS name is \"%s\"\n", name);
441 HeapFree(GetProcessHeap(), 0, name);
444 static void test_GetComputerNameExW(void)
451 if (!pGetComputerNameExW)
453 skip("GetComputerNameExW function not implemented\n");
458 ret = pGetComputerNameExW(ComputerNameDnsDomain, (LPWSTR)0xdeadbeef, &size);
459 error = GetLastError();
460 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
461 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
462 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
463 ret = pGetComputerNameExW(ComputerNameDnsDomain, nameW, &size);
464 ok(ret, "GetComputerNameExW(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
465 HeapFree(GetProcessHeap(), 0, nameW);
468 ret = pGetComputerNameExW(ComputerNameDnsFullyQualified, (LPWSTR)0xdeadbeef, &size);
469 error = GetLastError();
470 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
471 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
472 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
473 ret = pGetComputerNameExW(ComputerNameDnsFullyQualified, nameW, &size);
474 ok(ret, "GetComputerNameExW(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
475 HeapFree(GetProcessHeap(), 0, nameW);
478 ret = pGetComputerNameExW(ComputerNameDnsHostname, (LPWSTR)0xdeadbeef, &size);
479 error = GetLastError();
480 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
481 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
482 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
483 ret = pGetComputerNameExW(ComputerNameDnsHostname, nameW, &size);
484 ok(ret, "GetComputerNameExW(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
485 HeapFree(GetProcessHeap(), 0, nameW);
488 ret = pGetComputerNameExW(ComputerNameNetBIOS, (LPWSTR)0xdeadbeef, &size);
489 error = GetLastError();
490 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
491 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
492 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
493 ret = pGetComputerNameExW(ComputerNameNetBIOS, nameW, &size);
494 ok(ret, "GetComputerNameExW(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
495 HeapFree(GetProcessHeap(), 0, nameW);
500 init_functionpointers();
502 test_GetSetEnvironmentVariableA();
503 test_GetSetEnvironmentVariableW();
504 test_ExpandEnvironmentStringsA();
505 test_GetComputerName();
506 test_GetComputerNameExA();
507 test_GetComputerNameExW();