nsprpub/pr/tests/sockpong.c

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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

mercurial