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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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=%ld\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=%ld\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=%ld\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=%ld\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=%ld\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=%ld GetLastError=%ld\n",
74 ret_size
, GetLastError());
76 /* Check behavior of SetEnvironmentVariableA(name, "") */
77 ret
= SetEnvironmentVariableA(name
, value
);
79 "unexpected error in SetEnvironmentVariableA, GetLastError=%ld\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=%ld\n", ret_size
);
88 ret
= SetEnvironmentVariableA(name_cased
, "");
90 "should not fail with empty value but GetLastError=%ld\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=%ld GetLastError=%ld 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=%ld GetLastError=%ld\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=%ld GetLastError=%ld\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=%ld GetLastError=%ld\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=%ld\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=%ld\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=%ld\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=%ld\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=%ld\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=%ld GetLastError=%ld\n",
173 ret_size
, GetLastError());
175 /* Check behavior of SetEnvironmentVariableW(name, "") */
176 ret
= SetEnvironmentVariableW(name
, value
);
178 "unexpected error in SetEnvironmentVariableW, GetLastError=%ld\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=%ld\n", ret_size
);
187 ret
= SetEnvironmentVariableW(name_cased
, empty_strW
);
188 ok(ret
== TRUE
, "should not fail with empty value but GetLastError=%ld\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=%ld GetLastError=%ld\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=%ld GetLastError=%ld\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=%ld GetLastError=%ld\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=%ld\n",
211 ret
, GetLastError());
214 static void test_ExpandEnvironmentStringsA()
216 char buf
[256], buf1
[256];
217 DWORD ret_size
, ret_size1
;
219 ret_size1
= GetWindowsDirectoryA(buf1
,256);
220 ok ((ret_size1
>0) && (ret_size1
<256), "GetWindowsDirectory Failed\n");
221 ret_size
= ExpandEnvironmentStringsA("%SystemRoot%",buf
,sizeof(buf
));
222 if (ERROR_ENVVAR_NOT_FOUND
== GetLastError())
224 ok(!strcmp(buf
, buf1
), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %ld\n", buf
, buf1
, ret_size
);
229 test_GetSetEnvironmentVariableA();
230 test_GetSetEnvironmentVariableW();
231 test_ExpandEnvironmentStringsA();