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
20 PRBool failedAlready
= PR_FALSE
;
22 #define ENVNAME "NSPR_ENVIRONMENT_TEST_VARIABLE"
23 #define ENVVALUE "The expected result"
24 #define ENVBUFSIZE 256
26 char *envBuf
; /* buffer pointer. We leak memory here on purpose! */
28 static char * NewBuffer( size_t size
)
30 char *buf
= malloc( size
);
32 printf("env: NewBuffer() failed\n");
36 } /* end NewBuffer() */
38 int main(int argc
, char **argv
)
43 { /* Get command line options */
45 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "vd");
47 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
49 if (PL_OPT_BAD
== os
) continue;
55 case 'v': /* verbose */
62 PL_DestroyOptState(opt
);
63 } /* end block "Get command line options" */
68 ** This uses Windows native environment manipulation
69 ** as an experiment. Note the separation of namespace!
73 rv
= SetEnvironmentVariable( ENVNAME
, ENVVALUE
);
75 if (debug
) printf("env: Shit! SetEnvironmentVariable() failed\n");
76 failedAlready
= PR_TRUE
;
78 if (verbose
) printf("env: SetEnvironmentVariable() worked\n");
80 size
= GetEnvironmentVariable( ENVNAME
, envBuf
, ENVBUFSIZE
);
82 if (debug
) printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf
);
83 failedAlready
= PR_TRUE
;
85 if (verbose
) printf("env: GetEnvironmentVariable() worked. Found: %s\n", envBuf
);
87 value
= PR_GetEnv( ENVNAME
);
88 if ( (NULL
== value
) || (strcmp( value
, ENVVALUE
))) {
89 if (debug
) printf( "env: PR_GetEnv() failed retrieving WinNative. Found: %s\n", value
);
90 failedAlready
= PR_TRUE
;
92 if (verbose
) printf("env: PR_GetEnv() worked. Found: %s\n", value
);
96 /* set an environment variable, read it back */
97 envBuf
= NewBuffer( ENVBUFSIZE
);
98 sprintf( envBuf
, ENVNAME
"=" ENVVALUE
);
99 rc
= PR_SetEnv( envBuf
);
100 if ( PR_FAILURE
== rc
) {
101 if (debug
) printf( "env: PR_SetEnv() failed setting\n");
102 failedAlready
= PR_TRUE
;
104 if (verbose
) printf("env: PR_SetEnv() worked.\n");
107 value
= PR_GetEnv( ENVNAME
);
108 if ( (NULL
== value
) || (strcmp( value
, ENVVALUE
))) {
109 if (debug
) printf( "env: PR_GetEnv() Failed after setting\n" );
110 failedAlready
= PR_TRUE
;
112 if (verbose
) printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value
);
115 /* ---------------------------------------------------------------------- */
116 /* un-set the variable, using RAW name... should not work */
117 envBuf
= NewBuffer( ENVBUFSIZE
);
118 sprintf( envBuf
, ENVNAME
);
119 rc
= PR_SetEnv( envBuf
);
120 if ( PR_FAILURE
== rc
) {
121 if (verbose
) printf( "env: PR_SetEnv() not un-set using RAW name. Good!\n");
123 if (debug
) printf("env: PR_SetEnv() un-set using RAW name. Bad!\n" );
124 failedAlready
= PR_TRUE
;
127 value
= PR_GetEnv( ENVNAME
);
128 if ( NULL
== value
) {
129 if (debug
) printf("env: PR_GetEnv() after un-set using RAW name. Bad!\n" );
130 failedAlready
= PR_TRUE
;
132 if (verbose
) printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value
);
135 /* ---------------------------------------------------------------------- */
136 /* set it again ... */
137 envBuf
= NewBuffer( ENVBUFSIZE
);
138 sprintf( envBuf
, ENVNAME
"=" ENVVALUE
);
139 rc
= PR_SetEnv( envBuf
);
140 if ( PR_FAILURE
== rc
) {
141 if (debug
) printf( "env: PR_SetEnv() failed setting the second time.\n");
142 failedAlready
= PR_TRUE
;
144 if (verbose
) printf("env: PR_SetEnv() worked.\n");
147 /* un-set the variable using the form name= */
148 envBuf
= NewBuffer( ENVBUFSIZE
);
149 sprintf( envBuf
, ENVNAME
"=" );
150 rc
= PR_SetEnv( envBuf
);
151 if ( PR_FAILURE
== rc
) {
152 if (debug
) printf( "env: PR_SetEnv() failed un-setting using name=\n");
153 failedAlready
= PR_TRUE
;
155 if (verbose
) printf("env: PR_SetEnv() un-set using name= worked\n" );
158 value
= PR_GetEnv( ENVNAME
);
159 if (( NULL
== value
) || ( 0x00 == *value
)) {
160 if (verbose
) printf("env: PR_GetEnv() after un-set using name= worked\n" );
162 if (debug
) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value
);
163 failedAlready
= PR_TRUE
;
165 /* ---------------------------------------------------------------------- */
166 /* un-set the variable using the form name= */
167 envBuf
= NewBuffer( ENVBUFSIZE
);
168 sprintf( envBuf
, ENVNAME
"999=" );
169 rc
= PR_SetEnv( envBuf
);
170 if ( PR_FAILURE
== rc
) {
171 if (debug
) printf( "env: PR_SetEnv() failed un-setting using name=\n");
172 failedAlready
= PR_TRUE
;
174 if (verbose
) printf("env: PR_SetEnv() un-set using name= worked\n" );
177 value
= PR_GetEnv( ENVNAME
"999" );
178 if (( NULL
== value
) || ( 0x00 == *value
)) {
179 if (verbose
) printf("env: PR_GetEnv() after un-set using name= worked\n" );
181 if (debug
) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value
);
182 failedAlready
= PR_TRUE
;
185 /* ---------------------------------------------------------------------- */
186 if (debug
|| verbose
) printf("\n%s\n", (failedAlready
)? "FAILED" : "PASSED" );
187 return( (failedAlready
)? 1 : 0 );