nsprpub/pr/tests/pipepong.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/pipepong.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,60 @@
     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: pipepong.c
    1.11 + *
    1.12 + * Description:
    1.13 + * This test runs in conjunction with the pipeping test.
    1.14 + * The pipeping test creates two pipes and redirects the
    1.15 + * stdin and stdout of this test to the pipes.  Then the
    1.16 + * pipeping test writes "ping" to this test and this test
    1.17 + * writes "pong" back.  Note that this test does not depend
    1.18 + * on NSPR at all.  To run this pair of tests, just invoke
    1.19 + * pipeping.
    1.20 + *
    1.21 + * Tested areas: process creation, pipes, file descriptor
    1.22 + * inheritance, standard I/O redirection.
    1.23 + */
    1.24 +
    1.25 +#include <stdio.h>
    1.26 +#include <string.h>
    1.27 +#include <stdlib.h>
    1.28 +
    1.29 +#define NUM_ITERATIONS 10
    1.30 +
    1.31 +int main(int argc, char **argv)
    1.32 +{
    1.33 +    char buf[1024];
    1.34 +    size_t nBytes;
    1.35 +    int idx;
    1.36 +
    1.37 +    for (idx = 0; idx < NUM_ITERATIONS; idx++) {
    1.38 +        memset(buf, 0, sizeof(buf));
    1.39 +        nBytes = fread(buf, 1, 5, stdin);
    1.40 +        fprintf(stderr, "pong process: received \"%s\"\n", buf);
    1.41 +        if (nBytes != 5) {
    1.42 +            fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
    1.43 +                    nBytes);
    1.44 +            exit(1);
    1.45 +        }
    1.46 +        if (strcmp(buf, "ping") != 0) {
    1.47 +            fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
    1.48 +                    buf);
    1.49 +            exit(1);
    1.50 +        }
    1.51 +
    1.52 +        strcpy(buf, "pong");
    1.53 +        fprintf(stderr, "pong process: sending \"%s\"\n", buf);
    1.54 +        nBytes = fwrite(buf, 1, 5, stdout);
    1.55 +        if (nBytes != 5) {
    1.56 +            fprintf(stderr, "pong process: fwrite failed\n");
    1.57 +            exit(1);
    1.58 +        }
    1.59 +        fflush(stdout);
    1.60 +    }
    1.61 +
    1.62 +    return 0;
    1.63 +}

mercurial