nsprpub/pr/tests/pipeping2.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.

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

mercurial