1c2c66affSColin Finck /*
2c2c66affSColin Finck * Unit tests for C library environment routines
3c2c66affSColin Finck *
4c2c66affSColin Finck * Copyright 2004 Mike Hearn <mh@codeweavers.com>
5c2c66affSColin Finck *
6c2c66affSColin Finck * This library is free software; you can redistribute it and/or
7c2c66affSColin Finck * modify it under the terms of the GNU Lesser General Public
8c2c66affSColin Finck * License as published by the Free Software Foundation; either
9c2c66affSColin Finck * version 2.1 of the License, or (at your option) any later version.
10c2c66affSColin Finck *
11c2c66affSColin Finck * This library is distributed in the hope that it will be useful,
12c2c66affSColin Finck * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c2c66affSColin Finck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14c2c66affSColin Finck * Lesser General Public License for more details.
15c2c66affSColin Finck *
16c2c66affSColin Finck * You should have received a copy of the GNU Lesser General Public
17c2c66affSColin Finck * License along with this library; if not, write to the Free Software
18c2c66affSColin Finck * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19c2c66affSColin Finck */
20c2c66affSColin Finck
211c9ac7a2SAmine Khaldi #include "wine/test.h"
221c9ac7a2SAmine Khaldi #include <stdlib.h>
23c2c66affSColin Finck
24c2c66affSColin Finck static const char *a_very_long_env_string =
25c2c66affSColin Finck "LIBRARY_PATH="
26c2c66affSColin Finck "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/;"
27c2c66affSColin Finck "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/;"
28c2c66affSColin Finck "/mingw/lib/gcc/mingw32/3.4.2/;"
29c2c66affSColin Finck "/usr/lib/gcc/mingw32/3.4.2/;"
30c2c66affSColin Finck "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/lib/mingw32/3.4.2/;"
31c2c66affSColin Finck "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/lib/;"
32c2c66affSColin Finck "/mingw/mingw32/lib/mingw32/3.4.2/;"
33c2c66affSColin Finck "/mingw/mingw32/lib/;"
34c2c66affSColin Finck "/mingw/lib/mingw32/3.4.2/;"
35c2c66affSColin Finck "/mingw/lib/;"
36c2c66affSColin Finck "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../mingw32/3.4.2/;"
37c2c66affSColin Finck "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../;"
38c2c66affSColin Finck "/mingw/lib/mingw32/3.4.2/;"
39c2c66affSColin Finck "/mingw/lib/;"
40c2c66affSColin Finck "/lib/mingw32/3.4.2/;"
41c2c66affSColin Finck "/lib/;"
42c2c66affSColin Finck "/usr/lib/mingw32/3.4.2/;"
43c2c66affSColin Finck "/usr/lib/";
44c2c66affSColin Finck
45c2c66affSColin Finck void __cdecl __getmainargs(int *, char ***, char ***, int, int *);
46c2c66affSColin Finck void __cdecl __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, int *);
47c2c66affSColin Finck
48c2c66affSColin Finck static char ***(__cdecl *p__p__environ)(void);
49c2c66affSColin Finck static WCHAR ***(__cdecl *p__p__wenviron)(void);
50*ce2df64aSAmine Khaldi static void (*p_get_environ)(char ***);
51*ce2df64aSAmine Khaldi static void (*p_get_wenviron)(WCHAR ***);
52c2c66affSColin Finck
53c2c66affSColin Finck static char ***p_environ;
54c2c66affSColin Finck static WCHAR ***p_wenviron;
55c2c66affSColin Finck
init(void)56c2c66affSColin Finck static void init(void)
57c2c66affSColin Finck {
58c2c66affSColin Finck HMODULE hmod = GetModuleHandleA("msvcrt.dll");
59c2c66affSColin Finck
60c2c66affSColin Finck p__p__environ = (void *)GetProcAddress(hmod, "__p__environ");
61c2c66affSColin Finck p__p__wenviron = (void *)GetProcAddress(hmod, "__p__wenviron");
62c2c66affSColin Finck p_environ = (void *)GetProcAddress(hmod, "_environ");
63c2c66affSColin Finck p_wenviron = (void *)GetProcAddress(hmod, "_wenviron");
64*ce2df64aSAmine Khaldi p_get_environ = (void *)GetProcAddress(hmod, "_get_environ");
65*ce2df64aSAmine Khaldi p_get_wenviron = (void *)GetProcAddress(hmod, "_get_wenviron");
66c2c66affSColin Finck }
67c2c66affSColin Finck
test_system(void)68c2c66affSColin Finck static void test_system(void)
69c2c66affSColin Finck {
70c2c66affSColin Finck int ret = system(NULL);
71c2c66affSColin Finck ok(ret == 1, "Expected system to return 1, got %d\n", ret);
72c2c66affSColin Finck
73c2c66affSColin Finck ret = system("echo OK");
74c2c66affSColin Finck ok(ret == 0, "Expected system to return 0, got %d\n", ret);
75c2c66affSColin Finck }
76c2c66affSColin Finck
test__environ(void)77c2c66affSColin Finck static void test__environ(void)
78c2c66affSColin Finck {
79c2c66affSColin Finck int argc;
80c2c66affSColin Finck char **argv, **envp = NULL;
81c2c66affSColin Finck int i, mode = 0;
82c2c66affSColin Finck
83c2c66affSColin Finck ok( p_environ != NULL, "Expected the pointer to _environ to be non-NULL\n" );
84c2c66affSColin Finck if (p_environ)
85c2c66affSColin Finck ok( *p_environ != NULL, "Expected _environ to be initialized on startup\n" );
86c2c66affSColin Finck
87c2c66affSColin Finck if (!p_environ || !*p_environ)
88c2c66affSColin Finck {
89c2c66affSColin Finck skip( "_environ pointers are not valid\n" );
90c2c66affSColin Finck return;
91c2c66affSColin Finck }
92c2c66affSColin Finck
93c2c66affSColin Finck /* Examine the returned pointer from __p__environ(), if available. */
94c2c66affSColin Finck if (p__p__environ)
95c2c66affSColin Finck {
96c2c66affSColin Finck ok( *p__p__environ() == *p_environ,
97c2c66affSColin Finck "Expected _environ pointers to be identical\n" );
98c2c66affSColin Finck }
99c2c66affSColin Finck else
100*ce2df64aSAmine Khaldi skip( "__p__environ() is not available\n" );
101*ce2df64aSAmine Khaldi
102*ce2df64aSAmine Khaldi if (p_get_environ)
103*ce2df64aSAmine Khaldi {
104*ce2df64aSAmine Khaldi char **retptr;
105*ce2df64aSAmine Khaldi p_get_environ(&retptr);
106*ce2df64aSAmine Khaldi ok( retptr == *p_environ,
107*ce2df64aSAmine Khaldi "Expected _environ pointers to be identical\n" );
108*ce2df64aSAmine Khaldi }
109*ce2df64aSAmine Khaldi else
110*ce2df64aSAmine Khaldi win_skip( "_get_environ() is not available\n" );
111c2c66affSColin Finck
112c2c66affSColin Finck /* Note that msvcrt from Windows versions older than Vista
113c2c66affSColin Finck * expects the mode pointer parameter to be valid.*/
114c2c66affSColin Finck __getmainargs(&argc, &argv, &envp, 0, &mode);
115c2c66affSColin Finck
116c2c66affSColin Finck ok( envp != NULL, "Expected initial environment block pointer to be non-NULL\n" );
117c2c66affSColin Finck if (!envp)
118c2c66affSColin Finck {
119c2c66affSColin Finck skip( "Initial environment block pointer is not valid\n" );
120c2c66affSColin Finck return;
121c2c66affSColin Finck }
122c2c66affSColin Finck
123c2c66affSColin Finck for (i = 0; ; i++)
124c2c66affSColin Finck {
125c2c66affSColin Finck if ((*p_environ)[i])
126c2c66affSColin Finck {
127c2c66affSColin Finck ok( envp[i] != NULL, "Expected environment block pointer element to be non-NULL\n" );
128c2c66affSColin Finck ok( !strcmp((*p_environ)[i], envp[i]),
129c2c66affSColin Finck "Expected _environ and environment block pointer strings (%s vs. %s) to match\n",
130c2c66affSColin Finck (*p_environ)[i], envp[i] );
131c2c66affSColin Finck }
132c2c66affSColin Finck else
133c2c66affSColin Finck {
134c2c66affSColin Finck ok( !envp[i], "Expected environment block pointer element to be NULL, got %p\n", envp[i] );
135c2c66affSColin Finck break;
136c2c66affSColin Finck }
137c2c66affSColin Finck }
138c2c66affSColin Finck }
139c2c66affSColin Finck
test__wenviron(void)140c2c66affSColin Finck static void test__wenviron(void)
141c2c66affSColin Finck {
142c2c66affSColin Finck static const WCHAR cat_eq_dogW[] = {'c','a','t','=','d','o','g',0};
143c2c66affSColin Finck static const WCHAR cat_eqW[] = {'c','a','t','=',0};
144c2c66affSColin Finck
145c2c66affSColin Finck int argc;
146c2c66affSColin Finck char **argv, **envp = NULL;
147c2c66affSColin Finck WCHAR **wargv, **wenvp = NULL;
148c2c66affSColin Finck int i, mode = 0;
149c2c66affSColin Finck
150c2c66affSColin Finck ok( p_wenviron != NULL, "Expected the pointer to _wenviron to be non-NULL\n" );
151c2c66affSColin Finck if (p_wenviron)
152c2c66affSColin Finck ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron );
153c2c66affSColin Finck else
154c2c66affSColin Finck {
155c2c66affSColin Finck win_skip( "Pointer to _wenviron is not valid\n" );
156c2c66affSColin Finck return;
157c2c66affSColin Finck }
158c2c66affSColin Finck
159c2c66affSColin Finck /* Examine the returned pointer from __p__wenviron(), if available. */
160c2c66affSColin Finck if (p__p__wenviron)
161c2c66affSColin Finck {
162c2c66affSColin Finck ok( *p__p__wenviron() == NULL,
163c2c66affSColin Finck "Expected _wenviron pointers to be NULL\n" );
164c2c66affSColin Finck }
165c2c66affSColin Finck else
166*ce2df64aSAmine Khaldi skip( "__p__wenviron() is not available\n" );
167*ce2df64aSAmine Khaldi
168*ce2df64aSAmine Khaldi if (p_get_wenviron)
169*ce2df64aSAmine Khaldi {
170*ce2df64aSAmine Khaldi WCHAR **retptr;
171*ce2df64aSAmine Khaldi p_get_wenviron(&retptr);
172*ce2df64aSAmine Khaldi ok( retptr == NULL,
173*ce2df64aSAmine Khaldi "Expected _wenviron pointers to be NULL\n" );
174*ce2df64aSAmine Khaldi }
175*ce2df64aSAmine Khaldi else
176*ce2df64aSAmine Khaldi win_skip( "_get_wenviron() is not available\n" );
177c2c66affSColin Finck
178c2c66affSColin Finck /* __getmainargs doesn't initialize _wenviron. */
179c2c66affSColin Finck __getmainargs(&argc, &argv, &envp, 0, &mode);
180c2c66affSColin Finck
181c2c66affSColin Finck ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron);
182c2c66affSColin Finck ok( envp != NULL, "Expected initial environment block pointer to be non-NULL\n" );
183c2c66affSColin Finck if (!envp)
184c2c66affSColin Finck {
185c2c66affSColin Finck skip( "Initial environment block pointer is not valid\n" );
186c2c66affSColin Finck return;
187c2c66affSColin Finck }
188c2c66affSColin Finck
189c2c66affSColin Finck /* Neither does calling the non-Unicode environment manipulation functions. */
190c2c66affSColin Finck ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
191c2c66affSColin Finck ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron);
192c2c66affSColin Finck ok( _putenv("cat=") == 0, "failed deleting cat\n" );
193c2c66affSColin Finck
194c2c66affSColin Finck /* _wenviron isn't initialized until __wgetmainargs is called or
195c2c66affSColin Finck * one of the Unicode environment manipulation functions is called. */
196c2c66affSColin Finck ok( _wputenv(cat_eq_dogW) == 0, "failed setting cat=dog\n" );
197c2c66affSColin Finck ok( *p_wenviron != NULL, "Expected _wenviron to be non-NULL\n" );
198c2c66affSColin Finck ok( _wputenv(cat_eqW) == 0, "failed deleting cat\n" );
199c2c66affSColin Finck
200c2c66affSColin Finck __wgetmainargs(&argc, &wargv, &wenvp, 0, &mode);
201c2c66affSColin Finck
202c2c66affSColin Finck ok( *p_wenviron != NULL, "Expected _wenviron to be non-NULL\n" );
203c2c66affSColin Finck ok( wenvp != NULL, "Expected initial environment block pointer to be non-NULL\n" );
204c2c66affSColin Finck if (!wenvp)
205c2c66affSColin Finck {
206c2c66affSColin Finck skip( "Initial environment block pointer is not valid\n" );
207c2c66affSColin Finck return;
208c2c66affSColin Finck }
209c2c66affSColin Finck
210c2c66affSColin Finck /* Examine the returned pointer from __p__wenviron(),
211c2c66affSColin Finck * if available, after _wenviron is initialized. */
212c2c66affSColin Finck if (p__p__wenviron)
213c2c66affSColin Finck {
214c2c66affSColin Finck ok( *p__p__wenviron() == *p_wenviron,
215c2c66affSColin Finck "Expected _wenviron pointers to be identical\n" );
216c2c66affSColin Finck }
217c2c66affSColin Finck
218*ce2df64aSAmine Khaldi if (p_get_wenviron)
219*ce2df64aSAmine Khaldi {
220*ce2df64aSAmine Khaldi WCHAR **retptr;
221*ce2df64aSAmine Khaldi p_get_wenviron(&retptr);
222*ce2df64aSAmine Khaldi ok( retptr == *p_wenviron,
223*ce2df64aSAmine Khaldi "Expected _wenviron pointers to be identical\n" );
224*ce2df64aSAmine Khaldi }
225*ce2df64aSAmine Khaldi
226c2c66affSColin Finck for (i = 0; ; i++)
227c2c66affSColin Finck {
228c2c66affSColin Finck if ((*p_wenviron)[i])
229c2c66affSColin Finck {
230c2c66affSColin Finck ok( wenvp[i] != NULL, "Expected environment block pointer element to be non-NULL\n" );
231c2c66affSColin Finck ok( !winetest_strcmpW((*p_wenviron)[i], wenvp[i]),
232c2c66affSColin Finck "Expected _wenviron and environment block pointer strings (%s vs. %s) to match\n",
233c2c66affSColin Finck wine_dbgstr_w((*p_wenviron)[i]), wine_dbgstr_w(wenvp[i]) );
234c2c66affSColin Finck }
235c2c66affSColin Finck else
236c2c66affSColin Finck {
237c2c66affSColin Finck ok( !wenvp[i], "Expected environment block pointer element to be NULL, got %p\n", wenvp[i] );
238c2c66affSColin Finck break;
239c2c66affSColin Finck }
240c2c66affSColin Finck }
241c2c66affSColin Finck }
242c2c66affSColin Finck
test_environment_manipulation(void)243c2c66affSColin Finck static void test_environment_manipulation(void)
244c2c66affSColin Finck {
245c2c66affSColin Finck ok( _putenv("cat=") == 0, "_putenv failed on deletion of nonexistent environment variable\n" );
246c2c66affSColin Finck ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
247c2c66affSColin Finck ok( strcmp(getenv("cat"), "dog") == 0, "getenv did not return 'dog'\n" );
248c2c66affSColin Finck ok( _putenv("cat=") == 0, "failed deleting cat\n" );
249c2c66affSColin Finck
250c2c66affSColin Finck ok( _putenv("=") == -1, "should not accept '=' as input\n" );
251c2c66affSColin Finck ok( _putenv("=dog") == -1, "should not accept '=dog' as input\n" );
252c2c66affSColin Finck ok( _putenv(a_very_long_env_string) == 0, "_putenv failed for long environment string\n");
253c2c66affSColin Finck
254c2c66affSColin Finck ok( getenv("nonexistent") == NULL, "getenv should fail with nonexistent var name\n" );
255c2c66affSColin Finck }
256c2c66affSColin Finck
START_TEST(environ)257c2c66affSColin Finck START_TEST(environ)
258c2c66affSColin Finck {
259c2c66affSColin Finck init();
260c2c66affSColin Finck
261c2c66affSColin Finck /* The environ tests should always be run first, as they assume
262c2c66affSColin Finck * that the process has not manipulated the environment. */
263c2c66affSColin Finck test__environ();
264c2c66affSColin Finck test__wenviron();
265c2c66affSColin Finck test_environment_manipulation();
266c2c66affSColin Finck test_system();
267c2c66affSColin Finck }
268