append.c (3963B)
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 PRStatus rc; 35 PRInt32 rv; 36 PRFileDesc* fd; 37 PRIntn i; 38 PRInt32 sum = 0; 39 40 { /* Get command line options */ 41 PLOptStatus os; 42 PLOptState* opt = PL_CreateOptState(argc, argv, "vd"); 43 44 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { 45 if (PL_OPT_BAD == os) { 46 continue; 47 } 48 switch (opt->option) { 49 case 'd': /* debug */ 50 debug = 1; 51 break; 52 case 'v': /* verbose */ 53 verbose = 1; 54 break; 55 default: 56 break; 57 } 58 } 59 PL_DestroyOptState(opt); 60 } /* end block "Get command line options" */ 61 /* ---------------------------------------------------------------------- */ 62 fd = PR_Open("./tmp-nsprAppend", 63 (PR_APPEND | PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY), 0666); 64 if (NULL == fd) { 65 if (debug) { 66 printf("PR_Open() failed for writing: %d\n", PR_GetError()); 67 } 68 failedAlready = PR_TRUE; 69 goto Finished; 70 } 71 72 for (i = 0; i < addedBytes; i++) { 73 rv = PR_Write(fd, &buf, sizeof(buf)); 74 if (sizeof(buf) != rv) { 75 if (debug) { 76 printf("PR_Write() failed: %d\n", PR_GetError()); 77 } 78 failedAlready = PR_TRUE; 79 goto Finished; 80 } 81 rv = PR_Seek(fd, 0, PR_SEEK_SET); 82 if (-1 == rv) { 83 if (debug) { 84 printf("PR_Seek() failed: %d\n", PR_GetError()); 85 } 86 failedAlready = PR_TRUE; 87 goto Finished; 88 } 89 } 90 rc = PR_Close(fd); 91 if (PR_FAILURE == rc) { 92 if (debug) { 93 printf("PR_Close() failed after writing: %d\n", PR_GetError()); 94 } 95 failedAlready = PR_TRUE; 96 goto Finished; 97 } 98 /* ---------------------------------------------------------------------- */ 99 fd = PR_Open("./tmp-nsprAppend", PR_RDONLY, 0); 100 if (NULL == fd) { 101 if (debug) { 102 printf("PR_Open() failed for reading: %d\n", PR_GetError()); 103 } 104 failedAlready = PR_TRUE; 105 goto Finished; 106 } 107 108 for (i = 0; i < addedBytes; i++) { 109 rv = PR_Read(fd, &inBuf, sizeof(inBuf)); 110 if (sizeof(inBuf) != rv) { 111 if (debug) { 112 printf("PR_Write() failed: %d\n", PR_GetError()); 113 } 114 failedAlready = PR_TRUE; 115 goto Finished; 116 } 117 sum += inBuf; 118 } 119 120 rc = PR_Close(fd); 121 if (PR_FAILURE == rc) { 122 if (debug) { 123 printf("PR_Close() failed after reading: %d\n", PR_GetError()); 124 } 125 failedAlready = PR_TRUE; 126 goto Finished; 127 } 128 if (sum != addedBytes) { 129 if (debug) { 130 printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes, sum); 131 } 132 failedAlready = PR_TRUE; 133 goto Finished; 134 } 135 136 /* ---------------------------------------------------------------------- */ 137 Finished: 138 if (debug || verbose) { 139 printf("%s\n", (failedAlready) ? "FAILED" : "PASSED"); 140 } 141 return ((failedAlready) ? 1 : 0); 142 } /* main() */ 143 144 /* append.c */