1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/pipepong2.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,98 @@ 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: pipepong2.c 1.11 + * 1.12 + * Description: 1.13 + * This test runs in conjunction with the pipeping2 test. 1.14 + * The pipeping2 test creates two pipes and passes two 1.15 + * pipe fd's to this test. Then the pipeping2 test writes 1.16 + * "ping" to this test and this test writes "pong" back. 1.17 + * 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 + 1.26 +#include <stdio.h> 1.27 +#include <string.h> 1.28 +#include <stdlib.h> 1.29 + 1.30 +#define NUM_ITERATIONS 10 1.31 + 1.32 +int main(int argc, char **argv) 1.33 +{ 1.34 + PRFileDesc *pipe_read, *pipe_write; 1.35 + PRStatus status; 1.36 + char buf[1024]; 1.37 + PRInt32 nBytes; 1.38 + int idx; 1.39 + 1.40 + pipe_read = PR_GetInheritedFD("PIPE_READ"); 1.41 + if (pipe_read == NULL) { 1.42 + fprintf(stderr, "PR_GetInheritedFD failed\n"); 1.43 + exit(1); 1.44 + } 1.45 + status = PR_SetFDInheritable(pipe_read, PR_FALSE); 1.46 + if (status == PR_FAILURE) { 1.47 + fprintf(stderr, "PR_SetFDInheritable failed\n"); 1.48 + exit(1); 1.49 + } 1.50 + pipe_write = PR_GetInheritedFD("PIPE_WRITE"); 1.51 + if (pipe_write == NULL) { 1.52 + fprintf(stderr, "PR_GetInheritedFD failed\n"); 1.53 + exit(1); 1.54 + } 1.55 + status = PR_SetFDInheritable(pipe_write, PR_FALSE); 1.56 + if (status == PR_FAILURE) { 1.57 + fprintf(stderr, "PR_SetFDInheritable failed\n"); 1.58 + exit(1); 1.59 + } 1.60 + 1.61 + for (idx = 0; idx < NUM_ITERATIONS; idx++) { 1.62 + memset(buf, 0, sizeof(buf)); 1.63 + nBytes = PR_Read(pipe_read, buf, sizeof(buf)); 1.64 + if (nBytes == -1) { 1.65 + fprintf(stderr, "PR_Read failed: (%d, %d)\n", 1.66 + PR_GetError(), PR_GetOSError()); 1.67 + exit(1); 1.68 + } 1.69 + printf("pong process: received \"%s\"\n", buf); 1.70 + if (nBytes != 5) { 1.71 + fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n", 1.72 + nBytes); 1.73 + exit(1); 1.74 + } 1.75 + if (strcmp(buf, "ping") != 0) { 1.76 + fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n", 1.77 + buf); 1.78 + exit(1); 1.79 + } 1.80 + 1.81 + strcpy(buf, "pong"); 1.82 + printf("pong process: sending \"%s\"\n", buf); 1.83 + nBytes = PR_Write(pipe_write, buf, 5); 1.84 + if (nBytes == -1) { 1.85 + fprintf(stderr, "PR_Write failed\n"); 1.86 + exit(1); 1.87 + } 1.88 + } 1.89 + 1.90 + status = PR_Close(pipe_read); 1.91 + if (status == PR_FAILURE) { 1.92 + fprintf(stderr, "PR_Close failed\n"); 1.93 + exit(1); 1.94 + } 1.95 + status = PR_Close(pipe_write); 1.96 + if (status == PR_FAILURE) { 1.97 + fprintf(stderr, "PR_Close failed\n"); 1.98 + exit(1); 1.99 + } 1.100 + return 0; 1.101 +}