tstRTMemSafer.cpp revision 9236bebed5c5902b30061ea6378bba30ef0577cb
/* $Id$ */
/** @file
* IPRT Testcase - RTMemSafer* functions.
*/
/*
* Copyright (C) 2012 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/path.h>
#include <iprt/rand.h>
#include <iprt/string.h>
#include <iprt/stream.h>
#include <iprt/initterm.h>
#include <iprt/param.h>
#include <iprt/memsafer.h>
#include <iprt/test.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static void doMemSaferScramble(RTTEST hTest, void *pvBuf, size_t cbAlloc)
{
RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Testing scrambling (%u bytes) ...\n", cbAlloc);
RTRandBytes(pvBuf, cbAlloc);
void *pvRef = RTMemDup(pvBuf, cbAlloc);
if (!pvRef)
{
RTTestIFailed("No memory for reference buffer (%z bytes)\n", cbAlloc);
return;
}
int rc = RTMemSaferScramble(pvBuf, cbAlloc);
if (RT_SUCCESS(rc))
{
if (!memcmp(pvRef, pvBuf, cbAlloc))
RTTestIFailed("Memory blocks must differ (%z bytes, 0x%p vs. 0x%p)!\n",
cbAlloc, pvRef, pvBuf);
else
{
/* Test unscrambling. */
rc = RTMemSaferUnscramble(pvBuf, cbAlloc);
if (RT_SUCCESS(rc))
{
if (memcmp(pvRef, pvBuf, cbAlloc))
RTTestIFailed("Memory blocks must not differ (%z bytes, 0x%p vs. 0x%p)!\n",
cbAlloc, pvRef, pvBuf);
}
else
RTTestIFailed("Unscrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
}
}
else
RTTestIFailed("Scrambling %z bytes failed with %Rrc!\n", cbAlloc, rc);
RTMemFree(pvRef);
}
static void doMemSaferAllocation(RTTEST hTest)
{
size_t cbAlloc = RTRandS32Ex(1, _1M) * sizeof(uint8_t);
RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Testing allocation of secure memory (%u bytes) ...\n", cbAlloc);
void *pvBuf = NULL;
int rc = RTMemSaferAllocZEx(&pvBuf, cbAlloc, RTMEMSAFER_ALLOC_EX_FLAGS_DEFAULT);
if (RT_SUCCESS(rc))
{
/* Try to access memory. */
RTRandBytes(pvBuf, cbAlloc);
/* Scrambling test */
doMemSaferScramble(hTest, pvBuf, cbAlloc);
#if 0
/* Try to access memory after the allocation, should crash. */
size_t cbAllocAligned = RT_ALIGN_Z(cbAlloc, PAGE_SIZE);
*((uint8_t *)pvBuf + cbAllocAligned) = 0xcc;
#endif
RTMemSaferFree(pvBuf, cbAlloc);
}
else
RTTestIFailed("Allocating %z bytes of secure memory failed with %Rrc\n", cbAlloc, rc);
}
int main()
{
RTTEST hTest;
RTEXITCODE rcExit = RTTestInitAndCreate("memsafer", &hTest);
if (rcExit != RTEXITCODE_SUCCESS)
return rcExit;
RTTestBanner(hTest);
doMemSaferAllocation(hTest);
return RTTestSummaryAndDestroy(hTest);
}