nsprpub/pr/tests/sockpong.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/. */
     6 /*
     7  * File: sockpong.c
     8  *
     9  * Description:
    10  * This test runs in conjunction with the sockping test.
    11  * The sockping test creates a socket pair and passes one
    12  * socket to this test.  Then the sockping test writes
    13  * "ping" to this test and this test writes "pong" back.
    14  * To run this pair of tests, just invoke sockping.
    15  *
    16  * Tested areas: process creation, socket pairs, file
    17  * descriptor inheritance.
    18  */
    20 #include "prerror.h"
    21 #include "prio.h"
    23 #include <stdio.h>
    24 #include <string.h>
    25 #include <stdlib.h>
    27 #define NUM_ITERATIONS 10
    29 int main(int argc, char **argv)
    30 {
    31     PRFileDesc *sock;
    32     PRStatus status;
    33     char buf[1024];
    34     PRInt32 nBytes;
    35     int idx;
    37     sock = PR_GetInheritedFD("SOCKET");
    38     if (sock == NULL) {
    39         fprintf(stderr, "PR_GetInheritedFD failed\n");
    40         exit(1);
    41     }
    42     status = PR_SetFDInheritable(sock, PR_FALSE);
    43     if (status == PR_FAILURE) {
    44         fprintf(stderr, "PR_SetFDInheritable failed\n");
    45         exit(1);
    46     }
    48     for (idx = 0; idx < NUM_ITERATIONS; idx++) {
    49         memset(buf, 0, sizeof(buf));
    50         nBytes = PR_Read(sock, buf, sizeof(buf));
    51         if (nBytes == -1) {
    52             fprintf(stderr, "PR_Read failed: (%d, %d)\n",
    53                     PR_GetError(), PR_GetOSError());
    54             exit(1);
    55         }
    56         printf("pong process: received \"%s\"\n", buf);
    57         if (nBytes != 5) {
    58             fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n",
    59                     nBytes);
    60             exit(1);
    61         }
    62         if (strcmp(buf, "ping") != 0) {
    63             fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n",
    64                     buf);
    65             exit(1);
    66         }
    68         strcpy(buf, "pong");
    69         printf("pong process: sending \"%s\"\n", buf);
    70         nBytes = PR_Write(sock, buf, 5);
    71         if (nBytes == -1) {
    72             fprintf(stderr, "PR_Write failed\n");
    73             exit(1);
    74         }
    75     }
    77     status = PR_Close(sock);
    78     if (status == PR_FAILURE) {
    79         fprintf(stderr, "PR_Close failed\n");
    80         exit(1);
    81     }
    82     return 0;
    83 }

mercurial