michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: ** File: append.c michael@0: ** Description: Testing File writes where PR_APPEND was used on open michael@0: ** michael@0: ** append attempts to verify that a file opened with PR_APPEND michael@0: ** will always append to the end of file, regardless where the michael@0: ** current file pointer is positioned. To do this, PR_Seek() is michael@0: ** called before each write with the position set to beginning of michael@0: ** file. Subsequent writes should always append. michael@0: ** The file is read back, summing the integer data written to the michael@0: ** file. If the expected result is equal, the test passes. michael@0: ** michael@0: ** See BugSplat: 4090 michael@0: */ michael@0: #include "plgetopt.h" michael@0: #include "nspr.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: PRIntn debug = 0; michael@0: PRIntn verbose = 0; michael@0: PRBool failedAlready = PR_FALSE; michael@0: const PRInt32 addedBytes = 1000; michael@0: const PRInt32 buf = 1; /* constant written to fd, addedBytes times */ michael@0: PRInt32 inBuf; /* read it back into here */ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRStatus rc; michael@0: PRInt32 rv; michael@0: PRFileDesc *fd; michael@0: PRIntn i; michael@0: PRInt32 sum = 0; michael@0: michael@0: { /* Get command line options */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "vd"); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug */ michael@0: debug = 1; michael@0: break; michael@0: case 'v': /* verbose */ michael@0: verbose = 1; michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: } /* end block "Get command line options" */ michael@0: /* ---------------------------------------------------------------------- */ michael@0: fd = PR_Open( "/tmp/nsprAppend", (PR_APPEND | PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY), 0666 ); michael@0: if ( NULL == fd ) { michael@0: if (debug) printf("PR_Open() failed for writing: %d\n", PR_GetError()); michael@0: failedAlready = PR_TRUE; michael@0: goto Finished; michael@0: } michael@0: michael@0: for ( i = 0; i < addedBytes ; i++ ) { michael@0: rv = PR_Write( fd, &buf, sizeof(buf)); michael@0: if ( sizeof(buf) != rv ) { michael@0: if (debug) printf("PR_Write() failed: %d\n", PR_GetError()); michael@0: failedAlready = PR_TRUE; michael@0: goto Finished; michael@0: } michael@0: rv = PR_Seek( fd, 0 , PR_SEEK_SET ); michael@0: if ( -1 == rv ) { michael@0: if (debug) printf("PR_Seek() failed: %d\n", PR_GetError()); michael@0: failedAlready = PR_TRUE; michael@0: goto Finished; michael@0: } michael@0: } michael@0: rc = PR_Close( fd ); michael@0: if ( PR_FAILURE == rc ) { michael@0: if (debug) printf("PR_Close() failed after writing: %d\n", PR_GetError()); michael@0: failedAlready = PR_TRUE; michael@0: goto Finished; michael@0: } michael@0: /* ---------------------------------------------------------------------- */ michael@0: fd = PR_Open( "/tmp/nsprAppend", PR_RDONLY, 0 ); michael@0: if ( NULL == fd ) { michael@0: if (debug) printf("PR_Open() failed for reading: %d\n", PR_GetError()); michael@0: failedAlready = PR_TRUE; michael@0: goto Finished; michael@0: } michael@0: michael@0: for ( i = 0; i < addedBytes ; i++ ) { michael@0: rv = PR_Read( fd, &inBuf, sizeof(inBuf)); michael@0: if ( sizeof(inBuf) != rv) { michael@0: if (debug) printf("PR_Write() failed: %d\n", PR_GetError()); michael@0: failedAlready = PR_TRUE; michael@0: goto Finished; michael@0: } michael@0: sum += inBuf; michael@0: } michael@0: michael@0: rc = PR_Close( fd ); michael@0: if ( PR_FAILURE == rc ) { michael@0: if (debug) printf("PR_Close() failed after reading: %d\n", PR_GetError()); michael@0: failedAlready = PR_TRUE; michael@0: goto Finished; michael@0: } michael@0: if ( sum != addedBytes ) { michael@0: if (debug) printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes, sum); michael@0: failedAlready = PR_TRUE; michael@0: goto Finished; michael@0: } michael@0: michael@0: /* ---------------------------------------------------------------------- */ michael@0: Finished: michael@0: if (debug || verbose) printf("%s\n", (failedAlready)? "FAILED" : "PASSED" ); michael@0: return( (failedAlready)? 1 : 0 ); michael@0: } /* main() */ michael@0: michael@0: /* append.c */