VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTProcCreateEx.cpp@ 99110

Last change on this file since 99110 was 99110, checked in by vboxsync, 22 months ago

IPRT/process: Added testcases for RTPROC_FLAGS_CWD. bugref:8053

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 28.9 KB
Line 
1/* $Id: tstRTProcCreateEx.cpp 99110 2023-03-22 10:15:12Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTProcCreateEx.
4 */
5
6/*
7 * Copyright (C) 2010-2023 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/path.h> /* For CWD testing. */
51#include <iprt/pipe.h>
52#include <iprt/string.h>
53#include <iprt/stream.h>
54#include <iprt/test.h>
55#include <iprt/thread.h>
56
57#ifdef RT_OS_WINDOWS
58# define SECURITY_WIN32
59# include <iprt/win/windows.h>
60# include <Security.h>
61#endif
62
63
64/*********************************************************************************************************************************
65* Global Variables *
66*********************************************************************************************************************************/
67static RTENV g_hEnvInitial = NIL_RTENV;
68static char g_szExecName[RTPATH_MAX];
69
70
71static const char * const g_apszArgs4[] =
72{
73 /* 0 */ "non existing non executable file",
74 /* 1 */ "--testcase-child-4",
75 /* 2 */ "a b",
76 /* 3 */ " cdef",
77 /* 4 */ "ghijkl ",
78 /* 5 */ "\"",
79 /* 6 */ "\\",
80 /* 7 */ "\\\"",
81 /* 8 */ "\\\"\\",
82 /* 9 */ "\\\\\"\\",
83 /*10 */ "%TEMP%",
84 /*11 */ "%TEMP%\filename",
85 /*12 */ "%TEMP%postfix",
86 /*13 */ "Prefix%TEMP%postfix",
87 /*14 */ "%",
88 /*15 */ "%%",
89 /*16 */ "%%%",
90 /*17 */ "%X",
91 /*18 */ "%%X",
92 NULL
93};
94
95
96static int tstRTCreateProcExCwdChild(int argc, char **argv)
97{
98 int rc = RTR3InitExeNoArguments(0);
99 if (RT_FAILURE(rc))
100 return RTMsgInitFailure(rc);
101
102 int cErrors = 0;
103
104 if (argc < 3)
105 return RTEXITCODE_FAILURE;
106
107 const char *pszCWD = argv[2];
108
109 /* Validate if we really are in the CWD the parent told us. */
110 char szCWD[RTPATH_MAX];
111 rc = RTPathGetCurrent(szCWD, sizeof(szCWD));
112 if (RT_FAILURE(rc))
113 {
114 RTStrmPrintf(g_pStdErr, "childcwd: Unabled retrieve CWD, rc = %Rrc\n", rc);
115 return RTEXITCODE_FAILURE;
116 }
117
118 if (RTStrCmp(szCWD, pszCWD))
119 {
120 RTStrmPrintf(g_pStdErr, "childcwd: CWD is '%s', but expected '%s'\n", pszCWD, pszCWD);
121 return RTEXITCODE_FAILURE;
122 }
123
124 /* Check if we can query information of the current CWD. */
125 char *pszUser = NULL;
126 if ( argc >= 4
127 && argv[3][0] != '\0')
128 {
129 pszUser = argv[3];
130 }
131
132 RTFSOBJINFO objInfo;
133 rc = RTPathQueryInfo(pszCWD, &objInfo, RTFSOBJATTRADD_NOTHING);
134 if (pszUser)
135 RTStrmPrintf(g_pStdOut, "childcwd: Accessing CWD '%s' via user '%s' -> %Rrc\n", pszCWD, pszUser, rc);
136 else
137 RTStrmPrintf(g_pStdOut, "childcwd: Accesing CWD '%s' -> %Rrc\n", pszCWD, rc);
138 if (RT_FAILURE(rc))
139 cErrors++;
140
141 return cErrors == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
142}
143
144static void tstRTCreateProcExCwd(const char *pszAsUser, const char *pszPassword)
145{
146 RTTestISub("Current working directory (CWD)");
147
148 const char *apszArgs[5] =
149 {
150 g_szExecName,
151 "--testcase-child-cwd",
152 "<invalid-cwd>",
153 pszAsUser,
154 NULL
155 };
156
157 bool const fMayPanic = RTAssertSetMayPanic(false);
158 bool const fQuiet = RTAssertSetQuiet(true);
159
160 RTPROCESS hProc;
161 uint32_t fFlags = 0;
162 /* Missing RTPROC_FLAGS_CWD. */
163 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, fFlags,
164 NULL, NULL, NULL, pszAsUser, pszPassword,
165 (void *)apszArgs[2] /* pvExtraData (CWD) */, &hProc),
166 VERR_INVALID_PARAMETER);
167
168 /* RTPROC_FLAGS_CWD set, but CWD missing as pvExtradata. */
169 fFlags = RTPROC_FLAGS_CWD;
170 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, fFlags,
171 NULL, NULL, NULL, pszAsUser, pszPassword,
172 NULL, &hProc),
173 VINF_SUCCESS);
174
175 /* CWD is "<invalid-cwd>" from above, should fail. */
176 RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
177 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
178
179 char szCWD[RTPATH_MAX];
180
181 /* Try current CWD, whatever that is. */
182 RTTESTI_CHECK_RC_OK_RETV(RTPathGetCurrent(szCWD, sizeof(szCWD)));
183 apszArgs[2] = szCWD;
184 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, fFlags,
185 NULL, NULL, NULL, pszAsUser, pszPassword,
186 (void *)szCWD /* pvExtraData (CWD) */, &hProc),
187 VINF_SUCCESS);
188 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
189 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
190 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
191
192 /* Try temporary directory. */
193 RTTESTI_CHECK_RC_OK_RETV(RTPathTemp(szCWD, sizeof(szCWD)));
194 apszArgs[2] = szCWD;
195 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, fFlags,
196 NULL, NULL, NULL, pszAsUser, pszPassword,
197 (void *)szCWD /* pvExtraData (CWD) */, &hProc),
198 VINF_SUCCESS);
199 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
200 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
201 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
202
203 /* Try user home. */
204 RTTESTI_CHECK_RC_OK_RETV(RTPathUserHome(szCWD, sizeof(szCWD)));
205 apszArgs[2] = szCWD;
206 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, fFlags,
207 NULL, NULL, NULL, pszAsUser, pszPassword,
208 (void *)szCWD /* pvExtraData (CWD) */, &hProc),
209 VINF_SUCCESS);
210 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
211 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
212 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
213
214 RTAssertSetMayPanic(fMayPanic);
215 RTAssertSetQuiet(fQuiet);
216}
217
218static int tstRTCreateProcEx6Child(int argc, char **argv)
219{
220 int rc = RTR3InitExeNoArguments(0);
221 if (RT_FAILURE(rc))
222 return RTMsgInitFailure(rc);
223
224 int cErrors = 0;
225 char szValue[_16K];
226
227 /*
228 * Check for the environment variable we've set in the parent process.
229 */
230 if (argc >= 3 && strcmp(argv[2], "inherit") == 0)
231 {
232 if (!RTEnvExistEx(RTENV_DEFAULT, "testcase-child-6"))
233 {
234 RTStrmPrintf(g_pStdErr, "child6: Env.var. 'testcase-child-6' was not inherited from parent\n");
235 cErrors++;
236 }
237 }
238 else if (argc >= 3 && strstr(argv[2], "change-record") != NULL)
239 {
240 rc = RTEnvGetEx(RTENV_DEFAULT, "testcase-child-6", szValue, sizeof(szValue), NULL);
241 if (RT_SUCCESS(rc) && strcmp(szValue, "changed"))
242 {
243 RTStrmPrintf(g_pStdErr, "child6: Env.var. 'testcase-child-6'='%s', expected 'changed'.\n", szValue);
244 cErrors++;
245 }
246 else if (RT_FAILURE(rc))
247 {
248 RTStrmPrintf(g_pStdErr, "child6: RTEnvGetEx(,'testcase-child-6',,) -> %Rrc\n", rc);
249 cErrors++;
250 }
251 }
252 else
253 {
254 if (RTEnvExistEx(RTENV_DEFAULT, "testcase-child-6"))
255 {
256 RTStrmPrintf(g_pStdErr, "child6: Env.var. 'testcase-child-6' was inherited from parent\n");
257 cErrors++;
258 }
259 }
260
261 /*
262 * Check the user name if present we didn't inherit from parent.
263 */
264 if ( argc >= 4
265 && argv[3][0] != '\0'
266 && strstr(argv[2], "noinherit") != NULL)
267 {
268 static struct
269 {
270 const char *pszVarNm;
271 bool fReq;
272 } const s_aVars[] =
273 {
274#ifdef RT_OS_WINDOWS
275 { "USERNAME", true },
276#else
277 { "LOGNAME", true },
278 { "USER", false },
279#endif
280 };
281 for (unsigned i = 0; i < RT_ELEMENTS(s_aVars); i++)
282 {
283 rc = RTEnvGetEx(RTENV_DEFAULT, s_aVars[i].pszVarNm, szValue, sizeof(szValue), NULL);
284 if (RT_SUCCESS(rc))
285 {
286 if (strcmp(szValue, argv[3]))
287 {
288 RTStrmPrintf(g_pStdErr, "child6: env.var. '%s'='%s', expected '%s'\n",
289 s_aVars[i].pszVarNm, szValue, argv[3]);
290 cErrors++;
291 }
292 }
293 else if (rc != VERR_ENV_VAR_NOT_FOUND || s_aVars[i].fReq)
294 {
295 RTStrmPrintf(g_pStdErr, "child6: RTGetEnv('%s') -> %Rrc\n", s_aVars[i].pszVarNm, rc);
296 cErrors++;
297 }
298 }
299 }
300
301#if 1
302 /* For manual testing. */
303 if (strcmp(argv[2],"noinherit") == 0)
304 //if (strcmp(argv[2],"noinherit-change-record") == 0)
305 {
306 RTENV hEnv;
307 rc = RTEnvClone(&hEnv, RTENV_DEFAULT);
308 if (RT_SUCCESS(rc))
309 {
310 uint32_t cVars = RTEnvCountEx(hEnv);
311 for (uint32_t i = 0; i < cVars; i++)
312 {
313 char szVarNm[_1K];
314 rc = RTEnvGetByIndexEx(hEnv, i, szVarNm, sizeof(szVarNm), szValue, sizeof(szValue));
315 if (RT_SUCCESS(rc))
316 RTStrmPrintf(g_pStdErr, "child6: #%u: %s=%s\n", i, szVarNm, szValue);
317 else
318 {
319 RTStrmPrintf(g_pStdErr, "child6: #%u: %Rrc\n", i, rc);
320 cErrors++;
321 }
322 }
323 RTEnvDestroy(hEnv);
324 }
325 else
326 {
327 RTStrmPrintf(g_pStdErr, "child6: RTEnvClone failed: %Rrc\n", rc);
328 cErrors++;
329 }
330 }
331#endif
332
333 return cErrors == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
334}
335
336static void tstRTCreateProcEx6(const char *pszAsUser, const char *pszPassword)
337{
338 RTTestISub("Profile environment");
339
340 const char *apszArgs[5] =
341 {
342 g_szExecName,
343 "--testcase-child-6",
344 "inherit",
345 pszAsUser,
346 NULL
347 };
348
349 RTTESTI_CHECK_RC_RETV(RTEnvSetEx(RTENV_DEFAULT, "testcase-child-6", "true"), VINF_SUCCESS);
350
351 /* Use the process environment first. */
352 RTPROCESS hProc;
353 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/,
354 NULL, NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
355 RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
356 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
357
358 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
359 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
360
361 /* Use the process environment first with a little change. */
362 apszArgs[2] = "change-record";
363 RTENV hEnvChange;
364 RTTESTI_CHECK_RC_RETV(RTEnvCreateChangeRecord(&hEnvChange), VINF_SUCCESS);
365 RTTESTI_CHECK_RC_RETV(RTEnvSetEx(hEnvChange, "testcase-child-6", "changed"), VINF_SUCCESS);
366 int rc;
367 RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, hEnvChange, RTPROC_FLAGS_ENV_CHANGE_RECORD,
368 NULL, NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
369 if (RT_SUCCESS(rc))
370 {
371 ProcStatus.enmReason = RTPROCEXITREASON_ABEND;
372 ProcStatus.iStatus = -1;
373 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
374
375 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
376 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
377 }
378
379
380 /* Use profile environment this time. */
381 apszArgs[2] = "noinherit";
382 RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, RTPROC_FLAGS_PROFILE,
383 NULL, NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
384 if (RT_SUCCESS(rc))
385 {
386 ProcStatus.enmReason = RTPROCEXITREASON_ABEND;
387 ProcStatus.iStatus = -1;
388 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
389
390 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
391 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
392 }
393
394 /* Use profile environment this time. */
395 apszArgs[2] = "noinherit-change-record";
396 RTTESTI_CHECK_RC(rc = RTProcCreateEx(g_szExecName, apszArgs, hEnvChange,
397 RTPROC_FLAGS_PROFILE | RTPROC_FLAGS_ENV_CHANGE_RECORD,
398 NULL, NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
399 if (RT_SUCCESS(rc))
400 {
401 ProcStatus.enmReason = RTPROCEXITREASON_ABEND;
402 ProcStatus.iStatus = -1;
403 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
404
405 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
406 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
407 }
408
409
410 RTTESTI_CHECK_RC(RTEnvDestroy(hEnvChange), VINF_SUCCESS);
411
412 /*
413 * Restore the environment and check that the PROFILE flag didn't mess with
414 * the process environment. (Note! The bug may be elsewhere as well.)
415 */
416 RTTESTI_CHECK_RC(RTEnvUnsetEx(RTENV_DEFAULT, "testcase-child-6"), VINF_SUCCESS);
417
418 RTENV hEnvCur;
419 RTTESTI_CHECK_RC_RETV(RTEnvClone(&hEnvCur, RTENV_DEFAULT), VINF_SUCCESS);
420 uint32_t cCurrent = RTEnvCountEx(hEnvCur);
421 uint32_t cInitial = RTEnvCountEx(g_hEnvInitial);
422 RTTESTI_CHECK_MSG(cCurrent == cInitial, ("cCurrent=%u cInitial=%u\n", cCurrent, cInitial));
423 uint32_t cVars1;
424 RTENV hEnv1, hEnv2;
425 const char *pszEnv1, *pszEnv2;
426 if (cCurrent >= cInitial)
427 {
428 hEnv1 = hEnvCur;
429 pszEnv1 = "current";
430 cVars1 = cCurrent;
431 hEnv2 = g_hEnvInitial;
432 pszEnv2 = "initial";
433 }
434 else
435 {
436 hEnv2 = hEnvCur;
437 pszEnv2 = "current";
438 hEnv1 = g_hEnvInitial;
439 pszEnv1 = "initial";
440 cVars1 = cInitial;
441 }
442 for (uint32_t i = 0; i < cVars1; i++)
443 {
444 char szValue1[_16K];
445 char szVarNm[_1K];
446 rc = RTEnvGetByIndexEx(hEnv1, i, szVarNm, sizeof(szVarNm), szValue1, sizeof(szValue1));
447 if (RT_SUCCESS(rc))
448 {
449 char szValue2[_16K];
450 rc = RTEnvGetEx(hEnv2, szVarNm, szValue2, sizeof(szValue2), NULL);
451 if (RT_SUCCESS(rc))
452 {
453 if (strcmp(szValue1, szValue2) != 0)
454 {
455 RTTestIFailed("Variable '%s' differs", szVarNm);
456 RTTestIFailureDetails("%s: '%s'\n"
457 "%s: '%s'\n",
458 pszEnv1, szValue1,
459 pszEnv2, szValue2);
460 }
461 }
462 else
463 RTTestIFailed("RTEnvGetEx(%s,%s,,) failed: %Rrc", pszEnv2, szVarNm, rc);
464
465 }
466 else
467 RTTestIFailed("RTEnvGetByIndexEx(%s,%u,,,,) failed: %Rrc", pszEnv1, i, rc);
468 }
469}
470
471
472static int tstRTCreateProcEx5Child(int argc, char **argv)
473{
474 int rc = RTR3InitExe(argc, &argv, 0);
475 if (RT_FAILURE(rc))
476 return RTMsgInitFailure(rc);
477
478 uint32_t cErrors = 0;
479
480 /* Check that the OS thinks we're running as the user we're supposed to. */
481 char *pszUser;
482 rc = RTProcQueryUsernameA(NIL_RTPROCESS, &pszUser);
483 if (RT_SUCCESS(rc))
484 {
485#ifdef RT_OS_WINDOWS
486 if (RTStrICmp(pszUser, argv[2]) != 0)
487#else
488 if (RTStrCmp(pszUser, argv[2]) != 0)
489#endif
490 {
491 RTStrmPrintf(g_pStdErr, "child4: user name is '%s', expected '%s'\n", pszUser, argv[2]);
492 cErrors++;
493 }
494 RTStrFree(pszUser);
495 }
496 else
497 {
498 RTStrmPrintf(g_pStdErr, "child4: RTProcQueryUsernameA failed: %Rrc\n", rc);
499 cErrors++;
500 }
501
502 return cErrors == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
503}
504
505static void tstRTCreateProcEx5(const char *pszUser, const char *pszPassword)
506{
507 RTTestISubF("As user \"%s\" with password \"%s\"", pszUser, pszPassword);
508 RTTESTI_CHECK_RETV(pszUser && *pszUser);
509
510 const char * apszArgs[] =
511 {
512 "test", /* user name */
513 "--testcase-child-5",
514 pszUser,
515 NULL
516 };
517
518 /* Test for invalid logons. */
519 RTPROCESS hProc;
520 int rc = RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL, NULL, NULL,
521 "non-existing-user", "wrong-password", NULL, &hProc);
522 if (rc != VERR_AUTHENTICATION_FAILURE && rc != VERR_PRIVILEGE_NOT_HELD && rc != VERR_PROC_TCB_PRIV_NOT_HELD)
523 RTTestIFailed("rc=%Rrc", rc);
524
525 /* Test for invalid application. */
526 RTTESTI_CHECK_RC(RTProcCreateEx("non-existing-app", apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
527 NULL, NULL, NULL, NULL, NULL, &hProc), VERR_FILE_NOT_FOUND);
528
529 /* Test a (hopefully) valid user/password logon (given by parameters of this function). */
530 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
531 NULL, NULL, pszUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
532 RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
533 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
534
535 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
536 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
537}
538
539
540static int tstRTCreateProcEx4Child(int argc, char **argv)
541{
542 int rc = RTR3InitExeNoArguments(0);
543 if (RT_FAILURE(rc))
544 return RTMsgInitFailure(rc);
545
546 int cErrors = 0;
547 for (int i = 0; i < argc; i++)
548 if (strcmp(argv[i], g_apszArgs4[i]))
549 {
550 RTStrmPrintf(g_pStdErr,
551 "child4: argv[%2u]='%s'\n"
552 "child4: expected='%s'\n",
553 i, argv[i], g_apszArgs4[i]);
554 cErrors++;
555 }
556
557 return cErrors == 0 ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
558}
559
560static void tstRTCreateProcEx4(const char *pszAsUser, const char *pszPassword)
561{
562 RTTestISub("Argument with spaces and stuff");
563
564 RTPROCESS hProc;
565 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, g_apszArgs4, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
566 NULL, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
567 RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
568 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
569
570 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
571 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
572}
573
574
575static int tstRTCreateProcEx3Child(void)
576{
577 int rc = RTR3InitExeNoArguments(0);
578 if (RT_FAILURE(rc))
579 return RTMsgInitFailure(rc);
580
581 RTStrmPrintf(g_pStdOut, "w"); RTStrmFlush(g_pStdOut);
582 RTStrmPrintf(g_pStdErr, "o"); RTStrmFlush(g_pStdErr);
583 RTStrmPrintf(g_pStdOut, "r"); RTStrmFlush(g_pStdOut);
584 RTStrmPrintf(g_pStdErr, "k"); RTStrmFlush(g_pStdErr);
585 RTStrmPrintf(g_pStdOut, "s");
586
587 return RTEXITCODE_SUCCESS;
588}
589
590static void tstRTCreateProcEx3(const char *pszAsUser, const char *pszPassword)
591{
592 RTTestISub("Standard Out+Err");
593
594 RTPIPE hPipeR, hPipeW;
595 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
596 const char * apszArgs[3] =
597 {
598 "non-existing-non-executable-file",
599 "--testcase-child-3",
600 NULL
601 };
602 RTHANDLE Handle;
603 Handle.enmType = RTHANDLETYPE_PIPE;
604 Handle.u.hPipe = hPipeW;
605 RTPROCESS hProc;
606 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
607 &Handle, &Handle, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
608 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
609
610 char szOutput[_4K];
611 size_t offOutput = 0;
612 for (;;)
613 {
614 size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
615 RTTESTI_CHECK(cbLeft > 0);
616 if (cbLeft == 0)
617 break;
618
619 size_t cbRead;
620 int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
621 if (RT_FAILURE(rc))
622 {
623 RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
624 break;
625 }
626 offOutput += cbRead;
627 }
628 szOutput[offOutput] = '\0';
629 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
630
631 RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
632 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
633 RTThreadSleep(10);
634
635 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
636 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
637 else if ( offOutput != sizeof("works") - 1
638 || strcmp(szOutput, "works"))
639 RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
640}
641
642
643static int tstRTCreateProcEx2Child(void)
644{
645 int rc = RTR3InitExeNoArguments(0);
646 if (RT_FAILURE(rc))
647 return RTMsgInitFailure(rc);
648
649 RTStrmPrintf(g_pStdErr, "howdy");
650 RTStrmPrintf(g_pStdOut, "ignore this output\n");
651
652 return RTEXITCODE_SUCCESS;
653}
654
655static void tstRTCreateProcEx2(const char *pszAsUser, const char *pszPassword)
656{
657 RTTestISub("Standard Err");
658
659 RTPIPE hPipeR, hPipeW;
660 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
661 const char * apszArgs[3] =
662 {
663 "non-existing-non-executable-file",
664 "--testcase-child-2",
665 NULL
666 };
667 RTHANDLE Handle;
668 Handle.enmType = RTHANDLETYPE_PIPE;
669 Handle.u.hPipe = hPipeW;
670 RTPROCESS hProc;
671 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
672 NULL, &Handle, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
673 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
674
675 char szOutput[_4K];
676 size_t offOutput = 0;
677 for (;;)
678 {
679 size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
680 RTTESTI_CHECK(cbLeft > 0);
681 if (cbLeft == 0)
682 break;
683
684 size_t cbRead;
685 int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
686 if (RT_FAILURE(rc))
687 {
688 RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
689 break;
690 }
691 offOutput += cbRead;
692 }
693 szOutput[offOutput] = '\0';
694 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
695
696 RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
697 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
698 RTThreadSleep(10);
699
700 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
701 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
702 else if ( offOutput != sizeof("howdy") - 1
703 || strcmp(szOutput, "howdy"))
704 RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
705}
706
707
708static int tstRTCreateProcEx1Child(void)
709{
710 int rc = RTR3InitExeNoArguments(0);
711 if (RT_FAILURE(rc))
712 return RTMsgInitFailure(rc);
713
714 RTPrintf("it works");
715 RTStrmPrintf(g_pStdErr, "ignore this output\n");
716
717 return RTEXITCODE_SUCCESS;
718}
719
720
721static void tstRTCreateProcEx1(const char *pszAsUser, const char *pszPassword)
722{
723 RTTestISub("Standard Out");
724
725 RTPIPE hPipeR, hPipeW;
726 RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
727 const char * apszArgs[3] =
728 {
729 "non-existing-non-executable-file",
730 "--testcase-child-1",
731 NULL
732 };
733 RTHANDLE Handle;
734 Handle.enmType = RTHANDLETYPE_PIPE;
735 Handle.u.hPipe = hPipeW;
736 RTPROCESS hProc;
737 RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
738 &Handle, NULL, pszAsUser, pszPassword, NULL, &hProc), VINF_SUCCESS);
739 RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
740
741 char szOutput[_4K];
742 size_t offOutput = 0;
743 for (;;)
744 {
745 size_t cbLeft = sizeof(szOutput) - 1 - offOutput;
746 RTTESTI_CHECK(cbLeft > 0);
747 if (cbLeft == 0)
748 break;
749
750 size_t cbRead;
751 int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
752 if (RT_FAILURE(rc))
753 {
754 RTTESTI_CHECK_RC(rc, VERR_BROKEN_PIPE);
755 break;
756 }
757 offOutput += cbRead;
758 }
759 szOutput[offOutput] = '\0';
760 RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
761
762 RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
763 RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
764
765 if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
766 RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
767 else if ( offOutput != sizeof("it works") - 1
768 || strcmp(szOutput, "it works"))
769 RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
770}
771
772
773int main(int argc, char **argv)
774{
775 /*
776 * Deal with child processes first.
777 */
778 if (argc == 2 && !strcmp(argv[1], "--testcase-child-1"))
779 return tstRTCreateProcEx1Child();
780 if (argc == 2 && !strcmp(argv[1], "--testcase-child-2"))
781 return tstRTCreateProcEx2Child();
782 if (argc == 2 && !strcmp(argv[1], "--testcase-child-3"))
783 return tstRTCreateProcEx3Child();
784 if (argc >= 5 && !strcmp(argv[1], "--testcase-child-4"))
785 return tstRTCreateProcEx4Child(argc, argv);
786 if (argc >= 2 && !strcmp(argv[1], "--testcase-child-5"))
787 return tstRTCreateProcEx5Child(argc, argv);
788 if (argc >= 2 && !strcmp(argv[1], "--testcase-child-6"))
789 return tstRTCreateProcEx6Child(argc, argv);
790 if (argc >= 2 && !strcmp(argv[1], "--testcase-child-cwd"))
791 return tstRTCreateProcExCwdChild(argc, argv);
792
793 /*
794 * Main process.
795 */
796 const char *pszAsUser = NULL;
797 const char *pszPassword = NULL;
798 if (argc != 1)
799 {
800 if (argc != 4 || strcmp(argv[1], "--as-user"))
801 return 99;
802 pszAsUser = argv[2];
803 pszPassword = argv[3];
804 }
805
806 RTTEST hTest;
807 int rc = RTTestInitAndCreate("tstRTProcCreateEx", &hTest);
808 if (rc)
809 return rc;
810 RTTestBanner(hTest);
811
812 /*
813 * Init globals.
814 */
815 if (!RTProcGetExecutablePath(g_szExecName, sizeof(g_szExecName)))
816 RTStrCopy(g_szExecName, sizeof(g_szExecName), argv[0]);
817 RTTESTI_CHECK_RC(RTEnvClone(&g_hEnvInitial, RTENV_DEFAULT), VINF_SUCCESS);
818
819 /*
820 * The tests.
821 */
822 tstRTCreateProcEx1(pszAsUser, pszPassword);
823 tstRTCreateProcEx2(pszAsUser, pszPassword);
824 tstRTCreateProcEx3(pszAsUser, pszPassword);
825 tstRTCreateProcEx4(pszAsUser, pszPassword);
826 if (pszAsUser)
827 tstRTCreateProcEx5(pszAsUser, pszPassword);
828 tstRTCreateProcEx6(pszAsUser, pszPassword);
829 tstRTCreateProcExCwd(pszAsUser, pszPassword);
830
831 /** @todo Cover files, ++ */
832
833 RTEnvDestroy(g_hEnvInitial);
834
835 /*
836 * Summary.
837 */
838 return RTTestSummaryAndDestroy(hTest);
839}
840
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette