tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

pipepong.c (1688B)


      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  char buf[1024];
     30  size_t nBytes;
     31  int idx;
     32 
     33  for (idx = 0; idx < NUM_ITERATIONS; idx++) {
     34    memset(buf, 0, sizeof(buf));
     35    nBytes = fread(buf, 1, 5, stdin);
     36    fprintf(stderr, "pong process: received \"%s\"\n", buf);
     37    if (nBytes != 5) {
     38      fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
     39              nBytes);
     40      exit(1);
     41    }
     42    if (strcmp(buf, "ping") != 0) {
     43      fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n", buf);
     44      exit(1);
     45    }
     46 
     47    strcpy(buf, "pong");
     48    fprintf(stderr, "pong process: sending \"%s\"\n", buf);
     49    nBytes = fwrite(buf, 1, 5, stdout);
     50    if (nBytes != 5) {
     51      fprintf(stderr, "pong process: fwrite failed\n");
     52      exit(1);
     53    }
     54    fflush(stdout);
     55  }
     56 
     57  return 0;
     58 }