nsprpub/pr/tests/env.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/env.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,190 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 +** File:        env.c
    1.11 +** Description: Testing environment variable operations
    1.12 +**
    1.13 +*/
    1.14 +#include "prenv.h"
    1.15 +#include "plgetopt.h"
    1.16 +
    1.17 +#include <stdio.h>
    1.18 +#include <stdlib.h>
    1.19 +#include <string.h>
    1.20 +
    1.21 +PRIntn  debug = 0;
    1.22 +PRIntn  verbose = 0;
    1.23 +PRBool  failedAlready = PR_FALSE;
    1.24 +
    1.25 +#define  ENVNAME    "NSPR_ENVIRONMENT_TEST_VARIABLE"
    1.26 +#define  ENVVALUE   "The expected result"
    1.27 +#define  ENVBUFSIZE 256
    1.28 +
    1.29 +char    *envBuf; /* buffer pointer. We leak memory here on purpose! */
    1.30 +
    1.31 +static char * NewBuffer( size_t size )
    1.32 +{
    1.33 +    char *buf = malloc( size );
    1.34 +    if ( NULL == buf ) {
    1.35 +        printf("env: NewBuffer() failed\n");
    1.36 +        exit(1);
    1.37 +    }
    1.38 +    return(buf);
    1.39 +} /* end NewBuffer() */
    1.40 +
    1.41 +int main(int argc, char **argv)
    1.42 +{
    1.43 +    char    *value;
    1.44 +    PRStatus    rc;
    1.45 +
    1.46 +    {   /* Get command line options */
    1.47 +        PLOptStatus os;
    1.48 +        PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
    1.49 +
    1.50 +	    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    1.51 +        {
    1.52 +		    if (PL_OPT_BAD == os) continue;
    1.53 +            switch (opt->option)
    1.54 +            {
    1.55 +            case 'd':  /* debug */
    1.56 +                debug = 1;
    1.57 +                break;
    1.58 +            case 'v':  /* verbose */
    1.59 +                verbose = 1;
    1.60 +                break;
    1.61 +             default:
    1.62 +                break;
    1.63 +            }
    1.64 +        }
    1.65 +	    PL_DestroyOptState(opt);
    1.66 +    } /* end block "Get command line options" */
    1.67 +
    1.68 +#if 0 
    1.69 +    {
    1.70 +        /*
    1.71 +        ** This uses Windows native environment manipulation
    1.72 +        ** as an experiment. Note the separation of namespace!
    1.73 +        */
    1.74 +        BOOL rv;
    1.75 +        DWORD   size;
    1.76 +        rv = SetEnvironmentVariable( ENVNAME, ENVVALUE );
    1.77 +        if ( rv == 0 )  {
    1.78 +            if (debug) printf("env: Shit! SetEnvironmentVariable() failed\n");
    1.79 +            failedAlready = PR_TRUE;
    1.80 +        }
    1.81 +        if (verbose) printf("env: SetEnvironmentVariable() worked\n");
    1.82 +
    1.83 +        size = GetEnvironmentVariable( ENVNAME, envBuf, ENVBUFSIZE );    
    1.84 +        if ( size == 0 )  {
    1.85 +            if (debug) printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf );
    1.86 +            failedAlready = PR_TRUE;
    1.87 +        }
    1.88 +        if (verbose) printf("env: GetEnvironmentVariable() worked. Found: %s\n", envBuf);
    1.89 +
    1.90 +        value = PR_GetEnv( ENVNAME );
    1.91 +        if ( (NULL == value ) || (strcmp( value, ENVVALUE)))  {
    1.92 +            if (debug) printf( "env: PR_GetEnv() failed retrieving WinNative. Found: %s\n", value);
    1.93 +            failedAlready = PR_TRUE;
    1.94 +        }
    1.95 +        if (verbose) printf("env: PR_GetEnv() worked. Found: %s\n", value);
    1.96 +    }
    1.97 +#endif
    1.98 +
    1.99 +    /* set an environment variable, read it back */
   1.100 +    envBuf = NewBuffer( ENVBUFSIZE );
   1.101 +    sprintf( envBuf, ENVNAME "=" ENVVALUE );
   1.102 +    rc = PR_SetEnv( envBuf );
   1.103 +    if ( PR_FAILURE == rc )  {
   1.104 +        if (debug) printf( "env: PR_SetEnv() failed setting\n");
   1.105 +        failedAlready = PR_TRUE;
   1.106 +    } else {
   1.107 +        if (verbose) printf("env: PR_SetEnv() worked.\n");
   1.108 +    }
   1.109 +
   1.110 +    value = PR_GetEnv( ENVNAME );
   1.111 +    if ( (NULL == value ) || (strcmp( value, ENVVALUE)))  {
   1.112 +        if (debug) printf( "env: PR_GetEnv() Failed after setting\n" );
   1.113 +        failedAlready = PR_TRUE;
   1.114 +    } else {
   1.115 +        if (verbose) printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value );
   1.116 +    }
   1.117 +
   1.118 +/* ---------------------------------------------------------------------- */
   1.119 +    /* un-set the variable, using RAW name... should not work */
   1.120 +    envBuf = NewBuffer( ENVBUFSIZE );
   1.121 +    sprintf( envBuf, ENVNAME );
   1.122 +    rc = PR_SetEnv( envBuf );
   1.123 +    if ( PR_FAILURE == rc )  {
   1.124 +        if (verbose) printf( "env: PR_SetEnv() not un-set using RAW name. Good!\n");
   1.125 +    } else {
   1.126 +        if (debug) printf("env: PR_SetEnv() un-set using RAW name. Bad!\n" );
   1.127 +        failedAlready = PR_TRUE;
   1.128 +    }
   1.129 +
   1.130 +    value = PR_GetEnv( ENVNAME );
   1.131 +    if ( NULL == value ) {
   1.132 +        if (debug) printf("env: PR_GetEnv() after un-set using RAW name. Bad!\n" );
   1.133 +        failedAlready = PR_TRUE;
   1.134 +    } else {
   1.135 +        if (verbose) printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value );
   1.136 +    }
   1.137 +    
   1.138 +/* ---------------------------------------------------------------------- */
   1.139 +    /* set it again ... */
   1.140 +    envBuf = NewBuffer( ENVBUFSIZE );
   1.141 +    sprintf( envBuf, ENVNAME "=" ENVVALUE );
   1.142 +    rc = PR_SetEnv( envBuf );
   1.143 +    if ( PR_FAILURE == rc )  {
   1.144 +        if (debug) printf( "env: PR_SetEnv() failed setting the second time.\n");
   1.145 +        failedAlready = PR_TRUE;
   1.146 +    } else {
   1.147 +        if (verbose) printf("env: PR_SetEnv() worked.\n");
   1.148 +    }
   1.149 +
   1.150 +    /* un-set the variable using the form name= */
   1.151 +    envBuf = NewBuffer( ENVBUFSIZE );
   1.152 +    sprintf( envBuf, ENVNAME "=" );
   1.153 +    rc = PR_SetEnv( envBuf );
   1.154 +    if ( PR_FAILURE == rc )  {
   1.155 +        if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n");
   1.156 +        failedAlready = PR_TRUE;
   1.157 +    } else {
   1.158 +        if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
   1.159 +    }
   1.160 +
   1.161 +    value = PR_GetEnv( ENVNAME );
   1.162 +    if (( NULL == value ) || ( 0x00 == *value )) {
   1.163 +        if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
   1.164 +    } else {
   1.165 +        if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
   1.166 +        failedAlready = PR_TRUE;
   1.167 +    }
   1.168 +/* ---------------------------------------------------------------------- */
   1.169 +    /* un-set the variable using the form name= */
   1.170 +    envBuf = NewBuffer( ENVBUFSIZE );
   1.171 +    sprintf( envBuf, ENVNAME "999=" );
   1.172 +    rc = PR_SetEnv( envBuf );
   1.173 +    if ( PR_FAILURE == rc )  {
   1.174 +        if (debug) printf( "env: PR_SetEnv() failed un-setting using name=\n");
   1.175 +        failedAlready = PR_TRUE;
   1.176 +    } else {
   1.177 +        if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
   1.178 +    }
   1.179 +
   1.180 +    value = PR_GetEnv( ENVNAME "999" );
   1.181 +    if (( NULL == value ) || ( 0x00 == *value )) {
   1.182 +        if (verbose) printf("env: PR_GetEnv() after un-set using name= worked\n" );
   1.183 +    } else {
   1.184 +        if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
   1.185 +        failedAlready = PR_TRUE;
   1.186 +    }
   1.187 +
   1.188 +/* ---------------------------------------------------------------------- */
   1.189 +    if (debug || verbose) printf("\n%s\n", (failedAlready)? "FAILED" : "PASSED" );
   1.190 +    return( (failedAlready)? 1 : 0 );
   1.191 +}  /* main() */
   1.192 +
   1.193 +/* env.c */

mercurial