1 | /* $Id: tstRTMemWipe.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTMemWipe* functions.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2022 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/rand.h>
|
---|
33 | #include <iprt/string.h>
|
---|
34 | #include <iprt/stream.h>
|
---|
35 | #include <iprt/initterm.h>
|
---|
36 | #include <iprt/mem.h>
|
---|
37 | #include <iprt/test.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 |
|
---|
44 | static void doMemWipeThoroughly(RTTEST hTest)
|
---|
45 | {
|
---|
46 | for (uint32_t p = 0; p < RTRandU32Ex(1, 64); p++)
|
---|
47 | {
|
---|
48 | uint32_t cbAlloc = RTRandS32Ex(1, _1M) * sizeof(uint8_t);
|
---|
49 |
|
---|
50 | RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Testing wipe #%.02RU32 (%u bytes) ...\n",
|
---|
51 | p + 1, cbAlloc);
|
---|
52 |
|
---|
53 | void *pvBuf = RTMemAlloc(cbAlloc);
|
---|
54 | if (!pvBuf)
|
---|
55 | {
|
---|
56 | RTTestIFailed("No memory for first buffer (%z bytes)\n",
|
---|
57 | cbAlloc);
|
---|
58 | continue;
|
---|
59 | }
|
---|
60 | RTRandBytes(pvBuf, cbAlloc);
|
---|
61 |
|
---|
62 | void *pvWipe = RTMemDup(pvBuf, cbAlloc);
|
---|
63 | if (!pvWipe)
|
---|
64 | {
|
---|
65 | RTMemFree(pvBuf);
|
---|
66 |
|
---|
67 | RTTestIFailed("No memory for second buffer (%z bytes)\n",
|
---|
68 | cbAlloc);
|
---|
69 | continue;
|
---|
70 | }
|
---|
71 | uint32_t cbWipeRand = RTRandU32Ex(1, cbAlloc);
|
---|
72 | RTMemWipeThoroughly(pvWipe, RT_MIN(cbAlloc, cbWipeRand), p /* Passes */);
|
---|
73 | if (!memcmp(pvWipe, pvBuf, cbAlloc))
|
---|
74 | RTTestIFailed("Memory blocks must differ (%z bytes, 0x%p vs. 0x%p)!\n",
|
---|
75 | cbAlloc, pvWipe, pvBuf);
|
---|
76 |
|
---|
77 | RTMemFree(pvWipe);
|
---|
78 | RTMemFree(pvBuf);
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | int main()
|
---|
83 | {
|
---|
84 | RTTEST hTest;
|
---|
85 | RTEXITCODE rcExit = RTTestInitAndCreate("memwipe", &hTest);
|
---|
86 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
87 | return rcExit;
|
---|
88 | RTTestBanner(hTest);
|
---|
89 |
|
---|
90 | doMemWipeThoroughly(hTest);
|
---|
91 |
|
---|
92 | return RTTestSummaryAndDestroy(hTest);
|
---|
93 | }
|
---|
94 |
|
---|