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