nsprpub/pr/tests/semaerr1.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     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/. */
     6 #include "nspr.h"
     7 #include "plgetopt.h"
     9 #include <stdio.h>
    11 #ifdef SYMBIAN
    12 #define SEM_NAME1 "c:\\data\\foo.sem"
    13 #define SEM_NAME2 "c:\\data\\bar.sem"
    14 #else
    15 #define SEM_NAME1 "/tmp/foo.sem"
    16 #define SEM_NAME2 "/tmp/bar.sem"
    17 #endif
    18 #define SEM_MODE  0666
    20 static PRBool debug_mode = PR_FALSE;
    22 static void Help(void)
    23 {
    24     fprintf(stderr, "semaerr1 test program usage:\n");
    25     fprintf(stderr, "\t-d           debug mode         (FALSE)\n");
    26     fprintf(stderr, "\t-h           this message\n");
    27 }  /* Help */
    29 int main(int argc, char **argv)
    30 {
    31     PLOptStatus os;
    32     PLOptState *opt = PL_CreateOptState(argc, argv, "dh");
    33     PRSem *sem;
    35     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) {
    36         if (PL_OPT_BAD == os) continue;
    37         switch (opt->option) {
    38             case 'd':  /* debug mode */
    39                 debug_mode = PR_TRUE;
    40                 break;
    41             case 'h':
    42             default:
    43                 Help();
    44                 return 2;
    45         }
    46     }
    47     PL_DestroyOptState(opt);
    49     /*
    50      * PR_SEM_CREATE|PR_SEM_EXCL should be able to
    51      * create a nonexistent semaphore.
    52      */
    53     (void) PR_DeleteSemaphore(SEM_NAME2);
    54     sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
    55     if (sem == NULL) {
    56         fprintf(stderr, "PR_OpenSemaphore failed\n");
    57         exit(1);
    58     }
    59     if (PR_CloseSemaphore(sem) == PR_FAILURE) {
    60         fprintf(stderr, "PR_CloseSemaphore failed\n");
    61         exit(1);
    62     }
    63     if (PR_DeleteSemaphore(SEM_NAME2) == PR_FAILURE) {
    64         fprintf(stderr, "PR_DeleteSemaphore failed\n");
    65         exit(1);
    66     }
    68     /*
    69      * Opening an existing semaphore with PR_SEM_CREATE|PR_SEM_EXCL.
    70      * should fail with PR_FILE_EXISTS_ERROR.
    71      */
    72     sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
    73     if (sem != NULL) {
    74         fprintf(stderr, "PR_OpenSemaphore should fail but succeeded\n");
    75         exit(1);
    76     }
    77     if (PR_GetError() != PR_FILE_EXISTS_ERROR) {
    78         fprintf(stderr, "Expect %d but got %d\n", PR_FILE_EXISTS_ERROR,
    79                 PR_GetError());
    80         exit(1);
    81     }
    83     /*
    84      * Try again, with just PR_SEM_CREATE.  This should succeed.
    85      */
    86     sem = PR_OpenSemaphore(SEM_NAME1, PR_SEM_CREATE, SEM_MODE, 0);
    87     if (sem == NULL) {
    88         fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
    89                 PR_GetError(), PR_GetOSError());
    90         exit(1);
    91     }
    92     if (PR_CloseSemaphore(sem) == PR_FAILURE) {
    93         fprintf(stderr, "PR_CloseSemaphore failed\n");
    94         exit(1);
    95     }
    97     sem = PR_OpenSemaphore(SEM_NAME2, PR_SEM_CREATE|PR_SEM_EXCL, SEM_MODE, 0);
    98     if (sem == NULL) {
    99         fprintf(stderr, "PR_OpenSemaphore failed (%d, %d)\n",
   100                 PR_GetError(), PR_GetOSError());
   101         exit(1);
   102     }
   103     if (PR_CloseSemaphore(sem) == PR_FAILURE) {
   104         fprintf(stderr, "PR_CloseSemaphore failed\n");
   105         exit(1);
   106     }
   108     printf("PASS\n");
   109     return 0;
   110 }

mercurial