nsprpub/pr/tests/anonfm.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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: anonfm.c
     8 ** Description: Test anonymous file map 
     9 **
    10 ** Synopsis: anonfm [options] [dirName]
    11 **
    12 ** Options:
    13 ** -d   enable debug mode
    14 ** -h   display a help message
    15 ** -s <n>  size of the anonymous memory map, in KBytes. default: 100KBytes.
    16 ** -C 1 Operate this process as ClientOne() 
    17 ** -C 2 Operate this process as ClientTwo()
    18 **
    19 ** anonfn.c contains two tests, corresponding to the two protocols for
    20 ** passing an anonymous file map to a child process.
    21 **
    22 ** ServerOne()/ClientOne() tests the passing of "raw" file map; it uses
    23 ** PR_CreateProcess() [for portability of the test case] to create the
    24 ** child process, but does not use the PRProcessAttr structure for
    25 ** passing the file map data.
    26 **
    27 ** ServerTwo()/ClientTwo() tests the passing of the file map using the
    28 ** PRProcessAttr structure.
    29 **
    30 */
    31 #include <plgetopt.h> 
    32 #include <nspr.h> 
    33 #include <private/primpl.h>
    34 #include <stdio.h>
    35 #include <stdlib.h>
    36 #include <string.h>
    38 /*
    39 ** Test harness infrastructure
    40 */
    41 PRLogModuleInfo *lm;
    42 PRLogModuleLevel msgLevel = PR_LOG_NONE;
    43 PRUint32  failed_already = 0;
    45 PRIntn  debug = 0;
    46 PRIntn  client = 0; /* invoke client, style */
    47 char    dirName[512] = "."; /* directory name to contain anon mapped file */
    48 PRSize  fmSize = (100 * 1024 );
    49 PRUint32 fmMode = 0600;
    50 PRFileMapProtect fmProt = PR_PROT_READWRITE;
    51 const char *fmEnvName = "nsprFileMapEnvVariable";
    53 /*
    54 ** Emit help text for this test
    55 */
    56 static void Help( void )
    57 {
    58     printf("anonfm [options] [dirName]\n");
    59     printf("-d -- enable debug mode\n");
    60     printf("dirName is alternate directory name. Default: . (current directory)\n");
    61     exit(1);
    62 } /* end Help() */
    65 /*
    66 ** ClientOne() --
    67 */
    68 static void ClientOne( void )
    69 {
    70     PRFileMap   *fm;
    71     char        *fmString;
    72     char        *addr;
    73     PRStatus    rc;
    75     PR_LOG(lm, msgLevel,
    76         ("ClientOne() starting"));
    78     fmString = PR_GetEnv( fmEnvName );
    79     if ( NULL == fmString ) {
    80         failed_already = 1;    
    81         PR_LOG(lm, msgLevel,
    82                 ("ClientOne(): PR_Getenv() failed"));
    83         return;
    84     }
    85     PR_LOG(lm, msgLevel,
    86         ("ClientOne(): PR_Getenv(): found: %s", fmString));
    88     fm = PR_ImportFileMapFromString( fmString );
    89     if ( NULL == fm ) {
    90         failed_already = 1;    
    91         PR_LOG(lm, msgLevel,
    92                 ("ClientOne(): PR_ImportFileMapFromString() failed"));
    93         return;
    94     }
    95     PR_LOG(lm, msgLevel,
    96         ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));
    98     addr = PR_MemMap( fm, LL_ZERO, fmSize );
    99     if ( NULL == addr ) {
   100         failed_already = 1;    
   101         PR_LOG(lm, msgLevel,
   102             ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
   103         return;
   104     }
   105     PR_LOG(lm, msgLevel,
   106         ("ClientOne(): PR_MemMap(): addr: %p", addr ));
   108     /* write to memory map to release server */
   109     *addr = 1;
   111     rc = PR_MemUnmap( addr, fmSize );
   112     PR_ASSERT( rc == PR_SUCCESS );
   113     PR_LOG(lm, msgLevel,
   114         ("ClientOne(): PR_MemUnap(): success" ));
   116     rc = PR_CloseFileMap( fm );
   117     if ( PR_FAILURE == rc ) {
   118         failed_already = 1;    
   119         PR_LOG(lm, msgLevel,
   120             ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
   121         return;
   122     }
   123     PR_LOG(lm, msgLevel,
   124         ("ClientOne(): PR_CloseFileMap(): success" ));
   126     return;
   127 } /* end ClientOne() */
   129 /*
   130 ** ClientTwo() --
   131 */
   132 static void ClientTwo( void )
   133 {
   134     failed_already = 1;
   135 } /* end ClientTwo() */
   137 /*
   138 ** ServerOne() --
   139 */
   140 static void ServerOne( void )
   141 {
   142     PRFileMap   *fm;
   143     PRStatus    rc;
   144     PRIntn      i;
   145     char        *addr;
   146     char        fmString[256];
   147     char        envBuf[256];
   148     char        *child_argv[8];
   149     PRProcess   *proc;
   150     PRInt32     exit_status;
   152     PR_LOG(lm, msgLevel,
   153         ("ServerOne() starting"));
   155     fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
   156     if ( NULL == fm )      {
   157         failed_already = 1;    
   158         PR_LOG(lm, msgLevel,
   159                 ("PR_OpenAnonFileMap() failed"));
   160         return;
   161     }
   162     PR_LOG(lm, msgLevel,
   163         ("ServerOne(): FileMap: %p", fm ));
   165     rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
   166     if ( PR_FAILURE == rc )  {
   167         failed_already = 1;    
   168         PR_LOG(lm, msgLevel,
   169             ("PR_ExportFileMap() failed"));
   170         return;
   171     }
   173     /*
   174     ** put the string into the environment
   175     */
   176     PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
   177     putenv( envBuf );
   179     addr = PR_MemMap( fm, LL_ZERO, fmSize );
   180     if ( NULL == addr ) {
   181         failed_already = 1;    
   182         PR_LOG(lm, msgLevel,
   183             ("PR_MemMap() failed"));
   184         return;
   185     }
   187     /* set initial value for client */
   188     for (i = 0; i < (PRIntn)fmSize ; i++ )
   189         *(addr+i) = 0x00;  
   191     PR_LOG(lm, msgLevel,
   192         ("ServerOne(): PR_MemMap(): addr: %p", addr ));
   194     /*
   195     ** set arguments for child process
   196     */
   197     child_argv[0] = "anonfm";
   198     child_argv[1] = "-C";
   199     child_argv[2] = "1";
   200     child_argv[3] = NULL;
   202     proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
   203     PR_ASSERT( proc );
   204     PR_LOG(lm, msgLevel,
   205         ("ServerOne(): PR_CreateProcess(): proc: %x", proc ));
   207     /*
   208     ** ClientOne() will set the memory to 1
   209     */
   210     PR_LOG(lm, msgLevel,
   211         ("ServerOne(): waiting on Client, *addr: %x", *addr ));
   212     while( *addr == 0x00 ) {
   213         if ( debug )
   214             fprintf(stderr, ".");
   215         PR_Sleep(PR_MillisecondsToInterval(300));
   216     }
   217     if ( debug )
   218         fprintf(stderr, "\n");
   219     PR_LOG(lm, msgLevel,
   220         ("ServerOne(): Client responded" ));
   222     rc = PR_WaitProcess( proc, &exit_status );
   223     PR_ASSERT( PR_FAILURE != rc );
   225     rc = PR_MemUnmap( addr, fmSize);
   226     if ( PR_FAILURE == rc ) {
   227         failed_already = 1;    
   228         PR_LOG(lm, msgLevel,
   229             ("PR_MemUnmap() failed"));
   230         return;
   231     }
   232     PR_LOG(lm, msgLevel,
   233         ("ServerOne(): PR_MemUnmap(): success" ));
   235     rc = PR_CloseFileMap(fm);
   236     if ( PR_FAILURE == rc ) {
   237         failed_already = 1;    
   238         PR_LOG(lm, msgLevel,
   239             ("PR_CloseFileMap() failed"));
   240         return;
   241     }
   242     PR_LOG(lm, msgLevel,
   243         ("ServerOne(): PR_CloseFileMap() success" ));
   245     return;
   246 } /* end ServerOne() */
   248 /*
   249 ** ServerTwo() --
   250 */
   251 static void ServerTwo( void )
   252 {
   253     PR_LOG(lm, msgLevel,
   254         ("ServerTwo(): Not implemented yet" ));
   255 } /* end ServerTwo() */
   258 int main(int argc, char **argv)
   259 {
   260     {
   261         /*
   262         ** Get command line options
   263         */
   264         PLOptStatus os;
   265         PLOptState *opt = PL_CreateOptState(argc, argv, "hdC:");
   267 	    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
   268         {
   269 		    if (PL_OPT_BAD == os) continue;
   270             switch (opt->option)
   271             {
   272             case 'C':  /* Client style */
   273                 client = atol(opt->value);
   274                 break;
   275             case 's':  /* file size */
   276                 fmSize = atol( opt->value ) * 1024;
   277                 break;
   278             case 'd':  /* debug */
   279                 debug = 1;
   280 			    msgLevel = PR_LOG_DEBUG;
   281                 break;
   282             case 'h':  /* help message */
   283 			    Help();
   284                 break;
   285              default:
   286                 strcpy(dirName, opt->value);
   287                 break;
   288             }
   289         }
   290 	    PL_DestroyOptState(opt);
   291     }
   293     lm = PR_NewLogModule("Test");       /* Initialize logging */
   295     if ( client == 1 ) {
   296         ClientOne();
   297     } else if ( client == 2 )  {
   298         ClientTwo();
   299     } else {
   300         ServerOne();
   301         if ( failed_already ) goto Finished;
   302         ServerTwo();
   303     }
   305 Finished:
   306     if ( debug )
   307         printf("%s\n", (failed_already)? "FAIL" : "PASS");
   308     return( (failed_already == PR_TRUE )? 1 : 0 );
   309 }  /* main() */
   310 /* end anonfm.c */

mercurial