nsprpub/pr/tests/env.c

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

     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/. */
     6 /*
     7 ** File:        env.c
     8 ** Description: Testing environment variable operations
     9 **
    10 */
    11 #include "prenv.h"
    12 #include "plgetopt.h"
    14 #include <stdio.h>
    15 #include <stdlib.h>
    16 #include <string.h>
    18 PRIntn  debug = 0;
    19 PRIntn  verbose = 0;
    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 )
    29 {
    30     char *buf = malloc( size );
    31     if ( NULL == buf ) {
    32         printf("env: NewBuffer() failed\n");
    33         exit(1);
    34     }
    35     return(buf);
    36 } /* end NewBuffer() */
    38 int main(int argc, char **argv)
    39 {
    40     char    *value;
    41     PRStatus    rc;
    43     {   /* Get command line options */
    44         PLOptStatus os;
    45         PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
    47 	    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    48         {
    49 		    if (PL_OPT_BAD == os) continue;
    50             switch (opt->option)
    51             {
    52             case 'd':  /* debug */
    53                 debug = 1;
    54                 break;
    55             case 'v':  /* verbose */
    56                 verbose = 1;
    57                 break;
    58              default:
    59                 break;
    60             }
    61         }
    62 	    PL_DestroyOptState(opt);
    63     } /* end block "Get command line options" */
    65 #if 0 
    66     {
    67         /*
    68         ** This uses Windows native environment manipulation
    69         ** as an experiment. Note the separation of namespace!
    70         */
    71         BOOL rv;
    72         DWORD   size;
    73         rv = SetEnvironmentVariable( ENVNAME, ENVVALUE );
    74         if ( rv == 0 )  {
    75             if (debug) printf("env: Shit! SetEnvironmentVariable() failed\n");
    76             failedAlready = PR_TRUE;
    77         }
    78         if (verbose) printf("env: SetEnvironmentVariable() worked\n");
    80         size = GetEnvironmentVariable( ENVNAME, envBuf, ENVBUFSIZE );    
    81         if ( size == 0 )  {
    82             if (debug) printf("env: Shit! GetEnvironmentVariable() failed. Found: %s\n", envBuf );
    83             failedAlready = PR_TRUE;
    84         }
    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;
    91         }
    92         if (verbose) printf("env: PR_GetEnv() worked. Found: %s\n", value);
    93     }
    94 #endif
    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;
   103     } else {
   104         if (verbose) printf("env: PR_SetEnv() worked.\n");
   105     }
   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;
   111     } else {
   112         if (verbose) printf("env: PR_GetEnv() worked after setting it. Found: %s\n", value );
   113     }
   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");
   122     } else {
   123         if (debug) printf("env: PR_SetEnv() un-set using RAW name. Bad!\n" );
   124         failedAlready = PR_TRUE;
   125     }
   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;
   131     } else {
   132         if (verbose) printf( "env: PR_GetEnv() after RAW un-set found: %s\n", value );
   133     }
   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;
   143     } else {
   144         if (verbose) printf("env: PR_SetEnv() worked.\n");
   145     }
   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;
   154     } else {
   155         if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
   156     }
   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" );
   161     } else {
   162         if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
   163         failedAlready = PR_TRUE;
   164     }
   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;
   173     } else {
   174         if (verbose) printf("env: PR_SetEnv() un-set using name= worked\n" );
   175     }
   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" );
   180     } else {
   181         if (debug) printf( "env: PR_GetEnv() after un-set using name=. Found: %s\n", value );
   182         failedAlready = PR_TRUE;
   183     }
   185 /* ---------------------------------------------------------------------- */
   186     if (debug || verbose) printf("\n%s\n", (failedAlready)? "FAILED" : "PASSED" );
   187     return( (failedAlready)? 1 : 0 );
   188 }  /* main() */
   190 /* env.c */

mercurial