|
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/. */ |
|
5 |
|
6 /*********************************************************************** |
|
7 ** |
|
8 ** Name: op_excl.c |
|
9 ** |
|
10 ** Description: Test Program to verify function of PR_EXCL open flag |
|
11 ** |
|
12 ** Modification History: |
|
13 ** 27-Oct-1999 lth. Initial version |
|
14 ***********************************************************************/ |
|
15 |
|
16 #include <plgetopt.h> |
|
17 #include <nspr.h> |
|
18 #include <stdio.h> |
|
19 #include <stdlib.h> |
|
20 |
|
21 /* |
|
22 ** Test harness infrastructure |
|
23 */ |
|
24 PRLogModuleInfo *lm; |
|
25 PRLogModuleLevel msgLevel = PR_LOG_NONE; |
|
26 PRIntn debug = 0; |
|
27 PRUint32 failed_already = 0; |
|
28 /* end Test harness infrastructure */ |
|
29 /* |
|
30 ** Emit help text for this test |
|
31 */ |
|
32 static void Help( void ) |
|
33 { |
|
34 printf("op_excl: Help"); |
|
35 printf("op_excl [-d]"); |
|
36 printf("-d enables debug messages"); |
|
37 exit(1); |
|
38 } /* end Help() */ |
|
39 |
|
40 |
|
41 |
|
42 int main(int argc, char **argv) |
|
43 { |
|
44 PRFileDesc *fd; |
|
45 PRStatus rv; |
|
46 PRInt32 written; |
|
47 char outBuf[] = "op_excl.c test file"; |
|
48 #define OUT_SIZE sizeof(outBuf) |
|
49 #define NEW_FILENAME "xxxExclNewFile" |
|
50 |
|
51 { |
|
52 /* |
|
53 ** Get command line options |
|
54 */ |
|
55 PLOptStatus os; |
|
56 PLOptState *opt = PL_CreateOptState(argc, argv, "hd"); |
|
57 |
|
58 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
|
59 { |
|
60 if (PL_OPT_BAD == os) continue; |
|
61 switch (opt->option) |
|
62 { |
|
63 case 'd': /* debug */ |
|
64 debug = 1; |
|
65 msgLevel = PR_LOG_ERROR; |
|
66 break; |
|
67 case 'h': /* help message */ |
|
68 Help(); |
|
69 break; |
|
70 default: |
|
71 break; |
|
72 } |
|
73 } |
|
74 PL_DestroyOptState(opt); |
|
75 } |
|
76 |
|
77 lm = PR_NewLogModule("Test"); /* Initialize logging */ |
|
78 |
|
79 /* |
|
80 ** First, open a file, PR_EXCL, we believe not to exist |
|
81 */ |
|
82 fd = PR_Open( NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666 ); |
|
83 if ( NULL == fd ) { |
|
84 if (debug) fprintf( stderr, "Open exclusive. Expected success, got failure\n"); |
|
85 failed_already = 1; |
|
86 goto Finished; |
|
87 } |
|
88 |
|
89 written = PR_Write( fd, outBuf, OUT_SIZE ); |
|
90 if ( OUT_SIZE != written ) { |
|
91 if (debug) fprintf( stderr, "Write after open exclusive failed\n"); |
|
92 failed_already = 1; |
|
93 goto Finished; |
|
94 } |
|
95 |
|
96 rv = PR_Close(fd); |
|
97 if ( PR_FAILURE == rv ) { |
|
98 if (debug) fprintf( stderr, "Close after open exclusive failed\n"); |
|
99 failed_already = 1; |
|
100 goto Finished; |
|
101 } |
|
102 |
|
103 /* |
|
104 ** Second, open the same file, PR_EXCL, expect it to fail |
|
105 */ |
|
106 fd = PR_Open( NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666 ); |
|
107 if ( NULL != fd ) { |
|
108 if (debug) fprintf( stderr, "Open exclusive. Expected failure, got success\n"); |
|
109 failed_already = 1; |
|
110 PR_Close(fd); |
|
111 } |
|
112 |
|
113 rv = PR_Delete( NEW_FILENAME ); |
|
114 if ( PR_FAILURE == rv ) { |
|
115 if (debug) fprintf( stderr, "PR_Delete() failed\n"); |
|
116 failed_already = 1; |
|
117 } |
|
118 |
|
119 Finished: |
|
120 if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS"); |
|
121 return( (failed_already == PR_TRUE )? 1 : 0 ); |
|
122 } /* main() */ |
|
123 /* end op_excl.c */ |
|
124 |