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
21 #include "wine/test.h"
25 static void test_GetSetEnvironmentVariableA(void)
30 static const char name[] = "SomeWildName";
31 static const char name_cased[] = "sOMEwILDnAME";
32 static const char value[] = "SomeWildValue";
34 ret = SetEnvironmentVariableA(NULL, NULL);
35 ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with NULL, NULL");
37 ret = SetEnvironmentVariableA("", "");
38 ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with \"\", \"\"");
40 ret = SetEnvironmentVariableA(name, "");
41 ok(ret == TRUE, "should not fail with empty value");
43 ret = SetEnvironmentVariableA("", value);
44 ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with empty name");
46 ret = SetEnvironmentVariableA(name, value);
47 ok(ret == TRUE, "unexpected error in SetEnvironmentVariableA");
49 /* the following line should just crash */
50 /* ret_size = GetEnvironmentVariableA(name, NULL, lstrlenA(value) + 1); */
52 ret_size = GetEnvironmentVariableA(NULL, NULL, 0);
53 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
55 ret_size = GetEnvironmentVariableA(NULL, buf, lstrlenA(value) + 1);
56 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
58 ret_size = GetEnvironmentVariableA("", buf, lstrlenA(value) + 1);
59 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
62 ret_size = GetEnvironmentVariableA(name, buf, 0);
63 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
64 ok(ret_size == lstrlenA(value) + 1, "should return length with terminating 0");
67 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value));
68 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
69 ok(ret_size == lstrlenA(value) + 1, "should return length with terminating 0");
72 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
73 ok(lstrcmpA(buf, value) == 0, "should touch the buffer");
74 ok(ret_size == lstrlenA(value), "should return length without terminating 0");
77 ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
78 ok(lstrcmpA(buf, value) == 0, "should touch the buffer");
79 ok(ret_size == lstrlenA(value), "should return length without terminating 0");
81 ret = SetEnvironmentVariableA(name, NULL);
82 ok(ret == TRUE, "should erase existing variable");
85 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
86 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
87 ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
89 ret = SetEnvironmentVariableA(name, value);
90 ok(ret == TRUE, "unexpected error in SetEnvironmentVariableA");
92 ret = SetEnvironmentVariableA(name, "");
93 ok(ret == TRUE, "should not fail with empty value");
96 ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
97 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
98 ok(lstrcmpA(buf, "") == 0, "should copy an empty string");
101 static void test_GetSetEnvironmentVariableW(void)
106 static const WCHAR name[] = {'S','o','m','e','W','i','l','d','N','a','m','e',0};
107 static const WCHAR value[] = {'S','o','m','e','W','i','l','d','V','a','l','u','e',0};
108 static const WCHAR name_cased[] = {'s','O','M','E','w','I','L','D','n','A','M','E',0};
109 static const WCHAR empty_strW[] = { 0 };
110 static const WCHAR fooW[] = {'f','o','o',0};
112 ret = SetEnvironmentVariableW(NULL, NULL);
113 ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with NULL, NULL");
115 ret = SetEnvironmentVariableW(empty_strW, empty_strW);
116 ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with \"\", \"\"");
118 ret = SetEnvironmentVariableW(name, empty_strW);
119 ok(ret == TRUE, "should not fail with empty value");
121 ret = SetEnvironmentVariableW(empty_strW, value);
122 ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with empty name");
124 ret = SetEnvironmentVariableW(name, value);
125 ok(ret == TRUE, "unexpected error in SetEnvironmentVariableW");
127 /* the following line should just crash */
128 /* ret_size = GetEnvironmentVariableW(name, NULL, lstrlenW(value) + 1); */
130 ret_size = GetEnvironmentVariableW(NULL, NULL, 0);
131 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
133 ret_size = GetEnvironmentVariableW(NULL, buf, lstrlenW(value) + 1);
134 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
136 ret_size = GetEnvironmentVariableW(empty_strW, buf, lstrlenW(value) + 1);
137 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
140 ret_size = GetEnvironmentVariableW(name, buf, 0);
141 ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
142 ok(ret_size == lstrlenW(value) + 1, "should return length with terminating 0");
145 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value));
148 ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
150 ok(ret_size == lstrlenW(value) + 1, "should return length with terminating 0");
153 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
154 ok(lstrcmpW(buf, value) == 0, "should touch the buffer");
155 ok(ret_size == lstrlenW(value), "should return length without terminating 0");
158 ret_size = GetEnvironmentVariableW(name_cased, buf, lstrlenW(value) + 1);
159 ok(lstrcmpW(buf, value) == 0, "should touch the buffer");
160 ok(ret_size == lstrlenW(value), "should return length without terminating 0");
162 ret = SetEnvironmentVariableW(name, NULL);
163 ok(ret == TRUE, "should erase existing variable");
166 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
167 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
168 ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
170 ret = SetEnvironmentVariableW(name, value);
171 ok(ret == TRUE, "unexpected error in SetEnvironmentVariableW");
173 ret = SetEnvironmentVariableW(name, empty_strW);
174 ok(ret == TRUE, "should not fail with empty value");
177 ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
178 ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
181 ok(lstrcmpW(buf, empty_strW) == 0, "should copy an empty string");
187 test_GetSetEnvironmentVariableA();
188 test_GetSetEnvironmentVariableW();