nsprpub/pr/tests/pipeping.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial