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"
28 static void test_GetSetEnvironmentVariableA(void)
33 static const char name[] = "SomeWildName";
34 static const char name_cased[] = "sOMEwILDnAME";
35 static const char value[] = "SomeWildValue";
37 ret = SetEnvironmentVariableA(name, value);
39 "unexpected error in SetEnvironmentVariableA, GetLastError=%d\n",
42 /* Try to retrieve the environment variable we just set */
43 ret_size = GetEnvironmentVariableA(name, NULL, 0);
44 ok(ret_size == strlen(value) + 1,
45 "should return length with terminating 0 ret_size=%d\n", ret_size);
48 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value));
49 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
50 ok(ret_size == strlen(value) + 1,
51 "should return length with terminating 0 ret_size=%d\n", ret_size);
54 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
55 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
56 ok(ret_size == strlen(value),
57 "should return length without terminating 0 ret_size=%d\n", ret_size);
60 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
61 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
62 ok(ret_size == strlen(value),
63 "should return length without terminating 0 ret_size=%d\n", ret_size);
65 /* Remove that environment variable */
66 ret = SetEnvironmentVariableA(name_cased, NULL);
67 ok(ret == TRUE, "should erase existing variable\n");
70 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
71 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer\n");
72 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
73 "should not find variable but ret_size=%d GetLastError=%d\n",
74 ret_size, GetLastError());
76 /* Check behavior of SetEnvironmentVariableA(name, "") */
77 ret = SetEnvironmentVariableA(name, value);
79 "unexpected error in SetEnvironmentVariableA, GetLastError=%d\n",
83 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
84 ok(lstrcmpA(buf, value) == 0, "should touch the buffer\n");
85 ok(ret_size == strlen(value),
86 "should return length without terminating 0 ret_size=%d\n", ret_size);
88 ret = SetEnvironmentVariableA(name_cased, "");
90 "should not fail with empty value but GetLastError=%d\n", GetLastError());
94 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
96 ((GetLastError() == 0 && lstrcmpA(buf, "") == 0) ||
97 (GetLastError() == ERROR_ENVVAR_NOT_FOUND)),
98 "%s should be set to \"\" (NT) or removed (Win9x) but ret_size=%d GetLastError=%d and buf=%s\n",
99 name, ret_size, GetLastError(), buf);
101 /* Test the limits */
102 ret_size = GetEnvironmentVariableA(NULL, NULL, 0);
103 ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
104 "should not find variable but ret_size=%d GetLastError=%d\n",
105 ret_size, GetLastError());
107 ret_size = GetEnvironmentVariableA(NULL, buf, lstrlenA(value) + 1);
108 ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
109 "should not find variable but ret_size=%d GetLastError=%d\n",
110 ret_size, GetLastError());
112 ret_size = GetEnvironmentVariableA("", buf, lstrlenA(value) + 1);
113 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
114 "should not find variable but ret_size=%d GetLastError=%d\n",
115 ret_size, GetLastError());
118 static void test_GetSetEnvironmentVariableW(void)
123 static const WCHAR name[] = {'S','o','m','e','W','i','l','d','N','a','m','e',0};
124 static const WCHAR value[] = {'S','o','m','e','W','i','l','d','V','a','l','u','e',0};
125 static const WCHAR name_cased[] = {'s','O','M','E','w','I','L','D','n','A','M','E',0};
126 static const WCHAR empty_strW[] = { 0 };
127 static const WCHAR fooW[] = {'f','o','o',0};
129 ret = SetEnvironmentVariableW(name, value);
130 if (ret == FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
132 /* Must be Win9x which doesn't support the Unicode functions */
136 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
139 /* Try to retrieve the environment variable we just set */
140 ret_size = GetEnvironmentVariableW(name, NULL, 0);
141 ok(ret_size == lstrlenW(value) + 1,
142 "should return length with terminating 0 ret_size=%d\n",
146 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value));
147 ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
149 ok(ret_size == lstrlenW(value) + 1,
150 "should return length with terminating 0 ret_size=%d\n", ret_size);
153 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
154 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
155 ok(ret_size == lstrlenW(value),
156 "should return length without terminating 0 ret_size=%d\n", ret_size);
159 ret_size = GetEnvironmentVariableW(name_cased, buf, lstrlenW(value) + 1);
160 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
161 ok(ret_size == lstrlenW(value),
162 "should return length without terminating 0 ret_size=%d\n", ret_size);
164 /* Remove that environment variable */
165 ret = SetEnvironmentVariableW(name_cased, NULL);
166 ok(ret == TRUE, "should erase existing variable\n");
169 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
170 ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
171 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
172 "should not find variable but ret_size=%d GetLastError=%d\n",
173 ret_size, GetLastError());
175 /* Check behavior of SetEnvironmentVariableW(name, "") */
176 ret = SetEnvironmentVariableW(name, value);
178 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
182 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
183 ok(lstrcmpW(buf, value) == 0, "should touch the buffer\n");
184 ok(ret_size == lstrlenW(value),
185 "should return length without terminating 0 ret_size=%d\n", ret_size);
187 ret = SetEnvironmentVariableW(name_cased, empty_strW);
188 ok(ret == TRUE, "should not fail with empty value but GetLastError=%d\n", GetLastError());
191 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
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());
195 ok(lstrcmpW(buf, empty_strW) == 0, "should copy an empty string\n");
197 /* Test the limits */
198 ret_size = GetEnvironmentVariableW(NULL, NULL, 0);
199 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
200 "should not find variable but ret_size=%d GetLastError=%d\n",
201 ret_size, GetLastError());
203 ret_size = GetEnvironmentVariableW(NULL, buf, lstrlenW(value) + 1);
204 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
205 "should not find variable but ret_size=%d GetLastError=%d\n",
206 ret_size, GetLastError());
208 ret = SetEnvironmentVariableW(NULL, NULL);
209 ok(ret == FALSE && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
210 "should fail with NULL, NULL but ret=%d and GetLastError=%d\n",
211 ret, GetLastError());
214 static void test_ExpandEnvironmentStringsA(void)
216 char buf[256], buf1[256], buf2[0x8000];
217 DWORD ret_size, ret_size1;
219 /* test a large destination size */
220 strcpy(buf, "12345");
221 ret_size = ExpandEnvironmentStringsA(buf, buf2, sizeof(buf2));
222 ok(!strcmp(buf, buf2), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf2, ret_size);
224 ret_size1 = GetWindowsDirectoryA(buf1,256);
225 ok ((ret_size1 >0) && (ret_size1<256), "GetWindowsDirectory Failed\n");
226 ret_size = ExpandEnvironmentStringsA("%SystemRoot%",buf,sizeof(buf));
227 if (ERROR_ENVVAR_NOT_FOUND == GetLastError())
229 ok(!strcmp(buf, buf1), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf, buf1, ret_size);
232 static BOOL (WINAPI *pGetComputerNameExA)(COMPUTER_NAME_FORMAT,LPSTR,LPDWORD);
233 static BOOL (WINAPI *pGetComputerNameExW)(COMPUTER_NAME_FORMAT,LPWSTR,LPDWORD);
235 static void test_GetComputerName(void)
244 ret = GetComputerNameA((LPSTR)0xdeadbeef, &size);
245 error = GetLastError();
246 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameA should have failed with ERROR_MORE_DATA instead of %d\n", error);
247 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
248 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
249 ret = GetComputerNameA(name, &size);
250 ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
251 HeapFree(GetProcessHeap(), 0, name);
254 ret = GetComputerNameW((LPWSTR)0xdeadbeef, &size);
255 error = GetLastError();
256 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameW should have failed with ERROR_MORE_DATA instead of %d\n", error);
257 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
258 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
259 ret = GetComputerNameW(nameW, &size);
260 ok(ret, "GetComputerNameW failed with error %d\n", GetLastError());
261 HeapFree(GetProcessHeap(), 0, nameW);
263 pGetComputerNameExA = GetProcAddress(GetModuleHandle("kernel32.dll"), "GetComputerNameExA");
264 if (!pGetComputerNameExA)
266 skip("GetComputerNameExA function not implemented, so not testing\n");
271 ret = pGetComputerNameExA(ComputerNameDnsDomain, (LPSTR)0xdeadbeef, &size);
272 error = GetLastError();
273 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error);
274 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
275 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
276 ret = pGetComputerNameExA(ComputerNameDnsDomain, name, &size);
277 ok(ret, "GetComputerNameExA(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
278 trace("domain name is \"%s\"\n", name);
279 HeapFree(GetProcessHeap(), 0, name);
282 ret = pGetComputerNameExA(ComputerNameDnsFullyQualified, (LPSTR)0xdeadbeef, &size);
283 error = GetLastError();
284 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error);
285 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
286 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
287 ret = pGetComputerNameExA(ComputerNameDnsFullyQualified, name, &size);
288 ok(ret, "GetComputerNameExA(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
289 trace("fully qualified hostname is \"%s\"\n", name);
290 HeapFree(GetProcessHeap(), 0, name);
293 ret = pGetComputerNameExA(ComputerNameDnsHostname, (LPSTR)0xdeadbeef, &size);
294 error = GetLastError();
295 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error);
296 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
297 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
298 ret = pGetComputerNameExA(ComputerNameDnsHostname, name, &size);
299 ok(ret, "GetComputerNameExA(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
300 trace("hostname is \"%s\"\n", name);
301 HeapFree(GetProcessHeap(), 0, name);
304 ret = pGetComputerNameExA(ComputerNameNetBIOS, (LPSTR)0xdeadbeef, &size);
305 error = GetLastError();
306 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error);
307 name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
308 ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
309 ret = pGetComputerNameExA(ComputerNameNetBIOS, name, &size);
310 ok(ret, "GetComputerNameExA(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
311 trace("NetBIOS name is \"%s\"\n", name);
312 HeapFree(GetProcessHeap(), 0, name);
314 pGetComputerNameExW = GetProcAddress(GetModuleHandle("kernel32.dll"), "GetComputerNameExW");
315 if (!pGetComputerNameExW)
317 skip("GetComputerNameExW function not implemented, so not testing\n");
322 ret = pGetComputerNameExW(ComputerNameDnsDomain, (LPWSTR)0xdeadbeef, &size);
323 error = GetLastError();
324 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
325 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
326 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
327 ret = pGetComputerNameExW(ComputerNameDnsDomain, nameW, &size);
328 ok(ret, "GetComputerNameExW(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
329 HeapFree(GetProcessHeap(), 0, nameW);
332 ret = pGetComputerNameExW(ComputerNameDnsFullyQualified, (LPWSTR)0xdeadbeef, &size);
333 error = GetLastError();
334 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
335 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
336 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
337 ret = pGetComputerNameExW(ComputerNameDnsFullyQualified, nameW, &size);
338 ok(ret, "GetComputerNameExW(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
339 HeapFree(GetProcessHeap(), 0, nameW);
342 ret = pGetComputerNameExW(ComputerNameDnsHostname, (LPWSTR)0xdeadbeef, &size);
343 error = GetLastError();
344 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
345 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
346 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
347 ret = pGetComputerNameExW(ComputerNameDnsHostname, nameW, &size);
348 ok(ret, "GetComputerNameExW(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
349 HeapFree(GetProcessHeap(), 0, nameW);
352 ret = pGetComputerNameExW(ComputerNameNetBIOS, (LPWSTR)0xdeadbeef, &size);
353 error = GetLastError();
354 ok(!ret && error == ERROR_MORE_DATA, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error);
355 nameW = HeapAlloc(GetProcessHeap(), 0, size * sizeof(nameW[0]));
356 ok(nameW != NULL, "HeapAlloc failed with error %d\n", GetLastError());
357 ret = pGetComputerNameExW(ComputerNameNetBIOS, nameW, &size);
358 ok(ret, "GetComputerNameExW(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
359 HeapFree(GetProcessHeap(), 0, nameW);
364 test_GetSetEnvironmentVariableA();
365 test_GetSetEnvironmentVariableW();
366 test_ExpandEnvironmentStringsA();
367 test_GetComputerName();