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