~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/nsprpub/pr/tests/anonfm.c

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* 
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 * 
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 * 
 
13
 * The Original Code is the Netscape Portable Runtime (NSPR).
 
14
 * 
 
15
 * The Initial Developer of the Original Code is Netscape
 
16
 * Communications Corporation.  Portions created by Netscape are 
 
17
 * Copyright (C) 1998-2000 Netscape Communications Corporation.  All
 
18
 * Rights Reserved.
 
19
 * 
 
20
 * Contributor(s):
 
21
 * 
 
22
 * Alternatively, the contents of this file may be used under the
 
23
 * terms of the GNU General Public License Version 2 or later (the
 
24
 * "GPL"), in which case the provisions of the GPL are applicable 
 
25
 * instead of those above.  If you wish to allow use of your 
 
26
 * version of this file only under the terms of the GPL and not to
 
27
 * allow others to use your version of this file under the MPL,
 
28
 * indicate your decision by deleting the provisions above and
 
29
 * replace them with the notice and other provisions required by
 
30
 * the GPL.  If you do not delete the provisions above, a recipient
 
31
 * may use your version of this file under either the MPL or the
 
32
 * GPL.
 
33
 */
 
34
 
 
35
/*
 
36
** File: anonfm.c
 
37
** Description: Test anonymous file map 
 
38
**
 
39
** Synopsis: anonfm [options] [dirName]
 
40
**
 
41
** Options:
 
42
** -d   enable debug mode
 
43
** -h   display a help message
 
44
** -s <n>  size of the anonymous memory map, in KBytes. default: 100KBytes.
 
45
** -C 1 Operate this process as ClientOne() 
 
46
** -C 2 Operate this process as ClientTwo()
 
47
**
 
48
** anonfn.c contains two tests, corresponding to the two protocols for
 
49
** passing an anonymous file map to a child process.
 
50
**
 
51
** ServerOne()/ClientOne() tests the passing of "raw" file map; it uses
 
52
** PR_CreateProcess() [for portability of the test case] to create the
 
53
** child process, but does not use the PRProcessAttr structure for
 
54
** passing the file map data.
 
55
**
 
56
** ServerTwo()/ClientTwo() tests the passing of the file map using the
 
57
** PRProcessAttr structure.
 
58
**
 
59
*/
 
60
#include <plgetopt.h> 
 
61
#include <nspr.h> 
 
62
#include <private/primpl.h>
 
63
#include <stdio.h>
 
64
#include <stdlib.h>
 
65
#include <string.h>
 
66
 
 
67
/*
 
68
** Test harness infrastructure
 
69
*/
 
70
PRLogModuleInfo *lm;
 
71
PRLogModuleLevel msgLevel = PR_LOG_NONE;
 
72
PRUint32  failed_already = 0;
 
73
 
 
74
PRIntn  debug = 0;
 
75
PRIntn  client = 0; /* invoke client, style */
 
76
char    dirName[512] = "."; /* directory name to contain anon mapped file */
 
77
PRSize  fmSize = (100 * 1024 );
 
78
PRUint32 fmMode = 0600;
 
79
PRFileMapProtect fmProt = PR_PROT_READWRITE;
 
80
const char *fmEnvName = "nsprFileMapEnvVariable";
 
81
 
 
82
/*
 
83
** Emit help text for this test
 
84
*/
 
85
static void Help( void )
 
86
{
 
87
    printf("anonfm [options] [dirName]\n");
 
88
    printf("-d -- enable debug mode\n");
 
89
    printf("dirName is alternate directory name. Default: . (current directory)\n");
 
90
    exit(1);
 
91
} /* end Help() */
 
92
 
 
93
 
 
94
/*
 
95
** ClientOne() --
 
96
*/
 
97
static void ClientOne( void )
 
98
{
 
99
    PRFileMap   *fm;
 
100
    char        *fmString;
 
101
    char        *addr;
 
102
    PRStatus    rc;
 
103
 
 
104
    PR_LOG(lm, msgLevel,
 
105
        ("ClientOne() starting"));
 
106
    
 
107
    fmString = PR_GetEnv( fmEnvName );
 
108
    if ( NULL == fmString ) {
 
109
        failed_already = 1;    
 
110
        PR_LOG(lm, msgLevel,
 
111
                ("ClientOne(): PR_Getenv() failed"));
 
112
        return;
 
113
    }
 
114
    PR_LOG(lm, msgLevel,
 
115
        ("ClientOne(): PR_Getenv(): found: %s", fmString));
 
116
 
 
117
    fm = PR_ImportFileMapFromString( fmString );
 
118
    if ( NULL == fm ) {
 
119
        failed_already = 1;    
 
120
        PR_LOG(lm, msgLevel,
 
121
                ("ClientOne(): PR_ImportFileMapFromString() failed"));
 
122
        return;
 
123
    }
 
124
    PR_LOG(lm, msgLevel,
 
125
        ("ClientOne(): PR_ImportFileMapFromString(): fm: %p", fm ));
 
126
 
 
127
    addr = PR_MemMap( fm, LL_ZERO, fmSize );
 
128
    if ( NULL == addr ) {
 
129
        failed_already = 1;    
 
130
        PR_LOG(lm, msgLevel,
 
131
            ("ClientOne(): PR_MemMap() failed, OSError: %d", PR_GetOSError() ));
 
132
        return;
 
133
    }
 
134
    PR_LOG(lm, msgLevel,
 
135
        ("ClientOne(): PR_MemMap(): addr: %p", addr ));
 
136
 
 
137
    /* write to memory map to release server */
 
138
    *addr = 1;
 
139
 
 
140
    rc = PR_MemUnmap( addr, fmSize );
 
141
    PR_ASSERT( rc == PR_SUCCESS );
 
142
    PR_LOG(lm, msgLevel,
 
143
        ("ClientOne(): PR_MemUnap(): success" ));
 
144
 
 
145
    rc = PR_CloseFileMap( fm );
 
146
    if ( PR_FAILURE == rc ) {
 
147
        failed_already = 1;    
 
148
        PR_LOG(lm, msgLevel,
 
149
            ("ClientOne(): PR_MemUnap() failed, OSError: %d", PR_GetOSError() ));
 
150
        return;
 
151
    }
 
152
    PR_LOG(lm, msgLevel,
 
153
        ("ClientOne(): PR_CloseFileMap(): success" ));
 
154
 
 
155
    return;
 
156
} /* end ClientOne() */
 
157
 
 
158
/*
 
159
** ClientTwo() --
 
160
*/
 
161
static void ClientTwo( void )
 
162
{
 
163
    failed_already = 1;
 
164
} /* end ClientTwo() */
 
165
 
 
166
/*
 
167
** ServerOne() --
 
168
*/
 
169
static void ServerOne( void )
 
170
{
 
171
    PRFileMap   *fm;
 
172
    PRStatus    rc;
 
173
    PRIntn      i;
 
174
    char        *addr;
 
175
    char        fmString[256];
 
176
    char        envBuf[256];
 
177
    char        *child_argv[8];
 
178
    PRProcess   *proc;
 
179
    PRInt32     exit_status;
 
180
 
 
181
    PR_LOG(lm, msgLevel,
 
182
        ("ServerOne() starting"));
 
183
    
 
184
    fm = PR_OpenAnonFileMap( dirName, fmSize, fmProt );
 
185
    if ( NULL == fm )      {
 
186
        failed_already = 1;    
 
187
        PR_LOG(lm, msgLevel,
 
188
                ("PR_OpenAnonFileMap() failed"));
 
189
        return;
 
190
    }
 
191
    PR_LOG(lm, msgLevel,
 
192
        ("ServerOne(): FileMap: %p", fm ));
 
193
    
 
194
    rc = PR_ExportFileMapAsString( fm, sizeof(fmString), fmString );
 
195
    if ( PR_FAILURE == rc )  {
 
196
        failed_already = 1;    
 
197
        PR_LOG(lm, msgLevel,
 
198
            ("PR_ExportFileMap() failed"));
 
199
        return;
 
200
    }
 
201
 
 
202
    /*
 
203
    ** put the string into the environment
 
204
    */
 
205
    PR_snprintf( envBuf, sizeof(envBuf), "%s=%s", fmEnvName, fmString);
 
206
    putenv( envBuf );
 
207
    
 
208
    addr = PR_MemMap( fm, LL_ZERO, fmSize );
 
209
    if ( NULL == addr ) {
 
210
        failed_already = 1;    
 
211
        PR_LOG(lm, msgLevel,
 
212
            ("PR_MemMap() failed"));
 
213
        return;
 
214
    }
 
215
 
 
216
    /* set initial value for client */
 
217
    for (i = 0; i < (PRIntn)fmSize ; i++ )
 
218
        *(addr+i) = 0x00;  
 
219
 
 
220
    PR_LOG(lm, msgLevel,
 
221
        ("ServerOne(): PR_MemMap(): addr: %p", addr ));
 
222
    
 
223
    /*
 
224
    ** set arguments for child process
 
225
    */
 
226
    child_argv[0] = "anonfm";
 
227
    child_argv[1] = "-C";
 
228
    child_argv[2] = "1";
 
229
    child_argv[3] = NULL;
 
230
 
 
231
    proc = PR_CreateProcess(child_argv[0], child_argv, NULL, NULL);
 
232
    PR_ASSERT( proc );
 
233
    PR_LOG(lm, msgLevel,
 
234
        ("ServerOne(): PR_CreateProcess(): proc: %x", proc ));
 
235
 
 
236
    /*
 
237
    ** ClientOne() will set the memory to 1
 
238
    */
 
239
    PR_LOG(lm, msgLevel,
 
240
        ("ServerOne(): waiting on Client, *addr: %x", *addr ));
 
241
    while( *addr == 0x00 ) {
 
242
        if ( debug )
 
243
            fprintf(stderr, ".");
 
244
        PR_Sleep(PR_MillisecondsToInterval(300));
 
245
    }
 
246
    if ( debug )
 
247
        fprintf(stderr, "\n");
 
248
    PR_LOG(lm, msgLevel,
 
249
        ("ServerOne(): Client responded" ));
 
250
 
 
251
    rc = PR_WaitProcess( proc, &exit_status );
 
252
    PR_ASSERT( PR_FAILURE != rc );
 
253
 
 
254
    rc = PR_MemUnmap( addr, fmSize);
 
255
    if ( PR_FAILURE == rc ) {
 
256
        failed_already = 1;    
 
257
        PR_LOG(lm, msgLevel,
 
258
            ("PR_MemUnmap() failed"));
 
259
        return;
 
260
    }
 
261
    PR_LOG(lm, msgLevel,
 
262
        ("ServerOne(): PR_MemUnmap(): success" ));
 
263
 
 
264
    rc = PR_CloseFileMap(fm);
 
265
    if ( PR_FAILURE == rc ) {
 
266
        failed_already = 1;    
 
267
        PR_LOG(lm, msgLevel,
 
268
            ("PR_CloseFileMap() failed"));
 
269
        return;
 
270
    }
 
271
    PR_LOG(lm, msgLevel,
 
272
        ("ServerOne(): PR_CloseFileMap() success" ));
 
273
 
 
274
    return;
 
275
} /* end ServerOne() */
 
276
 
 
277
/*
 
278
** ServerTwo() --
 
279
*/
 
280
static void ServerTwo( void )
 
281
{
 
282
    PR_LOG(lm, msgLevel,
 
283
        ("ServerTwo(): Not implemented yet" ));
 
284
} /* end ServerTwo() */
 
285
 
 
286
 
 
287
PRIntn main(PRIntn argc, char *argv[])
 
288
{
 
289
    {
 
290
        /*
 
291
        ** Get command line options
 
292
        */
 
293
        PLOptStatus os;
 
294
        PLOptState *opt = PL_CreateOptState(argc, argv, "hdC:");
 
295
 
 
296
            while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
 
297
        {
 
298
                    if (PL_OPT_BAD == os) continue;
 
299
            switch (opt->option)
 
300
            {
 
301
            case 'C':  /* Client style */
 
302
                client = atol(opt->value);
 
303
                break;
 
304
            case 's':  /* file size */
 
305
                fmSize = atol( opt->value ) * 1024;
 
306
                break;
 
307
            case 'd':  /* debug */
 
308
                debug = 1;
 
309
                            msgLevel = PR_LOG_DEBUG;
 
310
                break;
 
311
            case 'h':  /* help message */
 
312
                            Help();
 
313
                break;
 
314
             default:
 
315
                strcpy(dirName, opt->value);
 
316
                break;
 
317
            }
 
318
        }
 
319
            PL_DestroyOptState(opt);
 
320
    }
 
321
 
 
322
    lm = PR_NewLogModule("Test");       /* Initialize logging */
 
323
 
 
324
    if ( client == 1 ) {
 
325
        ClientOne();
 
326
    } else if ( client == 2 )  {
 
327
        ClientTwo();
 
328
    } else {
 
329
        ServerOne();
 
330
        if ( failed_already ) goto Finished;
 
331
        ServerTwo();
 
332
    }
 
333
 
 
334
Finished:
 
335
    if ( debug )
 
336
        printf("%s\n", (failed_already)? "FAIL" : "PASS");
 
337
    return( (failed_already == PR_TRUE )? 1 : 0 );
 
338
}  /* main() */
 
339
/* end anonfm.c */
 
340