Free the debug info when making a critical section global.
[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(name, value);
35     ok(ret == TRUE,
36        "unexpected error in SetEnvironmentVariableA, GetLastError=%ld",
37        GetLastError());
38
39     /* Try to retrieve the environment variable we just set */
40     ret_size = GetEnvironmentVariableA(name, NULL, 0);
41     ok(ret_size == strlen(value) + 1,
42        "should return length with terminating 0 ret_size=%ld", ret_size);
43
44     lstrcpyA(buf, "foo");
45     ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value));
46     ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
47     ok(ret_size == strlen(value) + 1,
48        "should return length with terminating 0 ret_size=%ld", ret_size);
49
50     lstrcpyA(buf, "foo");
51     ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
52     ok(lstrcmpA(buf, value) == 0, "should touch the buffer");
53     ok(ret_size == strlen(value),
54        "should return length without terminating 0 ret_size=%ld", ret_size);
55
56     lstrcpyA(buf, "foo");
57     ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
58     ok(lstrcmpA(buf, value) == 0, "should touch the buffer");
59     ok(ret_size == strlen(value),
60        "should return length without terminating 0 ret_size=%ld", ret_size);
61
62     /* Remove that environment variable */
63     ret = SetEnvironmentVariableA(name_cased, NULL);
64     ok(ret == TRUE, "should erase existing variable");
65
66     lstrcpyA(buf, "foo");
67     ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
68     ok(lstrcmpA(buf, "foo") == 0, "should not touch the buffer");
69     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
70        "should not find variable but ret_size=%ld GetLastError=%ld",
71        ret_size, GetLastError());
72
73     /* Check behavior of SetEnvironmentVariableA(name, "") */
74     ret = SetEnvironmentVariableA(name, value);
75     ok(ret == TRUE,
76        "unexpected error in SetEnvironmentVariableA, GetLastError=%ld",
77        GetLastError());
78
79     lstrcpyA(buf, "foo");
80     ret_size = GetEnvironmentVariableA(name_cased, buf, lstrlenA(value) + 1);
81     ok(lstrcmpA(buf, value) == 0, "should touch the buffer");
82     ok(ret_size == strlen(value),
83        "should return length without terminating 0 ret_size=%ld", ret_size);
84
85     ret = SetEnvironmentVariableA(name_cased, "");
86     ok(ret == TRUE,
87        "should not fail with empty value but GetLastError=%ld", GetLastError());
88
89     lstrcpyA(buf, "foo");
90     SetLastError(0);
91     ret_size = GetEnvironmentVariableA(name, buf, lstrlenA(value) + 1);
92     ok(ret_size == 0 &&
93        ((GetLastError() == 0 && lstrcmpA(buf, "") == 0) ||
94         (GetLastError() == ERROR_ENVVAR_NOT_FOUND)),
95        "%s should be set to \"\" (NT) or removed (Win9x) but ret_size=%ld GetLastError=%ld and buf=%s",
96        name, ret_size, GetLastError(), buf);
97
98     /* Test the limits */
99     ret_size = GetEnvironmentVariableA(NULL, NULL, 0);
100     ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
101        "should not find variable but ret_size=%ld GetLastError=%ld",
102        ret_size, GetLastError());
103
104     ret_size = GetEnvironmentVariableA(NULL, buf, lstrlenA(value) + 1);
105     ok(ret_size == 0 && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
106        "should not find variable but ret_size=%ld GetLastError=%ld",
107        ret_size, GetLastError());
108
109     ret_size = GetEnvironmentVariableA("", buf, lstrlenA(value) + 1);
110     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
111        "should not find variable but ret_size=%ld GetLastError=%ld",
112        ret_size, GetLastError());
113 }
114
115 static void test_GetSetEnvironmentVariableW(void)
116 {
117     WCHAR buf[256];
118     BOOL ret;
119     DWORD ret_size;
120     static const WCHAR name[] = {'S','o','m','e','W','i','l','d','N','a','m','e',0};
121     static const WCHAR value[] = {'S','o','m','e','W','i','l','d','V','a','l','u','e',0};
122     static const WCHAR name_cased[] = {'s','O','M','E','w','I','L','D','n','A','M','E',0};
123     static const WCHAR empty_strW[] = { 0 };
124     static const WCHAR fooW[] = {'f','o','o',0};
125
126     ret = SetEnvironmentVariableW(name, value);
127     if (ret == FALSE && GetLastError()==ERROR_CALL_NOT_IMPLEMENTED)
128     {
129         /* Must be Win9x which doesn't support the Unicode functions */
130         return;
131     }
132     ok(ret == TRUE,
133        "unexpected error in SetEnvironmentVariableW, GetLastError=%ld",
134        GetLastError());
135
136     /* Try to retrieve the environment variable we just set */
137     ret_size = GetEnvironmentVariableW(name, NULL, 0);
138     ok(ret_size == lstrlenW(value) + 1,
139        "should return length with terminating 0 ret_size=%ld",
140        ret_size);
141
142     lstrcpyW(buf, fooW);
143     ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value));
144     ok(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
145
146     ok(ret_size == lstrlenW(value) + 1,
147        "should return length with terminating 0 ret_size=%ld", ret_size);
148
149     lstrcpyW(buf, fooW);
150     ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
151     ok(lstrcmpW(buf, value) == 0, "should touch the buffer");
152     ok(ret_size == lstrlenW(value),
153        "should return length without terminating 0 ret_size=%ld", ret_size);
154
155     lstrcpyW(buf, fooW);
156     ret_size = GetEnvironmentVariableW(name_cased, buf, lstrlenW(value) + 1);
157     ok(lstrcmpW(buf, value) == 0, "should touch the buffer");
158     ok(ret_size == lstrlenW(value),
159        "should return length without terminating 0 ret_size=%ld", ret_size);
160
161     /* Remove that environment variable */
162     ret = SetEnvironmentVariableW(name_cased, 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(lstrcmpW(buf, fooW) == 0, "should not touch the buffer");
168     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
169        "should not find variable but ret_size=%ld GetLastError=%ld",
170        ret_size, GetLastError());
171
172     /* Check behavior of SetEnvironmentVariableW(name, "") */
173     ret = SetEnvironmentVariableW(name, value);
174     ok(ret == TRUE,
175        "unexpected error in SetEnvironmentVariableW, GetLastError=%ld",
176        GetLastError());
177
178     lstrcpyW(buf, fooW);
179     ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
180     ok(lstrcmpW(buf, value) == 0, "should touch the buffer");
181     ok(ret_size == lstrlenW(value),
182        "should return length without terminating 0 ret_size=%ld", ret_size);
183
184     ret = SetEnvironmentVariableW(name_cased, empty_strW);
185     ok(ret == TRUE, "should not fail with empty value but GetLastError=%ld", GetLastError());
186
187     lstrcpyW(buf, fooW);
188     ret_size = GetEnvironmentVariableW(name, buf, lstrlenW(value) + 1);
189     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
190        "should not find variable but ret_size=%ld GetLastError=%ld",
191        ret_size, GetLastError());
192     ok(lstrcmpW(buf, empty_strW) == 0, "should copy an empty string");
193
194     /* Test the limits */
195     ret_size = GetEnvironmentVariableW(NULL, NULL, 0);
196     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
197        "should not find variable but ret_size=%ld GetLastError=%ld",
198        ret_size, GetLastError());
199
200     ret_size = GetEnvironmentVariableW(NULL, buf, lstrlenW(value) + 1);
201     ok(ret_size == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND,
202        "should not find variable but ret_size=%ld GetLastError=%ld",
203        ret_size, GetLastError());
204
205     ret = SetEnvironmentVariableW(NULL, NULL);
206     ok(ret == FALSE && (GetLastError() == ERROR_INVALID_PARAMETER || GetLastError() == ERROR_ENVVAR_NOT_FOUND),
207        "should fail with NULL, NULL but ret=%d and GetLastError=%ld",
208        ret, GetLastError());
209 }
210
211 START_TEST(environ)
212 {
213     test_GetSetEnvironmentVariableA();
214     test_GetSetEnvironmentVariableW();
215 }