1 | /* $Id: tstRTProcCreateEx.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTProcCreateEx.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/process.h>
|
---|
42 |
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #include <iprt/env.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/initterm.h>
|
---|
47 | #include <iprt/mem.h>
|
---|
48 | #include <iprt/message.h>
|
---|
49 | #include <iprt/param.h>
|
---|
50 | #include <iprt/pipe.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include <iprt/stream.h>
|
---|
53 | #include <iprt/test.h>
|
---|
54 | #include <iprt/thread.h>
|
---|
55 |
|
---|
56 | #ifdef RT_OS_WINDOWS
|
---|
57 | # define SECURITY_WIN32
|
---|
58 | # include <iprt/win/windows.h>
|
---|
59 | # include <Security.h>
|
---|
60 | #endif
|
---|
61 |
|
---|
62 |
|
---|
63 | /*********************************************************************************************************************************
|
---|
64 | * Global Variables *
|
---|
65 | *********************************************************************************************************************************/
|
---|
66 | static RTENV g_hEnvInitial = NIL_RTENV;
|
---|
67 | static char g_szExecName[RTPATH_MAX];
|
---|
68 |
|
---|
69 |
|
---|
70 | static const char * const g_apszArgs4[] =
|
---|
71 | {
|
---|
72 | /* 0 */ "non existing non executable file",
|
---|
73 | /* 1 */ "--testcase-child-4",
|
---|
74 | /* 2 */ "a b",
|
---|
75 | /* 3 */ " cdef",
|
---|
76 | /* 4 */ "ghijkl ",
|
---|
77 | /* 5 */ "\"",
|
---|
78 | /* 6 */ "\\",
|
---|
79 | /* 7 */ "\\\"",
|
---|
80 | /* 8 */ "\\\"\\",
|
---|
81 | /* 9 */ "\\\\\"\\",
|
---|
82 | /*10 */ "%TEMP%",
|
---|
83 | /*11 */ "%TEMP%\filename",
|
---|
84 | /*12 */ "%TEMP%postfix",
|
---|
85 | /*13 */ "Prefix%TEMP%postfix",
|
---|
86 | /*14 */ "%",
|
---|
87 | /*15 */ "%%",
|
---|
88 | /*16 */ "%%%",
|
---|
89 | /*17 */ "%X",
|
---|
90 | /*18 */ "%%X",
|
---|
91 | NULL
|
---|
92 | };
|
---|
93 |
|
---|
94 |
|
---|
95 | static int tstRTCreateProcEx6Child(int argc, char **argv)
|
---|
96 | {
|
---|
97 | int rc = RTR3InitExeNoArguments(0);
|
---|
98 | if (RT_FAILURE(rc))
|
---|
99 | return RTMsgInitFailure(rc);
|
---|
100 |
|
---|
101 | int cErrors = 0;
|
---|
102 | char szValue[_16K];
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Check for the environment variable we've set in the parent process.
|
---|
106 | */
|
---|
107 | if (argc >= 3 && strcmp(argv[2], "inherit") == 0)
|
---|
108 | {
|
---|
109 | if (!RTEnvExistEx(RTENV_DEFAULT, "testcase-child-6"))
|
---|
110 | {
|
---|
111 | RTStrmPrintf(g_pStdErr, "child6: Env.var. 'testcase-child-6' was not inherited from parent\n");
|
---|
112 | cErrors++;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else if (argc >= 3 && strstr(argv[2], "change-record") != NULL)
|
---|
116 | {
|
---|
117 | rc = RTEnvGetEx(RTENV_DEFAULT, "testcase-child-6", szValue, sizeof(szValue), NULL);
|
---|
118 | if (RT_SUCCESS(rc) && strcmp(szValue, "changed"))
|
---|
119 | {
|
---|
120 | RTStrmPrintf(g_pStdErr, "child6: Env.var. 'testcase-child-6'='%s', expected 'changed'.\n", szValue);
|
---|
121 | cErrors++;
|
---|
122 | }
|
---|
123 | else if (RT_FAILURE(rc))
|
---|
124 | {
|
---|
125 | RTStrmPrintf(g_pStdErr, "child6: RTEnvGetEx(,'testcase-child-6',,) -> %Rrc\n", rc);
|
---|
126 | cErrors++;
|
---|
127 | }
|
---|
128 | }
|
---|
129 | else
|
---|
130 | {
|
---|
131 | if (RTEnvExistEx(RTENV_DEFAULT, "testcase-child-6"))
|
---|
132 | {
|
---|
133 | RTStrmPrintf(g_pStdErr, "child6: Env.var. 'testcase-child-6' was inherited from parent\n");
|
---|
134 | cErrors++;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | /*
|
---|
139 | * Check the user name if present we didn't inherit from parent.
|
---|
140 | */
|
---|
141 | if ( argc >= 4
|
---|
142 | && argv[3][0] != '\0'
|
---|
143 | && strstr(argv[2], "noinherit") != NULL)
|
---|
144 | {
|
---|
145 | static struct
|
---|
146 | {
|
---|
147 | const char *pszVarNm;
|
---|
148 | bool fReq;
|
---|
149 | } const s_aVars[] =
|
---|
150 | {
|
---|
151 | #ifdef RT_OS_WINDOWS
|
---|
152 | { "USERNAME", true },
|
---|
153 | #else
|
---|
154 | { "LOGNAME", true },
|
---|
155 | { "USER", false },
|
---|
156 | #endif
|
---|
157 | };
|
---|
158 | for (unsigned i = 0; i < RT_ELEMENTS(s_aVars); i++)
|
---|
159 | {
|
---|
160 | rc = RTEnvGetEx(RTENV_DEFAULT, s_aVars[i].pszVarNm, szValue, sizeof(szValue), NULL);
|
---|
161 | if (RT_SUCCESS(rc))
|
---|
162 | {
|
---|
163 | if (strcmp(szValue, argv[3]))
|
---|
164 | {
|
---|
165 | RTStrmPrintf(g_pStdErr, "child6: env.var. '%s'='%s', expected '%s'\n",
|
---|
166 | s_aVars[i].pszVarNm, szValue, argv[3]);
|
---|
167 | cErrors++;
|
---|
168 | }
|
---|
169 | }
|
---|
170 | else if (rc != VERR_ENV_VAR_NOT_FOUND || s_aVars[i].fReq)
|
---|
171 | {
|
---|
172 | RTStrmPrintf(g_pStdErr, "child6: RTGetEnv('%s') -> %Rrc\n", s_aVars[i].pszVarNm, rc);
|
---|
173 | cErrors++;
|
---|
174 | }
|
---|
175 | }
|
---|
176 | }
|
---|
177 |
|
---|
178 | #if 1
|
---|
179 | /* For manual testing. */
|
---|
180 | if (strcmp(argv[2],"noinherit") == 0)
|
---|
181 | //if (strcmp(argv[2],"noinherit-change-record") == 0)
|
---|
182 | {
|
---|
183 | RTENV hEnv;
|
---|
184 | rc = RTEnvClone(&hEnv, RTENV_DEFAULT);
|
---|
185 | if (RT_SUCCESS(rc))
|
---|
186 | {
|
---|
187 | uint32_t cVars = RTEnvCountEx(hEnv);
|
---|
188 | for (uint32_t i = 0; i < cVars; i++)
|
---|
189 | {
|
---|
190 | char szVarNm[_1K];
|
---|
191 | rc = RTEnvGetByIndexEx(hEnv, i, szVarNm, sizeof(szVarNm), szValue, sizeof(szValue));
|
---|
192 | if (RT_SUCCESS(rc))
|
---|
193 | RTStrmPrintf(g_pStdErr, "child6: #%u: %s=%s\n", i, szVarNm, szValue);
|
---|
194 | else
|
---|
195 | {
|
---|
196 | RTStrmPrintf(g_pStdErr, "child6: #%u: %Rrc\n", i, rc);
|
---|
197 | cErrors++;
|
---|
198 | }
|
---|
199 | }
|
---|
200 | RTEnvDestroy(hEnv);
|
---|
201 | }
|
---|
202 | else
|
---|
203 | {
|
---|
204 | RTStrmPrintf(g_pStdErr, "child6: RTEnvClone failed: %Rrc\n", rc);
|
---|
205 | cErrors++;
|
---|
206 | }
|
---|
207 | }
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | return cErrors == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
211 | }
|
---|
212 |
|
---|
213 | static void tstRTCreateProcEx6(const char *pszAsUser, const char *pszPassword)
|
---|
214 | {
|
---|
215 | RTTestISub("Profile environment");
|
---|
216 |
|
---|
217 | const char *apszArgs[5] =
|
---|
218 | {
|
---|
219 | g_szExecName,
|
---|
220 | "--testcase-child-6",
|
---|
221 | "inherit",
|
---|
222 | pszAsUser,
|
---|
223 | NULL
|
---|
224 | };
|
---|
225 |
|
---|
226 | RTTESTI_CHECK_RC_RETV(RTEnvSetEx(RTENV_DEFAULT, "testcase-child-6", "true"), VINF_SUCCESS);
|
---|
227 |
|
---|
228 | /* Use the process environment first. */
|
---|
229 | RTPROCESS hProc;
|
---|
230 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/,
|
---|
231 | NULL, NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
232 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
233 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
234 |
|
---|
235 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
236 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
237 |
|
---|
238 | /* Use the process environment first with a little change. */
|
---|
239 | apszArgs[2] = "change-record";
|
---|
240 | RTENV hEnvChange;
|
---|
241 | RTTESTI_CHECK_RC_RETV(RTEnvCreateChangeRecord(&hEnvChange), VINF_SUCCESS);
|
---|
242 | RTTESTI_CHECK_RC_RETV(RTEnvSetEx(hEnvChange, "testcase-child-6", "changed"), VINF_SUCCESS);
|
---|
243 | int rc;
|
---|
244 | RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, hEnvChange, RTPROC_FLAGS_ENV_CHANGE_RECORD,
|
---|
245 | NULL, NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
246 | if (RT_SUCCESS(rc))
|
---|
247 | {
|
---|
248 | ProcStatus.enmReason = RTPROCEXITREASON_ABEND;
|
---|
249 | ProcStatus.iStatus = -1;
|
---|
250 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
251 |
|
---|
252 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
253 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /* Use profile environment this time. */
|
---|
258 | apszArgs[2] = "noinherit";
|
---|
259 | RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, RTPROC_FLAGS_PROFILE,
|
---|
260 | NULL, NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
261 | if (RT_SUCCESS(rc))
|
---|
262 | {
|
---|
263 | ProcStatus.enmReason = RTPROCEXITREASON_ABEND;
|
---|
264 | ProcStatus.iStatus = -1;
|
---|
265 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
266 |
|
---|
267 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
268 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* Use profile environment this time. */
|
---|
272 | apszArgs[2] = "noinherit-change-record";
|
---|
273 | RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, hEnvChange,
|
---|
274 | RTPROC_FLAGS_PROFILE | RTPROC_FLAGS_ENV_CHANGE_RECORD,
|
---|
275 | NULL, NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
276 | if (RT_SUCCESS(rc))
|
---|
277 | {
|
---|
278 | ProcStatus.enmReason = RTPROCEXITREASON_ABEND;
|
---|
279 | ProcStatus.iStatus = -1;
|
---|
280 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
281 |
|
---|
282 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
283 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | RTTESTI_CHECK_RC(RTEnvDestroy(hEnvChange), VINF_SUCCESS);
|
---|
288 |
|
---|
289 | /*
|
---|
290 | * Restore the environment and check that the PROFILE flag didn't mess with
|
---|
291 | * the process environment. (Note! The bug may be elsewhere as well.)
|
---|
292 | */
|
---|
293 | RTTESTI_CHECK_RC(RTEnvUnsetEx(RTENV_DEFAULT, "testcase-child-6"), VINF_SUCCESS);
|
---|
294 |
|
---|
295 | RTENV hEnvCur;
|
---|
296 | RTTESTI_CHECK_RC_RETV(RTEnvClone(&hEnvCur, RTENV_DEFAULT), VINF_SUCCESS);
|
---|
297 | uint32_t cCurrent = RTEnvCountEx(hEnvCur);
|
---|
298 | uint32_t cInitial = RTEnvCountEx(g_hEnvInitial);
|
---|
299 | RTTESTI_CHECK_MSG(cCurrent == cInitial, ("cCurrent=%u cInitial=%u\n", cCurrent, cInitial));
|
---|
300 | uint32_t cVars1;
|
---|
301 | RTENV hEnv1, hEnv2;
|
---|
302 | const char *pszEnv1, *pszEnv2;
|
---|
303 | if (cCurrent >= cInitial)
|
---|
304 | {
|
---|
305 | hEnv1 = hEnvCur;
|
---|
306 | pszEnv1 = "current";
|
---|
307 | cVars1 = cCurrent;
|
---|
308 | hEnv2 = g_hEnvInitial;
|
---|
309 | pszEnv2 = "initial";
|
---|
310 | }
|
---|
311 | else
|
---|
312 | {
|
---|
313 | hEnv2 = hEnvCur;
|
---|
314 | pszEnv2 = "current";
|
---|
315 | hEnv1 = g_hEnvInitial;
|
---|
316 | pszEnv1 = "initial";
|
---|
317 | cVars1 = cInitial;
|
---|
318 | }
|
---|
319 | for (uint32_t i = 0; i < cVars1; i++)
|
---|
320 | {
|
---|
321 | char szValue1[_16K];
|
---|
322 | char szVarNm[_1K];
|
---|
323 | rc = RTEnvGetByIndexEx(hEnv1, i, szVarNm, sizeof(szVarNm), szValue1, sizeof(szValue1));
|
---|
324 | if (RT_SUCCESS(rc))
|
---|
325 | {
|
---|
326 | char szValue2[_16K];
|
---|
327 | rc = RTEnvGetEx(hEnv2, szVarNm, szValue2, sizeof(szValue2), NULL);
|
---|
328 | if (RT_SUCCESS(rc))
|
---|
329 | {
|
---|
330 | if (strcmp(szValue1, szValue2) != 0)
|
---|
331 | {
|
---|
332 | RTTestIFailed("Variable '%s' differs", szVarNm);
|
---|
333 | RTTestIFailureDetails("%s: '%s'\n"
|
---|
334 | "%s: '%s'\n",
|
---|
335 | pszEnv1, szValue1,
|
---|
336 | pszEnv2, szValue2);
|
---|
337 | }
|
---|
338 | }
|
---|
339 | else
|
---|
340 | RTTestIFailed("RTEnvGetEx(%s,%s,,) failed: %Rrc", pszEnv2, szVarNm, rc);
|
---|
341 |
|
---|
342 | }
|
---|
343 | else
|
---|
344 | RTTestIFailed("RTEnvGetByIndexEx(%s,%u,,,,) failed: %Rrc", pszEnv1, i, rc);
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 |
|
---|
349 | static int tstRTCreateProcEx5Child(int argc, char **argv)
|
---|
350 | {
|
---|
351 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
352 | if (RT_FAILURE(rc))
|
---|
353 | return RTMsgInitFailure(rc);
|
---|
354 |
|
---|
355 | uint32_t cErrors = 0;
|
---|
356 |
|
---|
357 | /* Check that the OS thinks we're running as the user we're supposed to. */
|
---|
358 | char *pszUser;
|
---|
359 | rc = RTProcQueryUsernameA(NIL_RTPROCESS, &pszUser);
|
---|
360 | if (RT_SUCCESS(rc))
|
---|
361 | {
|
---|
362 | #ifdef RT_OS_WINDOWS
|
---|
363 | if (RTStrICmp(pszUser, argv[2]) != 0)
|
---|
364 | #else
|
---|
365 | if (RTStrCmp(pszUser, argv[2]) != 0)
|
---|
366 | #endif
|
---|
367 | {
|
---|
368 | RTStrmPrintf(g_pStdErr, "child4: user name is '%s', expected '%s'\n", pszUser, argv[2]);
|
---|
369 | cErrors++;
|
---|
370 | }
|
---|
371 | RTStrFree(pszUser);
|
---|
372 | }
|
---|
373 | else
|
---|
374 | {
|
---|
375 | RTStrmPrintf(g_pStdErr, "child4: RTProcQueryUsernameA failed: %Rrc\n", rc);
|
---|
376 | cErrors++;
|
---|
377 | }
|
---|
378 |
|
---|
379 | return cErrors == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
380 | }
|
---|
381 |
|
---|
382 | static void tstRTCreateProcEx5(const char *pszUser, const char *pszPassword)
|
---|
383 | {
|
---|
384 | RTTestISubF("As user \"%s\" with password \"%s\"", pszUser, pszPassword);
|
---|
385 | RTTESTI_CHECK_RETV(pszUser && *pszUser);
|
---|
386 |
|
---|
387 | const char * apszArgs[] =
|
---|
388 | {
|
---|
389 | "test", /* user name */
|
---|
390 | "--testcase-child-5",
|
---|
391 | pszUser,
|
---|
392 | NULL
|
---|
393 | };
|
---|
394 |
|
---|
395 | /* Test for invalid logons. */
|
---|
396 | RTPROCESS hProc;
|
---|
397 | int rc = RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, NULL,
|
---|
398 | "non-existing-user", "wrong-password", NULL, &hProc);
|
---|
399 | if (rc != VERR_AUTHENTICATION_FAILURE && rc != VERR_PRIVILEGE_NOT_HELD && rc != VERR_PROC_TCB_PRIV_NOT_HELD)
|
---|
400 | RTTestIFailed("rc=%Rrc", rc);
|
---|
401 |
|
---|
402 | /* Test for invalid application. */
|
---|
403 | RTTESTI_CHECK_RC(RTProcCreateEx("non-existing-app", apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
404 | NULL, NULL, NULL, NULL, NULL, &hProc), VERR_FILE_NOT_FOUND);
|
---|
405 |
|
---|
406 | /* Test a (hopefully) valid user/password logon (given by parameters of this function). */
|
---|
407 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
408 | NULL, NULL, pszUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
409 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
410 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
411 |
|
---|
412 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
413 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
414 | }
|
---|
415 |
|
---|
416 |
|
---|
417 | static int tstRTCreateProcEx4Child(int argc, char **argv)
|
---|
418 | {
|
---|
419 | int rc = RTR3InitExeNoArguments(0);
|
---|
420 | if (RT_FAILURE(rc))
|
---|
421 | return RTMsgInitFailure(rc);
|
---|
422 |
|
---|
423 | int cErrors = 0;
|
---|
424 | for (int i = 0; i < argc; i++)
|
---|
425 | if (strcmp(argv[i], g_apszArgs4[i]))
|
---|
426 | {
|
---|
427 | RTStrmPrintf(g_pStdErr,
|
---|
428 | "child4: argv[%2u]='%s'\n"
|
---|
429 | "child4: expected='%s'\n",
|
---|
430 | i, argv[i], g_apszArgs4[i]);
|
---|
431 | cErrors++;
|
---|
432 | }
|
---|
433 |
|
---|
434 | return cErrors == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
435 | }
|
---|
436 |
|
---|
437 | static void tstRTCreateProcEx4(const char *pszAsUser, const char *pszPassword)
|
---|
438 | {
|
---|
439 | RTTestISub("Argument with spaces and stuff");
|
---|
440 |
|
---|
441 | RTPROCESS hProc;
|
---|
442 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, g_apszArgs4, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
443 | NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
444 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
445 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
446 |
|
---|
447 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
448 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
449 | }
|
---|
450 |
|
---|
451 |
|
---|
452 | static int tstRTCreateProcEx3Child(void)
|
---|
453 | {
|
---|
454 | int rc = RTR3InitExeNoArguments(0);
|
---|
455 | if (RT_FAILURE(rc))
|
---|
456 | return RTMsgInitFailure(rc);
|
---|
457 |
|
---|
458 | RTStrmPrintf(g_pStdOut, "w"); RTStrmFlush(g_pStdOut);
|
---|
459 | RTStrmPrintf(g_pStdErr, "o"); RTStrmFlush(g_pStdErr);
|
---|
460 | RTStrmPrintf(g_pStdOut, "r"); RTStrmFlush(g_pStdOut);
|
---|
461 | RTStrmPrintf(g_pStdErr, "k"); RTStrmFlush(g_pStdErr);
|
---|
462 | RTStrmPrintf(g_pStdOut, "s");
|
---|
463 |
|
---|
464 | return RTEXITCODE_SUCCESS;
|
---|
465 | }
|
---|
466 |
|
---|
467 | static void tstRTCreateProcEx3(const char *pszAsUser, const char *pszPassword)
|
---|
468 | {
|
---|
469 | RTTestISub("Standard Out+Err");
|
---|
470 |
|
---|
471 | RTPIPE hPipeR, hPipeW;
|
---|
472 | RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
|
---|
473 | const char * apszArgs[3] =
|
---|
474 | {
|
---|
475 | "non-existing-non-executable-file",
|
---|
476 | "--testcase-child-3",
|
---|
477 | NULL
|
---|
478 | };
|
---|
479 | RTHANDLE Handle;
|
---|
480 | Handle.enmType = RTHANDLETYPE_PIPE;
|
---|
481 | Handle.u.hPipe = hPipeW;
|
---|
482 | RTPROCESS hProc;
|
---|
483 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
484 | &Handle, &Handle, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
485 | RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
|
---|
486 |
|
---|
487 | char szOutput[_4K];
|
---|
488 | size_t offOutput = 0;
|
---|
489 | for (;;)
|
---|
490 | {
|
---|
491 | size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
|
---|
492 | RTTESTI_CHECK(cbLeft > 0);
|
---|
493 | if (cbLeft == 0)
|
---|
494 | break;
|
---|
495 |
|
---|
496 | size_t cbRead;
|
---|
497 | int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
|
---|
498 | if (RT_FAILURE(rc))
|
---|
499 | {
|
---|
500 | RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
|
---|
501 | break;
|
---|
502 | }
|
---|
503 | offOutput += cbRead;
|
---|
504 | }
|
---|
505 | szOutput[offOutput] = '\0';
|
---|
506 | RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
|
---|
507 |
|
---|
508 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
509 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
510 | RTThreadSleep(10);
|
---|
511 |
|
---|
512 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
513 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
514 | else if ( offOutput != sizeof("works") - 1
|
---|
515 | || strcmp(szOutput, "works"))
|
---|
516 | RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
|
---|
517 | }
|
---|
518 |
|
---|
519 |
|
---|
520 | static int tstRTCreateProcEx2Child(void)
|
---|
521 | {
|
---|
522 | int rc = RTR3InitExeNoArguments(0);
|
---|
523 | if (RT_FAILURE(rc))
|
---|
524 | return RTMsgInitFailure(rc);
|
---|
525 |
|
---|
526 | RTStrmPrintf(g_pStdErr, "howdy");
|
---|
527 | RTStrmPrintf(g_pStdOut, "ignore this output\n");
|
---|
528 |
|
---|
529 | return RTEXITCODE_SUCCESS;
|
---|
530 | }
|
---|
531 |
|
---|
532 | static void tstRTCreateProcEx2(const char *pszAsUser, const char *pszPassword)
|
---|
533 | {
|
---|
534 | RTTestISub("Standard Err");
|
---|
535 |
|
---|
536 | RTPIPE hPipeR, hPipeW;
|
---|
537 | RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
|
---|
538 | const char * apszArgs[3] =
|
---|
539 | {
|
---|
540 | "non-existing-non-executable-file",
|
---|
541 | "--testcase-child-2",
|
---|
542 | NULL
|
---|
543 | };
|
---|
544 | RTHANDLE Handle;
|
---|
545 | Handle.enmType = RTHANDLETYPE_PIPE;
|
---|
546 | Handle.u.hPipe = hPipeW;
|
---|
547 | RTPROCESS hProc;
|
---|
548 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
549 | NULL, &Handle, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
550 | RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
|
---|
551 |
|
---|
552 | char szOutput[_4K];
|
---|
553 | size_t offOutput = 0;
|
---|
554 | for (;;)
|
---|
555 | {
|
---|
556 | size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
|
---|
557 | RTTESTI_CHECK(cbLeft > 0);
|
---|
558 | if (cbLeft == 0)
|
---|
559 | break;
|
---|
560 |
|
---|
561 | size_t cbRead;
|
---|
562 | int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
|
---|
563 | if (RT_FAILURE(rc))
|
---|
564 | {
|
---|
565 | RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
|
---|
566 | break;
|
---|
567 | }
|
---|
568 | offOutput += cbRead;
|
---|
569 | }
|
---|
570 | szOutput[offOutput] = '\0';
|
---|
571 | RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
|
---|
572 |
|
---|
573 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
574 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
575 | RTThreadSleep(10);
|
---|
576 |
|
---|
577 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
578 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
579 | else if ( offOutput != sizeof("howdy") - 1
|
---|
580 | || strcmp(szOutput, "howdy"))
|
---|
581 | RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | static int tstRTCreateProcEx1Child(void)
|
---|
586 | {
|
---|
587 | int rc = RTR3InitExeNoArguments(0);
|
---|
588 | if (RT_FAILURE(rc))
|
---|
589 | return RTMsgInitFailure(rc);
|
---|
590 |
|
---|
591 | RTPrintf("it works");
|
---|
592 | RTStrmPrintf(g_pStdErr, "ignore this output\n");
|
---|
593 |
|
---|
594 | return RTEXITCODE_SUCCESS;
|
---|
595 | }
|
---|
596 |
|
---|
597 |
|
---|
598 | static void tstRTCreateProcEx1(const char *pszAsUser, const char *pszPassword)
|
---|
599 | {
|
---|
600 | RTTestISub("Standard Out");
|
---|
601 |
|
---|
602 | RTPIPE hPipeR, hPipeW;
|
---|
603 | RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
|
---|
604 | const char * apszArgs[3] =
|
---|
605 | {
|
---|
606 | "non-existing-non-executable-file",
|
---|
607 | "--testcase-child-1",
|
---|
608 | NULL
|
---|
609 | };
|
---|
610 | RTHANDLE Handle;
|
---|
611 | Handle.enmType = RTHANDLETYPE_PIPE;
|
---|
612 | Handle.u.hPipe = hPipeW;
|
---|
613 | RTPROCESS hProc;
|
---|
614 | RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
|
---|
615 | &Handle, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
|
---|
616 | RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
|
---|
617 |
|
---|
618 | char szOutput[_4K];
|
---|
619 | size_t offOutput = 0;
|
---|
620 | for (;;)
|
---|
621 | {
|
---|
622 | size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
|
---|
623 | RTTESTI_CHECK(cbLeft > 0);
|
---|
624 | if (cbLeft == 0)
|
---|
625 | break;
|
---|
626 |
|
---|
627 | size_t cbRead;
|
---|
628 | int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
|
---|
629 | if (RT_FAILURE(rc))
|
---|
630 | {
|
---|
631 | RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
|
---|
632 | break;
|
---|
633 | }
|
---|
634 | offOutput += cbRead;
|
---|
635 | }
|
---|
636 | szOutput[offOutput] = '\0';
|
---|
637 | RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
|
---|
638 |
|
---|
639 | RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
|
---|
640 | RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
|
---|
641 |
|
---|
642 | if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
|
---|
643 | RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
|
---|
644 | else if ( offOutput != sizeof("it works") - 1
|
---|
645 | || strcmp(szOutput, "it works"))
|
---|
646 | RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
|
---|
647 | }
|
---|
648 |
|
---|
649 |
|
---|
650 | int main(int argc, char **argv)
|
---|
651 | {
|
---|
652 | /*
|
---|
653 | * Deal with child processes first.
|
---|
654 | */
|
---|
655 | if (argc == 2 && !strcmp(argv[1], "--testcase-child-1"))
|
---|
656 | return tstRTCreateProcEx1Child();
|
---|
657 | if (argc == 2 && !strcmp(argv[1], "--testcase-child-2"))
|
---|
658 | return tstRTCreateProcEx2Child();
|
---|
659 | if (argc == 2 && !strcmp(argv[1], "--testcase-child-3"))
|
---|
660 | return tstRTCreateProcEx3Child();
|
---|
661 | if (argc >= 5 && !strcmp(argv[1], "--testcase-child-4"))
|
---|
662 | return tstRTCreateProcEx4Child(argc, argv);
|
---|
663 | if (argc >= 2 && !strcmp(argv[1], "--testcase-child-5"))
|
---|
664 | return tstRTCreateProcEx5Child(argc, argv);
|
---|
665 | if (argc >= 2 && !strcmp(argv[1], "--testcase-child-6"))
|
---|
666 | return tstRTCreateProcEx6Child(argc, argv);
|
---|
667 |
|
---|
668 | /*
|
---|
669 | * Main process.
|
---|
670 | */
|
---|
671 | const char *pszAsUser = NULL;
|
---|
672 | const char *pszPassword = NULL;
|
---|
673 | if (argc != 1)
|
---|
674 | {
|
---|
675 | if (argc != 4 || strcmp(argv[1], "--as-user"))
|
---|
676 | return 99;
|
---|
677 | pszAsUser = argv[2];
|
---|
678 | pszPassword = argv[3];
|
---|
679 | }
|
---|
680 |
|
---|
681 | RTTEST hTest;
|
---|
682 | int rc = RTTestInitAndCreate("tstRTProcCreateEx", &hTest);
|
---|
683 | if (rc)
|
---|
684 | return rc;
|
---|
685 | RTTestBanner(hTest);
|
---|
686 |
|
---|
687 | /*
|
---|
688 | * Init globals.
|
---|
689 | */
|
---|
690 | if (!RTProcGetExecutablePath(g_szExecName, sizeof(g_szExecName)))
|
---|
691 | RTStrCopy(g_szExecName, sizeof(g_szExecName), argv[0]);
|
---|
692 | RTTESTI_CHECK_RC(RTEnvClone(&g_hEnvInitial, RTENV_DEFAULT), VINF_SUCCESS);
|
---|
693 |
|
---|
694 | /*
|
---|
695 | * The tests.
|
---|
696 | */
|
---|
697 | tstRTCreateProcEx1(pszAsUser, pszPassword);
|
---|
698 | tstRTCreateProcEx2(pszAsUser, pszPassword);
|
---|
699 | tstRTCreateProcEx3(pszAsUser, pszPassword);
|
---|
700 | tstRTCreateProcEx4(pszAsUser, pszPassword);
|
---|
701 | if (pszAsUser)
|
---|
702 | tstRTCreateProcEx5(pszAsUser, pszPassword);
|
---|
703 | tstRTCreateProcEx6(pszAsUser, pszPassword);
|
---|
704 |
|
---|
705 | /** @todo Cover files, ++ */
|
---|
706 |
|
---|
707 | RTEnvDestroy(g_hEnvInitial);
|
---|
708 |
|
---|
709 | /*
|
---|
710 | * Summary.
|
---|
711 | */
|
---|
712 | return RTTestSummaryAndDestroy(hTest);
|
---|
713 | }
|
---|
714 |
|
---|