1 | /* $Id: tstRTProcCreateEx.cpp 26824 2010-02-26 10:36:08Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTProcCreateEx.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/process.h>
|
---|
36 |
|
---|
37 | #include <iprt/assert.h>
|
---|
38 | #include <iprt/env.h>
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/initterm.h>
|
---|
41 | #include <iprt/message.h>
|
---|
42 | #include <iprt/param.h>
|
---|
43 | #include <iprt/pipe.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/stream.h>
|
---|
46 | #include <iprt/test.h>
|
---|
47 | #include <iprt/thread.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*******************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *******************************************************************************/
|
---|
53 | static char g_szExecName[RTPATH_MAX];
|
---|
54 |
|
---|
55 |
|
---|
56 | static int tstRTCreateProcEx3Child(void)
|
---|
57 | {
|
---|
58 | int rc = RTR3Init();
|
---|
59 | if (rc)
|
---|
60 | return RTMsgInitFailure(rc);
|
---|
61 |
|
---|
62 | RTStrmPrintf(g_pStdOut, "w"); RTStrmFlush(g_pStdOut);
|
---|
63 | RTStrmPrintf(g_pStdErr, "o"); RTStrmFlush(g_pStdErr);
|
---|
64 | RTStrmPrintf(g_pStdOut, "r"); RTStrmFlush(g_pStdOut);
|
---|
65 | RTStrmPrintf(g_pStdErr, "k"); RTStrmFlush(g_pStdErr);
|
---|
66 | RTStrmPrintf(g_pStdOut, "s");
|
---|
67 |
|
---|
68 | return 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | static void tstRTCreateProcEx3(void)
|
---|
72 | {
|
---|
73 | RTTestISub("Standard Out+Err");
|
---|
74 |
|
---|
75 | RTPIPE hPipeR, hPipeW;
|
---|
76 | RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
|
---|
77 | const char * apszArgs[3] =
|
---|
78 | {
|
---|
79 | "non-existing-non-executable-file",
|
---|
80 | "--testcase-child-3",
|
---|
81 | NULL
|
---|
82 | };
|
---|
83 | RTHANDLE Handle;
|
---|
84 | Handle.enmType = RTHANDLETYPE_PIPE;
|
---|
85 | Handle.u.hPipe = hPipeW;
|
---|
86 | RTPROCESS hProc;
|
---|
87 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
88 | &Handle, &Handle, NULL, &hProc), VINF_SUCCESS);
|
---|
89 | RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
|
---|
90 |
|
---|
91 | char szOutput[_4K];
|
---|
92 | size_t offOutput = 0;
|
---|
93 | for (;;)
|
---|
94 | {
|
---|
95 | size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
|
---|
96 | RTTESTI_CHECK(cbLeft > 0);
|
---|
97 | if (cbLeft == 0)
|
---|
98 | break;
|
---|
99 |
|
---|
100 | size_t cbRead;
|
---|
101 | int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
|
---|
102 | if (RT_FAILURE(rc))
|
---|
103 | {
|
---|
104 | RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | offOutput += cbRead;
|
---|
108 | }
|
---|
109 | szOutput[offOutput] = '\0';
|
---|
110 | RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
|
---|
111 |
|
---|
112 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
113 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
114 | RTThreadSleep(10);
|
---|
115 |
|
---|
116 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
117 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
118 | else if ( offOutput != sizeof("works") - 1
|
---|
119 | || strcmp(szOutput, "works"))
|
---|
120 | RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
|
---|
121 | else
|
---|
122 | RTTestIPassed(NULL);
|
---|
123 | }
|
---|
124 |
|
---|
125 | static int tstRTCreateProcEx2Child(void)
|
---|
126 | {
|
---|
127 | int rc = RTR3Init();
|
---|
128 | if (rc)
|
---|
129 | return RTMsgInitFailure(rc);
|
---|
130 |
|
---|
131 | RTStrmPrintf(g_pStdErr, "howdy");
|
---|
132 | RTStrmPrintf(g_pStdOut, "ignore this output\n");
|
---|
133 |
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 | static void tstRTCreateProcEx2(void)
|
---|
138 | {
|
---|
139 | RTTestISub("Standard Err");
|
---|
140 |
|
---|
141 | RTPIPE hPipeR, hPipeW;
|
---|
142 | RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
|
---|
143 | const char * apszArgs[3] =
|
---|
144 | {
|
---|
145 | "non-existing-non-executable-file",
|
---|
146 | "--testcase-child-2",
|
---|
147 | NULL
|
---|
148 | };
|
---|
149 | RTHANDLE Handle;
|
---|
150 | Handle.enmType = RTHANDLETYPE_PIPE;
|
---|
151 | Handle.u.hPipe = hPipeW;
|
---|
152 | RTPROCESS hProc;
|
---|
153 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
154 | NULL, &Handle, NULL, &hProc), VINF_SUCCESS);
|
---|
155 | RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
|
---|
156 |
|
---|
157 | char szOutput[_4K];
|
---|
158 | size_t offOutput = 0;
|
---|
159 | for (;;)
|
---|
160 | {
|
---|
161 | size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
|
---|
162 | RTTESTI_CHECK(cbLeft > 0);
|
---|
163 | if (cbLeft == 0)
|
---|
164 | break;
|
---|
165 |
|
---|
166 | size_t cbRead;
|
---|
167 | int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
|
---|
168 | if (RT_FAILURE(rc))
|
---|
169 | {
|
---|
170 | RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | offOutput += cbRead;
|
---|
174 | }
|
---|
175 | szOutput[offOutput] = '\0';
|
---|
176 | RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
|
---|
177 |
|
---|
178 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
179 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
180 | RTThreadSleep(10);
|
---|
181 |
|
---|
182 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
183 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
184 | else if ( offOutput != sizeof("howdy") - 1
|
---|
185 | || strcmp(szOutput, "howdy"))
|
---|
186 | RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
|
---|
187 | else
|
---|
188 | RTTestIPassed(NULL);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | static int tstRTCreateProcEx1Child(void)
|
---|
193 | {
|
---|
194 | int rc = RTR3Init();
|
---|
195 | if (rc)
|
---|
196 | return RTMsgInitFailure(rc);
|
---|
197 | RTPrintf("it works");
|
---|
198 | RTStrmPrintf(g_pStdErr, "ignore this output\n");
|
---|
199 | return 0;
|
---|
200 | }
|
---|
201 |
|
---|
202 |
|
---|
203 | static void tstRTCreateProcEx1(void)
|
---|
204 | {
|
---|
205 | RTTestISub("Standard Out");
|
---|
206 |
|
---|
207 | RTPIPE hPipeR, hPipeW;
|
---|
208 | RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
|
---|
209 | const char * apszArgs[3] =
|
---|
210 | {
|
---|
211 | "non-existing-non-executable-file",
|
---|
212 | "--testcase-child-1",
|
---|
213 | NULL
|
---|
214 | };
|
---|
215 | RTHANDLE Handle;
|
---|
216 | Handle.enmType = RTHANDLETYPE_PIPE;
|
---|
217 | Handle.u.hPipe = hPipeW;
|
---|
218 | RTPROCESS hProc;
|
---|
219 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
220 | &Handle, NULL, NULL, &hProc), VINF_SUCCESS);
|
---|
221 | RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
|
---|
222 |
|
---|
223 | char szOutput[_4K];
|
---|
224 | size_t offOutput = 0;
|
---|
225 | for (;;)
|
---|
226 | {
|
---|
227 | size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
|
---|
228 | RTTESTI_CHECK(cbLeft > 0);
|
---|
229 | if (cbLeft == 0)
|
---|
230 | break;
|
---|
231 |
|
---|
232 | size_t cbRead;
|
---|
233 | int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
|
---|
234 | if (RT_FAILURE(rc))
|
---|
235 | {
|
---|
236 | RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
|
---|
237 | break;
|
---|
238 | }
|
---|
239 | offOutput += cbRead;
|
---|
240 | }
|
---|
241 | szOutput[offOutput] = '\0';
|
---|
242 | RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
|
---|
243 |
|
---|
244 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
245 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
246 |
|
---|
247 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
248 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
249 | else if ( offOutput != sizeof("it works") - 1
|
---|
250 | || strcmp(szOutput, "it works"))
|
---|
251 | RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
|
---|
252 | else
|
---|
253 | RTTestIPassed(NULL);
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | int main(int argc, char **argv)
|
---|
258 | {
|
---|
259 | if (argc == 2 && !strcmp(argv[1], "--testcase-child-1"))
|
---|
260 | return tstRTCreateProcEx1Child();
|
---|
261 | if (argc == 2 && !strcmp(argv[1], "--testcase-child-2"))
|
---|
262 | return tstRTCreateProcEx2Child();
|
---|
263 | if (argc == 2 && !strcmp(argv[1], "--testcase-child-3"))
|
---|
264 | return tstRTCreateProcEx3Child();
|
---|
265 | if (argc != 1)
|
---|
266 | return 99;
|
---|
267 |
|
---|
268 | RTTEST hTest;
|
---|
269 | int rc = RTTestInitAndCreate("tstRTProcCreateEx", &hTest);
|
---|
270 | if (rc)
|
---|
271 | return rc;
|
---|
272 | RTTestBanner(hTest);
|
---|
273 |
|
---|
274 | if (!RTProcGetExecutableName(g_szExecName, sizeof(g_szExecName)))
|
---|
275 | RTStrCopy(g_szExecName, sizeof(g_szExecName), argv[0]);
|
---|
276 |
|
---|
277 | /*
|
---|
278 | * The tests.
|
---|
279 | */
|
---|
280 | tstRTCreateProcEx1();
|
---|
281 | tstRTCreateProcEx2();
|
---|
282 | tstRTCreateProcEx3();
|
---|
283 | /** @todo Cover files, pszAsUser, arguments with spaces, ++ */
|
---|
284 |
|
---|
285 | /*
|
---|
286 | * Summary.
|
---|
287 | */
|
---|
288 | return RTTestSummaryAndDestroy(hTest);
|
---|
289 | }
|
---|
290 |
|
---|