VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/sockping.c@ 32258

Last change on this file since 32258 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39 * File: sockping.c
40 *
41 * Description:
42 * This test runs in conjunction with the sockpong test.
43 * This test creates a socket pair and passes one socket
44 * to the sockpong test. Then this test writes "ping" to
45 * to the sockpong test and the sockpong test writes "pong"
46 * back. To run this pair of tests, just invoke sockping.
47 *
48 * Tested areas: process creation, socket pairs, file
49 * descriptor inheritance.
50 */
51
52#include "prerror.h"
53#include "prio.h"
54#include "prproces.h"
55
56#include <stdio.h>
57#include <string.h>
58#include <stdlib.h>
59
60#define NUM_ITERATIONS 10
61
62static char *child_argv[] = { "sockpong", NULL };
63
64int main()
65{
66 PRFileDesc *sock[2];
67 PRStatus status;
68 PRProcess *process;
69 PRProcessAttr *attr;
70 char buf[1024];
71 PRInt32 nBytes;
72 PRInt32 exitCode;
73 int idx;
74
75 status = PR_NewTCPSocketPair(sock);
76 if (status == PR_FAILURE) {
77 fprintf(stderr, "PR_NewTCPSocketPair failed\n");
78 exit(1);
79 }
80
81 status = PR_SetFDInheritable(sock[0], PR_FALSE);
82 if (status == PR_FAILURE) {
83 fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n",
84 PR_GetError(), PR_GetOSError());
85 exit(1);
86 }
87 status = PR_SetFDInheritable(sock[1], PR_TRUE);
88 if (status == PR_FAILURE) {
89 fprintf(stderr, "PR_SetFDInheritable failed: (%d, %d)\n",
90 PR_GetError(), PR_GetOSError());
91 exit(1);
92 }
93
94 attr = PR_NewProcessAttr();
95 if (attr == NULL) {
96 fprintf(stderr, "PR_NewProcessAttr failed\n");
97 exit(1);
98 }
99
100 status = PR_ProcessAttrSetInheritableFD(attr, sock[1], "SOCKET");
101 if (status == PR_FAILURE) {
102 fprintf(stderr, "PR_ProcessAttrSetInheritableFD failed\n");
103 exit(1);
104 }
105
106 process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr);
107 if (process == NULL) {
108 fprintf(stderr, "PR_CreateProcess failed\n");
109 exit(1);
110 }
111 PR_DestroyProcessAttr(attr);
112 status = PR_Close(sock[1]);
113 if (status == PR_FAILURE) {
114 fprintf(stderr, "PR_Close failed\n");
115 exit(1);
116 }
117
118 for (idx = 0; idx < NUM_ITERATIONS; idx++) {
119 strcpy(buf, "ping");
120 printf("ping process: sending \"%s\"\n", buf);
121 nBytes = PR_Write(sock[0], buf, 5);
122 if (nBytes == -1) {
123 fprintf(stderr, "PR_Write failed: (%d, %d)\n",
124 PR_GetError(), PR_GetOSError());
125 exit(1);
126 }
127 memset(buf, 0, sizeof(buf));
128 nBytes = PR_Read(sock[0], buf, sizeof(buf));
129 if (nBytes == -1) {
130 fprintf(stderr, "PR_Read failed: (%d, %d)\n",
131 PR_GetError(), PR_GetOSError());
132 exit(1);
133 }
134 printf("ping process: received \"%s\"\n", buf);
135 if (nBytes != 5) {
136 fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n",
137 nBytes);
138 exit(1);
139 }
140 if (strcmp(buf, "pong") != 0) {
141 fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n",
142 buf);
143 exit(1);
144 }
145 }
146
147 status = PR_Close(sock[0]);
148 if (status == PR_FAILURE) {
149 fprintf(stderr, "PR_Close failed\n");
150 exit(1);
151 }
152 status = PR_WaitProcess(process, &exitCode);
153 if (status == PR_FAILURE) {
154 fprintf(stderr, "PR_WaitProcess failed\n");
155 exit(1);
156 }
157 if (exitCode == 0) {
158 printf("PASS\n");
159 return 0;
160 } else {
161 printf("FAIL\n");
162 return 1;
163 }
164}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette