nsprpub/pr/tests/sockping.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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: sockping.c
michael@0 8 *
michael@0 9 * Description:
michael@0 10 * This test runs in conjunction with the sockpong test.
michael@0 11 * This test creates a socket pair and passes one socket
michael@0 12 * to the sockpong test. Then this test writes "ping" to
michael@0 13 * to the sockpong test and the sockpong test writes "pong"
michael@0 14 * back. To run this pair of tests, just invoke sockping.
michael@0 15 *
michael@0 16 * Tested areas: process creation, socket pairs, file
michael@0 17 * descriptor inheritance.
michael@0 18 */
michael@0 19
michael@0 20 #include "prerror.h"
michael@0 21 #include "prio.h"
michael@0 22 #include "prproces.h"
michael@0 23
michael@0 24 #include <stdio.h>
michael@0 25 #include <string.h>
michael@0 26 #include <stdlib.h>
michael@0 27
michael@0 28 #define NUM_ITERATIONS 10
michael@0 29
michael@0 30 static char *child_argv[] = { "sockpong", NULL };
michael@0 31
michael@0 32 int main(int argc, char **argv)
michael@0 33 {
michael@0 34 PRFileDesc *sock[2];
michael@0 35 PRStatus status;
michael@0 36 PRProcess *process;
michael@0 37 PRProcessAttr *attr;
michael@0 38 char buf[1024];
michael@0 39 PRInt32 nBytes;
michael@0 40 PRInt32 exitCode;
michael@0 41 int idx;
michael@0 42
michael@0 43 status = PR_NewTCPSocketPair(sock);
michael@0 44 if (status == PR_FAILURE) {
michael@0 45 fprintf(stderr, "PR_NewTCPSocketPair failed\n");
michael@0 46 exit(1);
michael@0 47 }
michael@0 48
michael@0 49 status = PR_SetFDInheritable(sock[0], PR_FALSE);
michael@0 50 if (status == PR_FAILURE) {
michael@0 51 fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n",
michael@0 52 PR_GetError(), PR_GetOSError());
michael@0 53 exit(1);
michael@0 54 }
michael@0 55 status = PR_SetFDInheritable(sock[1], PR_TRUE);
michael@0 56 if (status == PR_FAILURE) {
michael@0 57 fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n",
michael@0 58 PR_GetError(), PR_GetOSError());
michael@0 59 exit(1);
michael@0 60 }
michael@0 61
michael@0 62 attr = PR_NewProcessAttr();
michael@0 63 if (attr == NULL) {
michael@0 64 fprintf(stderr, "PR_NewProcessAttr failed\n");
michael@0 65 exit(1);
michael@0 66 }
michael@0 67
michael@0 68 status = PR_ProcessAttrSetInheritableFD(attr, sock[1], "SOCKET");
michael@0 69 if (status == PR_FAILURE) {
michael@0 70 fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n");
michael@0 71 exit(1);
michael@0 72 }
michael@0 73
michael@0 74 process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr);
michael@0 75 if (process == NULL) {
michael@0 76 fprintf(stderr, "PR_CreateProcess failed\n");
michael@0 77 exit(1);
michael@0 78 }
michael@0 79 PR_DestroyProcessAttr(attr);
michael@0 80 status = PR_Close(sock[1]);
michael@0 81 if (status == PR_FAILURE) {
michael@0 82 fprintf(stderr, "PR_Close failed\n");
michael@0 83 exit(1);
michael@0 84 }
michael@0 85
michael@0 86 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
michael@0 87 strcpy(buf, "ping");
michael@0 88 printf("ping process: sending \"%s\"\n", buf);
michael@0 89 nBytes = PR_Write(sock[0], buf, 5);
michael@0 90 if (nBytes == -1) {
michael@0 91 fprintf(stderr, "PR_Write failed: (%d, %d)\n",
michael@0 92 PR_GetError(), PR_GetOSError());
michael@0 93 exit(1);
michael@0 94 }
michael@0 95 memset(buf, 0, sizeof(buf));
michael@0 96 nBytes = PR_Read(sock[0], buf, sizeof(buf));
michael@0 97 if (nBytes == -1) {
michael@0 98 fprintf(stderr, "PR_Read failed: (%d, %d)\n",
michael@0 99 PR_GetError(), PR_GetOSError());
michael@0 100 exit(1);
michael@0 101 }
michael@0 102 printf("ping process: received \"%s\"\n", buf);
michael@0 103 if (nBytes != 5) {
michael@0 104 fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n",
michael@0 105 nBytes);
michael@0 106 exit(1);
michael@0 107 }
michael@0 108 if (strcmp(buf, "pong") != 0) {
michael@0 109 fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n",
michael@0 110 buf);
michael@0 111 exit(1);
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 status = PR_Close(sock[0]);
michael@0 116 if (status == PR_FAILURE) {
michael@0 117 fprintf(stderr, "PR_Close failed\n");
michael@0 118 exit(1);
michael@0 119 }
michael@0 120 status = PR_WaitProcess(process, &exitCode);
michael@0 121 if (status == PR_FAILURE) {
michael@0 122 fprintf(stderr, "PR_WaitProcess failed\n");
michael@0 123 exit(1);
michael@0 124 }
michael@0 125 if (exitCode == 0) {
michael@0 126 printf("PASS\n");
michael@0 127 return 0;
michael@0 128 } else {
michael@0 129 printf("FAIL\n");
michael@0 130 return 1;
michael@0 131 }
michael@0 132 }

mercurial