[29250] | 1 | /* $Id: tstVDShareable.cpp 32536 2010-09-15 18:25:32Z vboxsync $ */
|
---|
[1] | 2 | /** @file
|
---|
[31193] | 3 | * Simple VBox HDD container test utility for shareable images.
|
---|
[1] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[31193] | 7 | * Copyright (C) 2010 Oracle Corporation
|
---|
[1] | 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
|
---|
[5999] | 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.
|
---|
[1] | 16 | */
|
---|
| 17 |
|
---|
[29250] | 18 | /*******************************************************************************
|
---|
| 19 | * Header Files *
|
---|
| 20 | *******************************************************************************/
|
---|
[16873] | 21 | #include <VBox/VBoxHDD.h>
|
---|
[1] | 22 | #include <VBox/err.h>
|
---|
[14817] | 23 | #include <VBox/log.h>
|
---|
[29250] | 24 | #include <iprt/asm-amd64-x86.h>
|
---|
[13268] | 25 | #include <iprt/dir.h>
|
---|
[1] | 26 | #include <iprt/string.h>
|
---|
[728] | 27 | #include <iprt/stream.h>
|
---|
[7895] | 28 | #include <iprt/file.h>
|
---|
[728] | 29 | #include <iprt/mem.h>
|
---|
[7895] | 30 | #include <iprt/initterm.h>
|
---|
[9262] | 31 | #include <iprt/rand.h>
|
---|
| 32 | #include "stdio.h"
|
---|
| 33 | #include "stdlib.h"
|
---|
[1] | 34 |
|
---|
[13295] | 35 | #define VHD_TEST
|
---|
| 36 | #define VDI_TEST
|
---|
| 37 | #define VMDK_TEST
|
---|
| 38 |
|
---|
[7895] | 39 | /*******************************************************************************
|
---|
| 40 | * Global Variables *
|
---|
| 41 | *******************************************************************************/
|
---|
| 42 | /** The error count. */
|
---|
| 43 | unsigned g_cErrors = 0;
|
---|
[1] | 44 |
|
---|
[7895] | 45 |
|
---|
| 46 | static void tstVDError(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
| 47 | const char *pszFormat, va_list va)
|
---|
[1] | 48 | {
|
---|
[7895] | 49 | g_cErrors++;
|
---|
[13837] | 50 | RTPrintf("tstVD: Error %Rrc at %s:%u (%s): ", rc, RT_SRC_POS_ARGS);
|
---|
[7895] | 51 | RTPrintfV(pszFormat, va);
|
---|
| 52 | RTPrintf("\n");
|
---|
| 53 | }
|
---|
[1] | 54 |
|
---|
[32536] | 55 | static int tstVDMessage(void *pvUser, const char *pszFormat, va_list va)
|
---|
[26988] | 56 | {
|
---|
| 57 | RTPrintf("tstVD: ");
|
---|
| 58 | RTPrintfV(pszFormat, va);
|
---|
| 59 | return VINF_SUCCESS;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[31193] | 62 | static int tstVDCreateShareDelete(const char *pszBackend, const char *pszFilename,
|
---|
| 63 | uint64_t cbSize, unsigned uFlags)
|
---|
[8248] | 64 | {
|
---|
| 65 | int rc;
|
---|
[31193] | 66 | PVBOXHDD pVD = NULL, pVD2 = NULL;
|
---|
[32536] | 67 | VDGEOMETRY PCHS = { 0, 0, 0 };
|
---|
| 68 | VDGEOMETRY LCHS = { 0, 0, 0 };
|
---|
[11435] | 69 | PVDINTERFACE pVDIfs = NULL;
|
---|
[10715] | 70 | VDINTERFACE VDIError;
|
---|
| 71 | VDINTERFACEERROR VDIErrorCallbacks;
|
---|
[8248] | 72 |
|
---|
| 73 | #define CHECK(str) \
|
---|
| 74 | do \
|
---|
| 75 | { \
|
---|
[13837] | 76 | RTPrintf("%s rc=%Rrc\n", str, rc); \
|
---|
[13835] | 77 | if (RT_FAILURE(rc)) \
|
---|
[8248] | 78 | { \
|
---|
[13295] | 79 | VDDestroy(pVD); \
|
---|
[8248] | 80 | return rc; \
|
---|
| 81 | } \
|
---|
| 82 | } while (0)
|
---|
| 83 |
|
---|
[10715] | 84 | /* Create error interface. */
|
---|
| 85 | VDIErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
|
---|
| 86 | VDIErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
|
---|
| 87 | VDIErrorCallbacks.pfnError = tstVDError;
|
---|
[26988] | 88 | VDIErrorCallbacks.pfnMessage = tstVDMessage;
|
---|
[10715] | 89 |
|
---|
[11435] | 90 | rc = VDInterfaceAdd(&VDIError, "tstVD_Error", VDINTERFACETYPE_ERROR, &VDIErrorCallbacks,
|
---|
| 91 | NULL, &pVDIfs);
|
---|
[10715] | 92 | AssertRC(rc);
|
---|
| 93 |
|
---|
| 94 | rc = VDCreate(&VDIError, &pVD);
|
---|
[8248] | 95 | CHECK("VDCreate()");
|
---|
[31193] | 96 | rc = VDCreate(&VDIError, &pVD2);
|
---|
| 97 | CHECK("VDCreate() #2");
|
---|
[8248] | 98 |
|
---|
[17970] | 99 | rc = VDCreateBase(pVD, pszBackend, pszFilename, cbSize,
|
---|
[11353] | 100 | uFlags, "Test image", &PCHS, &LCHS, NULL,
|
---|
| 101 | VD_OPEN_FLAGS_NORMAL, NULL, NULL);
|
---|
[8248] | 102 | CHECK("VDCreateBase()");
|
---|
| 103 |
|
---|
[31193] | 104 | VDClose(pVD, false);
|
---|
[8248] | 105 |
|
---|
[31193] | 106 | rc = VDOpen(pVD, pszBackend, pszFilename, VD_OPEN_FLAGS_SHAREABLE, NULL);
|
---|
[13268] | 107 | CHECK("VDOpen()");
|
---|
[31193] | 108 | rc = VDOpen(pVD2, pszBackend, pszFilename, VD_OPEN_FLAGS_SHAREABLE, NULL);
|
---|
| 109 | CHECK("VDOpen() #2");
|
---|
| 110 | if (VDIsReadOnly(pVD2))
|
---|
| 111 | rc = VERR_VD_IMAGE_READ_ONLY;
|
---|
[13268] | 112 |
|
---|
[31193] | 113 | VDClose(pVD2, false);
|
---|
[13268] | 114 | VDClose(pVD, true);
|
---|
| 115 |
|
---|
[13295] | 116 | VDDestroy(pVD);
|
---|
[31193] | 117 | VDDestroy(pVD2);
|
---|
[13268] | 118 | #undef CHECK
|
---|
| 119 | return 0;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[9262] | 122 | int main(int argc, char *argv[])
|
---|
| 123 | {
|
---|
[14817] | 124 | RTR3Init();
|
---|
[9262] | 125 | int rc;
|
---|
| 126 |
|
---|
[7895] | 127 | RTPrintf("tstVD: TESTING...\n");
|
---|
[728] | 128 |
|
---|
[7895] | 129 | /*
|
---|
| 130 | * Clean up potential leftovers from previous unsuccessful runs.
|
---|
| 131 | */
|
---|
[8248] | 132 | RTFileDelete("tmpVDCreate.vdi");
|
---|
[13268] | 133 |
|
---|
| 134 | if (!RTDirExists("tmp"))
|
---|
| 135 | {
|
---|
| 136 | rc = RTDirCreate("tmp", RTFS_UNIX_IRWXU);
|
---|
[13835] | 137 | if (RT_FAILURE(rc))
|
---|
[13268] | 138 | {
|
---|
[13837] | 139 | RTPrintf("tstVD: Failed to create 'tmp' directory! rc=%Rrc\n", rc);
|
---|
[13268] | 140 | g_cErrors++;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[13295] | 144 | #ifdef VDI_TEST
|
---|
[31193] | 145 | rc = tstVDCreateShareDelete("VDI", "tmpVDCreate.vdi", 10 * _1M,
|
---|
| 146 | VD_IMAGE_FLAGS_FIXED);
|
---|
[13835] | 147 | if (RT_FAILURE(rc))
|
---|
[8248] | 148 | {
|
---|
[31193] | 149 | RTPrintf("tstVD: VDI shareable test failed! rc=%Rrc\n", rc);
|
---|
[8248] | 150 | g_cErrors++;
|
---|
| 151 | }
|
---|
[13295] | 152 | #endif /* VDI_TEST */
|
---|
[9528] | 153 |
|
---|
[7895] | 154 | /*
|
---|
| 155 | * Clean up any leftovers.
|
---|
| 156 | */
|
---|
[8248] | 157 | RTFileDelete("tmpVDCreate.vdi");
|
---|
[7895] | 158 |
|
---|
[14855] | 159 | rc = VDShutdown();
|
---|
| 160 | if (RT_FAILURE(rc))
|
---|
| 161 | {
|
---|
| 162 | RTPrintf("tstVD: unloading backends failed! rc=%Rrc\n", rc);
|
---|
| 163 | g_cErrors++;
|
---|
| 164 | }
|
---|
| 165 | /*
|
---|
[31193] | 166 | * Summary
|
---|
| 167 | */
|
---|
[7895] | 168 | if (!g_cErrors)
|
---|
| 169 | RTPrintf("tstVD: SUCCESS\n");
|
---|
[728] | 170 | else
|
---|
[7895] | 171 | RTPrintf("tstVD: FAILURE - %d errors\n", g_cErrors);
|
---|
[728] | 172 |
|
---|
[7895] | 173 | return !!g_cErrors;
|
---|
[1] | 174 | }
|
---|
| 175 |
|
---|