|
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 ** File: append.c |
|
8 ** Description: Testing File writes where PR_APPEND was used on open |
|
9 ** |
|
10 ** append attempts to verify that a file opened with PR_APPEND |
|
11 ** will always append to the end of file, regardless where the |
|
12 ** current file pointer is positioned. To do this, PR_Seek() is |
|
13 ** called before each write with the position set to beginning of |
|
14 ** file. Subsequent writes should always append. |
|
15 ** The file is read back, summing the integer data written to the |
|
16 ** file. If the expected result is equal, the test passes. |
|
17 ** |
|
18 ** See BugSplat: 4090 |
|
19 */ |
|
20 #include "plgetopt.h" |
|
21 #include "nspr.h" |
|
22 |
|
23 #include <stdio.h> |
|
24 #include <stdlib.h> |
|
25 |
|
26 PRIntn debug = 0; |
|
27 PRIntn verbose = 0; |
|
28 PRBool failedAlready = PR_FALSE; |
|
29 const PRInt32 addedBytes = 1000; |
|
30 const PRInt32 buf = 1; /* constant written to fd, addedBytes times */ |
|
31 PRInt32 inBuf; /* read it back into here */ |
|
32 |
|
33 int main(int argc, char **argv) |
|
34 { |
|
35 PRStatus rc; |
|
36 PRInt32 rv; |
|
37 PRFileDesc *fd; |
|
38 PRIntn i; |
|
39 PRInt32 sum = 0; |
|
40 |
|
41 { /* Get command line options */ |
|
42 PLOptStatus os; |
|
43 PLOptState *opt = PL_CreateOptState(argc, argv, "vd"); |
|
44 |
|
45 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) |
|
46 { |
|
47 if (PL_OPT_BAD == os) continue; |
|
48 switch (opt->option) |
|
49 { |
|
50 case 'd': /* debug */ |
|
51 debug = 1; |
|
52 break; |
|
53 case 'v': /* verbose */ |
|
54 verbose = 1; |
|
55 break; |
|
56 default: |
|
57 break; |
|
58 } |
|
59 } |
|
60 PL_DestroyOptState(opt); |
|
61 } /* end block "Get command line options" */ |
|
62 /* ---------------------------------------------------------------------- */ |
|
63 fd = PR_Open( "/tmp/nsprAppend", (PR_APPEND | PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY), 0666 ); |
|
64 if ( NULL == fd ) { |
|
65 if (debug) printf("PR_Open() failed for writing: %d\n", PR_GetError()); |
|
66 failedAlready = PR_TRUE; |
|
67 goto Finished; |
|
68 } |
|
69 |
|
70 for ( i = 0; i < addedBytes ; i++ ) { |
|
71 rv = PR_Write( fd, &buf, sizeof(buf)); |
|
72 if ( sizeof(buf) != rv ) { |
|
73 if (debug) printf("PR_Write() failed: %d\n", PR_GetError()); |
|
74 failedAlready = PR_TRUE; |
|
75 goto Finished; |
|
76 } |
|
77 rv = PR_Seek( fd, 0 , PR_SEEK_SET ); |
|
78 if ( -1 == rv ) { |
|
79 if (debug) printf("PR_Seek() failed: %d\n", PR_GetError()); |
|
80 failedAlready = PR_TRUE; |
|
81 goto Finished; |
|
82 } |
|
83 } |
|
84 rc = PR_Close( fd ); |
|
85 if ( PR_FAILURE == rc ) { |
|
86 if (debug) printf("PR_Close() failed after writing: %d\n", PR_GetError()); |
|
87 failedAlready = PR_TRUE; |
|
88 goto Finished; |
|
89 } |
|
90 /* ---------------------------------------------------------------------- */ |
|
91 fd = PR_Open( "/tmp/nsprAppend", PR_RDONLY, 0 ); |
|
92 if ( NULL == fd ) { |
|
93 if (debug) printf("PR_Open() failed for reading: %d\n", PR_GetError()); |
|
94 failedAlready = PR_TRUE; |
|
95 goto Finished; |
|
96 } |
|
97 |
|
98 for ( i = 0; i < addedBytes ; i++ ) { |
|
99 rv = PR_Read( fd, &inBuf, sizeof(inBuf)); |
|
100 if ( sizeof(inBuf) != rv) { |
|
101 if (debug) printf("PR_Write() failed: %d\n", PR_GetError()); |
|
102 failedAlready = PR_TRUE; |
|
103 goto Finished; |
|
104 } |
|
105 sum += inBuf; |
|
106 } |
|
107 |
|
108 rc = PR_Close( fd ); |
|
109 if ( PR_FAILURE == rc ) { |
|
110 if (debug) printf("PR_Close() failed after reading: %d\n", PR_GetError()); |
|
111 failedAlready = PR_TRUE; |
|
112 goto Finished; |
|
113 } |
|
114 if ( sum != addedBytes ) { |
|
115 if (debug) printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes, sum); |
|
116 failedAlready = PR_TRUE; |
|
117 goto Finished; |
|
118 } |
|
119 |
|
120 /* ---------------------------------------------------------------------- */ |
|
121 Finished: |
|
122 if (debug || verbose) printf("%s\n", (failedAlready)? "FAILED" : "PASSED" ); |
|
123 return( (failedAlready)? 1 : 0 ); |
|
124 } /* main() */ |
|
125 |
|
126 /* append.c */ |