tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

op_excl.c (3149B)


      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 **
      8 ** Name: op_excl.c
      9 **
     10 ** Description: Test Program to verify function of PR_EXCL open flag
     11 **
     12 ** Modification History:
     13 ** 27-Oct-1999 lth. Initial version
     14 ***********************************************************************/
     15 
     16 #include <plgetopt.h>
     17 #include <nspr.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 
     21 /*
     22 ** Test harness infrastructure
     23 */
     24 PRLogModuleInfo* lm;
     25 PRLogModuleLevel msgLevel = PR_LOG_NONE;
     26 PRIntn debug = 0;
     27 PRUint32 failed_already = 0;
     28 /* end Test harness infrastructure */
     29 /*
     30 ** Emit help text for this test
     31 */
     32 static void Help(void) {
     33  printf("op_excl: Help");
     34  printf("op_excl [-d]");
     35  printf("-d enables debug messages");
     36  exit(1);
     37 } /* end Help() */
     38 
     39 int main(int argc, char** argv) {
     40  PRFileDesc* fd;
     41  PRStatus rv;
     42  PRInt32 written;
     43  char outBuf[] = "op_excl.c test file";
     44 #define OUT_SIZE sizeof(outBuf)
     45 #define NEW_FILENAME "xxxExclNewFile"
     46 
     47  {
     48    /*
     49    ** Get command line options
     50    */
     51    PLOptStatus os;
     52    PLOptState* opt = PL_CreateOptState(argc, argv, "hd");
     53 
     54    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
     55      if (PL_OPT_BAD == os) {
     56        continue;
     57      }
     58      switch (opt->option) {
     59        case 'd': /* debug */
     60          debug = 1;
     61          msgLevel = PR_LOG_ERROR;
     62          break;
     63        case 'h': /* help message */
     64          Help();
     65          break;
     66        default:
     67          break;
     68      }
     69    }
     70    PL_DestroyOptState(opt);
     71  }
     72 
     73  lm = PR_NewLogModule("Test"); /* Initialize logging */
     74 
     75  /*
     76  ** First, open a file, PR_EXCL, we believe not to exist
     77  */
     78  fd = PR_Open(NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666);
     79  if (NULL == fd) {
     80    if (debug) {
     81      fprintf(stderr, "Open exclusive. Expected success, got failure\n");
     82    }
     83    failed_already = 1;
     84    goto Finished;
     85  }
     86 
     87  written = PR_Write(fd, outBuf, OUT_SIZE);
     88  if (OUT_SIZE != written) {
     89    if (debug) {
     90      fprintf(stderr, "Write after open exclusive failed\n");
     91    }
     92    failed_already = 1;
     93    goto Finished;
     94  }
     95 
     96  rv = PR_Close(fd);
     97  if (PR_FAILURE == rv) {
     98    if (debug) {
     99      fprintf(stderr, "Close after open exclusive failed\n");
    100    }
    101    failed_already = 1;
    102    goto Finished;
    103  }
    104 
    105  /*
    106  ** Second, open the same file, PR_EXCL, expect it to fail
    107  */
    108  fd = PR_Open(NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666);
    109  if (NULL != fd) {
    110    if (debug) {
    111      fprintf(stderr, "Open exclusive. Expected failure, got success\n");
    112    }
    113    failed_already = 1;
    114    PR_Close(fd);
    115  }
    116 
    117  rv = PR_Delete(NEW_FILENAME);
    118  if (PR_FAILURE == rv) {
    119    if (debug) {
    120      fprintf(stderr, "PR_Delete() failed\n");
    121    }
    122    failed_already = 1;
    123  }
    124 
    125 Finished:
    126  if (debug) {
    127    printf("%s\n", (failed_already) ? "FAIL" : "PASS");
    128  }
    129  return ((failed_already == PR_TRUE) ? 1 : 0);
    130 } /* main() */
    131 /* end op_excl.c */