michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * File: sockpong.c michael@0: * michael@0: * Description: michael@0: * This test runs in conjunction with the sockping test. michael@0: * The sockping test creates a socket pair and passes one michael@0: * socket to this test. Then the sockping test writes michael@0: * "ping" to this test and this test writes "pong" back. michael@0: * To run this pair of tests, just invoke sockping. michael@0: * michael@0: * Tested areas: process creation, socket pairs, file michael@0: * descriptor inheritance. michael@0: */ michael@0: michael@0: #include "prerror.h" michael@0: #include "prio.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define NUM_ITERATIONS 10 michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRFileDesc *sock; michael@0: PRStatus status; michael@0: char buf[1024]; michael@0: PRInt32 nBytes; michael@0: int idx; michael@0: michael@0: sock = PR_GetInheritedFD("SOCKET"); michael@0: if (sock == NULL) { michael@0: fprintf(stderr, "PR_GetInheritedFD failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_SetFDInheritable(sock, PR_FALSE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: for (idx = 0; idx < NUM_ITERATIONS; idx++) { michael@0: memset(buf, 0, sizeof(buf)); michael@0: nBytes = PR_Read(sock, buf, sizeof(buf)); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Read failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: printf("pong process: received \"%s\"\n", buf); michael@0: if (nBytes != 5) { michael@0: fprintf(stderr, "pong process: expected 5 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: if (strcmp(buf, "ping") != 0) { michael@0: fprintf(stderr, "pong process: expected \"ping\" but got \"%s\"\n", michael@0: buf); michael@0: exit(1); michael@0: } michael@0: michael@0: strcpy(buf, "pong"); michael@0: printf("pong process: sending \"%s\"\n", buf); michael@0: nBytes = PR_Write(sock, buf, 5); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Write failed\n"); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: status = PR_Close(sock); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: return 0; michael@0: }