nsprpub/pr/tests/env.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 ** File: env.c
michael@0 8 ** Description: Testing environment variable operations
michael@0 9 **
michael@0 10 */
michael@0 11 #include "prenv.h"
michael@0 12 #include "plgetopt.h"
michael@0 13
michael@0 14 #include <stdio.h>
michael@0 15 #include <stdlib.h>
michael@0 16 #include <string.h>
michael@0 17
michael@0 18 PRIntn debug = 0;
michael@0 19 PRIntn verbose = 0;
michael@0 20 PRBool failedAlready = PR_FALSE;
michael@0 21
michael@0 22 #define ENVNAME "NSPR_ENVIRONMENT_TEST_VARIABLE"
michael@0 23 #define ENVVALUE "The expected result"
michael@0 24 #define ENVBUFSIZE 256
michael@0 25
michael@0 26 char *envBuf; /* buffer pointer. We leak memory here on purpose! */
michael@0 27
michael@0 28 static char * NewBuffer( size_t size )
michael@0 29 {
michael@0 30 char *buf = malloc( size );
michael@0 31 if ( NULL == buf ) {
michael@0 32 printf("env: NewBuffer() failed\n");
michael@0 33 exit(1);
michael@0 34 }
michael@0 35 return(buf);
michael@0 36 } /* end NewBuffer() */
michael@0 37
michael@0 38 int main(int argc, char **argv)
michael@0 39 {
michael@0 40 char *value;
michael@0 41 PRStatus rc;
michael@0 42
michael@0 43 { /* Get command line options */
michael@0 44 PLOptStatus os;
michael@0 45 PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
michael@0 46
michael@0 47 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 48 {
michael@0 49 if (PL_OPT_BAD == os) continue;
michael@0 50 switch (opt->option)
michael@0 51 {
michael@0 52 case 'd': /* debug */
michael@0 53 debug = 1;
michael@0 54 break;
michael@0 55 case 'v': /* verbose */
michael@0 56 verbose = 1;
michael@0 57 break;
michael@0 58 default:
michael@0 59 break;
michael@0 60 }
michael@0 61 }
michael@0 62 PL_DestroyOptState(opt);
michael@0 63 } /* end block "Get command line options" */
michael@0 64
michael@0 65 #if 0
michael@0 66 {
michael@0 67 /*
michael@0 68 ** This uses Windows native environment manipulation
michael@0 69 ** as an experiment. Note the separation of namespace!
michael@0 70 */
michael@0 71 BOOL rv;
michael@0 72 DWORD size;
michael@0 73 rv = SetEnvironmentVariable( ENVNAME, ENVVALUE );
michael@0 74 if ( rv == 0 ) {
michael@0 75 if (debug) printf("env: Shit! SetEnvironmentVariable() failed\n");
michael@0 76 failedAlready = PR_TRUE;
michael@0 77 }
michael@0 78 if (verbose) printf("env: SetEnvironmentVariable() worked\n");
michael@0 79
michael@0 80 size = GetEnvironmentVariable( ENVNAME, envBuf, ENVBUFSIZE );
michael@0 81 if ( size == 0 ) {
michael@0 82 if (debug) printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf );
michael@0 83 failedAlready = PR_TRUE;
michael@0 84 }
michael@0 85 if (verbose) printf("env: GetEnvironmentVariable() worked. Found: %s\n", envBuf);
michael@0 86
michael@0 87 value = PR_GetEnv( ENVNAME );
michael@0 88 if ( (NULL == value ) || (strcmp( value, ENVVALUE))) {
michael@0 89 if (debug) printf( "env: PR_GetEnv() failed retrieving WinNative. Found: %s\n", value);
michael@0 90 failedAlready = PR_TRUE;
michael@0 91 }
michael@0 92 if (verbose) printf("env: PR_GetEnv() worked. Found: %s\n", value);
michael@0 93 }
michael@0 94 #endif
michael@0 95
michael@0 96 /* set an environment variable, read it back */
michael@0 97 envBuf = NewBuffer( ENVBUFSIZE );
michael@0 98 sprintf( envBuf, ENVNAME "=" ENVVALUE );
michael@0 99 rc = PR_SetEnv( envBuf );
michael@0 100 if ( PR_FAILURE == rc ) {
michael@0 101 if (debug) printf( "env: PR_SetEnv() failed setting\n");
michael@0 102 failedAlready = PR_TRUE;
michael@0 103 } else {
michael@0 104 if (verbose) printf("env: PR_SetEnv() worked.\n");
michael@0 105 }
michael@0 106
michael@0 107 value = PR_GetEnv( ENVNAME );
michael@0 108 if ( (NULL == value ) || (strcmp( value, ENVVALUE))) {
michael@0 109 if (debug) printf( "env: PR_GetEnv() Failed after setting\n" );
michael@0 110 failedAlready = PR_TRUE;
michael@0 111 } else {
michael@0 112 if (verbose) printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value );
michael@0 113 }
michael@0 114
michael@0 115 /* ---------------------------------------------------------------------- */
michael@0 116 /* un-set the variable, using RAW name... should not work */
michael@0 117 envBuf = NewBuffer( ENVBUFSIZE );
michael@0 118 sprintf( envBuf, ENVNAME );
michael@0 119 rc = PR_SetEnv( envBuf );
michael@0 120 if ( PR_FAILURE == rc ) {
michael@0 121 if (verbose) printf( "env: PR_SetEnv() not un-set using RAW name. Good!\n");
michael@0 122 } else {
michael@0 123 if (debug) printf("env: PR_SetEnv() un-set using RAW name. Bad!\n" );
michael@0 124 failedAlready = PR_TRUE;
michael@0 125 }
michael@0 126
michael@0 127 value = PR_GetEnv( ENVNAME );
michael@0 128 if ( NULL == value ) {
michael@0 129 if (debug) printf("env: PR_GetEnv() after un-set using RAW name. Bad!\n" );
michael@0 130 failedAlready = PR_TRUE;
michael@0 131 } else {
michael@0 132 if (verbose) printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value );
michael@0 133 }
michael@0 134
michael@0 135 /* ---------------------------------------------------------------------- */
michael@0 136 /* set it again ... */
michael@0 137 envBuf = NewBuffer( ENVBUFSIZE );
michael@0 138 sprintf( envBuf, ENVNAME "=" ENVVALUE );
michael@0 139 rc = PR_SetEnv( envBuf );
michael@0 140 if ( PR_FAILURE == rc ) {
michael@0 141 if (debug) printf( "env: PR_SetEnv() failed setting the second time.\n");
michael@0 142 failedAlready = PR_TRUE;
michael@0 143 } else {
michael@0 144 if (verbose) printf("env: PR_SetEnv() worked.\n");
michael@0 145 }
michael@0 146
michael@0 147 /* un-set the variable using the form name= */
michael@0 148 envBuf = NewBuffer( ENVBUFSIZE );
michael@0 149 sprintf( envBuf, ENVNAME "=" );
michael@0 150 rc = PR_SetEnv( envBuf );
michael@0 151 if ( PR_FAILURE == rc ) {
michael@0 152 if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n");
michael@0 153 failedAlready = PR_TRUE;
michael@0 154 } else {
michael@0 155 if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
michael@0 156 }
michael@0 157
michael@0 158 value = PR_GetEnv( ENVNAME );
michael@0 159 if (( NULL == value ) || ( 0x00 == *value )) {
michael@0 160 if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
michael@0 161 } else {
michael@0 162 if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
michael@0 163 failedAlready = PR_TRUE;
michael@0 164 }
michael@0 165 /* ---------------------------------------------------------------------- */
michael@0 166 /* un-set the variable using the form name= */
michael@0 167 envBuf = NewBuffer( ENVBUFSIZE );
michael@0 168 sprintf( envBuf, ENVNAME "999=" );
michael@0 169 rc = PR_SetEnv( envBuf );
michael@0 170 if ( PR_FAILURE == rc ) {
michael@0 171 if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n");
michael@0 172 failedAlready = PR_TRUE;
michael@0 173 } else {
michael@0 174 if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
michael@0 175 }
michael@0 176
michael@0 177 value = PR_GetEnv( ENVNAME "999" );
michael@0 178 if (( NULL == value ) || ( 0x00 == *value )) {
michael@0 179 if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
michael@0 180 } else {
michael@0 181 if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
michael@0 182 failedAlready = PR_TRUE;
michael@0 183 }
michael@0 184
michael@0 185 /* ---------------------------------------------------------------------- */
michael@0 186 if (debug || verbose) printf("\n%s\n", (failedAlready)? "FAILED" : "PASSED" );
michael@0 187 return( (failedAlready)? 1 : 0 );
michael@0 188 } /* main() */
michael@0 189
michael@0 190 /* env.c */

mercurial