1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is the Netscape Portable Runtime (NSPR).
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998-2000
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | ** File: append.c
|
---|
40 | ** Description: Testing File writes where PR_APPEND was used on open
|
---|
41 | **
|
---|
42 | ** append attempts to verify that a file opened with PR_APPEND
|
---|
43 | ** will always append to the end of file, regardless where the
|
---|
44 | ** current file pointer is positioned. To do this, PR_Seek() is
|
---|
45 | ** called before each write with the position set to beginning of
|
---|
46 | ** file. Subsequent writes should always append.
|
---|
47 | ** The file is read back, summing the integer data written to the
|
---|
48 | ** file. If the expected result is equal, the test passes.
|
---|
49 | **
|
---|
50 | ** See BugSplat: 4090
|
---|
51 | */
|
---|
52 | #include "plgetopt.h"
|
---|
53 | #include "nspr.h"
|
---|
54 |
|
---|
55 | #include <stdio.h>
|
---|
56 | #include <stdlib.h>
|
---|
57 |
|
---|
58 | PRIntn debug = 0;
|
---|
59 | PRIntn verbose = 0;
|
---|
60 | PRBool failedAlready = PR_FALSE;
|
---|
61 | const PRInt32 addedBytes = 1000;
|
---|
62 | const PRInt32 buf = 1; /* constant written to fd, addedBytes times */
|
---|
63 | PRInt32 inBuf; /* read it back into here */
|
---|
64 |
|
---|
65 | PRIntn main(PRIntn argc, char *argv[])
|
---|
66 | {
|
---|
67 | PRStatus rc;
|
---|
68 | PRInt32 rv;
|
---|
69 | PRFileDesc *fd;
|
---|
70 | PRIntn i;
|
---|
71 | PRInt32 sum = 0;
|
---|
72 |
|
---|
73 | { /* Get command line options */
|
---|
74 | PLOptStatus os;
|
---|
75 | PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
|
---|
76 |
|
---|
77 | while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
|
---|
78 | {
|
---|
79 | if (PL_OPT_BAD == os) continue;
|
---|
80 | switch (opt->option)
|
---|
81 | {
|
---|
82 | case 'd': /* debug */
|
---|
83 | debug = 1;
|
---|
84 | break;
|
---|
85 | case 'v': /* verbose */
|
---|
86 | verbose = 1;
|
---|
87 | break;
|
---|
88 | default:
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | PL_DestroyOptState(opt);
|
---|
93 | } /* end block "Get command line options" */
|
---|
94 | /* ---------------------------------------------------------------------- */
|
---|
95 | fd = PR_Open( "/tmp/nsprAppend", (PR_APPEND | PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY), 0666 );
|
---|
96 | if ( NULL == fd ) {
|
---|
97 | if (debug) printf("PR_Open() failed for writing: %d\n", PR_GetError());
|
---|
98 | failedAlready = PR_TRUE;
|
---|
99 | goto Finished;
|
---|
100 | }
|
---|
101 |
|
---|
102 | for ( i = 0; i < addedBytes ; i++ ) {
|
---|
103 | rv = PR_Write( fd, &buf, sizeof(buf));
|
---|
104 | if ( sizeof(buf) != rv ) {
|
---|
105 | if (debug) printf("PR_Write() failed: %d\n", PR_GetError());
|
---|
106 | failedAlready = PR_TRUE;
|
---|
107 | goto Finished;
|
---|
108 | }
|
---|
109 | rv = PR_Seek( fd, 0 , PR_SEEK_SET );
|
---|
110 | if ( -1 == rv ) {
|
---|
111 | if (debug) printf("PR_Seek() failed: %d\n", PR_GetError());
|
---|
112 | failedAlready = PR_TRUE;
|
---|
113 | goto Finished;
|
---|
114 | }
|
---|
115 | }
|
---|
116 | rc = PR_Close( fd );
|
---|
117 | if ( PR_FAILURE == rc ) {
|
---|
118 | if (debug) printf("PR_Close() failed after writing: %d\n", PR_GetError());
|
---|
119 | failedAlready = PR_TRUE;
|
---|
120 | goto Finished;
|
---|
121 | }
|
---|
122 | /* ---------------------------------------------------------------------- */
|
---|
123 | fd = PR_Open( "/tmp/nsprAppend", PR_RDONLY, 0 );
|
---|
124 | if ( NULL == fd ) {
|
---|
125 | if (debug) printf("PR_Open() failed for reading: %d\n", PR_GetError());
|
---|
126 | failedAlready = PR_TRUE;
|
---|
127 | goto Finished;
|
---|
128 | }
|
---|
129 |
|
---|
130 | for ( i = 0; i < addedBytes ; i++ ) {
|
---|
131 | rv = PR_Read( fd, &inBuf, sizeof(inBuf));
|
---|
132 | if ( sizeof(inBuf) != rv) {
|
---|
133 | if (debug) printf("PR_Write() failed: %d\n", PR_GetError());
|
---|
134 | failedAlready = PR_TRUE;
|
---|
135 | goto Finished;
|
---|
136 | }
|
---|
137 | sum += inBuf;
|
---|
138 | }
|
---|
139 |
|
---|
140 | rc = PR_Close( fd );
|
---|
141 | if ( PR_FAILURE == rc ) {
|
---|
142 | if (debug) printf("PR_Close() failed after reading: %d\n", PR_GetError());
|
---|
143 | failedAlready = PR_TRUE;
|
---|
144 | goto Finished;
|
---|
145 | }
|
---|
146 | if ( sum != addedBytes ) {
|
---|
147 | if (debug) printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes, sum);
|
---|
148 | failedAlready = PR_TRUE;
|
---|
149 | goto Finished;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* ---------------------------------------------------------------------- */
|
---|
153 | Finished:
|
---|
154 | if (debug || verbose) printf("%s\n", (failedAlready)? "FAILED" : "PASSED" );
|
---|
155 | return( (failedAlready)? 1 : 0 );
|
---|
156 | } /* main() */
|
---|
157 |
|
---|
158 | /* append.c */
|
---|