michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /*********************************************************************** michael@0: ** michael@0: ** Name: op_excl.c michael@0: ** michael@0: ** Description: Test Program to verify function of PR_EXCL open flag michael@0: ** michael@0: ** Modification History: michael@0: ** 27-Oct-1999 lth. Initial version michael@0: ***********************************************************************/ michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: /* michael@0: ** Test harness infrastructure michael@0: */ michael@0: PRLogModuleInfo *lm; michael@0: PRLogModuleLevel msgLevel = PR_LOG_NONE; michael@0: PRIntn debug = 0; michael@0: PRUint32 failed_already = 0; michael@0: /* end Test harness infrastructure */ michael@0: /* michael@0: ** Emit help text for this test michael@0: */ michael@0: static void Help( void ) michael@0: { michael@0: printf("op_excl: Help"); michael@0: printf("op_excl [-d]"); michael@0: printf("-d enables debug messages"); michael@0: exit(1); michael@0: } /* end Help() */ michael@0: michael@0: michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRFileDesc *fd; michael@0: PRStatus rv; michael@0: PRInt32 written; michael@0: char outBuf[] = "op_excl.c test file"; michael@0: #define OUT_SIZE sizeof(outBuf) michael@0: #define NEW_FILENAME "xxxExclNewFile" michael@0: michael@0: { michael@0: /* michael@0: ** Get command line options michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt = PL_CreateOptState(argc, argv, "hd"); michael@0: michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug */ michael@0: debug = 1; michael@0: msgLevel = PR_LOG_ERROR; michael@0: break; michael@0: case 'h': /* help message */ michael@0: Help(); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: } michael@0: michael@0: lm = PR_NewLogModule("Test"); /* Initialize logging */ michael@0: michael@0: /* michael@0: ** First, open a file, PR_EXCL, we believe not to exist michael@0: */ michael@0: fd = PR_Open( NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666 ); michael@0: if ( NULL == fd ) { michael@0: if (debug) fprintf( stderr, "Open exclusive. Expected success, got failure\n"); michael@0: failed_already = 1; michael@0: goto Finished; michael@0: } michael@0: michael@0: written = PR_Write( fd, outBuf, OUT_SIZE ); michael@0: if ( OUT_SIZE != written ) { michael@0: if (debug) fprintf( stderr, "Write after open exclusive failed\n"); michael@0: failed_already = 1; michael@0: goto Finished; michael@0: } michael@0: michael@0: rv = PR_Close(fd); michael@0: if ( PR_FAILURE == rv ) { michael@0: if (debug) fprintf( stderr, "Close after open exclusive failed\n"); michael@0: failed_already = 1; michael@0: goto Finished; michael@0: } michael@0: michael@0: /* michael@0: ** Second, open the same file, PR_EXCL, expect it to fail michael@0: */ michael@0: fd = PR_Open( NEW_FILENAME, PR_CREATE_FILE | PR_EXCL | PR_WRONLY, 0666 ); michael@0: if ( NULL != fd ) { michael@0: if (debug) fprintf( stderr, "Open exclusive. Expected failure, got success\n"); michael@0: failed_already = 1; michael@0: PR_Close(fd); michael@0: } michael@0: michael@0: rv = PR_Delete( NEW_FILENAME ); michael@0: if ( PR_FAILURE == rv ) { michael@0: if (debug) fprintf( stderr, "PR_Delete() failed\n"); michael@0: failed_already = 1; michael@0: } michael@0: michael@0: Finished: michael@0: if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS"); michael@0: return( (failed_already == PR_TRUE )? 1 : 0 ); michael@0: } /* main() */ michael@0: /* end op_excl.c */ michael@0: