1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
10 * This test runs in conjunction with the sockpong test.
11 * This test creates a socket pair and passes one socket
12 * to the sockpong test. Then this test writes "ping" to
13 * to the sockpong test and the sockpong test writes "pong"
14 * back. To run this pair of tests, just invoke sockping.
16 * Tested areas: process creation, socket pairs, file
17 * descriptor inheritance.
28 #define NUM_ITERATIONS 10
30 static char *child_argv
[] = { "sockpong", NULL
};
32 int main(int argc
, char **argv
)
43 status
= PR_NewTCPSocketPair(sock
);
44 if (status
== PR_FAILURE
) {
45 fprintf(stderr
, "PR_NewTCPSocketPair failed\n");
49 status
= PR_SetFDInheritable(sock
[0], PR_FALSE
);
50 if (status
== PR_FAILURE
) {
51 fprintf(stderr
, "PR_SetFDInheritable failed: (%d, %d)\n",
52 PR_GetError(), PR_GetOSError());
55 status
= PR_SetFDInheritable(sock
[1], PR_TRUE
);
56 if (status
== PR_FAILURE
) {
57 fprintf(stderr
, "PR_SetFDInheritable failed: (%d, %d)\n",
58 PR_GetError(), PR_GetOSError());
62 attr
= PR_NewProcessAttr();
64 fprintf(stderr
, "PR_NewProcessAttr failed\n");
68 status
= PR_ProcessAttrSetInheritableFD(attr
, sock
[1], "SOCKET");
69 if (status
== PR_FAILURE
) {
70 fprintf(stderr
, "PR_ProcessAttrSetInheritableFD failed\n");
74 process
= PR_CreateProcess(child_argv
[0], child_argv
, NULL
, attr
);
75 if (process
== NULL
) {
76 fprintf(stderr
, "PR_CreateProcess failed\n");
79 PR_DestroyProcessAttr(attr
);
80 status
= PR_Close(sock
[1]);
81 if (status
== PR_FAILURE
) {
82 fprintf(stderr
, "PR_Close failed\n");
86 for (idx
= 0; idx
< NUM_ITERATIONS
; idx
++) {
88 printf("ping process: sending \"%s\"\n", buf
);
89 nBytes
= PR_Write(sock
[0], buf
, 5);
91 fprintf(stderr
, "PR_Write failed: (%d, %d)\n",
92 PR_GetError(), PR_GetOSError());
95 memset(buf
, 0, sizeof(buf
));
96 nBytes
= PR_Read(sock
[0], buf
, sizeof(buf
));
98 fprintf(stderr
, "PR_Read failed: (%d, %d)\n",
99 PR_GetError(), PR_GetOSError());
102 printf("ping process: received \"%s\"\n", buf
);
104 fprintf(stderr
, "ping process: expected 5 bytes but got %d bytes\n",
108 if (strcmp(buf
, "pong") != 0) {
109 fprintf(stderr
, "ping process: expected \"pong\" but got \"%s\"\n",
115 status
= PR_Close(sock
[0]);
116 if (status
== PR_FAILURE
) {
117 fprintf(stderr
, "PR_Close failed\n");
120 status
= PR_WaitProcess(process
, &exitCode
);
121 if (status
== PR_FAILURE
) {
122 fprintf(stderr
, "PR_WaitProcess failed\n");