tstRTProcCreateEx.cpp revision d1966fe9681e9a100f8c895f08e450fc32dafd48
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * IPRT Testcase - RTProcCreateEx.
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * Copyright (C) 2010 Sun Microsystems, Inc.
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * available from http://www.virtualbox.org. This file is free software;
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * you can redistribute it and/or modify it under the terms of the GNU
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * General Public License (GPL) as published by the Free Software
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * The contents of this file may alternatively be used under the terms
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * of the Common Development and Distribution License Version 1.0
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * VirtualBox OSE distribution, in which case the provisions of the
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * CDDL are applicable instead of those of the GPL.
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * You may elect to license modified versions of this file under the
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * terms and conditions of either the GPL or the CDDL or both.
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * additional information or have any questions.
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync/*******************************************************************************
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync* Header Files *
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync*******************************************************************************/
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync/*******************************************************************************
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync* Global Variables *
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync*******************************************************************************/
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsyncstatic const char * const g_apszArgs4[] =
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsyncstatic int tstRTCreateProcEx5Child(int argc, char **argv)
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync /** @todo Does not yet handle ERROR_MORE_DATA for user names longer than 32767. */
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync if (!GetUserNameEx(NameSamCompatible, szUser, &cbLen))
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync RTPrintf("GetUserNameEx failed with last error=%ld\n", GetLastError());
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync /* First try to figure out how much space for SID + domain name we need. */
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync BOOL bRet = LookupAccountName(NULL /* current system*/,
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync RTPrintf("LookupAccountName(1) failed with last error=%ld\n", dwErr);
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync /* Now try getting the real SID + domain name. */
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync char *pszDomain = (char *)RTMemAlloc(cbDomain); /* Size in TCHAR! */
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync RTPrintf("LookupAccountName(2) failed with last error=%ld\n", GetLastError());
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync /** @todo Lookup UID/effective UID, maybe GID? */
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsyncstatic void tstRTCreateProcEx5(void)
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync "--testcase-child-5",
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
cd899b2444ca69566bd04cfac96828714d3bd1b0vboxsync NULL, NULL, "testcase", "test", &hProc), VINF_SUCCESS);
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsyncstatic int tstRTCreateProcEx4Child(int argc, char **argv)
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync for (int i = 0; i < argc; i++)
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync "child4: argv[%2u]='%s'\n"
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync "child4: expected='%s'\n",
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsyncstatic void tstRTCreateProcEx4(void)
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, g_apszArgs4, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTStrmPrintf(g_pStdOut, "w"); RTStrmFlush(g_pStdOut);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTStrmPrintf(g_pStdErr, "o"); RTStrmFlush(g_pStdErr);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTStrmPrintf(g_pStdOut, "r"); RTStrmFlush(g_pStdOut);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTStrmPrintf(g_pStdErr, "k"); RTStrmFlush(g_pStdErr);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsyncstatic void tstRTCreateProcEx3(void)
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync "non-existing-non-executable-file",
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync "--testcase-child-3",
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
cd899b2444ca69566bd04cfac96828714d3bd1b0vboxsync &Handle, &Handle, NULL, NULL, &hProc), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsyncstatic void tstRTCreateProcEx2(void)
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync "non-existing-non-executable-file",
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync "--testcase-child-2",
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsyncstatic void tstRTCreateProcEx1(void)
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC_RETV(RTPipeCreate(&hPipeR, &hPipeW, RTPIPE_C_INHERIT_WRITE), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync "non-existing-non-executable-file",
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync "--testcase-child-1",
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC_RETV(RTProcCreateEx(g_szExecName, apszArgs, RTENV_DEFAULT, 0 /*fFlags*/, NULL,
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTPipeClose(hPipeW), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync int rc = RTPipeReadBlocking(hPipeR, &szOutput[offOutput], cbLeft, &cbRead);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTPipeClose(hPipeR), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTPROCSTATUS ProcStatus = { -1, RTPROCEXITREASON_ABEND };
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTESTI_CHECK_RC(RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &ProcStatus), VINF_SUCCESS);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync if (ProcStatus.enmReason != RTPROCEXITREASON_NORMAL || ProcStatus.iStatus != 0)
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTestIFailed("enmReason=%d iStatus=%d", ProcStatus.enmReason, ProcStatus.iStatus);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTTestIFailed("wrong output: \"%s\" (len=%u)", szOutput, offOutput);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync if (argc == 2 && !strcmp(argv[1], "--testcase-child-1"))
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync if (argc == 2 && !strcmp(argv[1], "--testcase-child-2"))
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync if (argc == 2 && !strcmp(argv[1], "--testcase-child-3"))
4ee5a4cd660730c997785c6cbc12881a115079e8vboxsync if (argc >= 5 && !strcmp(argv[1], "--testcase-child-4"))
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync if (argc == 2 && !strcmp(argv[1], "--testcase-child-5"))
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync int rc = RTTestInitAndCreate("tstRTProcCreateEx", &hTest);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync if (!RTProcGetExecutableName(g_szExecName, sizeof(g_szExecName)))
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync RTStrCopy(g_szExecName, sizeof(g_szExecName), argv[0]);
b1d7d513c459787311cd09c440524044fa7ff8a9vboxsync * The tests.
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync /** @todo Do not run tstRTCreateProcEx5 on NT4, may not work (?) */
9ac9eda1e04d193f54339eaeffe1c8b2f8ea04b1vboxsync /** @todo Cover files, ++ */