1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/sockpong.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,83 @@ 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: sockpong.c 1.11 + * 1.12 + * Description: 1.13 + * This test runs in conjunction with the sockping test. 1.14 + * The sockping test creates a socket pair and passes one 1.15 + * socket to this test. Then the sockping test writes 1.16 + * "ping" to this test and this test writes "pong" back. 1.17 + * 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 + 1.26 +#include <stdio.h> 1.27 +#include <string.h> 1.28 +#include <stdlib.h> 1.29 + 1.30 +#define NUM_ITERATIONS 10 1.31 + 1.32 +int main(int argc, char **argv) 1.33 +{ 1.34 + PRFileDesc *sock; 1.35 + PRStatus status; 1.36 + char buf[1024]; 1.37 + PRInt32 nBytes; 1.38 + int idx; 1.39 + 1.40 + sock = PR_GetInheritedFD("SOCKET"); 1.41 + if (sock == NULL) { 1.42 + fprintf(stderr, "PR_GetInheritedFD failed\n"); 1.43 + exit(1); 1.44 + } 1.45 + status = PR_SetFDInheritable(sock, PR_FALSE); 1.46 + if (status == PR_FAILURE) { 1.47 + fprintf(stderr, "PR_SetFDInheritable failed\n"); 1.48 + exit(1); 1.49 + } 1.50 + 1.51 + for (idx = 0; idx < NUM_ITERATIONS; idx++) { 1.52 + memset(buf, 0, sizeof(buf)); 1.53 + nBytes = PR_Read(sock, buf, sizeof(buf)); 1.54 + if (nBytes == -1) { 1.55 + fprintf(stderr, "PR_Read failed: (%d, %d)\n", 1.56 + PR_GetError(), PR_GetOSError()); 1.57 + exit(1); 1.58 + } 1.59 + printf("pong process: received \"%s\"\n", buf); 1.60 + if (nBytes != 5) { 1.61 + fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n", 1.62 + nBytes); 1.63 + exit(1); 1.64 + } 1.65 + if (strcmp(buf, "ping") != 0) { 1.66 + fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n", 1.67 + buf); 1.68 + exit(1); 1.69 + } 1.70 + 1.71 + strcpy(buf, "pong"); 1.72 + printf("pong process: sending \"%s\"\n", buf); 1.73 + nBytes = PR_Write(sock, buf, 5); 1.74 + if (nBytes == -1) { 1.75 + fprintf(stderr, "PR_Write failed\n"); 1.76 + exit(1); 1.77 + } 1.78 + } 1.79 + 1.80 + status = PR_Close(sock); 1.81 + if (status == PR_FAILURE) { 1.82 + fprintf(stderr, "PR_Close failed\n"); 1.83 + exit(1); 1.84 + } 1.85 + return 0; 1.86 +}