Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / msvcrt / tests / environ.c
blob9e1263a0f74cfd3112ad7b8a77b2275c86353c8d
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>
24 static const char *a_very_long_env_string =
25 "LIBRARY_PATH="
26 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/;"
27 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/;"
28 "/mingw/lib/gcc/mingw32/3.4.2/;"
29 "/usr/lib/gcc/mingw32/3.4.2/;"
30 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/lib/mingw32/3.4.2/;"
31 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/lib/;"
32 "/mingw/mingw32/lib/mingw32/3.4.2/;"
33 "/mingw/mingw32/lib/;"
34 "/mingw/lib/mingw32/3.4.2/;"
35 "/mingw/lib/;"
36 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../mingw32/3.4.2/;"
37 "C:/Program Files/GLBasic/Compiler/platform/Win32/Bin/../lib/gcc/mingw32/3.4.2/../../../;"
38 "/mingw/lib/mingw32/3.4.2/;"
39 "/mingw/lib/;"
40 "/lib/mingw32/3.4.2/;"
41 "/lib/;"
42 "/usr/lib/mingw32/3.4.2/;"
43 "/usr/lib/";
45 void __cdecl __getmainargs(int *, char ***, char ***, int, int *);
46 void __cdecl __wgetmainargs(int *, wchar_t ***, wchar_t ***, int, int *);
48 static char ***(__cdecl *p__p__environ)(void);
49 static WCHAR ***(__cdecl *p__p__wenviron)(void);
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");
64 static void test_system(void)
66 int ret = system(NULL);
67 ok(ret == 1, "Expected system to return 1, got %d\n", ret);
69 ret = system("echo OK");
70 ok(ret == 0, "Expected system to return 0, got %d\n", ret);
73 static void test__environ(void)
75 int argc;
76 char **argv, **envp = NULL;
77 int i, mode = 0;
79 ok( p_environ != NULL, "Expected the pointer to _environ to be non-NULL\n" );
80 if (p_environ)
81 ok( *p_environ != NULL, "Expected _environ to be initialized on startup\n" );
83 if (!p_environ || !*p_environ)
85 skip( "_environ pointers are not valid\n" );
86 return;
89 /* Examine the returned pointer from __p__environ(), if available. */
90 if (p__p__environ)
92 ok( *p__p__environ() == *p_environ,
93 "Expected _environ pointers to be identical\n" );
95 else
96 skip( "__p__environ() is not available\n" );
98 /* Note that msvcrt from Windows versions older than Vista
99 * expects the mode pointer parameter to be valid.*/
100 __getmainargs(&argc, &argv, &envp, 0, &mode);
102 ok( envp != NULL, "Expected initial environment block pointer to be non-NULL\n" );
103 if (!envp)
105 skip( "Initial environment block pointer is not valid\n" );
106 return;
109 for (i = 0; ; i++)
111 if ((*p_environ)[i])
113 ok( envp[i] != NULL, "Expected environment block pointer element to be non-NULL\n" );
114 ok( !strcmp((*p_environ)[i], envp[i]),
115 "Expected _environ and environment block pointer strings (%s vs. %s) to match\n",
116 (*p_environ)[i], envp[i] );
118 else
120 ok( !envp[i], "Expected environment block pointer element to be NULL, got %p\n", envp[i] );
121 break;
126 static void test__wenviron(void)
128 static const WCHAR cat_eq_dogW[] = {'c','a','t','=','d','o','g',0};
129 static const WCHAR cat_eqW[] = {'c','a','t','=',0};
131 int argc;
132 char **argv, **envp = NULL;
133 WCHAR **wargv, **wenvp = NULL;
134 int i, mode = 0;
136 ok( p_wenviron != NULL, "Expected the pointer to _wenviron to be non-NULL\n" );
137 if (p_wenviron)
138 ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron );
139 else
141 skip( "Pointer to _wenviron is not valid\n" );
142 return;
145 /* Examine the returned pointer from __p__wenviron(), if available. */
146 if (p__p__wenviron)
148 ok( *p__p__wenviron() == NULL,
149 "Expected _wenviron pointers to be NULL\n" );
151 else
152 skip( "__p__wenviron() is not available\n" );
154 /* __getmainargs doesn't initialize _wenviron. */
155 __getmainargs(&argc, &argv, &envp, 0, &mode);
157 ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron);
158 ok( envp != NULL, "Expected initial environment block pointer to be non-NULL\n" );
159 if (!envp)
161 skip( "Initial environment block pointer is not valid\n" );
162 return;
165 /* Neither does calling the non-Unicode environment manipulation functions. */
166 ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
167 ok( *p_wenviron == NULL, "Expected _wenviron to be NULL, got %p\n", *p_wenviron);
168 ok( _putenv("cat=") == 0, "failed deleting cat\n" );
170 /* _wenviron isn't initialized until __wgetmainargs is called or
171 * one of the Unicode environment manipulation functions is called. */
172 ok( _wputenv(cat_eq_dogW) == 0, "failed setting cat=dog\n" );
173 ok( *p_wenviron != NULL, "Expected _wenviron to be non-NULL\n" );
174 ok( _wputenv(cat_eqW) == 0, "failed deleting cat\n" );
176 __wgetmainargs(&argc, &wargv, &wenvp, 0, &mode);
178 ok( *p_wenviron != NULL, "Expected _wenviron to be non-NULL\n" );
179 ok( wenvp != NULL, "Expected initial environment block pointer to be non-NULL\n" );
180 if (!wenvp)
182 skip( "Initial environment block pointer is not valid\n" );
183 return;
186 /* Examine the returned pointer from __p__wenviron(),
187 * if available, after _wenviron is initialized. */
188 if (p__p__wenviron)
190 ok( *p__p__wenviron() == *p_wenviron,
191 "Expected _wenviron pointers to be identical\n" );
194 for (i = 0; ; i++)
196 if ((*p_wenviron)[i])
198 ok( wenvp[i] != NULL, "Expected environment block pointer element to be non-NULL\n" );
199 ok( !winetest_strcmpW((*p_wenviron)[i], wenvp[i]),
200 "Expected _wenviron and environment block pointer strings (%s vs. %s) to match\n",
201 wine_dbgstr_w((*p_wenviron)[i]), wine_dbgstr_w(wenvp[i]) );
203 else
205 ok( !wenvp[i], "Expected environment block pointer element to be NULL, got %p\n", wenvp[i] );
206 break;
211 static void test_environment_manipulation(void)
213 ok( _putenv("cat=") == 0, "_putenv failed on deletion of nonexistent environment variable\n" );
214 ok( _putenv("cat=dog") == 0, "failed setting cat=dog\n" );
215 ok( strcmp(getenv("cat"), "dog") == 0, "getenv did not return 'dog'\n" );
216 ok( _putenv("cat=") == 0, "failed deleting cat\n" );
218 ok( _putenv("=") == -1, "should not accept '=' as input\n" );
219 ok( _putenv("=dog") == -1, "should not accept '=dog' as input\n" );
220 ok( _putenv(a_very_long_env_string) == 0, "_putenv failed for long environment string\n");
222 ok( getenv("nonexistent") == NULL, "getenv should fail with nonexistent var name\n" );
225 START_TEST(environ)
227 init();
229 /* The environ tests should always be run first, as they assume
230 * that the process has not manipulated the environment. */
231 test__environ();
232 test__wenviron();
233 test_environment_manipulation();
234 test_system();