nsprpub/pr/tests/anonfm.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/anonfm.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,311 @@
     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: anonfm.c
    1.11 +** Description: Test anonymous file map 
    1.12 +**
    1.13 +** Synopsis: anonfm [options] [dirName]
    1.14 +**
    1.15 +** Options:
    1.16 +** -d   enable debug mode
    1.17 +** -h   display a help message
    1.18 +** -s <n>  size of the anonymous memory map, in KBytes. default: 100KBytes.
    1.19 +** -C 1 Operate this process as ClientOne() 
    1.20 +** -C 2 Operate this process as ClientTwo()
    1.21 +**
    1.22 +** anonfn.c contains two tests, corresponding to the two protocols for
    1.23 +** passing an anonymous file map to a child process.
    1.24 +**
    1.25 +** ServerOne()/ClientOne() tests the passing of "raw" file map; it uses
    1.26 +** PR_CreateProcess() [for portability of the test case] to create the
    1.27 +** child process, but does not use the PRProcessAttr structure for
    1.28 +** passing the file map data.
    1.29 +**
    1.30 +** ServerTwo()/ClientTwo() tests the passing of the file map using the
    1.31 +** PRProcessAttr structure.
    1.32 +**
    1.33 +*/
    1.34 +#include <plgetopt.h> 
    1.35 +#include <nspr.h> 
    1.36 +#include <private/primpl.h>
    1.37 +#include <stdio.h>
    1.38 +#include <stdlib.h>
    1.39 +#include <string.h>
    1.40 +
    1.41 +/*
    1.42 +** Test harness infrastructure
    1.43 +*/
    1.44 +PRLogModuleInfo *lm;
    1.45 +PRLogModuleLevel msgLevel = PR_LOG_NONE;
    1.46 +PRUint32  failed_already = 0;
    1.47 +
    1.48 +PRIntn  debug = 0;
    1.49 +PRIntn  client = 0; /* invoke client, style */
    1.50 +char    dirName[512] = "."; /* directory name to contain anon mapped file */
    1.51 +PRSize  fmSize = (100 * 1024 );
    1.52 +PRUint32 fmMode = 0600;
    1.53 +PRFileMapProtect fmProt = PR_PROT_READWRITE;
    1.54 +const char *fmEnvName = "nsprFileMapEnvVariable";
    1.55 +
    1.56 +/*
    1.57 +** Emit help text for this test
    1.58 +*/
    1.59 +static void Help( void )
    1.60 +{
    1.61 +    printf("anonfm [options] [dirName]\n");
    1.62 +    printf("-d -- enable debug mode\n");
    1.63 +    printf("dirName is alternate directory name. Default: . (current directory)\n");
    1.64 +    exit(1);
    1.65 +} /* end Help() */
    1.66 +
    1.67 +
    1.68 +/*
    1.69 +** ClientOne() --
    1.70 +*/
    1.71 +static void ClientOne( void )
    1.72 +{
    1.73 +    PRFileMap   *fm;
    1.74 +    char        *fmString;
    1.75 +    char        *addr;
    1.76 +    PRStatus    rc;
    1.77 +
    1.78 +    PR_LOG(lm, msgLevel,
    1.79 +        ("ClientOne() starting"));
    1.80 +    
    1.81 +    fmString = PR_GetEnv( fmEnvName );
    1.82 +    if ( NULL == fmString ) {
    1.83 +        failed_already = 1;    
    1.84 +        PR_LOG(lm, msgLevel,
    1.85 +                ("ClientOne(): PR_Getenv() failed"));
    1.86 +        return;
    1.87 +    }
    1.88 +    PR_LOG(lm, msgLevel,
    1.89 +        ("ClientOne(): PR_Getenv(): found: %s", fmString));
    1.90 +
    1.91 +    fm = PR_ImportFileMapFromString( fmString );
    1.92 +    if ( NULL == fm ) {
    1.93 +        failed_already = 1;    
    1.94 +        PR_LOG(lm, msgLevel,
    1.95 +                ("ClientOne(): PR_ImportFileMapFromString() failed"));
    1.96 +        return;
    1.97 +    }
    1.98 +    PR_LOG(lm, msgLevel,
    1.99 +        ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));
   1.100 +
   1.101 +    addr = PR_MemMap( fm, LL_ZERO, fmSize );
   1.102 +    if ( NULL == addr ) {
   1.103 +        failed_already = 1;    
   1.104 +        PR_LOG(lm, msgLevel,
   1.105 +            ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
   1.106 +        return;
   1.107 +    }
   1.108 +    PR_LOG(lm, msgLevel,
   1.109 +        ("ClientOne(): PR_MemMap(): addr: %p", addr ));
   1.110 +
   1.111 +    /* write to memory map to release server */
   1.112 +    *addr = 1;
   1.113 +
   1.114 +    rc = PR_MemUnmap( addr, fmSize );
   1.115 +    PR_ASSERT( rc == PR_SUCCESS );
   1.116 +    PR_LOG(lm, msgLevel,
   1.117 +        ("ClientOne(): PR_MemUnap(): success" ));
   1.118 +
   1.119 +    rc = PR_CloseFileMap( fm );
   1.120 +    if ( PR_FAILURE == rc ) {
   1.121 +        failed_already = 1;    
   1.122 +        PR_LOG(lm, msgLevel,
   1.123 +            ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
   1.124 +        return;
   1.125 +    }
   1.126 +    PR_LOG(lm, msgLevel,
   1.127 +        ("ClientOne(): PR_CloseFileMap(): success" ));
   1.128 +
   1.129 +    return;
   1.130 +} /* end ClientOne() */
   1.131 +
   1.132 +/*
   1.133 +** ClientTwo() --
   1.134 +*/
   1.135 +static void ClientTwo( void )
   1.136 +{
   1.137 +    failed_already = 1;
   1.138 +} /* end ClientTwo() */
   1.139 +
   1.140 +/*
   1.141 +** ServerOne() --
   1.142 +*/
   1.143 +static void ServerOne( void )
   1.144 +{
   1.145 +    PRFileMap   *fm;
   1.146 +    PRStatus    rc;
   1.147 +    PRIntn      i;
   1.148 +    char        *addr;
   1.149 +    char        fmString[256];
   1.150 +    char        envBuf[256];
   1.151 +    char        *child_argv[8];
   1.152 +    PRProcess   *proc;
   1.153 +    PRInt32     exit_status;
   1.154 +
   1.155 +    PR_LOG(lm, msgLevel,
   1.156 +        ("ServerOne() starting"));
   1.157 +    
   1.158 +    fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
   1.159 +    if ( NULL == fm )      {
   1.160 +        failed_already = 1;    
   1.161 +        PR_LOG(lm, msgLevel,
   1.162 +                ("PR_OpenAnonFileMap() failed"));
   1.163 +        return;
   1.164 +    }
   1.165 +    PR_LOG(lm, msgLevel,
   1.166 +        ("ServerOne(): FileMap: %p", fm ));
   1.167 +    
   1.168 +    rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
   1.169 +    if ( PR_FAILURE == rc )  {
   1.170 +        failed_already = 1;    
   1.171 +        PR_LOG(lm, msgLevel,
   1.172 +            ("PR_ExportFileMap() failed"));
   1.173 +        return;
   1.174 +    }
   1.175 +
   1.176 +    /*
   1.177 +    ** put the string into the environment
   1.178 +    */
   1.179 +    PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
   1.180 +    putenv( envBuf );
   1.181 +    
   1.182 +    addr = PR_MemMap( fm, LL_ZERO, fmSize );
   1.183 +    if ( NULL == addr ) {
   1.184 +        failed_already = 1;    
   1.185 +        PR_LOG(lm, msgLevel,
   1.186 +            ("PR_MemMap() failed"));
   1.187 +        return;
   1.188 +    }
   1.189 +
   1.190 +    /* set initial value for client */
   1.191 +    for (i = 0; i < (PRIntn)fmSize ; i++ )
   1.192 +        *(addr+i) = 0x00;  
   1.193 +
   1.194 +    PR_LOG(lm, msgLevel,
   1.195 +        ("ServerOne(): PR_MemMap(): addr: %p", addr ));
   1.196 +    
   1.197 +    /*
   1.198 +    ** set arguments for child process
   1.199 +    */
   1.200 +    child_argv[0] = "anonfm";
   1.201 +    child_argv[1] = "-C";
   1.202 +    child_argv[2] = "1";
   1.203 +    child_argv[3] = NULL;
   1.204 +
   1.205 +    proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
   1.206 +    PR_ASSERT( proc );
   1.207 +    PR_LOG(lm, msgLevel,
   1.208 +        ("ServerOne(): PR_CreateProcess(): proc: %x", proc ));
   1.209 +
   1.210 +    /*
   1.211 +    ** ClientOne() will set the memory to 1
   1.212 +    */
   1.213 +    PR_LOG(lm, msgLevel,
   1.214 +        ("ServerOne(): waiting on Client, *addr: %x", *addr ));
   1.215 +    while( *addr == 0x00 ) {
   1.216 +        if ( debug )
   1.217 +            fprintf(stderr, ".");
   1.218 +        PR_Sleep(PR_MillisecondsToInterval(300));
   1.219 +    }
   1.220 +    if ( debug )
   1.221 +        fprintf(stderr, "\n");
   1.222 +    PR_LOG(lm, msgLevel,
   1.223 +        ("ServerOne(): Client responded" ));
   1.224 +
   1.225 +    rc = PR_WaitProcess( proc, &exit_status );
   1.226 +    PR_ASSERT( PR_FAILURE != rc );
   1.227 +
   1.228 +    rc = PR_MemUnmap( addr, fmSize);
   1.229 +    if ( PR_FAILURE == rc ) {
   1.230 +        failed_already = 1;    
   1.231 +        PR_LOG(lm, msgLevel,
   1.232 +            ("PR_MemUnmap() failed"));
   1.233 +        return;
   1.234 +    }
   1.235 +    PR_LOG(lm, msgLevel,
   1.236 +        ("ServerOne(): PR_MemUnmap(): success" ));
   1.237 +
   1.238 +    rc = PR_CloseFileMap(fm);
   1.239 +    if ( PR_FAILURE == rc ) {
   1.240 +        failed_already = 1;    
   1.241 +        PR_LOG(lm, msgLevel,
   1.242 +            ("PR_CloseFileMap() failed"));
   1.243 +        return;
   1.244 +    }
   1.245 +    PR_LOG(lm, msgLevel,
   1.246 +        ("ServerOne(): PR_CloseFileMap() success" ));
   1.247 +
   1.248 +    return;
   1.249 +} /* end ServerOne() */
   1.250 +
   1.251 +/*
   1.252 +** ServerTwo() --
   1.253 +*/
   1.254 +static void ServerTwo( void )
   1.255 +{
   1.256 +    PR_LOG(lm, msgLevel,
   1.257 +        ("ServerTwo(): Not implemented yet" ));
   1.258 +} /* end ServerTwo() */
   1.259 +
   1.260 +
   1.261 +int main(int argc, char **argv)
   1.262 +{
   1.263 +    {
   1.264 +        /*
   1.265 +        ** Get command line options
   1.266 +        */
   1.267 +        PLOptStatus os;
   1.268 +        PLOptState *opt = PL_CreateOptState(argc, argv, "hdC:");
   1.269 +
   1.270 +	    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   1.271 +        {
   1.272 +		    if (PL_OPT_BAD == os) continue;
   1.273 +            switch (opt->option)
   1.274 +            {
   1.275 +            case 'C':  /* Client style */
   1.276 +                client = atol(opt->value);
   1.277 +                break;
   1.278 +            case 's':  /* file size */
   1.279 +                fmSize = atol( opt->value ) * 1024;
   1.280 +                break;
   1.281 +            case 'd':  /* debug */
   1.282 +                debug = 1;
   1.283 +			    msgLevel = PR_LOG_DEBUG;
   1.284 +                break;
   1.285 +            case 'h':  /* help message */
   1.286 +			    Help();
   1.287 +                break;
   1.288 +             default:
   1.289 +                strcpy(dirName, opt->value);
   1.290 +                break;
   1.291 +            }
   1.292 +        }
   1.293 +	    PL_DestroyOptState(opt);
   1.294 +    }
   1.295 +
   1.296 +    lm = PR_NewLogModule("Test");       /* Initialize logging */
   1.297 +
   1.298 +    if ( client == 1 ) {
   1.299 +        ClientOne();
   1.300 +    } else if ( client == 2 )  {
   1.301 +        ClientTwo();
   1.302 +    } else {
   1.303 +        ServerOne();
   1.304 +        if ( failed_already ) goto Finished;
   1.305 +        ServerTwo();
   1.306 +    }
   1.307 +
   1.308 +Finished:
   1.309 +    if ( debug )
   1.310 +        printf("%s\n", (failed_already)? "FAIL" : "PASS");
   1.311 +    return( (failed_already == PR_TRUE )? 1 : 0 );
   1.312 +}  /* main() */
   1.313 +/* end anonfm.c */
   1.314 +

mercurial