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: pipeping.c
|
---|
40 | *
|
---|
41 | * Description:
|
---|
42 | * This test runs in conjunction with the pipepong test.
|
---|
43 | * This test creates two pipes and redirects the stdin and
|
---|
44 | * stdout of the pipepong test to the pipes. Then this
|
---|
45 | * test writes "ping" to the pipepong test and the pipepong
|
---|
46 | * test writes "pong" back. To run this pair of tests,
|
---|
47 | * just invoke pipeping.
|
---|
48 | *
|
---|
49 | * Tested areas: process creation, pipes, file descriptor
|
---|
50 | * inheritance, standard I/O redirection.
|
---|
51 | */
|
---|
52 |
|
---|
53 | #include "prerror.h"
|
---|
54 | #include "prio.h"
|
---|
55 | #include "prproces.h"
|
---|
56 |
|
---|
57 | #include <stdio.h>
|
---|
58 | #include <stdlib.h>
|
---|
59 | #include <string.h>
|
---|
60 |
|
---|
61 | #ifdef XP_OS2
|
---|
62 | static char *child_argv[] = { "pipepong.exe", NULL };
|
---|
63 | #else
|
---|
64 | static char *child_argv[] = { "pipepong", NULL };
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | #define NUM_ITERATIONS 10
|
---|
68 |
|
---|
69 | int main()
|
---|
70 | {
|
---|
71 | PRFileDesc *in_pipe[2];
|
---|
72 | PRFileDesc *out_pipe[2];
|
---|
73 | PRStatus status;
|
---|
74 | PRProcess *process;
|
---|
75 | PRProcessAttr *attr;
|
---|
76 | char buf[1024];
|
---|
77 | PRInt32 nBytes;
|
---|
78 | PRInt32 exitCode;
|
---|
79 | int idx;
|
---|
80 |
|
---|
81 | status = PR_CreatePipe(&in_pipe[0], &in_pipe[1]);
|
---|
82 | if (status == PR_FAILURE) {
|
---|
83 | fprintf(stderr, "PR_CreatePipe failed\n");
|
---|
84 | exit(1);
|
---|
85 | }
|
---|
86 | status = PR_CreatePipe(&out_pipe[0], &out_pipe[1]);
|
---|
87 | if (status == PR_FAILURE) {
|
---|
88 | fprintf(stderr, "PR_CreatePipe failed\n");
|
---|
89 | exit(1);
|
---|
90 | }
|
---|
91 |
|
---|
92 | status = PR_SetFDInheritable(in_pipe[0], PR_FALSE);
|
---|
93 | if (status == PR_FAILURE) {
|
---|
94 | fprintf(stderr, "PR_SetFDInheritable failed\n");
|
---|
95 | exit(1);
|
---|
96 | }
|
---|
97 | status = PR_SetFDInheritable(in_pipe[1], PR_TRUE);
|
---|
98 | if (status == PR_FAILURE) {
|
---|
99 | fprintf(stderr, "PR_SetFDInheritable failed\n");
|
---|
100 | exit(1);
|
---|
101 | }
|
---|
102 | status = PR_SetFDInheritable(out_pipe[0], PR_TRUE);
|
---|
103 | if (status == PR_FAILURE) {
|
---|
104 | fprintf(stderr, "PR_SetFDInheritable failed\n");
|
---|
105 | exit(1);
|
---|
106 | }
|
---|
107 | status = PR_SetFDInheritable(out_pipe[1], PR_FALSE);
|
---|
108 | if (status == PR_FAILURE) {
|
---|
109 | fprintf(stderr, "PR_SetFDInheritable failed\n");
|
---|
110 | exit(1);
|
---|
111 | }
|
---|
112 |
|
---|
113 | attr = PR_NewProcessAttr();
|
---|
114 | if (attr == NULL) {
|
---|
115 | fprintf(stderr, "PR_NewProcessAttr failed\n");
|
---|
116 | exit(1);
|
---|
117 | }
|
---|
118 |
|
---|
119 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardInput, out_pipe[0]);
|
---|
120 | PR_ProcessAttrSetStdioRedirect(attr, PR_StandardOutput, in_pipe[1]);
|
---|
121 |
|
---|
122 | process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr);
|
---|
123 | if (process == NULL) {
|
---|
124 | fprintf(stderr, "PR_CreateProcess failed\n");
|
---|
125 | exit(1);
|
---|
126 | }
|
---|
127 | PR_DestroyProcessAttr(attr);
|
---|
128 | status = PR_Close(out_pipe[0]);
|
---|
129 | if (status == PR_FAILURE) {
|
---|
130 | fprintf(stderr, "PR_Close failed\n");
|
---|
131 | exit(1);
|
---|
132 | }
|
---|
133 | status = PR_Close(in_pipe[1]);
|
---|
134 | if (status == PR_FAILURE) {
|
---|
135 | fprintf(stderr, "PR_Close failed\n");
|
---|
136 | exit(1);
|
---|
137 | }
|
---|
138 |
|
---|
139 | for (idx = 0; idx < NUM_ITERATIONS; idx++) {
|
---|
140 | strcpy(buf, "ping");
|
---|
141 | printf("ping process: sending \"%s\"\n", buf);
|
---|
142 | nBytes = PR_Write(out_pipe[1], buf, 5);
|
---|
143 | if (nBytes == -1) {
|
---|
144 | fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
|
---|
145 | PR_GetOSError());
|
---|
146 | exit(1);
|
---|
147 | }
|
---|
148 | memset(buf, 0, sizeof(buf));
|
---|
149 | nBytes = PR_Read(in_pipe[0], buf, sizeof(buf));
|
---|
150 | if (nBytes == -1) {
|
---|
151 | fprintf(stderr, "PR_Read failed: (%d, %d)\n",
|
---|
152 | PR_GetError(), PR_GetOSError());
|
---|
153 | exit(1);
|
---|
154 | }
|
---|
155 | printf("ping process: received \"%s\"\n", buf);
|
---|
156 | if (nBytes != 5) {
|
---|
157 | fprintf(stderr, "ping process: expected 5 bytes but got %d bytes\n",
|
---|
158 | nBytes);
|
---|
159 | exit(1);
|
---|
160 | }
|
---|
161 | if (strcmp(buf, "pong") != 0) {
|
---|
162 | fprintf(stderr, "ping process: expected \"pong\" but got \"%s\"\n",
|
---|
163 | buf);
|
---|
164 | exit(1);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | status = PR_Close(in_pipe[0]);
|
---|
169 | if (status == PR_FAILURE) {
|
---|
170 | fprintf(stderr, "PR_Close failed\n");
|
---|
171 | exit(1);
|
---|
172 | }
|
---|
173 | status = PR_Close(out_pipe[1]);
|
---|
174 | if (status == PR_FAILURE) {
|
---|
175 | fprintf(stderr, "PR_Close failed\n");
|
---|
176 | exit(1);
|
---|
177 | }
|
---|
178 | status = PR_WaitProcess(process, &exitCode);
|
---|
179 | if (status == PR_FAILURE) {
|
---|
180 | fprintf(stderr, "PR_WaitProcess failed\n");
|
---|
181 | exit(1);
|
---|
182 | }
|
---|
183 | if (exitCode == 0) {
|
---|
184 | printf("PASS\n");
|
---|
185 | return 0;
|
---|
186 | } else {
|
---|
187 | printf("FAIL\n");
|
---|
188 | return 1;
|
---|
189 | }
|
---|
190 | }
|
---|