michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * File: pipeping.c michael@0: * michael@0: * Description: michael@0: * This test runs in conjunction with the pipepong test. michael@0: * This test creates two pipes and redirects the stdin and michael@0: * stdout of the pipepong test to the pipes. Then this michael@0: * test writes "ping" to the pipepong test and the pipepong michael@0: * test writes "pong" back. To run this pair of tests, michael@0: * just invoke pipeping. michael@0: * michael@0: * Tested areas: process creation, pipes, file descriptor michael@0: * inheritance, standard I/O redirection. michael@0: */ michael@0: michael@0: #include "prerror.h" michael@0: #include "prio.h" michael@0: #include "prproces.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef XP_OS2 michael@0: static char *child_argv[] = { "pipepong.exe", NULL }; michael@0: #else michael@0: static char *child_argv[] = { "pipepong", NULL }; michael@0: #endif michael@0: michael@0: #define NUM_ITERATIONS 10 michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRFileDesc *in_pipe[2]; michael@0: PRFileDesc *out_pipe[2]; michael@0: PRStatus status; michael@0: PRProcess *process; michael@0: PRProcessAttr *attr; michael@0: char buf[1024]; michael@0: PRInt32 nBytes; michael@0: PRInt32 exitCode; michael@0: int idx; michael@0: michael@0: status = PR_CreatePipe(&in_pipe[0], &in_pipe[1]); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CreatePipe failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_CreatePipe(&out_pipe[0], &out_pipe[1]); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_CreatePipe failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: status = PR_SetFDInheritable(in_pipe[0], PR_FALSE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_SetFDInheritable(in_pipe[1], PR_TRUE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_SetFDInheritable(out_pipe[0], PR_TRUE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_SetFDInheritable(out_pipe[1], PR_FALSE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: attr = PR_NewProcessAttr(); michael@0: if (attr == NULL) { michael@0: fprintf(stderr, "PR_NewProcessAttr failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: PR_ProcessAttrSetStdioRedirect(attr, PR_StandardInput, out_pipe[0]); michael@0: PR_ProcessAttrSetStdioRedirect(attr, PR_StandardOutput, in_pipe[1]); michael@0: michael@0: process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr); michael@0: if (process == NULL) { michael@0: fprintf(stderr, "PR_CreateProcess failed\n"); michael@0: exit(1); michael@0: } michael@0: PR_DestroyProcessAttr(attr); michael@0: status = PR_Close(out_pipe[0]); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(in_pipe[1]); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: for (idx = 0; idx < NUM_ITERATIONS; idx++) { michael@0: strcpy(buf, "ping"); michael@0: printf("ping process: sending \"%s\"\n", buf); michael@0: nBytes = PR_Write(out_pipe[1], buf, 5); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(), michael@0: PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: memset(buf, 0, sizeof(buf)); michael@0: nBytes = PR_Read(in_pipe[0], buf, sizeof(buf)); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Read failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: printf("ping process: received \"%s\"\n", buf); michael@0: if (nBytes != 5) { michael@0: fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: if (strcmp(buf, "pong") != 0) { michael@0: fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n", michael@0: buf); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: status = PR_Close(in_pipe[0]); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_Close(out_pipe[1]); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_WaitProcess(process, &exitCode); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_WaitProcess failed\n"); michael@0: exit(1); michael@0: } michael@0: if (exitCode == 0) { michael@0: printf("PASS\n"); michael@0: return 0; michael@0: } else { michael@0: printf("FAIL\n"); michael@0: return 1; michael@0: } michael@0: }