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
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/
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
15 * The Original Code is the Netscape Portable Runtime (NSPR).
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.
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.
36 * ***** END LICENSE BLOCK ***** */
40 ** Description: Testing File writes where PR_APPEND was used on open
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.
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 */
65 PRIntn
main(PRIntn argc
, char *argv
[])
73 { /* Get command line options */
75 PLOptState
*opt
= PL_CreateOptState(argc
, argv
, "vd");
77 while (PL_OPT_EOL
!= (os
= PL_GetNextOpt(opt
)))
79 if (PL_OPT_BAD
== os
) continue;
85 case 'v': /* verbose */
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 );
97 if (debug
) printf("PR_Open() failed for writing: %d\n", PR_GetError());
98 failedAlready
= PR_TRUE
;
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
;
109 rv
= PR_Seek( fd
, 0 , PR_SEEK_SET
);
111 if (debug
) printf("PR_Seek() failed: %d\n", PR_GetError());
112 failedAlready
= PR_TRUE
;
117 if ( PR_FAILURE
== rc
) {
118 if (debug
) printf("PR_Close() failed after writing: %d\n", PR_GetError());
119 failedAlready
= PR_TRUE
;
122 /* ---------------------------------------------------------------------- */
123 fd
= PR_Open( "/tmp/nsprAppend", PR_RDONLY
, 0 );
125 if (debug
) printf("PR_Open() failed for reading: %d\n", PR_GetError());
126 failedAlready
= PR_TRUE
;
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
;
141 if ( PR_FAILURE
== rc
) {
142 if (debug
) printf("PR_Close() failed after reading: %d\n", PR_GetError());
143 failedAlready
= PR_TRUE
;
146 if ( sum
!= addedBytes
) {
147 if (debug
) printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes
, sum
);
148 failedAlready
= PR_TRUE
;
152 /* ---------------------------------------------------------------------- */
154 if (debug
|| verbose
) printf("%s\n", (failedAlready
)? "FAILED" : "PASSED" );
155 return( (failedAlready
)? 1 : 0 );