2 * Unit tests for C library environment routines
4 * Copyright 2004 Mike Hearn <mh@codeweavers.com>
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "wine/test.h"
25 static const char *a_very_long_env_string
=
27 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/;"
28 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/;"
29 "/mingw/lib/gcc/mingw32/3.4.2/;"
30 "/usr/lib/gcc/mingw32/3.4.2/;"
31 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/lib/mingw32/3.4.2/;"
32 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/lib/;"
33 "/mingw/mingw32/lib/mingw32/3.4.2/;"
34 "/mingw/mingw32/lib/;"
35 "/mingw/lib/mingw32/3.4.2/;"
37 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../mingw32/3.4.2/;"
38 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../;"
39 "/mingw/lib/mingw32/3.4.2/;"
41 "/lib/mingw32/3.4.2/;"
43 "/usr/lib/mingw32/3.4.2/;"
46 static char ***(__cdecl
*p__p__environ
)(void);
47 static WCHAR
***(__cdecl
*p__p__wenviron
)(void);
48 static void (*p_get_environ
)(char ***);
49 static void (*p_get_wenviron
)(WCHAR
***);
51 static char ***p_environ
;
52 static WCHAR
***p_wenviron
;
54 static void init(void)
56 HMODULE hmod
= GetModuleHandleA("msvcrt.dll");
58 p__p__environ
= (void *)GetProcAddress(hmod
, "__p__environ");
59 p__p__wenviron
= (void *)GetProcAddress(hmod
, "__p__wenviron");
60 p_environ
= (void *)GetProcAddress(hmod
, "_environ");
61 p_wenviron
= (void *)GetProcAddress(hmod
, "_wenviron");
62 p_get_environ
= (void *)GetProcAddress(hmod
, "_get_environ");
63 p_get_wenviron
= (void *)GetProcAddress(hmod
, "_get_wenviron");
66 static void test_system(void)
68 int ret
= system(NULL
);
69 ok(ret
== 1, "Expected system to return 1, got %d\n", ret
);
71 ret
= system("echo OK");
72 ok(ret
== 0, "Expected system to return 0, got %d\n", ret
);
75 static void test__environ(void)
78 char **argv
, **envp
= NULL
;
81 ok( p_environ
!= NULL
, "Expected the pointer to _environ to be non-NULL\n" );
83 ok( *p_environ
!= NULL
, "Expected _environ to be initialized on startup\n" );
85 if (!p_environ
|| !*p_environ
)
87 skip( "_environ pointers are not valid\n" );
91 /* Examine the returned pointer from __p__environ(), if available. */
94 ok( *p__p__environ() == *p_environ
,
95 "Expected _environ pointers to be identical\n" );
98 skip( "__p__environ() is not available\n" );
103 p_get_environ(&retptr
);
104 ok( retptr
== *p_environ
,
105 "Expected _environ pointers to be identical\n" );
108 win_skip( "_get_environ() is not available\n" );
110 /* Note that msvcrt from Windows versions older than Vista
111 * expects the mode pointer parameter to be valid.*/
112 __getmainargs(&argc
, &argv
, &envp
, 0, &mode
);
114 ok( envp
!= NULL
, "Expected initial environment block pointer to be non-NULL\n" );
117 skip( "Initial environment block pointer is not valid\n" );
125 ok( envp
[i
] != NULL
, "Expected environment block pointer element to be non-NULL\n" );
126 ok( !strcmp((*p_environ
)[i
], envp
[i
]),
127 "Expected _environ and environment block pointer strings (%s vs. %s) to match\n",
128 (*p_environ
)[i
], envp
[i
] );
132 ok( !envp
[i
], "Expected environment block pointer element to be NULL, got %p\n", envp
[i
] );
138 static void test__wenviron(void)
140 static const WCHAR cat_eq_dogW
[] = {'c','a','t','=','d','o','g',0};
141 static const WCHAR cat_eqW
[] = {'c','a','t','=',0};
144 char **argv
, **envp
= NULL
;
145 WCHAR
**wargv
, **wenvp
= NULL
;
148 ok( p_wenviron
!= NULL
, "Expected the pointer to _wenviron to be non-NULL\n" );
150 ok( *p_wenviron
== NULL
, "Expected _wenviron to be NULL, got %p\n", *p_wenviron
);
153 win_skip( "Pointer to _wenviron is not valid\n" );
157 /* Examine the returned pointer from __p__wenviron(), if available. */
160 ok( *p__p__wenviron() == NULL
,
161 "Expected _wenviron pointers to be NULL\n" );
164 skip( "__p__wenviron() is not available\n" );
169 p_get_wenviron(&retptr
);
171 "Expected _wenviron pointers to be NULL\n" );
174 win_skip( "_get_wenviron() is not available\n" );
176 /* __getmainargs doesn't initialize _wenviron. */
177 __getmainargs(&argc
, &argv
, &envp
, 0, &mode
);
179 ok( *p_wenviron
== NULL
, "Expected _wenviron to be NULL, got %p\n", *p_wenviron
);
180 ok( envp
!= NULL
, "Expected initial environment block pointer to be non-NULL\n" );
183 skip( "Initial environment block pointer is not valid\n" );
187 /* Neither does calling the non-Unicode environment manipulation functions. */
188 ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
189 ok( *p_wenviron
== NULL
, "Expected _wenviron to be NULL, got %p\n", *p_wenviron
);
190 ok( _putenv("cat=") == 0, "failed deleting cat\n" );
192 /* _wenviron isn't initialized until __wgetmainargs is called or
193 * one of the Unicode environment manipulation functions is called. */
194 ok( _wputenv(cat_eq_dogW
) == 0, "failed setting cat=dog\n" );
195 ok( *p_wenviron
!= NULL
, "Expected _wenviron to be non-NULL\n" );
196 ok( _wputenv(cat_eqW
) == 0, "failed deleting cat\n" );
198 __wgetmainargs(&argc
, &wargv
, &wenvp
, 0, &mode
);
200 ok( *p_wenviron
!= NULL
, "Expected _wenviron to be non-NULL\n" );
201 ok( wenvp
!= NULL
, "Expected initial environment block pointer to be non-NULL\n" );
204 skip( "Initial environment block pointer is not valid\n" );
208 /* Examine the returned pointer from __p__wenviron(),
209 * if available, after _wenviron is initialized. */
212 ok( *p__p__wenviron() == *p_wenviron
,
213 "Expected _wenviron pointers to be identical\n" );
219 p_get_wenviron(&retptr
);
220 ok( retptr
== *p_wenviron
,
221 "Expected _wenviron pointers to be identical\n" );
226 if ((*p_wenviron
)[i
])
228 ok( wenvp
[i
] != NULL
, "Expected environment block pointer element to be non-NULL\n" );
229 ok( !wcscmp((*p_wenviron
)[i
], wenvp
[i
]),
230 "Expected _wenviron and environment block pointer strings (%s vs. %s) to match\n",
231 wine_dbgstr_w((*p_wenviron
)[i
]), wine_dbgstr_w(wenvp
[i
]) );
235 ok( !wenvp
[i
], "Expected environment block pointer element to be NULL, got %p\n", wenvp
[i
] );
241 static void test_environment_manipulation(void)
243 ok( _putenv("cat=") == 0, "_putenv failed on deletion of nonexistent environment variable\n" );
244 ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
245 ok( strcmp(getenv("cat"), "dog") == 0, "getenv did not return 'dog'\n" );
246 ok( _putenv("cat=") == 0, "failed deleting cat\n" );
248 ok( _putenv("=") == -1, "should not accept '=' as input\n" );
249 ok( _putenv("=dog") == -1, "should not accept '=dog' as input\n" );
250 ok( _putenv(a_very_long_env_string
) == 0, "_putenv failed for long environment string\n");
252 ok( getenv("nonexistent") == NULL
, "getenv should fail with nonexistent var name\n" );
259 /* The environ tests should always be run first, as they assume
260 * that the process has not manipulated the environment. */
263 test_environment_manipulation();