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: sockping.c michael@0: * michael@0: * Description: michael@0: * This test runs in conjunction with the sockpong test. michael@0: * This test creates a socket pair and passes one socket michael@0: * to the sockpong test. Then this test writes "ping" to michael@0: * to the sockpong test and the sockpong test writes "pong" michael@0: * back. 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: #include "prproces.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define NUM_ITERATIONS 10 michael@0: michael@0: static char *child_argv[] = { "sockpong", NULL }; michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: PRFileDesc *sock[2]; michael@0: PRStatus status; michael@0: PRProcess *process; michael@0: PRProcessAttr *attr; michael@0: char buf[1024]; michael@0: PRInt32 nBytes; michael@0: PRInt32 exitCode; michael@0: int idx; michael@0: michael@0: status = PR_NewTCPSocketPair(sock); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_NewTCPSocketPair failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: status = PR_SetFDInheritable(sock[0], PR_FALSE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: status = PR_SetFDInheritable(sock[1], PR_TRUE); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: michael@0: attr = PR_NewProcessAttr(); michael@0: if (attr == NULL) { michael@0: fprintf(stderr, "PR_NewProcessAttr failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: status = PR_ProcessAttrSetInheritableFD(attr, sock[1], "SOCKET"); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr); michael@0: if (process == NULL) { michael@0: fprintf(stderr, "PR_CreateProcess failed\n"); michael@0: exit(1); michael@0: } michael@0: PR_DestroyProcessAttr(attr); michael@0: status = PR_Close(sock[1]); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: michael@0: for (idx = 0; idx < NUM_ITERATIONS; idx++) { michael@0: strcpy(buf, "ping"); michael@0: printf("ping process: sending \"%s\"\n", buf); michael@0: nBytes = PR_Write(sock[0], buf, 5); michael@0: if (nBytes == -1) { michael@0: fprintf(stderr, "PR_Write failed: (%d, %d)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: exit(1); michael@0: } michael@0: memset(buf, 0, sizeof(buf)); michael@0: nBytes = PR_Read(sock[0], 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("ping process: received \"%s\"\n", buf); michael@0: if (nBytes != 5) { michael@0: fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n", michael@0: nBytes); michael@0: exit(1); michael@0: } michael@0: if (strcmp(buf, "pong") != 0) { michael@0: fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n", michael@0: buf); michael@0: exit(1); michael@0: } michael@0: } michael@0: michael@0: status = PR_Close(sock[0]); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_Close failed\n"); michael@0: exit(1); michael@0: } michael@0: status = PR_WaitProcess(process, &exitCode); michael@0: if (status == PR_FAILURE) { michael@0: fprintf(stderr, "PR_WaitProcess failed\n"); michael@0: exit(1); michael@0: } michael@0: if (exitCode == 0) { michael@0: printf("PASS\n"); michael@0: return 0; michael@0: } else { michael@0: printf("FAIL\n"); michael@0: return 1; michael@0: } michael@0: }