nsprpub/pr/tests/sockping.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/tests/sockping.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,132 @@
     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: sockping.c
    1.11 + *
    1.12 + * Description:
    1.13 + * This test runs in conjunction with the sockpong test.
    1.14 + * This test creates a socket pair and passes one socket
    1.15 + * to the sockpong test.  Then this test writes "ping" to
    1.16 + * to the sockpong test and the sockpong test writes "pong"
    1.17 + * back.  To run this pair of tests, just invoke sockping.
    1.18 + *
    1.19 + * Tested areas: process creation, socket pairs, file
    1.20 + * descriptor inheritance.
    1.21 + */
    1.22 +
    1.23 +#include "prerror.h"
    1.24 +#include "prio.h"
    1.25 +#include "prproces.h"
    1.26 +
    1.27 +#include <stdio.h>
    1.28 +#include <string.h>
    1.29 +#include <stdlib.h>
    1.30 +
    1.31 +#define NUM_ITERATIONS 10
    1.32 +
    1.33 +static char *child_argv[] = { "sockpong", NULL };
    1.34 +
    1.35 +int main(int argc, char **argv)
    1.36 +{
    1.37 +    PRFileDesc *sock[2];
    1.38 +    PRStatus status;
    1.39 +    PRProcess *process;
    1.40 +    PRProcessAttr *attr;
    1.41 +    char buf[1024];
    1.42 +    PRInt32 nBytes;
    1.43 +    PRInt32 exitCode;
    1.44 +    int idx;
    1.45 +
    1.46 +    status = PR_NewTCPSocketPair(sock);
    1.47 +    if (status == PR_FAILURE) {
    1.48 +        fprintf(stderr, "PR_NewTCPSocketPair failed\n");
    1.49 +        exit(1);
    1.50 +    }
    1.51 +
    1.52 +    status = PR_SetFDInheritable(sock[0], PR_FALSE);
    1.53 +    if (status == PR_FAILURE) {
    1.54 +        fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n",
    1.55 +                PR_GetError(), PR_GetOSError());
    1.56 +        exit(1);
    1.57 +    }
    1.58 +    status = PR_SetFDInheritable(sock[1], PR_TRUE);
    1.59 +    if (status == PR_FAILURE) {
    1.60 +        fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n",
    1.61 +                PR_GetError(), PR_GetOSError());
    1.62 +        exit(1);
    1.63 +    }
    1.64 +
    1.65 +    attr = PR_NewProcessAttr();
    1.66 +    if (attr == NULL) {
    1.67 +        fprintf(stderr, "PR_NewProcessAttr failed\n");
    1.68 +        exit(1);
    1.69 +    }
    1.70 +
    1.71 +    status = PR_ProcessAttrSetInheritableFD(attr, sock[1], "SOCKET");
    1.72 +    if (status == PR_FAILURE) {
    1.73 +        fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n");
    1.74 +        exit(1);
    1.75 +    }
    1.76 +
    1.77 +    process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr);
    1.78 +    if (process == NULL) {
    1.79 +        fprintf(stderr, "PR_CreateProcess failed\n");
    1.80 +        exit(1);
    1.81 +    }
    1.82 +    PR_DestroyProcessAttr(attr);
    1.83 +    status = PR_Close(sock[1]);
    1.84 +    if (status == PR_FAILURE) {
    1.85 +        fprintf(stderr, "PR_Close failed\n");
    1.86 +        exit(1);
    1.87 +    }
    1.88 +
    1.89 +    for (idx = 0; idx < NUM_ITERATIONS; idx++) {
    1.90 +        strcpy(buf, "ping");
    1.91 +        printf("ping process: sending \"%s\"\n", buf);
    1.92 +        nBytes = PR_Write(sock[0], buf, 5);
    1.93 +        if (nBytes == -1) {
    1.94 +            fprintf(stderr, "PR_Write failed: (%d, %d)\n",
    1.95 +                    PR_GetError(), PR_GetOSError());
    1.96 +            exit(1);
    1.97 +        }
    1.98 +        memset(buf, 0, sizeof(buf));
    1.99 +        nBytes = PR_Read(sock[0], buf, sizeof(buf));
   1.100 +        if (nBytes == -1) {
   1.101 +            fprintf(stderr, "PR_Read failed: (%d, %d)\n",
   1.102 +                    PR_GetError(), PR_GetOSError());
   1.103 +            exit(1);
   1.104 +        }
   1.105 +        printf("ping process: received \"%s\"\n", buf);
   1.106 +        if (nBytes != 5) {
   1.107 +            fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n",
   1.108 +                    nBytes);
   1.109 +            exit(1);
   1.110 +        }
   1.111 +        if (strcmp(buf, "pong") != 0) {
   1.112 +            fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n",
   1.113 +                    buf);
   1.114 +            exit(1);
   1.115 +        }
   1.116 +    }
   1.117 +
   1.118 +    status = PR_Close(sock[0]);
   1.119 +    if (status == PR_FAILURE) {
   1.120 +        fprintf(stderr, "PR_Close failed\n");
   1.121 +        exit(1);
   1.122 +    }
   1.123 +    status = PR_WaitProcess(process, &exitCode);
   1.124 +    if (status == PR_FAILURE) {
   1.125 +        fprintf(stderr, "PR_WaitProcess failed\n");
   1.126 +        exit(1);
   1.127 +    }
   1.128 +    if (exitCode == 0) {
   1.129 +        printf("PASS\n");
   1.130 +        return 0;
   1.131 +    } else {
   1.132 +        printf("FAIL\n");
   1.133 +        return 1;
   1.134 +    }
   1.135 +}

mercurial