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