1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/pipeping2.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,160 @@ 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 + * File: pipeping2.c 1.11 + * 1.12 + * Description: 1.13 + * This test runs in conjunction with the pipepong2 test. 1.14 + * This test creates two pipes and passes two pipe fd's 1.15 + * to the pipepong2 test. Then this test writes "ping" to 1.16 + * to the pipepong2 test and the pipepong2 test writes "pong" 1.17 + * back. To run this pair of tests, just invoke pipeping2. 1.18 + * 1.19 + * Tested areas: process creation, pipes, file descriptor 1.20 + * inheritance. 1.21 + */ 1.22 + 1.23 +#include "prerror.h" 1.24 +#include "prio.h" 1.25 +#include "prproces.h" 1.26 + 1.27 +#include <stdio.h> 1.28 +#include <string.h> 1.29 +#include <stdlib.h> 1.30 + 1.31 +#define NUM_ITERATIONS 10 1.32 + 1.33 +static char *child_argv[] = { "pipepong2", NULL }; 1.34 + 1.35 +int main(int argc, char **argv) 1.36 +{ 1.37 + PRFileDesc *in_pipe[2]; 1.38 + PRFileDesc *out_pipe[2]; 1.39 + PRStatus status; 1.40 + PRProcess *process; 1.41 + PRProcessAttr *attr; 1.42 + char buf[1024]; 1.43 + PRInt32 nBytes; 1.44 + PRInt32 exitCode; 1.45 + int idx; 1.46 + 1.47 + status = PR_CreatePipe(&in_pipe[0], &in_pipe[1]); 1.48 + if (status == PR_FAILURE) { 1.49 + fprintf(stderr, "PR_CreatePipe failed\n"); 1.50 + exit(1); 1.51 + } 1.52 + status = PR_CreatePipe(&out_pipe[0], &out_pipe[1]); 1.53 + if (status == PR_FAILURE) { 1.54 + fprintf(stderr, "PR_CreatePipe failed\n"); 1.55 + exit(1); 1.56 + } 1.57 + 1.58 + status = PR_SetFDInheritable(in_pipe[0], PR_FALSE); 1.59 + if (status == PR_FAILURE) { 1.60 + fprintf(stderr, "PR_SetFDInheritable failed\n"); 1.61 + exit(1); 1.62 + } 1.63 + status = PR_SetFDInheritable(in_pipe[1], PR_TRUE); 1.64 + if (status == PR_FAILURE) { 1.65 + fprintf(stderr, "PR_SetFDInheritable failed\n"); 1.66 + exit(1); 1.67 + } 1.68 + status = PR_SetFDInheritable(out_pipe[0], PR_TRUE); 1.69 + if (status == PR_FAILURE) { 1.70 + fprintf(stderr, "PR_SetFDInheritable failed\n"); 1.71 + exit(1); 1.72 + } 1.73 + status = PR_SetFDInheritable(out_pipe[1], PR_FALSE); 1.74 + if (status == PR_FAILURE) { 1.75 + fprintf(stderr, "PR_SetFDInheritable failed\n"); 1.76 + exit(1); 1.77 + } 1.78 + 1.79 + attr = PR_NewProcessAttr(); 1.80 + if (attr == NULL) { 1.81 + fprintf(stderr, "PR_NewProcessAttr failed\n"); 1.82 + exit(1); 1.83 + } 1.84 + 1.85 + status = PR_ProcessAttrSetInheritableFD(attr, out_pipe[0], "PIPE_READ"); 1.86 + if (status == PR_FAILURE) { 1.87 + fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n"); 1.88 + exit(1); 1.89 + } 1.90 + status = PR_ProcessAttrSetInheritableFD(attr, in_pipe[1], "PIPE_WRITE"); 1.91 + if (status == PR_FAILURE) { 1.92 + fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n"); 1.93 + exit(1); 1.94 + } 1.95 + 1.96 + process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr); 1.97 + if (process == NULL) { 1.98 + fprintf(stderr, "PR_CreateProcess failed\n"); 1.99 + exit(1); 1.100 + } 1.101 + PR_DestroyProcessAttr(attr); 1.102 + status = PR_Close(out_pipe[0]); 1.103 + if (status == PR_FAILURE) { 1.104 + fprintf(stderr, "PR_Close failed\n"); 1.105 + exit(1); 1.106 + } 1.107 + status = PR_Close(in_pipe[1]); 1.108 + if (status == PR_FAILURE) { 1.109 + fprintf(stderr, "PR_Close failed\n"); 1.110 + exit(1); 1.111 + } 1.112 + 1.113 + for (idx = 0; idx < NUM_ITERATIONS; idx++) { 1.114 + strcpy(buf, "ping"); 1.115 + printf("ping process: sending \"%s\"\n", buf); 1.116 + nBytes = PR_Write(out_pipe[1], buf, 5); 1.117 + if (nBytes == -1) { 1.118 + fprintf(stderr, "PR_Write failed: (%d, %d)\n", 1.119 + PR_GetError(), PR_GetOSError()); 1.120 + exit(1); 1.121 + } 1.122 + memset(buf, 0, sizeof(buf)); 1.123 + nBytes = PR_Read(in_pipe[0], buf, sizeof(buf)); 1.124 + if (nBytes == -1) { 1.125 + fprintf(stderr, "PR_Read failed\n"); 1.126 + exit(1); 1.127 + } 1.128 + printf("ping process: received \"%s\"\n", buf); 1.129 + if (nBytes != 5) { 1.130 + fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n", 1.131 + nBytes); 1.132 + exit(1); 1.133 + } 1.134 + if (strcmp(buf, "pong") != 0) { 1.135 + fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n", 1.136 + buf); 1.137 + exit(1); 1.138 + } 1.139 + } 1.140 + 1.141 + status = PR_Close(in_pipe[0]); 1.142 + if (status == PR_FAILURE) { 1.143 + fprintf(stderr, "PR_Close failed\n"); 1.144 + exit(1); 1.145 + } 1.146 + status = PR_Close(out_pipe[1]); 1.147 + if (status == PR_FAILURE) { 1.148 + fprintf(stderr, "PR_Close failed\n"); 1.149 + exit(1); 1.150 + } 1.151 + status = PR_WaitProcess(process, &exitCode); 1.152 + if (status == PR_FAILURE) { 1.153 + fprintf(stderr, "PR_WaitProcess failed\n"); 1.154 + exit(1); 1.155 + } 1.156 + if (exitCode == 0) { 1.157 + printf("PASS\n"); 1.158 + return 0; 1.159 + } else { 1.160 + printf("FAIL\n"); 1.161 + return 1; 1.162 + } 1.163 +}