nsprpub/pr/tests/pipeping.c

Wed, 31 Dec 2014 07:53:36 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:53:36 +0100
branch
TOR_BUG_3246
changeset 5
4ab42b5ab56c
permissions
-rw-r--r--

Correct small whitespace inconsistency, lost while renaming variables.

     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: pipeping.c
     8  *
     9  * Description:
    10  * This test runs in conjunction with the pipepong test.
    11  * This test creates two pipes and redirects the stdin and
    12  * stdout of the pipepong test to the pipes.  Then this
    13  * test writes "ping" to the pipepong test and the pipepong
    14  * test writes "pong" back.  To run this pair of tests,
    15  * just invoke pipeping.
    16  *
    17  * Tested areas: process creation, pipes, file descriptor
    18  * inheritance, standard I/O redirection.
    19  */
    21 #include "prerror.h"
    22 #include "prio.h"
    23 #include "prproces.h"
    25 #include <stdio.h>
    26 #include <stdlib.h>
    27 #include <string.h>
    29 #ifdef XP_OS2
    30 static char *child_argv[] = { "pipepong.exe", NULL };
    31 #else
    32 static char *child_argv[] = { "pipepong", NULL };
    33 #endif
    35 #define NUM_ITERATIONS 10
    37 int main(int argc, char **argv)
    38 {
    39     PRFileDesc *in_pipe[2];
    40     PRFileDesc *out_pipe[2];
    41     PRStatus status;
    42     PRProcess *process;
    43     PRProcessAttr *attr;
    44     char buf[1024];
    45     PRInt32 nBytes;
    46     PRInt32 exitCode;
    47     int idx;
    49     status = PR_CreatePipe(&in_pipe[0], &in_pipe[1]);
    50     if (status == PR_FAILURE) {
    51         fprintf(stderr, "PR_CreatePipe failed\n");
    52         exit(1);
    53     }
    54     status = PR_CreatePipe(&out_pipe[0], &out_pipe[1]);
    55     if (status == PR_FAILURE) {
    56         fprintf(stderr, "PR_CreatePipe failed\n");
    57         exit(1);
    58     }
    60     status = PR_SetFDInheritable(in_pipe[0], PR_FALSE);
    61     if (status == PR_FAILURE) {
    62         fprintf(stderr, "PR_SetFDInheritable failed\n");
    63         exit(1);
    64     }
    65     status = PR_SetFDInheritable(in_pipe[1], PR_TRUE);
    66     if (status == PR_FAILURE) {
    67         fprintf(stderr, "PR_SetFDInheritable failed\n");
    68         exit(1);
    69     }
    70     status = PR_SetFDInheritable(out_pipe[0], PR_TRUE);
    71     if (status == PR_FAILURE) {
    72         fprintf(stderr, "PR_SetFDInheritable failed\n");
    73         exit(1);
    74     }
    75     status = PR_SetFDInheritable(out_pipe[1], PR_FALSE);
    76     if (status == PR_FAILURE) {
    77         fprintf(stderr, "PR_SetFDInheritable failed\n");
    78         exit(1);
    79     }
    81     attr = PR_NewProcessAttr();
    82     if (attr == NULL) {
    83         fprintf(stderr, "PR_NewProcessAttr failed\n");
    84         exit(1);
    85     }
    87     PR_ProcessAttrSetStdioRedirect(attr, PR_StandardInput, out_pipe[0]);
    88     PR_ProcessAttrSetStdioRedirect(attr, PR_StandardOutput, in_pipe[1]);
    90     process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr);
    91     if (process == NULL) {
    92         fprintf(stderr, "PR_CreateProcess failed\n");
    93         exit(1);
    94     }
    95     PR_DestroyProcessAttr(attr);
    96     status = PR_Close(out_pipe[0]);
    97     if (status == PR_FAILURE) {
    98         fprintf(stderr, "PR_Close failed\n");
    99         exit(1);
   100     }
   101     status = PR_Close(in_pipe[1]);
   102     if (status == PR_FAILURE) {
   103         fprintf(stderr, "PR_Close failed\n");
   104         exit(1);
   105     }
   107     for (idx = 0; idx < NUM_ITERATIONS; idx++) {
   108         strcpy(buf, "ping");
   109         printf("ping process: sending \"%s\"\n", buf);
   110         nBytes = PR_Write(out_pipe[1], buf, 5);
   111         if (nBytes == -1) {
   112             fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
   113                     PR_GetOSError());
   114             exit(1);
   115         }
   116         memset(buf, 0, sizeof(buf));
   117         nBytes = PR_Read(in_pipe[0], buf, sizeof(buf));
   118         if (nBytes == -1) {
   119             fprintf(stderr, "PR_Read failed: (%d, %d)\n",
   120                     PR_GetError(), PR_GetOSError());
   121             exit(1);
   122         }
   123         printf("ping process: received \"%s\"\n", buf);
   124         if (nBytes != 5) {
   125             fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n",
   126                     nBytes);
   127             exit(1);
   128         }
   129         if (strcmp(buf, "pong") != 0) {
   130             fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n",
   131                     buf);
   132             exit(1);
   133         }
   134     }
   136     status = PR_Close(in_pipe[0]);
   137     if (status == PR_FAILURE) {
   138         fprintf(stderr, "PR_Close failed\n");
   139         exit(1);
   140     }
   141     status = PR_Close(out_pipe[1]);
   142     if (status == PR_FAILURE) {
   143         fprintf(stderr, "PR_Close failed\n");
   144         exit(1);
   145     }
   146     status = PR_WaitProcess(process, &exitCode);
   147     if (status == PR_FAILURE) {
   148         fprintf(stderr, "PR_WaitProcess failed\n");
   149         exit(1);
   150     }
   151     if (exitCode == 0) {
   152         printf("PASS\n");
   153         return 0;
   154     } else {
   155         printf("FAIL\n");
   156         return 1;
   157     }
   158 }

mercurial