Do not include wine/unicode.h.
[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 "wine/test.h"
22 #include "winbase.h"
23 #include "winerror.h"
24
25 static void test_GetSetEnvironmentVariableA(void)
26 {
27     char buf[256];
28     BOOL ret;
29     DWORD ret_size;
30     static const char name[] = "SomeWildName";
31     static const char name_cased[] = "sOMEwILDnAME";
32     static const char value[] = "SomeWildValue";
33
34     ret = SetEnvironmentVariableA(NULL, NULL);
35     ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with NULL, NULL");
36
37     ret = SetEnvironmentVariableA("", "");
38     ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with \"\", \"\"");
39
40     ret = SetEnvironmentVariableA(name, "");
41     ok(ret == TRUE, "should not fail with empty value");
42
43     ret = SetEnvironmentVariableA("", value);
44     ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with empty name");
45
46     ret = SetEnvironmentVariableA(name, value);
47     ok(ret == TRUE, "unexpected error in SetEnvironmentVariableA");
48
49     /* the following line should just crash */
50     /* ret_size = GetEnvironmentVariableA(name, NULL, lstrlenA(value) + 1); */
51
52     ret_size = GetEnvironmentVariableA(NULL, NULL, 0);
53     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
54
55     ret_size = GetEnvironmentVariableA(NULL, buf, lstrlenA(value) + 1);
56     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
57
58     ret_size = GetEnvironmentVariableA("", buf, lstrlenA(value) + 1);
59     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
60
61     lstrcpyA(buf, "foo");
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");
65
66     lstrcpyA(buf, "foo");
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");
70
71     lstrcpyA(buf, "foo");
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");
75
76     lstrcpyA(buf, "foo");
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");
80
81     ret = SetEnvironmentVariableA(name, NULL);
82     ok(ret == TRUE, "should erase existing variable");
83
84     lstrcpyA(buf, "foo");
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");
88
89     ret = SetEnvironmentVariableA(name, value);
90     ok(ret == TRUE, "unexpected error in SetEnvironmentVariableA");
91
92     ret = SetEnvironmentVariableA(name, "");
93     ok(ret == TRUE, "should not fail with empty value");
94
95     lstrcpyA(buf, "foo");
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");
99 }
100
101 static void test_GetSetEnvironmentVariableW(void)
102 {
103     WCHAR buf[256];
104     BOOL ret;
105     DWORD ret_size;
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};
111
112     ret = SetEnvironmentVariableW(NULL, NULL);
113     ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with NULL, NULL");
114
115     ret = SetEnvironmentVariableW(empty_strW, empty_strW);
116     ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with \"\", \"\"");
117
118     ret = SetEnvironmentVariableW(name, empty_strW);
119     ok(ret == TRUE, "should not fail with empty value");
120
121     ret = SetEnvironmentVariableW(empty_strW, value);
122     ok(ret == FALSE && GetLastError() == ERROR_INVALID_PARAMETER, "should fail with empty name");
123
124     ret = SetEnvironmentVariableW(name, value);
125     ok(ret == TRUE, "unexpected error in SetEnvironmentVariableW");
126
127     /* the following line should just crash */
128     /* ret_size = GetEnvironmentVariableW(name, NULL, lstrlenW(value) + 1); */
129
130     ret_size = GetEnvironmentVariableW(NULL, NULL, 0);
131     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
132
133     ret_size = GetEnvironmentVariableW(NULL, buf, lstrlenW(value) + 1);
134     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
135
136     ret_size = GetEnvironmentVariableW(empty_strW, buf, lstrlenW(value) + 1);
137     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
138
139     lstrcpyW(buf, fooW);
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");
143
144     lstrcpyW(buf, fooW);
145     ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value));
146 todo_wine
147 {
148     ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
149 };
150     ok(ret_size == lstrlenW(value) + 1, "should return length with terminating 0");
151
152     lstrcpyW(buf, fooW);
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");
156
157     lstrcpyW(buf, fooW);
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");
161
162     ret = SetEnvironmentVariableW(name, NULL);
163     ok(ret == TRUE, "should erase existing variable");
164
165     lstrcpyW(buf, fooW);
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");
169
170     ret = SetEnvironmentVariableW(name, value);
171     ok(ret == TRUE, "unexpected error in SetEnvironmentVariableW");
172
173     ret = SetEnvironmentVariableW(name, empty_strW);
174     ok(ret == TRUE, "should not fail with empty value");
175
176     lstrcpyW(buf, fooW);
177     ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
178     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND, "should not find variable");
179 todo_wine
180 {
181     ok(lstrcmpW(buf, empty_strW) == 0, "should copy an empty string");
182 };
183 }
184
185 START_TEST(environ)
186 {
187     test_GetSetEnvironmentVariableA();
188     test_GetSetEnvironmentVariableW();
189 }