makefiles: Explicitly create destination dirs when installing symlinks.
[wine/zf.git] / dlls / msvcrt / tests / environ.c
blob6829d80e953fab5720eb555c9532e5814dfead99
1 /*
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"
22 #include <stdlib.h>
23 #include <process.h>
25 static const char *a_very_long_env_string =
26 "LIBRARY_PATH="
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/;"
36 "/mingw/lib/;"
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/;"
40 "/mingw/lib/;"
41 "/lib/mingw32/3.4.2/;"
42 "/lib/;"
43 "/usr/lib/mingw32/3.4.2/;"
44 "/usr/lib/";
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)
77 int argc;
78 char **argv, **envp = NULL;
79 int i, mode = 0;
81 ok( p_environ != NULL, "Expected the pointer to _environ to be non-NULL\n" );
82 if (p_environ)
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" );
88 return;
91 /* Examine the returned pointer from __p__environ(), if available. */
92 if (p__p__environ)
94 ok( *p__p__environ() == *p_environ,
95 "Expected _environ pointers to be identical\n" );
97 else
98 skip( "__p__environ() is not available\n" );
100 if (p_get_environ)
102 char **retptr;
103 p_get_environ(&retptr);
104 ok( retptr == *p_environ,
105 "Expected _environ pointers to be identical\n" );
107 else
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" );
115 if (!envp)
117 skip( "Initial environment block pointer is not valid\n" );
118 return;
121 for (i = 0; ; i++)
123 if ((*p_environ)[i])
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] );
130 else
132 ok( !envp[i], "Expected environment block pointer element to be NULL, got %p\n", envp[i] );
133 break;
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};
143 int argc;
144 char **argv, **envp = NULL;
145 WCHAR **wargv, **wenvp = NULL;
146 int i, mode = 0;
148 ok( p_wenviron != NULL, "Expected the pointer to _wenviron to be non-NULL\n" );
149 if (p_wenviron)
150 ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron );
151 else
153 win_skip( "Pointer to _wenviron is not valid\n" );
154 return;
157 /* Examine the returned pointer from __p__wenviron(), if available. */
158 if (p__p__wenviron)
160 ok( *p__p__wenviron() == NULL,
161 "Expected _wenviron pointers to be NULL\n" );
163 else
164 skip( "__p__wenviron() is not available\n" );
166 if (p_get_wenviron)
168 WCHAR **retptr;
169 p_get_wenviron(&retptr);
170 ok( retptr == NULL,
171 "Expected _wenviron pointers to be NULL\n" );
173 else
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" );
181 if (!envp)
183 skip( "Initial environment block pointer is not valid\n" );
184 return;
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" );
202 if (!wenvp)
204 skip( "Initial environment block pointer is not valid\n" );
205 return;
208 /* Examine the returned pointer from __p__wenviron(),
209 * if available, after _wenviron is initialized. */
210 if (p__p__wenviron)
212 ok( *p__p__wenviron() == *p_wenviron,
213 "Expected _wenviron pointers to be identical\n" );
216 if (p_get_wenviron)
218 WCHAR **retptr;
219 p_get_wenviron(&retptr);
220 ok( retptr == *p_wenviron,
221 "Expected _wenviron pointers to be identical\n" );
224 for (i = 0; ; i++)
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]) );
233 else
235 ok( !wenvp[i], "Expected environment block pointer element to be NULL, got %p\n", wenvp[i] );
236 break;
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" );
255 START_TEST(environ)
257 init();
259 /* The environ tests should always be run first, as they assume
260 * that the process has not manipulated the environment. */
261 test__environ();
262 test__wenviron();
263 test_environment_manipulation();
264 test_system();