nsprpub/pr/tests/pipepong2.c

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

     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 /*
     7  * File: pipepong2.c
     8  *
     9  * Description:
    10  * This test runs in conjunction with the pipeping2 test.
    11  * The pipeping2 test creates two pipes and passes two
    12  * pipe fd's to this test.  Then the pipeping2 test writes
    13  * "ping" to this test and this test writes "pong" back.
    14  * To run this pair of tests, just invoke pipeping2.
    15  *
    16  * Tested areas: process creation, pipes, file descriptor
    17  * inheritance.
    18  */
    20 #include "prerror.h"
    21 #include "prio.h"
    23 #include <stdio.h>
    24 #include <string.h>
    25 #include <stdlib.h>
    27 #define NUM_ITERATIONS 10
    29 int main(int argc, char **argv)
    30 {
    31     PRFileDesc *pipe_read, *pipe_write;
    32     PRStatus status;
    33     char buf[1024];
    34     PRInt32 nBytes;
    35     int idx;
    37     pipe_read = PR_GetInheritedFD("PIPE_READ");
    38     if (pipe_read == NULL) {
    39         fprintf(stderr, "PR_GetInheritedFD failed\n");
    40         exit(1);
    41     }
    42     status = PR_SetFDInheritable(pipe_read, PR_FALSE);
    43     if (status == PR_FAILURE) {
    44         fprintf(stderr, "PR_SetFDInheritable failed\n");
    45         exit(1);
    46     }
    47     pipe_write = PR_GetInheritedFD("PIPE_WRITE");
    48     if (pipe_write == NULL) {
    49         fprintf(stderr, "PR_GetInheritedFD failed\n");
    50         exit(1);
    51     }
    52     status = PR_SetFDInheritable(pipe_write, PR_FALSE);
    53     if (status == PR_FAILURE) {
    54         fprintf(stderr, "PR_SetFDInheritable failed\n");
    55         exit(1);
    56     }
    58     for (idx = 0; idx < NUM_ITERATIONS; idx++) {
    59         memset(buf, 0, sizeof(buf));
    60         nBytes = PR_Read(pipe_read, buf, sizeof(buf));
    61         if (nBytes == -1) {
    62             fprintf(stderr, "PR_Read failed: (%d, %d)\n",
    63                     PR_GetError(), PR_GetOSError());
    64             exit(1);
    65         }
    66         printf("pong process: received \"%s\"\n", buf);
    67         if (nBytes != 5) {
    68             fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
    69                     nBytes);
    70             exit(1);
    71         }
    72         if (strcmp(buf, "ping") != 0) {
    73             fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
    74                     buf);
    75             exit(1);
    76         }
    78         strcpy(buf, "pong");
    79         printf("pong process: sending \"%s\"\n", buf);
    80         nBytes = PR_Write(pipe_write, buf, 5);
    81         if (nBytes == -1) {
    82             fprintf(stderr, "PR_Write failed\n");
    83             exit(1);
    84         }
    85     }
    87     status = PR_Close(pipe_read);
    88     if (status == PR_FAILURE) {
    89         fprintf(stderr, "PR_Close failed\n");
    90         exit(1);
    91     }
    92     status = PR_Close(pipe_write);
    93     if (status == PR_FAILURE) {
    94         fprintf(stderr, "PR_Close failed\n");
    95         exit(1);
    96     }
    97     return 0;
    98 }

mercurial