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 if (0) /* Both tests crash on Vista */
206 ret_size
= GetEnvironmentVariableW(NULL
, buf
, lstrlenW(value
) + 1);
207 ok(ret_size
== 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND
,
208 "should not find variable but ret_size=%d GetLastError=%d\n",
209 ret_size
, GetLastError());
211 ret
= SetEnvironmentVariableW(NULL
, NULL
);
212 ok(ret
== FALSE
&& (GetLastError() == ERROR_INVALID_PARAMETER
|| GetLastError() == ERROR_ENVVAR_NOT_FOUND
),
213 "should fail with NULL, NULL but ret=%d and GetLastError=%d\n",
214 ret
, GetLastError());
218 static void test_ExpandEnvironmentStringsA(void)
220 char buf
[256], buf1
[256], buf2
[0x8000];
221 DWORD ret_size
, ret_size1
;
223 /* test a large destination size */
224 strcpy(buf
, "12345");
225 ret_size
= ExpandEnvironmentStringsA(buf
, buf2
, sizeof(buf2
));
226 ok(!strcmp(buf
, buf2
), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf
, buf2
, ret_size
);
228 ret_size1
= GetWindowsDirectoryA(buf1
,256);
229 ok ((ret_size1
>0) && (ret_size1
<256), "GetWindowsDirectory Failed\n");
230 ret_size
= ExpandEnvironmentStringsA("%SystemRoot%",buf
,sizeof(buf
));
231 if (ERROR_ENVVAR_NOT_FOUND
== GetLastError())
233 ok(!strcmp(buf
, buf1
), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %d\n", buf
, buf1
, ret_size
);
236 static BOOL (WINAPI
*pGetComputerNameExA
)(COMPUTER_NAME_FORMAT
,LPSTR
,LPDWORD
);
237 static BOOL (WINAPI
*pGetComputerNameExW
)(COMPUTER_NAME_FORMAT
,LPWSTR
,LPDWORD
);
239 static void test_GetComputerName(void)
249 ret
= GetComputerNameA((LPSTR
)0xdeadbeef, &size
);
250 error
= GetLastError();
252 ok(!ret
&& error
== ERROR_BUFFER_OVERFLOW
, "GetComputerNameA should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error
);
253 size
++; /* nul terminating character */
254 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
255 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
256 ret
= GetComputerNameA(name
, &size
);
257 ok(ret
, "GetComputerNameA failed with error %d\n", GetLastError());
258 HeapFree(GetProcessHeap(), 0, name
);
260 size
= MAX_COMPUTERNAME_LENGTH
+ 1;
261 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
262 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
263 ret
= GetComputerNameA(name
, &size
);
264 ok(ret
, "GetComputerNameA failed with error %d\n", GetLastError());
265 trace("computer name is \"%s\"\n", name
);
266 name_len
= strlen(name
);
267 ok(size
== name_len
, "size should be same as length, name_len=%d, size=%d\n", name_len
, size
);
268 HeapFree(GetProcessHeap(), 0, name
);
271 SetLastError(0xdeadbeef);
272 ret
= GetComputerNameW((LPWSTR
)0xdeadbeef, &size
);
273 error
= GetLastError();
274 if (error
== ERROR_CALL_NOT_IMPLEMENTED
)
275 skip("GetComputerNameW is not implemented\n");
279 ok(!ret
&& error
== ERROR_BUFFER_OVERFLOW
, "GetComputerNameW should have failed with ERROR_BUFFER_OVERFLOW instead of %d\n", error
);
280 size
++; /* nul terminating character */
281 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
282 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
283 ret
= GetComputerNameW(nameW
, &size
);
284 ok(ret
, "GetComputerNameW failed with error %d\n", GetLastError());
285 HeapFree(GetProcessHeap(), 0, nameW
);
288 pGetComputerNameExA
= (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetComputerNameExA");
289 if (!pGetComputerNameExA
)
291 skip("GetComputerNameExA function not implemented, so not testing\n");
296 ret
= pGetComputerNameExA(ComputerNameDnsDomain
, (LPSTR
)0xdeadbeef, &size
);
297 error
= GetLastError();
298 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error
);
299 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
300 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
301 ret
= pGetComputerNameExA(ComputerNameDnsDomain
, name
, &size
);
302 ok(ret
, "GetComputerNameExA(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
303 trace("domain name is \"%s\"\n", name
);
304 HeapFree(GetProcessHeap(), 0, name
);
307 ret
= pGetComputerNameExA(ComputerNameDnsFullyQualified
, (LPSTR
)0xdeadbeef, &size
);
308 error
= GetLastError();
309 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error
);
310 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
311 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
312 ret
= pGetComputerNameExA(ComputerNameDnsFullyQualified
, name
, &size
);
313 ok(ret
, "GetComputerNameExA(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
314 trace("fully qualified hostname is \"%s\"\n", name
);
315 HeapFree(GetProcessHeap(), 0, name
);
318 ret
= pGetComputerNameExA(ComputerNameDnsHostname
, (LPSTR
)0xdeadbeef, &size
);
319 error
= GetLastError();
320 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error
);
321 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
322 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
323 ret
= pGetComputerNameExA(ComputerNameDnsHostname
, name
, &size
);
324 ok(ret
, "GetComputerNameExA(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
325 trace("hostname is \"%s\"\n", name
);
326 HeapFree(GetProcessHeap(), 0, name
);
329 ret
= pGetComputerNameExA(ComputerNameNetBIOS
, (LPSTR
)0xdeadbeef, &size
);
330 error
= GetLastError();
331 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExA should have failed with ERROR_MORE_DATA instead of %d\n", error
);
332 name
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(name
[0]));
333 ok(name
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
334 ret
= pGetComputerNameExA(ComputerNameNetBIOS
, name
, &size
);
335 ok(ret
, "GetComputerNameExA(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
336 trace("NetBIOS name is \"%s\"\n", name
);
337 HeapFree(GetProcessHeap(), 0, name
);
339 pGetComputerNameExW
= (void *)GetProcAddress(GetModuleHandle("kernel32.dll"), "GetComputerNameExW");
340 if (!pGetComputerNameExW
)
342 skip("GetComputerNameExW function not implemented, so not testing\n");
347 ret
= pGetComputerNameExW(ComputerNameDnsDomain
, (LPWSTR
)0xdeadbeef, &size
);
348 error
= GetLastError();
349 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error
);
350 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
351 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
352 ret
= pGetComputerNameExW(ComputerNameDnsDomain
, nameW
, &size
);
353 ok(ret
, "GetComputerNameExW(ComputerNameDnsDomain) failed with error %d\n", GetLastError());
354 HeapFree(GetProcessHeap(), 0, nameW
);
357 ret
= pGetComputerNameExW(ComputerNameDnsFullyQualified
, (LPWSTR
)0xdeadbeef, &size
);
358 error
= GetLastError();
359 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error
);
360 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
361 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
362 ret
= pGetComputerNameExW(ComputerNameDnsFullyQualified
, nameW
, &size
);
363 ok(ret
, "GetComputerNameExW(ComputerNameDnsFullyQualified) failed with error %d\n", GetLastError());
364 HeapFree(GetProcessHeap(), 0, nameW
);
367 ret
= pGetComputerNameExW(ComputerNameDnsHostname
, (LPWSTR
)0xdeadbeef, &size
);
368 error
= GetLastError();
369 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error
);
370 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
371 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
372 ret
= pGetComputerNameExW(ComputerNameDnsHostname
, nameW
, &size
);
373 ok(ret
, "GetComputerNameExW(ComputerNameDnsHostname) failed with error %d\n", GetLastError());
374 HeapFree(GetProcessHeap(), 0, nameW
);
377 ret
= pGetComputerNameExW(ComputerNameNetBIOS
, (LPWSTR
)0xdeadbeef, &size
);
378 error
= GetLastError();
379 ok(!ret
&& error
== ERROR_MORE_DATA
, "GetComputerNameExW should have failed with ERROR_MORE_DATA instead of %d\n", error
);
380 nameW
= HeapAlloc(GetProcessHeap(), 0, size
* sizeof(nameW
[0]));
381 ok(nameW
!= NULL
, "HeapAlloc failed with error %d\n", GetLastError());
382 ret
= pGetComputerNameExW(ComputerNameNetBIOS
, nameW
, &size
);
383 ok(ret
, "GetComputerNameExW(ComputerNameNetBIOS) failed with error %d\n", GetLastError());
384 HeapFree(GetProcessHeap(), 0, nameW
);
389 test_GetSetEnvironmentVariableA();
390 test_GetSetEnvironmentVariableW();
391 test_ExpandEnvironmentStringsA();
392 test_GetComputerName();