1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/append.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** File: append.c 1.11 +** Description: Testing File writes where PR_APPEND was used on open 1.12 +** 1.13 +** append attempts to verify that a file opened with PR_APPEND 1.14 +** will always append to the end of file, regardless where the 1.15 +** current file pointer is positioned. To do this, PR_Seek() is 1.16 +** called before each write with the position set to beginning of 1.17 +** file. Subsequent writes should always append. 1.18 +** The file is read back, summing the integer data written to the 1.19 +** file. If the expected result is equal, the test passes. 1.20 +** 1.21 +** See BugSplat: 4090 1.22 +*/ 1.23 +#include "plgetopt.h" 1.24 +#include "nspr.h" 1.25 + 1.26 +#include <stdio.h> 1.27 +#include <stdlib.h> 1.28 + 1.29 +PRIntn debug = 0; 1.30 +PRIntn verbose = 0; 1.31 +PRBool failedAlready = PR_FALSE; 1.32 +const PRInt32 addedBytes = 1000; 1.33 +const PRInt32 buf = 1; /* constant written to fd, addedBytes times */ 1.34 +PRInt32 inBuf; /* read it back into here */ 1.35 + 1.36 +int main(int argc, char **argv) 1.37 +{ 1.38 + PRStatus rc; 1.39 + PRInt32 rv; 1.40 + PRFileDesc *fd; 1.41 + PRIntn i; 1.42 + PRInt32 sum = 0; 1.43 + 1.44 + { /* Get command line options */ 1.45 + PLOptStatus os; 1.46 + PLOptState *opt = PL_CreateOptState(argc, argv, "vd"); 1.47 + 1.48 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.49 + { 1.50 + if (PL_OPT_BAD == os) continue; 1.51 + switch (opt->option) 1.52 + { 1.53 + case 'd': /* debug */ 1.54 + debug = 1; 1.55 + break; 1.56 + case 'v': /* verbose */ 1.57 + verbose = 1; 1.58 + break; 1.59 + default: 1.60 + break; 1.61 + } 1.62 + } 1.63 + PL_DestroyOptState(opt); 1.64 + } /* end block "Get command line options" */ 1.65 +/* ---------------------------------------------------------------------- */ 1.66 + fd = PR_Open( "/tmp/nsprAppend", (PR_APPEND | PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY), 0666 ); 1.67 + if ( NULL == fd ) { 1.68 + if (debug) printf("PR_Open() failed for writing: %d\n", PR_GetError()); 1.69 + failedAlready = PR_TRUE; 1.70 + goto Finished; 1.71 + } 1.72 + 1.73 + for ( i = 0; i < addedBytes ; i++ ) { 1.74 + rv = PR_Write( fd, &buf, sizeof(buf)); 1.75 + if ( sizeof(buf) != rv ) { 1.76 + if (debug) printf("PR_Write() failed: %d\n", PR_GetError()); 1.77 + failedAlready = PR_TRUE; 1.78 + goto Finished; 1.79 + } 1.80 + rv = PR_Seek( fd, 0 , PR_SEEK_SET ); 1.81 + if ( -1 == rv ) { 1.82 + if (debug) printf("PR_Seek() failed: %d\n", PR_GetError()); 1.83 + failedAlready = PR_TRUE; 1.84 + goto Finished; 1.85 + } 1.86 + } 1.87 + rc = PR_Close( fd ); 1.88 + if ( PR_FAILURE == rc ) { 1.89 + if (debug) printf("PR_Close() failed after writing: %d\n", PR_GetError()); 1.90 + failedAlready = PR_TRUE; 1.91 + goto Finished; 1.92 + } 1.93 +/* ---------------------------------------------------------------------- */ 1.94 + fd = PR_Open( "/tmp/nsprAppend", PR_RDONLY, 0 ); 1.95 + if ( NULL == fd ) { 1.96 + if (debug) printf("PR_Open() failed for reading: %d\n", PR_GetError()); 1.97 + failedAlready = PR_TRUE; 1.98 + goto Finished; 1.99 + } 1.100 + 1.101 + for ( i = 0; i < addedBytes ; i++ ) { 1.102 + rv = PR_Read( fd, &inBuf, sizeof(inBuf)); 1.103 + if ( sizeof(inBuf) != rv) { 1.104 + if (debug) printf("PR_Write() failed: %d\n", PR_GetError()); 1.105 + failedAlready = PR_TRUE; 1.106 + goto Finished; 1.107 + } 1.108 + sum += inBuf; 1.109 + } 1.110 + 1.111 + rc = PR_Close( fd ); 1.112 + if ( PR_FAILURE == rc ) { 1.113 + if (debug) printf("PR_Close() failed after reading: %d\n", PR_GetError()); 1.114 + failedAlready = PR_TRUE; 1.115 + goto Finished; 1.116 + } 1.117 + if ( sum != addedBytes ) { 1.118 + if (debug) printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes, sum); 1.119 + failedAlready = PR_TRUE; 1.120 + goto Finished; 1.121 + } 1.122 + 1.123 +/* ---------------------------------------------------------------------- */ 1.124 +Finished: 1.125 + if (debug || verbose) printf("%s\n", (failedAlready)? "FAILED" : "PASSED" ); 1.126 + return( (failedAlready)? 1 : 0 ); 1.127 +} /* main() */ 1.128 + 1.129 +/* append.c */