1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/op_excl.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,124 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/*********************************************************************** 1.10 +** 1.11 +** Name: op_excl.c 1.12 +** 1.13 +** Description: Test Program to verify function of PR_EXCL open flag 1.14 +** 1.15 +** Modification History: 1.16 +** 27-Oct-1999 lth. Initial version 1.17 +***********************************************************************/ 1.18 + 1.19 +#include <plgetopt.h> 1.20 +#include <nspr.h> 1.21 +#include <stdio.h> 1.22 +#include <stdlib.h> 1.23 + 1.24 +/* 1.25 +** Test harness infrastructure 1.26 +*/ 1.27 +PRLogModuleInfo *lm; 1.28 +PRLogModuleLevel msgLevel = PR_LOG_NONE; 1.29 +PRIntn debug = 0; 1.30 +PRUint32 failed_already = 0; 1.31 +/* end Test harness infrastructure */ 1.32 +/* 1.33 +** Emit help text for this test 1.34 +*/ 1.35 +static void Help( void ) 1.36 +{ 1.37 + printf("op_excl: Help"); 1.38 + printf("op_excl [-d]"); 1.39 + printf("-d enables debug messages"); 1.40 + exit(1); 1.41 +} /* end Help() */ 1.42 + 1.43 + 1.44 + 1.45 +int main(int argc, char **argv) 1.46 +{ 1.47 + PRFileDesc *fd; 1.48 + PRStatus rv; 1.49 + PRInt32 written; 1.50 + char outBuf[] = "op_excl.c test file"; 1.51 +#define OUT_SIZE sizeof(outBuf) 1.52 +#define NEW_FILENAME "xxxExclNewFile" 1.53 + 1.54 + { 1.55 + /* 1.56 + ** Get command line options 1.57 + */ 1.58 + PLOptStatus os; 1.59 + PLOptState *opt = PL_CreateOptState(argc, argv, "hd"); 1.60 + 1.61 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) 1.62 + { 1.63 + if (PL_OPT_BAD == os) continue; 1.64 + switch (opt->option) 1.65 + { 1.66 + case 'd': /* debug */ 1.67 + debug = 1; 1.68 + msgLevel = PR_LOG_ERROR; 1.69 + break; 1.70 + case 'h': /* help message */ 1.71 + Help(); 1.72 + break; 1.73 + default: 1.74 + break; 1.75 + } 1.76 + } 1.77 + PL_DestroyOptState(opt); 1.78 + } 1.79 + 1.80 + lm = PR_NewLogModule("Test"); /* Initialize logging */ 1.81 + 1.82 + /* 1.83 + ** First, open a file, PR_EXCL, we believe not to exist 1.84 + */ 1.85 + fd = PR_Open( NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666 ); 1.86 + if ( NULL == fd ) { 1.87 + if (debug) fprintf( stderr, "Open exclusive. Expected success, got failure\n"); 1.88 + failed_already = 1; 1.89 + goto Finished; 1.90 + } 1.91 + 1.92 + written = PR_Write( fd, outBuf, OUT_SIZE ); 1.93 + if ( OUT_SIZE != written ) { 1.94 + if (debug) fprintf( stderr, "Write after open exclusive failed\n"); 1.95 + failed_already = 1; 1.96 + goto Finished; 1.97 + } 1.98 + 1.99 + rv = PR_Close(fd); 1.100 + if ( PR_FAILURE == rv ) { 1.101 + if (debug) fprintf( stderr, "Close after open exclusive failed\n"); 1.102 + failed_already = 1; 1.103 + goto Finished; 1.104 + } 1.105 + 1.106 + /* 1.107 + ** Second, open the same file, PR_EXCL, expect it to fail 1.108 + */ 1.109 + fd = PR_Open( NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666 ); 1.110 + if ( NULL != fd ) { 1.111 + if (debug) fprintf( stderr, "Open exclusive. Expected failure, got success\n"); 1.112 + failed_already = 1; 1.113 + PR_Close(fd); 1.114 + } 1.115 + 1.116 + rv = PR_Delete( NEW_FILENAME ); 1.117 + if ( PR_FAILURE == rv ) { 1.118 + if (debug) fprintf( stderr, "PR_Delete() failed\n"); 1.119 + failed_already = 1; 1.120 + } 1.121 + 1.122 +Finished: 1.123 + if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS"); 1.124 + return( (failed_already == PR_TRUE )? 1 : 0 ); 1.125 +} /* main() */ 1.126 +/* end op_excl.c */ 1.127 +