nsprpub/pr/tests/append.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 ** File: append.c
michael@0 8 ** Description: Testing File writes where PR_APPEND was used on open
michael@0 9 **
michael@0 10 ** append attempts to verify that a file opened with PR_APPEND
michael@0 11 ** will always append to the end of file, regardless where the
michael@0 12 ** current file pointer is positioned. To do this, PR_Seek() is
michael@0 13 ** called before each write with the position set to beginning of
michael@0 14 ** file. Subsequent writes should always append.
michael@0 15 ** The file is read back, summing the integer data written to the
michael@0 16 ** file. If the expected result is equal, the test passes.
michael@0 17 **
michael@0 18 ** See BugSplat: 4090
michael@0 19 */
michael@0 20 #include "plgetopt.h"
michael@0 21 #include "nspr.h"
michael@0 22
michael@0 23 #include <stdio.h>
michael@0 24 #include <stdlib.h>
michael@0 25
michael@0 26 PRIntn debug = 0;
michael@0 27 PRIntn verbose = 0;
michael@0 28 PRBool failedAlready = PR_FALSE;
michael@0 29 const PRInt32 addedBytes = 1000;
michael@0 30 const PRInt32 buf = 1; /* constant written to fd, addedBytes times */
michael@0 31 PRInt32 inBuf; /* read it back into here */
michael@0 32
michael@0 33 int main(int argc, char **argv)
michael@0 34 {
michael@0 35 PRStatus rc;
michael@0 36 PRInt32 rv;
michael@0 37 PRFileDesc *fd;
michael@0 38 PRIntn i;
michael@0 39 PRInt32 sum = 0;
michael@0 40
michael@0 41 { /* Get command line options */
michael@0 42 PLOptStatus os;
michael@0 43 PLOptState *opt = PL_CreateOptState(argc, argv, "vd");
michael@0 44
michael@0 45 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
michael@0 46 {
michael@0 47 if (PL_OPT_BAD == os) continue;
michael@0 48 switch (opt->option)
michael@0 49 {
michael@0 50 case 'd': /* debug */
michael@0 51 debug = 1;
michael@0 52 break;
michael@0 53 case 'v': /* verbose */
michael@0 54 verbose = 1;
michael@0 55 break;
michael@0 56 default:
michael@0 57 break;
michael@0 58 }
michael@0 59 }
michael@0 60 PL_DestroyOptState(opt);
michael@0 61 } /* end block "Get command line options" */
michael@0 62 /* ---------------------------------------------------------------------- */
michael@0 63 fd = PR_Open( "/tmp/nsprAppend", (PR_APPEND | PR_CREATE_FILE | PR_TRUNCATE | PR_WRONLY), 0666 );
michael@0 64 if ( NULL == fd ) {
michael@0 65 if (debug) printf("PR_Open() failed for writing: %d\n", PR_GetError());
michael@0 66 failedAlready = PR_TRUE;
michael@0 67 goto Finished;
michael@0 68 }
michael@0 69
michael@0 70 for ( i = 0; i < addedBytes ; i++ ) {
michael@0 71 rv = PR_Write( fd, &buf, sizeof(buf));
michael@0 72 if ( sizeof(buf) != rv ) {
michael@0 73 if (debug) printf("PR_Write() failed: %d\n", PR_GetError());
michael@0 74 failedAlready = PR_TRUE;
michael@0 75 goto Finished;
michael@0 76 }
michael@0 77 rv = PR_Seek( fd, 0 , PR_SEEK_SET );
michael@0 78 if ( -1 == rv ) {
michael@0 79 if (debug) printf("PR_Seek() failed: %d\n", PR_GetError());
michael@0 80 failedAlready = PR_TRUE;
michael@0 81 goto Finished;
michael@0 82 }
michael@0 83 }
michael@0 84 rc = PR_Close( fd );
michael@0 85 if ( PR_FAILURE == rc ) {
michael@0 86 if (debug) printf("PR_Close() failed after writing: %d\n", PR_GetError());
michael@0 87 failedAlready = PR_TRUE;
michael@0 88 goto Finished;
michael@0 89 }
michael@0 90 /* ---------------------------------------------------------------------- */
michael@0 91 fd = PR_Open( "/tmp/nsprAppend", PR_RDONLY, 0 );
michael@0 92 if ( NULL == fd ) {
michael@0 93 if (debug) printf("PR_Open() failed for reading: %d\n", PR_GetError());
michael@0 94 failedAlready = PR_TRUE;
michael@0 95 goto Finished;
michael@0 96 }
michael@0 97
michael@0 98 for ( i = 0; i < addedBytes ; i++ ) {
michael@0 99 rv = PR_Read( fd, &inBuf, sizeof(inBuf));
michael@0 100 if ( sizeof(inBuf) != rv) {
michael@0 101 if (debug) printf("PR_Write() failed: %d\n", PR_GetError());
michael@0 102 failedAlready = PR_TRUE;
michael@0 103 goto Finished;
michael@0 104 }
michael@0 105 sum += inBuf;
michael@0 106 }
michael@0 107
michael@0 108 rc = PR_Close( fd );
michael@0 109 if ( PR_FAILURE == rc ) {
michael@0 110 if (debug) printf("PR_Close() failed after reading: %d\n", PR_GetError());
michael@0 111 failedAlready = PR_TRUE;
michael@0 112 goto Finished;
michael@0 113 }
michael@0 114 if ( sum != addedBytes ) {
michael@0 115 if (debug) printf("Uh Oh! addedBytes: %d. Sum: %d\n", addedBytes, sum);
michael@0 116 failedAlready = PR_TRUE;
michael@0 117 goto Finished;
michael@0 118 }
michael@0 119
michael@0 120 /* ---------------------------------------------------------------------- */
michael@0 121 Finished:
michael@0 122 if (debug || verbose) printf("%s\n", (failedAlready)? "FAILED" : "PASSED" );
michael@0 123 return( (failedAlready)? 1 : 0 );
michael@0 124 } /* main() */
michael@0 125
michael@0 126 /* append.c */

mercurial