1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/semaerr1.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,110 @@ 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 +#include "nspr.h" 1.10 +#include "plgetopt.h" 1.11 + 1.12 +#include <stdio.h> 1.13 + 1.14 +#ifdef SYMBIAN 1.15 +#define SEM_NAME1 "c:\\data\\foo.sem" 1.16 +#define SEM_NAME2 "c:\\data\\bar.sem" 1.17 +#else 1.18 +#define SEM_NAME1 "/tmp/foo.sem" 1.19 +#define SEM_NAME2 "/tmp/bar.sem" 1.20 +#endif 1.21 +#define SEM_MODE 0666 1.22 + 1.23 +static PRBool debug_mode = PR_FALSE; 1.24 + 1.25 +static void Help(void) 1.26 +{ 1.27 + fprintf(stderr, "semaerr1 test program usage:\n"); 1.28 + fprintf(stderr, "\t-d debug mode (FALSE)\n"); 1.29 + fprintf(stderr, "\t-h this message\n"); 1.30 +} /* Help */ 1.31 + 1.32 +int main(int argc, char **argv) 1.33 +{ 1.34 + PLOptStatus os; 1.35 + PLOptState *opt = PL_CreateOptState(argc, argv, "dh"); 1.36 + PRSem *sem; 1.37 + 1.38 + while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { 1.39 + if (PL_OPT_BAD == os) continue; 1.40 + switch (opt->option) { 1.41 + case 'd': /* debug mode */ 1.42 + debug_mode = PR_TRUE; 1.43 + break; 1.44 + case 'h': 1.45 + default: 1.46 + Help(); 1.47 + return 2; 1.48 + } 1.49 + } 1.50 + PL_DestroyOptState(opt); 1.51 + 1.52 + /* 1.53 + * PR_SEM_CREATE|PR_SEM_EXCL should be able to 1.54 + * create a nonexistent semaphore. 1.55 + */ 1.56 + (void) PR_DeleteSemaphore(SEM_NAME2); 1.57 + sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0); 1.58 + if (sem == NULL) { 1.59 + fprintf(stderr, "PR_OpenSemaphore failed\n"); 1.60 + exit(1); 1.61 + } 1.62 + if (PR_CloseSemaphore(sem) == PR_FAILURE) { 1.63 + fprintf(stderr, "PR_CloseSemaphore failed\n"); 1.64 + exit(1); 1.65 + } 1.66 + if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) { 1.67 + fprintf(stderr, "PR_DeleteSemaphore failed\n"); 1.68 + exit(1); 1.69 + } 1.70 + 1.71 + /* 1.72 + * Opening an existing semaphore with PR_SEM_CREATE|PR_SEM_EXCL. 1.73 + * should fail with PR_FILE_EXISTS_ERROR. 1.74 + */ 1.75 + sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0); 1.76 + if (sem != NULL) { 1.77 + fprintf(stderr, "PR_OpenSemaphore should fail but succeeded\n"); 1.78 + exit(1); 1.79 + } 1.80 + if (PR_GetError() != PR_FILE_EXISTS_ERROR) { 1.81 + fprintf(stderr, "Expect %d but got %d\n", PR_FILE_EXISTS_ERROR, 1.82 + PR_GetError()); 1.83 + exit(1); 1.84 + } 1.85 + 1.86 + /* 1.87 + * Try again, with just PR_SEM_CREATE. This should succeed. 1.88 + */ 1.89 + sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0); 1.90 + if (sem == NULL) { 1.91 + fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", 1.92 + PR_GetError(), PR_GetOSError()); 1.93 + exit(1); 1.94 + } 1.95 + if (PR_CloseSemaphore(sem) == PR_FAILURE) { 1.96 + fprintf(stderr, "PR_CloseSemaphore failed\n"); 1.97 + exit(1); 1.98 + } 1.99 + 1.100 + sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0); 1.101 + if (sem == NULL) { 1.102 + fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n", 1.103 + PR_GetError(), PR_GetOSError()); 1.104 + exit(1); 1.105 + } 1.106 + if (PR_CloseSemaphore(sem) == PR_FAILURE) { 1.107 + fprintf(stderr, "PR_CloseSemaphore failed\n"); 1.108 + exit(1); 1.109 + } 1.110 + 1.111 + printf("PASS\n"); 1.112 + return 0; 1.113 +}