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