tstRTProcCreateEx.cpp revision 8e03ddcc7e406ba01ce5adb465bc1bb034681b40
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley/* $Id$ */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley/** @file
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * IPRT Testcase - RTProcCreateEx.
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley/*
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * Copyright (C) 2010 Sun Microsystems, Inc.
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley *
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * This file is part of VirtualBox Open Source Edition (OSE), as
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * available from http://www.virtualbox.org. This file is free software;
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * you can redistribute it and/or modify it under the terms of the GNU
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * General Public License (GPL) as published by the Free Software
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * Foundation, in version 2 as it comes in the "COPYING" file of the
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley *
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * The contents of this file may alternatively be used under the terms
53d53f147b9727979582b74891195772df932d1aMichael Graff * of the Common Development and Distribution License Version 1.0
53d53f147b9727979582b74891195772df932d1aMichael Graff * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * VirtualBox OSE distribution, in which case the provisions of the
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * CDDL are applicable instead of those of the GPL.
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley *
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * You may elect to license modified versions of this file under the
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * terms and conditions of either the GPL or the CDDL or both.
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley *
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley * Clara, CA 95054 USA or visit http://www.sun.com if you need
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley * additional information or have any questions.
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley/*******************************************************************************
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley* Header Files *
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley*******************************************************************************/
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#include <iprt/process.h>
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#include <iprt/assert.h>
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#include <iprt/env.h>
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley#include <iprt/err.h>
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley#include <iprt/initterm.h>
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley#include <iprt/mem.h>
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley#include <iprt/message.h>
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#include <iprt/param.h>
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley#include <iprt/pipe.h>
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley#include <iprt/string.h>
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#include <iprt/stream.h>
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#include <iprt/test.h>
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#include <iprt/thread.h>
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#ifdef RT_OS_WINDOWS
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley# define SECURITY_WIN32
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley# include <windows.h>
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley# include <Security.h>
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley#endif
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley/*******************************************************************************
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley* Global Variables *
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley*******************************************************************************/
7c9b885120f02ac6af10853684a2e8350d224639Bob Halleystatic char g_szExecName[RTPATH_MAX];
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halleystatic const char * const g_apszArgs4[] =
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley{
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 0 */ "non existing non executable file",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 1 */ "--testcase-child-4",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 2 */ "a b",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 3 */ " cdef",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 4 */ "ghijkl ",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 5 */ "\"",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 6 */ "\\",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 7 */ "\\\"",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 8 */ "\\\"\\",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* 9 */ "\\\\\"\\",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley NULL
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley};
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halleystatic int tstRTCreateProcEx5Child(int argc, char **argv)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley{
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley int rc = RTR3Init();
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (rc)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley return RTMsgInitFailure(rc);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#ifdef RT_OS_WINDOWS
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley char szUser[_1K];
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley DWORD cbLen = sizeof(szUser);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /** @todo Does not yet handle ERROR_MORE_DATA for user names longer than 32767. */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (!GetUserName(szUser, &cbLen))
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley {
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTPrintf("GetUserName failed with last error=%ld\n", GetLastError());
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley return VERR_AUTHENTICATION_FAILURE;
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley }
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley else
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley {
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley/* Does not work on NT4 (yet). */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#if 0
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley DWORD cbSid = 0;
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley DWORD cbDomain = 0;
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley SID_NAME_USE sidUse;
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley /* First try to figure out how much space for SID + domain name we need. */
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley BOOL bRet = LookupAccountName(NULL /* current system*/,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley szUser,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley NULL,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley &cbSid,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley NULL,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley &cbDomain,
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley &sidUse);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (!bRet)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley {
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley DWORD dwErr = GetLastError();
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (dwErr != ERROR_INSUFFICIENT_BUFFER)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley {
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTPrintf("LookupAccountName(1) failed with last error=%ld\n", dwErr);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley return VERR_AUTHENTICATION_FAILURE;
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley }
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley }
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* Now try getting the real SID + domain name. */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley SID *pSid = (SID *)RTMemAlloc(cbSid);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley AssertPtr(pSid);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley char *pszDomain = (char *)RTMemAlloc(cbDomain); /* Size in TCHAR! */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley AssertPtr(pszDomain);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (!LookupAccountName(NULL /* Current system */,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley szUser,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley pSid,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley &cbSid,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley pszDomain,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley &cbDomain,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley &sidUse))
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley {
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTPrintf("LookupAccountName(2) failed with last error=%ld\n", GetLastError());
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley return VERR_AUTHENTICATION_FAILURE;
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley }
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTMemFree(pSid);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTMemFree(pszDomain);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#endif
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley }
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#else
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /** @todo Lookup UID/effective UID, maybe GID? */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley#endif
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley return rc;
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley}
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halleystatic void tstRTCreateProcEx5(const char *pszUser, const char *pszPassword)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley{
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTestISubF("As user \"%s\" with password \"%s\"", pszUser, pszPassword);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley const char * apszArgs[3] =
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley {
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley "test", /* user name */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley "--testcase-child-5",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley NULL
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley };
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley RTPROCESS hProc;
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* Test for invalid logons. */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley NULL, NULL, "non-existing-user", "wrong-password", &hProc), VERR_LOGON_FAILURE);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* Test for invalid application. */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTESTI_CHECK_RC_RETV(RTProcCreateEx("non-existing-app", apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley NULL, NULL, NULL, NULL, &hProc), VERR_PATH_NOT_FOUND);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley /* Test a (hopefully) valid user/password logon (given by parameters of this function). */
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley NULL, NULL, pszUser, pszPassword, &hProc), VINF_SUCCESS);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley else
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTestIPassed(NULL);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley}
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halleystatic int tstRTCreateProcEx4Child(int argc, char **argv)
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley{
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley int rc = RTR3Init();
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (rc)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley return RTMsgInitFailure(rc);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley for (int i = 0; i < argc; i++)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (strcmp(argv[i], g_apszArgs4[i]))
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley {
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTStrmPrintf(g_pStdErr,
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley "child4: argv[%2u]='%s'\n"
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley "child4: expected='%s'\n",
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley i, argv[i], g_apszArgs4[i]);
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley rc++;
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley }
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley return rc;
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley}
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halleystatic void tstRTCreateProcEx4(void)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley{
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTestISub("Argument with spaces and stuff");
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTPROCESS hProc;
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, g_apszArgs4, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley NULL, NULL, NULL, NULL, &hProc), VINF_SUCCESS);
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley else
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley RTTestIPassed(NULL);
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley}
f058f9648463dcd77bdb74df14e9e53bad56518bBob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley
7c9b885120f02ac6af10853684a2e8350d224639Bob Halleystatic int tstRTCreateProcEx3Child(void)
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley{
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley int rc = RTR3Init();
7c9b885120f02ac6af10853684a2e8350d224639Bob Halley if (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 0;
}
static void tstRTCreateProcEx3(void)
{
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, NULL, NULL, &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);
else
RTTestIPassed(NULL);
}
static int tstRTCreateProcEx2Child(void)
{
int rc = RTR3Init();
if (rc)
return RTMsgInitFailure(rc);
RTStrmPrintf(g_pStdErr, "howdy");
RTStrmPrintf(g_pStdOut, "ignore this output\n");
return 0;
}
static void tstRTCreateProcEx2(void)
{
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, NULL, NULL, &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);
else
RTTestIPassed(NULL);
}
static int tstRTCreateProcEx1Child(void)
{
int rc = RTR3Init();
if (rc)
return RTMsgInitFailure(rc);
RTPrintf("it works");
RTStrmPrintf(g_pStdErr, "ignore this output\n");
return 0;
}
static void tstRTCreateProcEx1(void)
{
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, NULL, NULL, &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);
else
RTTestIPassed(NULL);
}
int main(int argc, char **argv)
{
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);
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);
if (!RTProcGetExecutableName(g_szExecName, sizeof(g_szExecName)))
RTStrCopy(g_szExecName, sizeof(g_szExecName), argv[0]);
/*
* The tests.
*/
tstRTCreateProcEx1();
tstRTCreateProcEx2();
tstRTCreateProcEx3();
tstRTCreateProcEx4();
if (pszAsUser)
tstRTCreateProcEx5(pszAsUser, pszPassword);
/** @todo Cover files, ++ */
/*
* Summary.
*/
return RTTestSummaryAndDestroy(hTest);
}