1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /*
7  * File: pipepong.c
8  *
9  * Description:
10  * This test runs in conjunction with the pipeping test.
11  * The pipeping test creates two pipes and redirects the
12  * stdin and stdout of this test to the pipes.  Then the
13  * pipeping test writes "ping" to this test and this test
14  * writes "pong" back.  Note that this test does not depend
15  * on NSPR at all.  To run this pair of tests, just invoke
16  * pipeping.
17  *
18  * Tested areas: process creation, pipes, file descriptor
19  * inheritance, standard I/O redirection.
20  */
21 
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 
26 #define NUM_ITERATIONS 10
27 
28 int main(int argc, char **argv)
29 {
30     char buf[1024];
31     size_t nBytes;
main(int argc,char ** argv)32     int idx;
33 
34     for (idx = 0; idx < NUM_ITERATIONS; idx++) {
35         memset(buf, 0, sizeof(buf));
36         nBytes = fread(buf, 1, 5, stdin);
37         fprintf(stderr, "pong process: received \"%s\"\n", buf);
38         if (nBytes != 5) {
39             fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
40                     nBytes);
41             exit(1);
42         }
43         if (strcmp(buf, "ping") != 0) {
44             fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
45                     buf);
46             exit(1);
47         }
48 
49         strcpy(buf, "pong");
50         fprintf(stderr, "pong process: sending \"%s\"\n", buf);
51         nBytes = fwrite(buf, 1, 5, stdout);
52         if (nBytes != 5) {
53             fprintf(stderr, "pong process: fwrite failed\n");
54             exit(1);
55         }
56         fflush(stdout);
57     }
58 
59     return 0;
60 }
61