nsprpub/pr/tests/fileio.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     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/. */
     6 /***********************************************************************
     7 **
     8 ** Name: fileio.c
     9 **
    10 ** Description: Program to copy one file to another. 
    11 **
    12 ** Modification History:
    13 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
    14 **	         The debug mode will print all of the printfs associated with this test.
    15 **			 The regress mode will be the default mode. Since the regress tool limits
    16 **           the output to a one line status:PASS or FAIL,all of the printf statements
    17 **			 have been handled with an if (debug_mode) statement.
    18 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
    19 **			recognize the return code from tha main program.
    20 ** 12-June-97 Revert to return code 0 and 1, remove debug option (obsolete).
    21 ***********************************************************************/
    23 /***********************************************************************
    24 ** Includes
    25 ***********************************************************************/
    26 #include "prinit.h"
    27 #include "prthread.h"
    28 #include "prlock.h"
    29 #include "prcvar.h"
    30 #include "prmon.h"
    31 #include "prmem.h"
    32 #include "prio.h"
    33 #include "prlog.h"
    35 #include <stdio.h>
    37 #include "obsolete/prsem.h"
    40 #define TBSIZE 1024
    42 static PRUint8 tbuf[TBSIZE];
    44 static PRFileDesc *t1, *t2;
    46 PRIntn failed_already=0;
    47 PRIntn debug_mode;
    48 static void InitialSetup(void)
    49 {
    50 	PRUintn	i;
    51 	PRInt32 nWritten, rv;
    53 	t1 = PR_Open("t1.tmp", PR_CREATE_FILE | PR_RDWR, 0);
    54 	PR_ASSERT(t1 != NULL);	
    56 	for (i=0; i<TBSIZE; i++)
    57 		tbuf[i] = i;
    59 	nWritten = PR_Write((PRFileDesc*)t1, tbuf, TBSIZE);
    60 	PR_ASSERT(nWritten == TBSIZE);	
    62 	rv = PR_Seek(t1,0,PR_SEEK_SET);
    63 	PR_ASSERT(rv == 0);	
    65    	t2 = PR_Open("t2.tmp", PR_CREATE_FILE | PR_RDWR, 0);
    66 	PR_ASSERT(t2 != NULL);	
    67 }
    70 static void VerifyAndCleanup(void)
    71 {
    72 	PRUintn	i;
    73 	PRInt32 nRead, rv;
    75 	for (i=0; i<TBSIZE; i++)
    76 		tbuf[i] = 0;
    78 	rv = PR_Seek(t2,0,PR_SEEK_SET);
    79 	PR_ASSERT(rv == 0);	
    81 	nRead = PR_Read((PRFileDesc*)t2, tbuf, TBSIZE);
    82 	PR_ASSERT(nRead == TBSIZE);	
    84 	for (i=0; i<TBSIZE; i++)
    85 		if (tbuf[i] != (PRUint8)i) {
    86 			if (debug_mode) printf("data mismatch for index= %d \n", i);
    87 			else failed_already=1;
    88 		}
    89    	PR_Close(t1);
    90    	PR_Close(t2);
    92    	PR_Delete("t1.tmp");
    93    	PR_Delete("t2.tmp");
    95     if (debug_mode) printf("fileio test passed\n");
    96 }
    99 /*------------------ Following is the real test program ---------*/
   100 /*
   101 	Program to copy one file to another.  Two temporary files get
   102 	created.  First one gets written in one write call.  Then,
   103 	a reader thread reads from this file into a double buffer.
   104 	The writer thread writes from double buffer into the other
   105 	temporary file.  The second temporary file gets verified
   106 	for accurate data.
   107 */
   109 PRSemaphore	*emptyBufs;	/* number of empty buffers */
   110 PRSemaphore *fullBufs;	/* number of buffers that are full */
   112 #define BSIZE	100
   114 struct {
   115 	char data[BSIZE];
   116 	PRUintn nbytes;		/* number of bytes in this buffer */
   117 } buf[2];
   119 static void PR_CALLBACK reader(void *arg)
   120 {
   121 	PRUintn	i = 0;
   122 	PRInt32	nbytes;
   124 	do {
   125 		(void) PR_WaitSem(emptyBufs);
   126 		nbytes = PR_Read((PRFileDesc*)arg, buf[i].data, BSIZE);
   127 		if (nbytes >= 0) {
   128 			buf[i].nbytes = nbytes;
   129 			PR_PostSem(fullBufs);
   130 			i = (i + 1) % 2;
   131 		}
   132 	} while (nbytes > 0);
   133 }
   135 static void PR_CALLBACK writer(void *arg)
   136 {
   137 	PRUintn	i = 0;
   138 	PRInt32	nbytes;
   140 	do {
   141 		(void) PR_WaitSem(fullBufs);
   142 		nbytes = buf[i].nbytes;
   143 		if (nbytes > 0) {
   144 			nbytes = PR_Write((PRFileDesc*)arg, buf[i].data, nbytes);
   145 			PR_PostSem(emptyBufs);
   146 			i = (i + 1) % 2;
   147 		}
   148 	} while (nbytes > 0);
   149 }
   151 int main(int argc, char **argv)
   152 {
   153 	PRThread *r, *w;
   156 	PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
   157     PR_STDIO_INIT();
   159     emptyBufs = PR_NewSem(2);	/* two empty buffers */
   161     fullBufs = PR_NewSem(0);	/* zero full buffers */
   163 	/* Create initial temp file setup */
   164 	InitialSetup();
   166 	/* create the reader thread */
   168 	r = PR_CreateThread(PR_USER_THREAD,
   169 				      reader, t1, 
   170 				      PR_PRIORITY_NORMAL,
   171 				      PR_LOCAL_THREAD,
   172     				  PR_JOINABLE_THREAD,
   173 				      0);
   175 	w = PR_CreateThread(PR_USER_THREAD,
   176 				      writer, t2, 
   177 				      PR_PRIORITY_NORMAL,
   178                       PR_LOCAL_THREAD,
   179                       PR_JOINABLE_THREAD,
   180                       0);
   182     /* Do the joining for both threads */
   183     (void) PR_JoinThread(r);
   184     (void) PR_JoinThread(w);
   186     /* Do the verification and clean up */
   187     VerifyAndCleanup();
   189     PR_DestroySem(emptyBufs);
   190     PR_DestroySem(fullBufs);
   192     PR_Cleanup();
   194     if(failed_already)
   195     {
   196         printf("Fail\n");
   197         return 1;
   198     }
   199     else
   200     {
   201         printf("PASS\n");
   202         return 0;
   203     }
   206 }

mercurial