1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | ** File: env.c
|
---|
40 | ** Description: Testing environment variable operations
|
---|
41 | **
|
---|
42 | */
|
---|
43 | #include "prenv.h"
|
---|
44 | #include "plgetopt.h"
|
---|
45 |
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 |
|
---|
49 | PRIntn debug = 0;
|
---|
50 | PRIntn verbose = 0;
|
---|
51 | PRBool failedAlready = PR_FALSE;
|
---|
52 |
|
---|
53 | #define ENVNAME "NSPR_ENVIRONMENT_TEST_VARIABLE"
|
---|
54 | #define ENVVALUE "The expected result"
|
---|
55 | #define ENVBUFSIZE 256
|
---|
56 |
|
---|
57 | char *envBuf; /* buffer pointer. We leak memory here on purpose! */
|
---|
58 |
|
---|
59 | static char * NewBuffer( size_t size )
|
---|
60 | {
|
---|
61 | char *buf = malloc( size );
|
---|
62 | if ( NULL == buf ) {
|
---|
63 | printf("env: NewBuffer() failed\n");
|
---|
64 | exit(1);
|
---|
65 | }
|
---|
66 | return(buf);
|
---|
67 | } /* end NewBuffer() */
|
---|
68 |
|
---|
69 | PRIntn main(PRIntn argc, char *argv[])
|
---|
70 | {
|
---|
71 | char *value;
|
---|
72 | PRStatus rc;
|
---|
73 |
|
---|
74 | { /* Get command line options */
|
---|
75 | PLOptStatus os;
|
---|
76 | PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
|
---|
77 |
|
---|
78 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
79 | {
|
---|
80 | if (PL_OPT_BAD == os) continue;
|
---|
81 | switch (opt->option)
|
---|
82 | {
|
---|
83 | case 'd': /* debug */
|
---|
84 | debug = 1;
|
---|
85 | break;
|
---|
86 | case 'v': /* verbose */
|
---|
87 | verbose = 1;
|
---|
88 | break;
|
---|
89 | default:
|
---|
90 | break;
|
---|
91 | }
|
---|
92 | }
|
---|
93 | PL_DestroyOptState(opt);
|
---|
94 | } /* end block "Get command line options" */
|
---|
95 |
|
---|
96 | #if 0
|
---|
97 | {
|
---|
98 | /*
|
---|
99 | ** This uses Windows native environment manipulation
|
---|
100 | ** as an experiment. Note the separation of namespace!
|
---|
101 | */
|
---|
102 | BOOL rv;
|
---|
103 | DWORD size;
|
---|
104 | rv = SetEnvironmentVariable( ENVNAME, ENVVALUE );
|
---|
105 | if ( rv == 0 ) {
|
---|
106 | if (debug) printf("env: Shit! SetEnvironmentVariable() failed\n");
|
---|
107 | failedAlready = PR_TRUE;
|
---|
108 | }
|
---|
109 | if (verbose) printf("env: SetEnvironmentVariable() worked\n");
|
---|
110 |
|
---|
111 | size = GetEnvironmentVariable( ENVNAME, envBuf, ENVBUFSIZE );
|
---|
112 | if ( size == 0 ) {
|
---|
113 | if (debug) printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf );
|
---|
114 | failedAlready = PR_TRUE;
|
---|
115 | }
|
---|
116 | if (verbose) printf("env: GetEnvironmentVariable() worked. Found: %s\n", envBuf);
|
---|
117 |
|
---|
118 | value = PR_GetEnv( ENVNAME );
|
---|
119 | if ( (NULL == value ) || (strcmp( value, ENVVALUE))) {
|
---|
120 | if (debug) printf( "env: PR_GetEnv() failed retrieving WinNative. Found: %s\n", value);
|
---|
121 | failedAlready = PR_TRUE;
|
---|
122 | }
|
---|
123 | if (verbose) printf("env: PR_GetEnv() worked. Found: %s\n", value);
|
---|
124 | }
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | /* set an environment variable, read it back */
|
---|
128 | envBuf = NewBuffer( ENVBUFSIZE );
|
---|
129 | sprintf( envBuf, ENVNAME "=" ENVVALUE );
|
---|
130 | rc = PR_SetEnv( envBuf );
|
---|
131 | if ( PR_FAILURE == rc ) {
|
---|
132 | if (debug) printf( "env: PR_SetEnv() failed setting\n");
|
---|
133 | failedAlready = PR_TRUE;
|
---|
134 | } else {
|
---|
135 | if (verbose) printf("env: PR_SetEnv() worked.\n");
|
---|
136 | }
|
---|
137 |
|
---|
138 | value = PR_GetEnv( ENVNAME );
|
---|
139 | if ( (NULL == value ) || (strcmp( value, ENVVALUE))) {
|
---|
140 | if (debug) printf( "env: PR_GetEnv() Failed after setting\n" );
|
---|
141 | failedAlready = PR_TRUE;
|
---|
142 | } else {
|
---|
143 | if (verbose) printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value );
|
---|
144 | }
|
---|
145 |
|
---|
146 | /* ---------------------------------------------------------------------- */
|
---|
147 | /* un-set the variable, using RAW name... should not work */
|
---|
148 | envBuf = NewBuffer( ENVBUFSIZE );
|
---|
149 | sprintf( envBuf, ENVNAME );
|
---|
150 | rc = PR_SetEnv( envBuf );
|
---|
151 | if ( PR_FAILURE == rc ) {
|
---|
152 | if (verbose) printf( "env: PR_SetEnv() not un-set using RAW name. Good!\n");
|
---|
153 | } else {
|
---|
154 | if (debug) printf("env: PR_SetEnv() un-set using RAW name. Bad!\n" );
|
---|
155 | failedAlready = PR_TRUE;
|
---|
156 | }
|
---|
157 |
|
---|
158 | value = PR_GetEnv( ENVNAME );
|
---|
159 | if ( NULL == value ) {
|
---|
160 | if (debug) printf("env: PR_GetEnv() after un-set using RAW name. Bad!\n" );
|
---|
161 | failedAlready = PR_TRUE;
|
---|
162 | } else {
|
---|
163 | if (verbose) printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value );
|
---|
164 | }
|
---|
165 |
|
---|
166 | /* ---------------------------------------------------------------------- */
|
---|
167 | /* set it again ... */
|
---|
168 | envBuf = NewBuffer( ENVBUFSIZE );
|
---|
169 | sprintf( envBuf, ENVNAME "=" ENVVALUE );
|
---|
170 | rc = PR_SetEnv( envBuf );
|
---|
171 | if ( PR_FAILURE == rc ) {
|
---|
172 | if (debug) printf( "env: PR_SetEnv() failed setting the second time.\n");
|
---|
173 | failedAlready = PR_TRUE;
|
---|
174 | } else {
|
---|
175 | if (verbose) printf("env: PR_SetEnv() worked.\n");
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* un-set the variable using the form name= */
|
---|
179 | envBuf = NewBuffer( ENVBUFSIZE );
|
---|
180 | sprintf( envBuf, ENVNAME "=" );
|
---|
181 | rc = PR_SetEnv( envBuf );
|
---|
182 | if ( PR_FAILURE == rc ) {
|
---|
183 | if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n");
|
---|
184 | failedAlready = PR_TRUE;
|
---|
185 | } else {
|
---|
186 | if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
|
---|
187 | }
|
---|
188 |
|
---|
189 | value = PR_GetEnv( ENVNAME );
|
---|
190 | if (( NULL == value ) || ( 0x00 == *value )) {
|
---|
191 | if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
|
---|
192 | } else {
|
---|
193 | if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
|
---|
194 | failedAlready = PR_TRUE;
|
---|
195 | }
|
---|
196 | /* ---------------------------------------------------------------------- */
|
---|
197 | /* un-set the variable using the form name= */
|
---|
198 | envBuf = NewBuffer( ENVBUFSIZE );
|
---|
199 | sprintf( envBuf, ENVNAME "999=" );
|
---|
200 | rc = PR_SetEnv( envBuf );
|
---|
201 | if ( PR_FAILURE == rc ) {
|
---|
202 | if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n");
|
---|
203 | failedAlready = PR_TRUE;
|
---|
204 | } else {
|
---|
205 | if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
|
---|
206 | }
|
---|
207 |
|
---|
208 | value = PR_GetEnv( ENVNAME "999" );
|
---|
209 | if (( NULL == value ) || ( 0x00 == *value )) {
|
---|
210 | if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
|
---|
211 | } else {
|
---|
212 | if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
|
---|
213 | failedAlready = PR_TRUE;
|
---|
214 | }
|
---|
215 |
|
---|
216 | /* ---------------------------------------------------------------------- */
|
---|
217 | if (debug || verbose) printf("\n%s\n", (failedAlready)? "FAILED" : "PASSED" );
|
---|
218 | return( (failedAlready)? 1 : 0 );
|
---|
219 | } /* main() */
|
---|
220 |
|
---|
221 | /* env.c */
|
---|