1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 ** Description: Testing environment variable operations
22 PRBool failedAlready
= PR_FALSE
;
24 #define ENVNAME "NSPR_ENVIRONMENT_TEST_VARIABLE"
25 #define ENVVALUE "The expected result"
26 #define ENVBUFSIZE 256
28 char *envBuf
; /* buffer pointer. We leak memory here on purpose! */
30 static char * NewBuffer( size_t size
)
32 char *buf
= malloc( size
);
34 printf("env: NewBuffer() failed\n");
38 } /* end NewBuffer() */
40 int main(int argc
, char **argv
)
45 { /* Get command line options */
47 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "vds");
49 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
51 if (PL_OPT_BAD
== os
) {
59 case 'v': /* verbose */
62 case 's': /* secure / set[ug]id */
64 ** To test PR_GetEnvSecure, make this executable (or a
65 ** copy of it) setuid / setgid / otherwise inherently
66 ** privileged (e.g., file capabilities) and run it
75 PL_DestroyOptState(opt
);
76 } /* end block "Get command line options" */
81 ** This uses Windows native environment manipulation
82 ** as an experiment. Note the separation of namespace!
86 rv
= SetEnvironmentVariable( ENVNAME
, ENVVALUE
);
89 printf("env: Shit! SetEnvironmentVariable() failed\n");
91 failedAlready
= PR_TRUE
;
94 printf("env: SetEnvironmentVariable() worked\n");
97 size
= GetEnvironmentVariable( ENVNAME
, envBuf
, ENVBUFSIZE
);
100 printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf
);
102 failedAlready
= PR_TRUE
;
105 printf("env: GetEnvironmentVariable() worked. Found: %s\n", envBuf
);
108 value
= PR_GetEnv( ENVNAME
);
109 if ( (NULL
== value
) || (strcmp( value
, ENVVALUE
))) {
111 printf( "env: PR_GetEnv() failed retrieving WinNative. Found: %s\n", value
);
113 failedAlready
= PR_TRUE
;
116 printf("env: PR_GetEnv() worked. Found: %s\n", value
);
121 /* set an environment variable, read it back */
122 envBuf
= NewBuffer( ENVBUFSIZE
);
123 sprintf( envBuf
, ENVNAME
"=" ENVVALUE
);
124 rc
= PR_SetEnv( envBuf
);
125 if ( PR_FAILURE
== rc
) {
127 printf( "env: PR_SetEnv() failed setting\n");
129 failedAlready
= PR_TRUE
;
132 printf("env: PR_SetEnv() worked.\n");
136 value
= PR_GetEnv( ENVNAME
);
137 if ( (NULL
== value
) || (strcmp( value
, ENVVALUE
))) {
139 printf( "env: PR_GetEnv() Failed after setting\n" );
141 failedAlready
= PR_TRUE
;
144 printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value
);
150 ** In this case we've been run with elevated privileges, so
151 ** test that PR_GetEnvSecure *doesn't* find that env var.
153 value
= PR_GetEnvSecure( ENVNAME
);
154 if ( NULL
!= value
) {
156 printf( "env: PR_GetEnvSecure() failed; expected NULL, found \"%s\"\n", value
);
158 failedAlready
= PR_TRUE
;
161 printf("env: PR_GetEnvSecure() worked\n" );
166 ** In this case the program is being run normally, so do the
167 ** same check for PR_GetEnvSecure as for PR_GetEnv.
169 value
= PR_GetEnvSecure( ENVNAME
);
170 if ( (NULL
== value
) || (strcmp( value
, ENVVALUE
))) {
172 printf( "env: PR_GetEnvSecure() Failed after setting\n" );
174 failedAlready
= PR_TRUE
;
177 printf("env: PR_GetEnvSecure() worked after setting it. Found: %s\n", value
);
182 /* ---------------------------------------------------------------------- */
183 /* check that PR_DuplicateEnvironment() agrees with PR_GetEnv() */
185 #if defined(XP_UNIX) && (!defined(DARWIN) || defined(HAVE_CRT_EXTERNS_H))
186 static const PRBool expect_failure
= PR_FALSE
;
188 static const PRBool expect_failure
= PR_TRUE
;
190 char **i
, **dupenv
= PR_DuplicateEnvironment();
193 if ( NULL
== dupenv
) {
194 if (expect_failure
) {
195 if (verbose
) printf("env: PR_DuplicateEnvironment failed, "
196 "as expected on this platform.\n");
199 printf("env: PR_DuplicateEnvironment() failed.\n");
201 failedAlready
= PR_TRUE
;
206 if (expect_failure
) {
207 if (debug
) printf("env: PR_DuplicateEnvironment() succeeded, "
208 "but failure is expected on this platform.\n");
209 failedAlready
= PR_TRUE
;
212 printf("env: PR_DuplicateEnvironment() succeeded.\n");
215 for (i
= dupenv
; *i
; i
++) {
216 char *equals
= strchr(*i
, '=');
218 if ( equals
== NULL
) {
219 if (debug
) printf("env: PR_DuplicateEnvironment() returned a string"
220 " with no '=': %s\n", *i
);
221 failedAlready
= PR_TRUE
;
223 /* We own this string, so we can temporarily alter it */
224 /* *i is the null-terminated name; equals + 1 is the value */
227 if ( strcmp(*i
, ENVNAME
) == 0) {
229 if (verbose
) printf("env: PR_DuplicateEnvironment() found " ENVNAME
230 " (%u so far).\n", found
);
233 /* Multiple values for the same name can't happen, according to POSIX. */
234 value
= PR_GetEnv(*i
);
235 if ( value
== NULL
) {
236 if (debug
) printf("env: PR_DuplicateEnvironment() returned a name"
237 " which PR_GetEnv() failed to find: %s\n", *i
);
238 failedAlready
= PR_TRUE
;
239 } else if ( strcmp(equals
+ 1, value
) != 0) {
240 if (debug
) printf("env: PR_DuplicateEnvironment() returned the wrong"
241 " value for %s: expected %s; found %s\n",
242 *i
, value
, equals
+ 1);
243 failedAlready
= PR_TRUE
;
245 if (verbose
) printf("env: PR_DuplicateEnvironment() agreed with"
246 " PR_GetEnv() about %s\n", *i
);
254 if (debug
) printf("env: PR_DuplicateEnvironment() found %u entries for " ENVNAME
255 " (expected 1)\n", found
);
256 failedAlready
= PR_TRUE
;
259 printf("env: PR_DuplicateEnvironment() found 1 entry for " ENVNAME
"\n");
265 /* ---------------------------------------------------------------------- */
266 /* un-set the variable, using RAW name... should not work */
267 envBuf
= NewBuffer( ENVBUFSIZE
);
268 sprintf( envBuf
, ENVNAME
);
269 rc
= PR_SetEnv( envBuf
);
270 if ( PR_FAILURE
== rc
) {
272 printf( "env: PR_SetEnv() not un-set using RAW name. Good!\n");
276 printf("env: PR_SetEnv() un-set using RAW name. Bad!\n" );
278 failedAlready
= PR_TRUE
;
281 value
= PR_GetEnv( ENVNAME
);
282 if ( NULL
== value
) {
284 printf("env: PR_GetEnv() after un-set using RAW name. Bad!\n" );
286 failedAlready
= PR_TRUE
;
289 printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value
);
293 /* ---------------------------------------------------------------------- */
294 /* set it again ... */
295 envBuf
= NewBuffer( ENVBUFSIZE
);
296 sprintf( envBuf
, ENVNAME
"=" ENVVALUE
);
297 rc
= PR_SetEnv( envBuf
);
298 if ( PR_FAILURE
== rc
) {
300 printf( "env: PR_SetEnv() failed setting the second time.\n");
302 failedAlready
= PR_TRUE
;
305 printf("env: PR_SetEnv() worked.\n");
309 /* un-set the variable using the form name= */
310 envBuf
= NewBuffer( ENVBUFSIZE
);
311 sprintf( envBuf
, ENVNAME
"=" );
312 rc
= PR_SetEnv( envBuf
);
313 if ( PR_FAILURE
== rc
) {
315 printf( "env: PR_SetEnv() failed un-setting using name=\n");
317 failedAlready
= PR_TRUE
;
320 printf("env: PR_SetEnv() un-set using name= worked\n" );
324 value
= PR_GetEnv( ENVNAME
);
325 if (( NULL
== value
) || ( 0x00 == *value
)) {
327 printf("env: PR_GetEnv() after un-set using name= worked\n" );
331 printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value
);
333 failedAlready
= PR_TRUE
;
335 /* ---------------------------------------------------------------------- */
336 /* un-set the variable using the form name= */
337 envBuf
= NewBuffer( ENVBUFSIZE
);
338 sprintf( envBuf
, ENVNAME
"999=" );
339 rc
= PR_SetEnv( envBuf
);
340 if ( PR_FAILURE
== rc
) {
342 printf( "env: PR_SetEnv() failed un-setting using name=\n");
344 failedAlready
= PR_TRUE
;
347 printf("env: PR_SetEnv() un-set using name= worked\n" );
351 value
= PR_GetEnv( ENVNAME
"999" );
352 if (( NULL
== value
) || ( 0x00 == *value
)) {
354 printf("env: PR_GetEnv() after un-set using name= worked\n" );
358 printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value
);
360 failedAlready
= PR_TRUE
;
363 /* ---------------------------------------------------------------------- */
364 if (debug
|| verbose
) {
365 printf("\n%s\n", (failedAlready
)? "FAILED" : "PASSED" );
367 return( (failedAlready
)? 1 : 0 );