nsprpub/pr/tests/sigpipe.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/sigpipe.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     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 + *
    1.12 + * Test: sigpipe.c
    1.13 + *
    1.14 + *     Test the SIGPIPE handler in NSPR.  This test applies to Unix only.
    1.15 + *
    1.16 + *************************************************************************
    1.17 + */
    1.18 +
    1.19 +#if !defined(XP_UNIX) && !defined(XP_OS2)
    1.20 +
    1.21 +int main(void)
    1.22 +{
    1.23 +    /* This test applies to Unix and OS/2. */
    1.24 +    return 0;
    1.25 +}
    1.26 +
    1.27 +#else /* XP_UNIX && OS/2 */
    1.28 +
    1.29 +#include "nspr.h"
    1.30 +
    1.31 +#ifdef XP_OS2
    1.32 +#define INCL_DOSQUEUES
    1.33 +#define INCL_DOSERRORS
    1.34 +#include <os2.h>
    1.35 +#endif
    1.36 +
    1.37 +#include <stdio.h>
    1.38 +#include <unistd.h>
    1.39 +#include <errno.h>
    1.40 +
    1.41 +static void Test(void *arg)
    1.42 +{
    1.43 +#ifdef XP_OS2
    1.44 +    HFILE pipefd[2];
    1.45 +#else
    1.46 +    int pipefd[2];
    1.47 +#endif
    1.48 +    int rv;
    1.49 +    char c = '\0';
    1.50 +
    1.51 +#ifdef XP_OS2
    1.52 +    if (DosCreatePipe(&pipefd[0], &pipefd[1], 4096) != 0) {
    1.53 +#else
    1.54 +    if (pipe(pipefd) == -1) {
    1.55 +#endif
    1.56 +        fprintf(stderr, "cannot create pipe: %d\n", errno);
    1.57 +        exit(1);
    1.58 +    }
    1.59 +    close(pipefd[0]);
    1.60 +
    1.61 +    rv = write(pipefd[1], &c, 1);
    1.62 +    if (rv != -1) {
    1.63 +        fprintf(stderr, "write to broken pipe should have failed with EPIPE but returned %d\n", rv);
    1.64 +        exit(1);
    1.65 +    }
    1.66 +#ifdef SYMBIAN
    1.67 +    /* Have mercy on the unknown 142 errno, it seems ok */
    1.68 +    if (errno != EPIPE && errno != 142) {
    1.69 +#else
    1.70 +    if (errno != EPIPE) {
    1.71 +#endif
    1.72 +        fprintf(stderr, "write to broken pipe failed but with wrong errno: %d\n", errno);
    1.73 +        exit(1);
    1.74 +    }
    1.75 +    close(pipefd[1]);
    1.76 +    printf("write to broken pipe failed with EPIPE, as expected\n");
    1.77 +}
    1.78 +
    1.79 +int main(int argc, char **argv)
    1.80 +{
    1.81 +    PRThread *thread;
    1.82 +
    1.83 +    /* This initializes NSPR. */
    1.84 +    PR_SetError(0, 0);
    1.85 +
    1.86 +    thread = PR_CreateThread(PR_USER_THREAD, Test, NULL, PR_PRIORITY_NORMAL,
    1.87 +            PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
    1.88 +    if (thread == NULL) {
    1.89 +        fprintf(stderr, "PR_CreateThread failed\n");
    1.90 +        exit(1);
    1.91 +    }
    1.92 +    if (PR_JoinThread(thread) == PR_FAILURE) {
    1.93 +        fprintf(stderr, "PR_JoinThread failed\n");
    1.94 +        exit(1);
    1.95 +    }
    1.96 +    Test(NULL);
    1.97 +
    1.98 +    printf("PASSED\n");
    1.99 +    return 0;
   1.100 +}
   1.101 +
   1.102 +#endif /* XP_UNIX */

mercurial