michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** File: env.c michael@0: ** Description: Testing environment variable operations michael@0: ** michael@0: */ michael@0: #include "prenv.h" michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: PRIntn debug = 0; michael@0: PRIntn verbose = 0; michael@0: PRBool failedAlready = PR_FALSE; michael@0: michael@0: #define ENVNAME "NSPR_ENVIRONMENT_TEST_VARIABLE" michael@0: #define ENVVALUE "The expected result" michael@0: #define ENVBUFSIZE 256 michael@0: michael@0: char *envBuf; /* buffer pointer. We leak memory here on purpose! */ michael@0: michael@0: static char * NewBuffer( size_t size ) michael@0: { michael@0: char *buf = malloc( size ); michael@0: if ( NULL == buf ) { michael@0: printf("env: NewBuffer() failed\n"); michael@0: exit(1); michael@0: } michael@0: return(buf); michael@0: } /* end NewBuffer() */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: char *value; michael@0: PRStatus rc; michael@0: michael@0: { /* Get command line options */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "vd"); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug */ michael@0: debug = 1; michael@0: break; michael@0: case 'v': /* verbose */ michael@0: verbose = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: } /* end block "Get command line options" */ michael@0: michael@0: #if 0 michael@0: { michael@0: /* michael@0: ** This uses Windows native environment manipulation michael@0: ** as an experiment. Note the separation of namespace! michael@0: */ michael@0: BOOL rv; michael@0: DWORD size; michael@0: rv = SetEnvironmentVariable( ENVNAME, ENVVALUE ); michael@0: if ( rv == 0 ) { michael@0: if (debug) printf("env: Shit! SetEnvironmentVariable() failed\n"); michael@0: failedAlready = PR_TRUE; michael@0: } michael@0: if (verbose) printf("env: SetEnvironmentVariable() worked\n"); michael@0: michael@0: size = GetEnvironmentVariable( ENVNAME, envBuf, ENVBUFSIZE ); michael@0: if ( size == 0 ) { michael@0: if (debug) printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf ); michael@0: failedAlready = PR_TRUE; michael@0: } michael@0: if (verbose) printf("env: GetEnvironmentVariable() worked. Found: %s\n", envBuf); michael@0: michael@0: value = PR_GetEnv( ENVNAME ); michael@0: if ( (NULL == value ) || (strcmp( value, ENVVALUE))) { michael@0: if (debug) printf( "env: PR_GetEnv() failed retrieving WinNative. Found: %s\n", value); michael@0: failedAlready = PR_TRUE; michael@0: } michael@0: if (verbose) printf("env: PR_GetEnv() worked. Found: %s\n", value); michael@0: } michael@0: #endif michael@0: michael@0: /* set an environment variable, read it back */ michael@0: envBuf = NewBuffer( ENVBUFSIZE ); michael@0: sprintf( envBuf, ENVNAME "=" ENVVALUE ); michael@0: rc = PR_SetEnv( envBuf ); michael@0: if ( PR_FAILURE == rc ) { michael@0: if (debug) printf( "env: PR_SetEnv() failed setting\n"); michael@0: failedAlready = PR_TRUE; michael@0: } else { michael@0: if (verbose) printf("env: PR_SetEnv() worked.\n"); michael@0: } michael@0: michael@0: value = PR_GetEnv( ENVNAME ); michael@0: if ( (NULL == value ) || (strcmp( value, ENVVALUE))) { michael@0: if (debug) printf( "env: PR_GetEnv() Failed after setting\n" ); michael@0: failedAlready = PR_TRUE; michael@0: } else { michael@0: if (verbose) printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value ); michael@0: } michael@0: michael@0: /* ---------------------------------------------------------------------- */ michael@0: /* un-set the variable, using RAW name... should not work */ michael@0: envBuf = NewBuffer( ENVBUFSIZE ); michael@0: sprintf( envBuf, ENVNAME ); michael@0: rc = PR_SetEnv( envBuf ); michael@0: if ( PR_FAILURE == rc ) { michael@0: if (verbose) printf( "env: PR_SetEnv() not un-set using RAW name. Good!\n"); michael@0: } else { michael@0: if (debug) printf("env: PR_SetEnv() un-set using RAW name. Bad!\n" ); michael@0: failedAlready = PR_TRUE; michael@0: } michael@0: michael@0: value = PR_GetEnv( ENVNAME ); michael@0: if ( NULL == value ) { michael@0: if (debug) printf("env: PR_GetEnv() after un-set using RAW name. Bad!\n" ); michael@0: failedAlready = PR_TRUE; michael@0: } else { michael@0: if (verbose) printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value ); michael@0: } michael@0: michael@0: /* ---------------------------------------------------------------------- */ michael@0: /* set it again ... */ michael@0: envBuf = NewBuffer( ENVBUFSIZE ); michael@0: sprintf( envBuf, ENVNAME "=" ENVVALUE ); michael@0: rc = PR_SetEnv( envBuf ); michael@0: if ( PR_FAILURE == rc ) { michael@0: if (debug) printf( "env: PR_SetEnv() failed setting the second time.\n"); michael@0: failedAlready = PR_TRUE; michael@0: } else { michael@0: if (verbose) printf("env: PR_SetEnv() worked.\n"); michael@0: } michael@0: michael@0: /* un-set the variable using the form name= */ michael@0: envBuf = NewBuffer( ENVBUFSIZE ); michael@0: sprintf( envBuf, ENVNAME "=" ); michael@0: rc = PR_SetEnv( envBuf ); michael@0: if ( PR_FAILURE == rc ) { michael@0: if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n"); michael@0: failedAlready = PR_TRUE; michael@0: } else { michael@0: if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" ); michael@0: } michael@0: michael@0: value = PR_GetEnv( ENVNAME ); michael@0: if (( NULL == value ) || ( 0x00 == *value )) { michael@0: if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" ); michael@0: } else { michael@0: if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value ); michael@0: failedAlready = PR_TRUE; michael@0: } michael@0: /* ---------------------------------------------------------------------- */ michael@0: /* un-set the variable using the form name= */ michael@0: envBuf = NewBuffer( ENVBUFSIZE ); michael@0: sprintf( envBuf, ENVNAME "999=" ); michael@0: rc = PR_SetEnv( envBuf ); michael@0: if ( PR_FAILURE == rc ) { michael@0: if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n"); michael@0: failedAlready = PR_TRUE; michael@0: } else { michael@0: if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" ); michael@0: } michael@0: michael@0: value = PR_GetEnv( ENVNAME "999" ); michael@0: if (( NULL == value ) || ( 0x00 == *value )) { michael@0: if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" ); michael@0: } else { michael@0: if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value ); michael@0: failedAlready = PR_TRUE; michael@0: } michael@0: michael@0: /* ---------------------------------------------------------------------- */ michael@0: if (debug || verbose) printf("\n%s\n", (failedAlready)? "FAILED" : "PASSED" ); michael@0: return( (failedAlready)? 1 : 0 ); michael@0: } /* main() */ michael@0: michael@0: /* env.c */