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 */
133 skip("SetEnvironmentVariableW is not implemented\n");
137 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
140 /* Try to retrieve the environment variable we just set */
141 ret_size
= GetEnvironmentVariableW(name
, NULL
, 0);
142 ok(ret_size
== lstrlenW(value
) + 1,
143 "should return length with terminating 0 ret_size=%d\n",
147 ret_size
= GetEnvironmentVariableW(name
, buf
, lstrlenW(value
));
148 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
150 ok(ret_size
== lstrlenW(value
) + 1,
151 "should return length with terminating 0 ret_size=%d\n", ret_size
);
154 ret_size
= GetEnvironmentVariableW(name
, buf
, lstrlenW(value
) + 1);
155 ok(lstrcmpW(buf
, value
) == 0, "should touch the buffer\n");
156 ok(ret_size
== lstrlenW(value
),
157 "should return length without terminating 0 ret_size=%d\n", ret_size
);
160 ret_size
= GetEnvironmentVariableW(name_cased
, buf
, lstrlenW(value
) + 1);
161 ok(lstrcmpW(buf
, value
) == 0, "should touch the buffer\n");
162 ok(ret_size
== lstrlenW(value
),
163 "should return length without terminating 0 ret_size=%d\n", ret_size
);
165 /* Remove that environment variable */
166 ret
= SetEnvironmentVariableW(name_cased
, NULL
);
167 ok(ret
== TRUE
, "should erase existing variable\n");
170 ret_size
= GetEnvironmentVariableW(name
, buf
, lstrlenW(value
) + 1);
171 ok(lstrcmpW(buf
, fooW
) == 0, "should not touch the buffer\n");
172 ok(ret_size
== 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND
,
173 "should not find variable but ret_size=%d GetLastError=%d\n",
174 ret_size
, GetLastError());
176 /* Check behavior of SetEnvironmentVariableW(name, "") */
177 ret
= SetEnvironmentVariableW(name
, value
);
179 "unexpected error in SetEnvironmentVariableW, GetLastError=%d\n",
183 ret_size
= GetEnvironmentVariableW(name
, buf
, lstrlenW(value
) + 1);
184 ok(lstrcmpW(buf
, value
) == 0, "should touch the buffer\n");
185 ok(ret_size
== lstrlenW(value
),
186 "should return length without terminating 0 ret_size=%d\n", ret_size
);
188 ret
= SetEnvironmentVariableW(name_cased
, empty_strW
);
189 ok(ret
== TRUE
, "should not fail with empty value but GetLastError=%d\n", GetLastError());
192 ret_size
= GetEnvironmentVariableW(name
, buf
, lstrlenW(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());
196 ok(lstrcmpW(buf
, empty_strW
) == 0, "should copy an empty string\n");
198 /* Test the limits */
199 ret_size
= GetEnvironmentVariableW(NULL
, NULL
, 0);
200 ok(ret_size
== 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND
,
201 "should not find variable but ret_size=%d GetLastError=%d\n",
202 ret_size
, GetLastError());
204 ret_size
= GetEnvironmentVariableW(NULL
, buf
, lstrlenW(value
) + 1);
205 ok(ret_size
== 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND
,
206 "should not find variable but ret_size=%d GetLastError=%d\n",
207 ret_size
, GetLastError());
209 ret
= SetEnvironmentVariableW(NULL
, NULL
);
210 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_ENVVAR_NOT_FOUND
),
211 "should fail with NULL, NULL but ret=%d and GetLastError=%d\n",
212 ret
, GetLastError());
215 static void test_ExpandEnvironmentStringsA(void)
217 char buf
[256], buf1
[256], buf2
[0x8000];
218 DWORD ret_size
, ret_size1
;
220 /* test a large destination size */
221 strcpy(buf
, "12345");
222 ret_size
= ExpandEnvironmentStringsA(buf
, buf2
, sizeof(buf2
));
223 ok(!strcmp(buf
, buf2
), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf
, buf2
, ret_size
);
225 ret_size1
= GetWindowsDirectoryA(buf1
,256);
226 ok ((ret_size1
>0) && (ret_size1
<256), "GetWindowsDirectory Failed\n");
227 ret_size
= ExpandEnvironmentStringsA("%SystemRoot%",buf
,sizeof(buf
));
228 if (ERROR_ENVVAR_NOT_FOUND
== GetLastError())
230 ok(!strcmp(buf
, buf1
), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf
, buf1
, ret_size
);
233 static BOOL (WINAPI
*pGetComputerNameExA
)(COMPUTER_NAME_FORMAT
,LPSTR
,LPDWORD
);
234 static BOOL (WINAPI
*pGetComputerNameExW
)(COMPUTER_NAME_FORMAT
,LPWSTR
,LPDWORD
);
236 static void test_GetComputerName(void)
246 ret
= GetComputerNameA((LPSTR
)0xdeadbeef, &size
);
247 error
= GetLastError();
249 ok(!ret
&& error
== ERROR_BUFFER_OVERFLOW
, "GetComputerNameA should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error
);
250 size
++; /* nul terminating character */
251 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
252 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
253 ret
= GetComputerNameA(name
, &size
);
254 ok(ret
, "GetComputerNameA failed with error %d\n", GetLastError());
255 HeapFree(GetProcessHeap(), 0, name
);
257 size
= MAX_COMPUTERNAME_LENGTH
+ 1;
258 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
259 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
260 ret
= GetComputerNameA(name
, &size
);
261 ok(ret
, "GetComputerNameA failed with error %d\n", GetLastError());
262 trace("computer name is \"%s\"\n", name
);
263 name_len
= strlen(name
);
264 ok(size
== name_len
, "size should be same as length, name_len=%d, size=%d\n", name_len
, size
);
265 HeapFree(GetProcessHeap(), 0, name
);
268 SetLastError(0xdeadbeef);
269 ret
= GetComputerNameW((LPWSTR
)0xdeadbeef, &size
);
270 error
= GetLastError();
271 if (error
== ERROR_CALL_NOT_IMPLEMENTED
)
272 skip("GetComputerNameW is not implemented\n");
276 ok(!ret
&& error
== ERROR_BUFFER_OVERFLOW
, "GetComputerNameW should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error
);
277 size
++; /* nul terminating character */
278 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
279 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
280 ret
= GetComputerNameW(nameW
, &size
);
281 ok(ret
, "GetComputerNameW failed with error %d\n", GetLastError());
282 HeapFree(GetProcessHeap(), 0, nameW
);
285 pGetComputerNameExA
= (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetComputerNameExA");
286 if (!pGetComputerNameExA
)
288 skip("GetComputerNameExA function not implemented, so not testing\n");
293 ret
= pGetComputerNameExA(ComputerNameDnsDomain
, (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(ComputerNameDnsDomain
, name
, &size
);
299 ok(ret
, "GetComputerNameExA(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
300 trace("domain name is \"%s\"\n", name
);
301 HeapFree(GetProcessHeap(), 0, name
);
304 ret
= pGetComputerNameExA(ComputerNameDnsFullyQualified
, (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(ComputerNameDnsFullyQualified
, name
, &size
);
310 ok(ret
, "GetComputerNameExA(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
311 trace("fully qualified hostname is \"%s\"\n", name
);
312 HeapFree(GetProcessHeap(), 0, name
);
315 ret
= pGetComputerNameExA(ComputerNameDnsHostname
, (LPSTR
)0xdeadbeef, &size
);
316 error
= GetLastError();
317 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error
);
318 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
319 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
320 ret
= pGetComputerNameExA(ComputerNameDnsHostname
, name
, &size
);
321 ok(ret
, "GetComputerNameExA(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
322 trace("hostname is \"%s\"\n", name
);
323 HeapFree(GetProcessHeap(), 0, name
);
326 ret
= pGetComputerNameExA(ComputerNameNetBIOS
, (LPSTR
)0xdeadbeef, &size
);
327 error
= GetLastError();
328 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error
);
329 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
330 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
331 ret
= pGetComputerNameExA(ComputerNameNetBIOS
, name
, &size
);
332 ok(ret
, "GetComputerNameExA(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
333 trace("NetBIOS name is \"%s\"\n", name
);
334 HeapFree(GetProcessHeap(), 0, name
);
336 pGetComputerNameExW
= (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetComputerNameExW");
337 if (!pGetComputerNameExW
)
339 skip("GetComputerNameExW function not implemented, so not testing\n");
344 ret
= pGetComputerNameExW(ComputerNameDnsDomain
, (LPWSTR
)0xdeadbeef, &size
);
345 error
= GetLastError();
346 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error
);
347 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
348 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
349 ret
= pGetComputerNameExW(ComputerNameDnsDomain
, nameW
, &size
);
350 ok(ret
, "GetComputerNameExW(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
351 HeapFree(GetProcessHeap(), 0, nameW
);
354 ret
= pGetComputerNameExW(ComputerNameDnsFullyQualified
, (LPWSTR
)0xdeadbeef, &size
);
355 error
= GetLastError();
356 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error
);
357 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
358 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
359 ret
= pGetComputerNameExW(ComputerNameDnsFullyQualified
, nameW
, &size
);
360 ok(ret
, "GetComputerNameExW(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
361 HeapFree(GetProcessHeap(), 0, nameW
);
364 ret
= pGetComputerNameExW(ComputerNameDnsHostname
, (LPWSTR
)0xdeadbeef, &size
);
365 error
= GetLastError();
366 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error
);
367 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
368 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
369 ret
= pGetComputerNameExW(ComputerNameDnsHostname
, nameW
, &size
);
370 ok(ret
, "GetComputerNameExW(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
371 HeapFree(GetProcessHeap(), 0, nameW
);
374 ret
= pGetComputerNameExW(ComputerNameNetBIOS
, (LPWSTR
)0xdeadbeef, &size
);
375 error
= GetLastError();
376 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error
);
377 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
378 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
379 ret
= pGetComputerNameExW(ComputerNameNetBIOS
, nameW
, &size
);
380 ok(ret
, "GetComputerNameExW(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
381 HeapFree(GetProcessHeap(), 0, nameW
);
386 test_GetSetEnvironmentVariableA();
387 test_GetSetEnvironmentVariableW();
388 test_ExpandEnvironmentStringsA();
389 test_GetComputerName();