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 pipepong test.
11 * This test creates two pipes and redirects the stdin and
12 * stdout of the pipepong test to the pipes. Then this
13 * test writes "ping" to the pipepong test and the pipepong
14 * test writes "pong" back. To run this pair of tests,
15 * just invoke pipeping.
17 * Tested areas: process creation, pipes, file descriptor
18 * inheritance, standard I/O redirection.
30 static char *child_argv
[] = { "pipepong.exe", NULL
};
32 static char *child_argv
[] = { "pipepong", NULL
};
35 #define NUM_ITERATIONS 10
37 int main(int argc
, char **argv
)
39 PRFileDesc
*in_pipe
[2];
40 PRFileDesc
*out_pipe
[2];
49 status
= PR_CreatePipe(&in_pipe
[0], &in_pipe
[1]);
50 if (status
== PR_FAILURE
) {
51 fprintf(stderr
, "PR_CreatePipe failed\n");
54 status
= PR_CreatePipe(&out_pipe
[0], &out_pipe
[1]);
55 if (status
== PR_FAILURE
) {
56 fprintf(stderr
, "PR_CreatePipe failed\n");
60 status
= PR_SetFDInheritable(in_pipe
[0], PR_FALSE
);
61 if (status
== PR_FAILURE
) {
62 fprintf(stderr
, "PR_SetFDInheritable failed\n");
65 status
= PR_SetFDInheritable(in_pipe
[1], PR_TRUE
);
66 if (status
== PR_FAILURE
) {
67 fprintf(stderr
, "PR_SetFDInheritable failed\n");
70 status
= PR_SetFDInheritable(out_pipe
[0], PR_TRUE
);
71 if (status
== PR_FAILURE
) {
72 fprintf(stderr
, "PR_SetFDInheritable failed\n");
75 status
= PR_SetFDInheritable(out_pipe
[1], PR_FALSE
);
76 if (status
== PR_FAILURE
) {
77 fprintf(stderr
, "PR_SetFDInheritable failed\n");
81 attr
= PR_NewProcessAttr();
83 fprintf(stderr
, "PR_NewProcessAttr failed\n");
87 PR_ProcessAttrSetStdioRedirect(attr
, PR_StandardInput
, out_pipe
[0]);
88 PR_ProcessAttrSetStdioRedirect(attr
, PR_StandardOutput
, in_pipe
[1]);
90 process
= PR_CreateProcess(child_argv
[0], child_argv
, NULL
, attr
);
91 if (process
== NULL
) {
92 fprintf(stderr
, "PR_CreateProcess failed\n");
95 PR_DestroyProcessAttr(attr
);
96 status
= PR_Close(out_pipe
[0]);
97 if (status
== PR_FAILURE
) {
98 fprintf(stderr
, "PR_Close failed\n");
101 status
= PR_Close(in_pipe
[1]);
102 if (status
== PR_FAILURE
) {
103 fprintf(stderr
, "PR_Close failed\n");
107 for (idx
= 0; idx
< NUM_ITERATIONS
; idx
++) {
109 printf("ping process: sending \"%s\"\n", buf
);
110 nBytes
= PR_Write(out_pipe
[1], buf
, 5);
112 fprintf(stderr
, "PR_Write failed: (%d, %d)\n", PR_GetError(),
116 memset(buf
, 0, sizeof(buf
));
117 nBytes
= PR_Read(in_pipe
[0], buf
, sizeof(buf
));
119 fprintf(stderr
, "PR_Read failed: (%d, %d)\n",
120 PR_GetError(), PR_GetOSError());
123 printf("ping process: received \"%s\"\n", buf
);
125 fprintf(stderr
, "ping process: expected 5 bytes but got %d bytes\n",
129 if (strcmp(buf
, "pong") != 0) {
130 fprintf(stderr
, "ping process: expected \"pong\" but got \"%s\"\n",
136 status
= PR_Close(in_pipe
[0]);
137 if (status
== PR_FAILURE
) {
138 fprintf(stderr
, "PR_Close failed\n");
141 status
= PR_Close(out_pipe
[1]);
142 if (status
== PR_FAILURE
) {
143 fprintf(stderr
, "PR_Close failed\n");
146 status
= PR_WaitProcess(process
, &exitCode
);
147 if (status
== PR_FAILURE
) {
148 fprintf(stderr
, "PR_WaitProcess failed\n");