- OpenConsole: try to open the Unix console if we fail with wineserver
[wine] / dlls / kernel / tests / environ.c
1 /*
2  * Unit test suite for environment functions.
3  *
4  * Copyright 2002 Dmitry Timoshkov
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #include "wine/test.h"
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27
28 static void test_GetSetEnvironmentVariableA(void)
29 {
30     char buf[256];
31     BOOL ret;
32     DWORD ret_size;
33     static const char name[] = "SomeWildName";
34     static const char name_cased[] = "sOMEwILDnAME";
35     static const char value[] = "SomeWildValue";
36
37     ret = SetEnvironmentVariableA(name, value);
38     ok(ret == TRUE,
39        "unexpected error in SetEnvironmentVariableA, GetLastError=%ld\n",
40        GetLastError());
41
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);
46
47     lstrcpyA(buf, "foo");
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);
52
53     lstrcpyA(buf, "foo");
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);
58
59     lstrcpyA(buf, "foo");
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);
64
65     /* Remove that environment variable */
66     ret = SetEnvironmentVariableA(name_cased, NULL);
67     ok(ret == TRUE, "should erase existing variable\n");
68
69     lstrcpyA(buf, "foo");
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());
75
76     /* Check behavior of SetEnvironmentVariableA(name, "") */
77     ret = SetEnvironmentVariableA(name, value);
78     ok(ret == TRUE,
79        "unexpected error in SetEnvironmentVariableA, GetLastError=%ld\n",
80        GetLastError());
81
82     lstrcpyA(buf, "foo");
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);
87
88     ret = SetEnvironmentVariableA(name_cased, "");
89     ok(ret == TRUE,
90        "should not fail with empty value but GetLastError=%ld\n", GetLastError());
91
92     lstrcpyA(buf, "foo");
93     SetLastError(0);
94     ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
95     ok(ret_size == 0 &&
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);
100
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());
106
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());
111
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());
116 }
117
118 static void test_GetSetEnvironmentVariableW(void)
119 {
120     WCHAR buf[256];
121     BOOL ret;
122     DWORD ret_size;
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};
128
129     ret = SetEnvironmentVariableW(name, value);
130     if (ret == FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
131     {
132         /* Must be Win9x which doesn't support the Unicode functions */
133         return;
134     }
135     ok(ret == TRUE,
136        "unexpected error in SetEnvironmentVariableW, GetLastError=%ld\n",
137        GetLastError());
138
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",
143        ret_size);
144
145     lstrcpyW(buf, fooW);
146     ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value));
147     ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer\n");
148
149     ok(ret_size == lstrlenW(value) + 1,
150        "should return length with terminating 0 ret_size=%ld\n", ret_size);
151
152     lstrcpyW(buf, fooW);
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);
157
158     lstrcpyW(buf, fooW);
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);
163
164     /* Remove that environment variable */
165     ret = SetEnvironmentVariableW(name_cased, NULL);
166     ok(ret == TRUE, "should erase existing variable\n");
167
168     lstrcpyW(buf, fooW);
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());
174
175     /* Check behavior of SetEnvironmentVariableW(name, "") */
176     ret = SetEnvironmentVariableW(name, value);
177     ok(ret == TRUE,
178        "unexpected error in SetEnvironmentVariableW, GetLastError=%ld\n",
179        GetLastError());
180
181     lstrcpyW(buf, fooW);
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);
186
187     ret = SetEnvironmentVariableW(name_cased, empty_strW);
188     ok(ret == TRUE, "should not fail with empty value but GetLastError=%ld\n", GetLastError());
189
190     lstrcpyW(buf, fooW);
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");
196
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());
202
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());
207
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());
212 }
213
214 static void test_ExpandEnvironmentStringsA(void)
215 {
216     char buf[256], buf1[256];
217     DWORD ret_size, ret_size1;
218
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())
223         return;
224     ok(!strcmp(buf, buf1), "ExpandEnvironmentStrings failed %s vs %s. ret_size = %ld\n", buf, buf1, ret_size);
225 }
226
227 START_TEST(environ)
228 {
229     test_GetSetEnvironmentVariableA();
230     test_GetSetEnvironmentVariableW();
231     test_ExpandEnvironmentStringsA();
232 }