nsprpub/pr/tests/pipepong.c

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial