test.cpp revision 0b07f19b2d822b86714ed48e3581477a046f1c79
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * IPRT - Testcase Framework.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * Copyright (C) 2009 Oracle Corporation
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * available from http://www.virtualbox.org. This file is free software;
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * you can redistribute it and/or modify it under the terms of the GNU
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * General Public License (GPL) as published by the Free Software
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * The contents of this file may alternatively be used under the terms
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * of the Common Development and Distribution License Version 1.0
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * VirtualBox OSE distribution, in which case the provisions of the
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * CDDL are applicable instead of those of the GPL.
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * You may elect to license modified versions of this file under the
3f1f30f349c6d9ef74ba8d16ff0c5b0ac47def6cvboxsync * terms and conditions of either the GPL or the CDDL or both.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/*******************************************************************************
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync* Header Files *
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync*******************************************************************************/
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/*******************************************************************************
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync* Structures and Typedefs *
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync*******************************************************************************/
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * Guarded memory allocation record.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Pointer to the next record. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The address we return to the user. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The base address of the allocation. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The size of the allocation. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Guards. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The guard address. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The guard size. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/** Pointer to an guarded memory allocation. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * Test instance structure.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsynctypedef struct RTTESTINT
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Magic. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The number of errors. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The test name. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync const char *pszTest;
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The length of the test name. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The size of a guard. Multiple of PAGE_SIZE. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The verbosity level. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Critical section serializing output. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The output stream. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Whether we're currently at a newline. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Critical section serializing access to the members following it. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The list of guarded memory allocations. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The current sub-test. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The length of the sub-test name. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Whether we've reported the sub-test result or not. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The start error count of the current subtest. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The number of sub tests. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The number of sub tests that failed. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Set if XML output is enabled. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** Test pipe for the XML output stream going to the server. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** File where the XML output stream might be directed. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** The number of XML elements on the stack. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync /** XML element stack. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/** Pointer to a test instance. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/*******************************************************************************
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync* Defined Constants And Macros *
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync*******************************************************************************/
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/** Validate a test instance. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync AssertReturn(pTest->u32Magic == RTTESTINT_MAGIC, VERR_INVALID_HANDLE); \
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync } while (0)
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/** Gets and validates a test instance.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * If the handle is nil, we will try retrieve it from the test TLS entry.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync AssertReturn(pTest->u32Magic == RTTESTINT_MAGIC, VERR_INVALID_MAGIC); \
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync } while (0)
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/** Gets and validates a test instance.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * If the handle is nil, we will try retrieve it from the test TLS entry.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync AssertReturn(pTest->u32Magic == RTTESTINT_MAGIC, (rc)); \
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync } while (0)
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/*******************************************************************************
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync* Internal Functions *
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync*******************************************************************************/
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestGuardedFreeOne(PRTTESTGUARDEDMEM pMem);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic int rtTestPrintf(PRTTESTINT pTest, const char *pszFormat, ...);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestXmlStart(PRTTESTINT pTest, const char *pszTest);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestXmlElemV(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, va_list va);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestXmlElem(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, ...);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestXmlElemStartV(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, va_list va);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestXmlElemStart(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, ...);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestXmlElemValueV(PRTTESTINT pTest, const char *pszFormat, va_list va);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestXmlElemValue(PRTTESTINT pTest, const char *pszFormat, ...);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic void rtTestXmlElemEnd(PRTTESTINT pTest, const char *pszTag);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/*******************************************************************************
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync* Global Variables *
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync*******************************************************************************/
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/** For serializing TLS init. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync/** Our TLS entry. */
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * Init TLS index once.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * @returns IPRT status code.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * @param pvUser1 Ignored.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * @param pvUser2 Ignored.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncstatic DECLCALLBACK(int32_t) rtTestInitOnce(void *pvUser1, void *pvUser2)
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * Creates a test instance.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * @returns IPRT status code.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * @param pszTest The test name.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * @param phTest Where to store the test instance handle.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsyncRTR3DECL(int) RTTestCreate(const char *pszTest, PRTTEST phTest)
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * Global init.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync int rc = RTOnce(&g_TestInitOnce, rtTestInitOnce, NULL, NULL);
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync * Create the instance.
9de2fa82343af2df7df171b18afbe32b6f37ed84vboxsync PRTTESTINT pTest = (PRTTESTINT)RTMemAllocZ(sizeof(*pTest));
RTStrmPrintf(g_pStdErr, "%s: test pipe error: RTPipeFromNative(,\"%s\",WRITE) -> %Rrc\n", pszTest, szEnvVal, rc);
RTStrmPrintf(g_pStdErr, "%s: test pipe error: RTStrToInt32Full(\"%s\") -> %Rrc\n", pszTest, szEnvVal, rc);
rc = RTFileOpen(&pTest->hXmlFile, szEnvVal, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE);
RTStrmPrintf(g_pStdErr, "%s: test file error: RTFileOpen(,\"%s\",) -> %Rrc\n", pszTest, szEnvVal, rc);
return VINF_SUCCESS;
return rc;
RTStrmPrintf(g_pStdErr, "%s: fatal error: RTR3InitExeNoArguments failed with rc=%Rrc\n", pszTest, rc);
return RTEXITCODE_INIT;
return RTEXITCODE_INIT;
return RTEXITCODE_SUCCESS;
return VINF_SUCCESS;
while (pMem)
return VINF_SUCCESS;
if (phOldTest)
RTR3DECL(int) RTTestGuardedAlloc(RTTEST hTest, size_t cb, uint32_t cbAlign, bool fHead, void **ppvUser)
if (cbAlign == 0)
if (!fHead)
if (off)
return VINF_SUCCESS;
return rc;
void *pvUser;
return pvUser;
return NULL;
void *pvUser;
return pvUser;
return NULL;
int rc;
rc = RTMemProtect(pMem->aGuards[0].pv, pMem->aGuards[0].cb, RTMEM_PROT_WRITE | RTMEM_PROT_READ); AssertRC(rc);
rc = RTMemProtect(pMem->aGuards[1].pv, pMem->aGuards[1].cb, RTMEM_PROT_WRITE | RTMEM_PROT_READ); AssertRC(rc);
if (!pv)
return VINF_SUCCESS;
if (pPrev)
return rc;
char *pszStr;
if (pszStr)
static void rtTestXmlElemV(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, va_list va)
static void rtTestXmlElemStartV(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, va_list va)
char *pszValue;
if (pszValue)
AssertReturnVoid(i > 0);
AssertReturnVoid(i > 0);
if (cbChars)
if (!pchEnd)
} while (cbChars);
return cch;
return cch;
int cch = 0;
return cch;
return cch;
int cch = 0;
return cch;
return cch;
int cch = 0;
if (!cErrors)
return cch;
int cch = 0;
return cch;
return enmExitCode;
if (pszReasonFmt)
return enmExitCode;
return enmExitCode;
int cch = 0;
return cch;
return cch;
char *pszSubTest;
if (pszSubTest)
return cch;
return cch;
int cch = 0;
return cch;
return cch;
switch (enmUnit)
case RTTESTUNIT_INVALID:
case RTTESTUNIT_END:
return VINF_SUCCESS;
RTR3DECL(int) RTTestValueF(RTTEST hTest, uint64_t u64Value, RTTESTUNIT enmUnit, const char *pszNameFmt, ...)
return rc;
RTR3DECL(int) RTTestValueV(RTTEST hTest, uint64_t u64Value, RTTESTUNIT enmUnit, const char *pszNameFmt, va_list va)
char *pszName;
if (!pszName)
return VERR_NO_MEMORY;
return rc;
return VINF_SUCCESS;
int cch = 0;
return cch;
return cch;
return cch;