1 | /** @file
|
---|
2 | *
|
---|
3 | * Simple VBox HDD container test utility.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include <VBox/err.h>
|
---|
23 | #include <VBox/VBoxHDD-new.h>
|
---|
24 | #include <iprt/string.h>
|
---|
25 | #include <iprt/stream.h>
|
---|
26 | #include <iprt/file.h>
|
---|
27 | #include <iprt/mem.h>
|
---|
28 | #include <iprt/initterm.h>
|
---|
29 |
|
---|
30 | /*******************************************************************************
|
---|
31 | * Global Variables *
|
---|
32 | *******************************************************************************/
|
---|
33 | /** The error count. */
|
---|
34 | unsigned g_cErrors = 0;
|
---|
35 |
|
---|
36 |
|
---|
37 | static void tstVDError(void *pvUser, int rc, RT_SRC_POS_DECL,
|
---|
38 | const char *pszFormat, va_list va)
|
---|
39 | {
|
---|
40 | g_cErrors++;
|
---|
41 | RTPrintf("tstVD: Error %Vrc at %s:%u (%s): ");
|
---|
42 | RTPrintfV(pszFormat, va);
|
---|
43 | RTPrintf("\n");
|
---|
44 | }
|
---|
45 |
|
---|
46 |
|
---|
47 | static int tstVDCreateDelete(const char *pszBackend, const char *pszFilename,
|
---|
48 | uint64_t cbSize, VDIMAGETYPE enmType,
|
---|
49 | unsigned uFlags, bool fDelete)
|
---|
50 | {
|
---|
51 | int rc;
|
---|
52 | PVBOXHDD pVD = NULL;
|
---|
53 | PDMMEDIAGEOMETRY PCHS = { 0, 0, 0 };
|
---|
54 | PDMMEDIAGEOMETRY LCHS = { 0, 0, 0 };
|
---|
55 |
|
---|
56 | #define CHECK(str) \
|
---|
57 | do \
|
---|
58 | { \
|
---|
59 | RTPrintf("%s rc=%Vrc\n", str, rc); \
|
---|
60 | if (VBOX_FAILURE(rc)) \
|
---|
61 | { \
|
---|
62 | VDCloseAll(pVD); \
|
---|
63 | return rc; \
|
---|
64 | } \
|
---|
65 | } while (0)
|
---|
66 |
|
---|
67 | rc = VDCreate(tstVDError, NULL, &pVD);
|
---|
68 | CHECK("VDCreate()");
|
---|
69 |
|
---|
70 | rc = VDCreateBase(pVD, pszBackend, pszFilename, enmType, cbSize,
|
---|
71 | uFlags, "Test image", &PCHS, &LCHS, VD_OPEN_FLAGS_NORMAL,
|
---|
72 | NULL, NULL);
|
---|
73 | CHECK("VDCreateBase()");
|
---|
74 |
|
---|
75 | VDDumpImages(pVD);
|
---|
76 |
|
---|
77 | VDClose(pVD, fDelete);
|
---|
78 | #undef CHECK
|
---|
79 | return 0;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | static int tstVDOpenCreateWriteMerge(const char *pszBackend,
|
---|
84 | const char *pszBaseFilename,
|
---|
85 | const char *pszDiffFilename)
|
---|
86 | {
|
---|
87 | int rc;
|
---|
88 | PVBOXHDD pVD = NULL;
|
---|
89 | char *pszFormat;
|
---|
90 | PDMMEDIAGEOMETRY PCHS = { 0, 0, 0 };
|
---|
91 | PDMMEDIAGEOMETRY LCHS = { 0, 0, 0 };
|
---|
92 |
|
---|
93 | #define CHECK(str) \
|
---|
94 | do \
|
---|
95 | { \
|
---|
96 | RTPrintf("%s rc=%Vrc\n", str, rc); \
|
---|
97 | if (VBOX_FAILURE(rc)) \
|
---|
98 | { \
|
---|
99 | VDCloseAll(pVD); \
|
---|
100 | return rc; \
|
---|
101 | } \
|
---|
102 | } while (0)
|
---|
103 |
|
---|
104 | rc = VDCreate(tstVDError, NULL, &pVD);
|
---|
105 | CHECK("VDCreate()");
|
---|
106 |
|
---|
107 | RTFILE File;
|
---|
108 | rc = RTFileOpen(&File, pszBaseFilename, RTFILE_O_READ);
|
---|
109 | if (VBOX_SUCCESS(rc))
|
---|
110 | {
|
---|
111 | RTFileClose(File);
|
---|
112 | rc = VDGetFormat(pszBaseFilename, &pszFormat);
|
---|
113 | RTPrintf("VDGetFormat() pszFormat=%s rc=%Vrc\n", pszFormat, rc);
|
---|
114 | if (VBOX_SUCCESS(rc) && strcmp(pszFormat, pszBackend))
|
---|
115 | {
|
---|
116 | rc = VERR_GENERAL_FAILURE;
|
---|
117 | RTPrintf("VDGetFormat() returned incorrect backend name\n");
|
---|
118 | }
|
---|
119 | RTStrFree(pszFormat);
|
---|
120 | CHECK("VDGetFormat()");
|
---|
121 |
|
---|
122 | rc = VDOpen(pVD, pszBackend, pszBaseFilename, VD_OPEN_FLAGS_NORMAL);
|
---|
123 | CHECK("VDOpen()");
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | rc = VDCreateBase(pVD, pszBackend, pszBaseFilename,
|
---|
128 | VD_IMAGE_TYPE_NORMAL, 1000 * _1M,
|
---|
129 | VD_IMAGE_FLAGS_NONE, "Test image",
|
---|
130 | &PCHS, &LCHS, VD_OPEN_FLAGS_NORMAL,
|
---|
131 | NULL, NULL);
|
---|
132 | CHECK("VDCreateBase()");
|
---|
133 | }
|
---|
134 |
|
---|
135 | void *pvBuf = RTMemAlloc(_1M);
|
---|
136 |
|
---|
137 | memset(pvBuf, 0x33, _1M);
|
---|
138 | rc = VDWrite(pVD, 20 * _1M + 594432, pvBuf, _1M);
|
---|
139 | CHECK("VDWrite()");
|
---|
140 |
|
---|
141 | memset(pvBuf, 0x46, _1M);
|
---|
142 | rc = VDWrite(pVD, 20 * _1M + 594432, pvBuf, _1K);
|
---|
143 | CHECK("VDWrite()");
|
---|
144 |
|
---|
145 | memset(pvBuf, 0x51, _1M);
|
---|
146 | rc = VDWrite(pVD, 40 * _1M + 594432, pvBuf, _1K);
|
---|
147 | CHECK("VDWrite()");
|
---|
148 |
|
---|
149 | rc = VDCreateDiff(pVD, pszBackend, pszDiffFilename,
|
---|
150 | VD_IMAGE_FLAGS_NONE, "Test diff image",
|
---|
151 | VD_OPEN_FLAGS_NORMAL, NULL, NULL);
|
---|
152 | CHECK("VDCreateDiff()");
|
---|
153 |
|
---|
154 | memset(pvBuf, '_', _1M);
|
---|
155 | rc = VDWrite(pVD, 20 * _1M + 594432, pvBuf, 512);
|
---|
156 | CHECK("VDWrite()");
|
---|
157 |
|
---|
158 | rc = VDWrite(pVD, 22 * _1M + 594432, pvBuf, 78336);
|
---|
159 | CHECK("VDWrite()");
|
---|
160 | rc = VDWrite(pVD, 13 * _1M + 594432, pvBuf, 783360);
|
---|
161 | CHECK("VDWrite()");
|
---|
162 | rc = VDWrite(pVD, 44 * _1M + 594432, pvBuf, 68096);
|
---|
163 | CHECK("VDWrite()");
|
---|
164 |
|
---|
165 | VDDumpImages(pVD);
|
---|
166 |
|
---|
167 | RTPrintf("Merging diff into base..\n");
|
---|
168 | rc = VDMerge(pVD, -1, 0, NULL, NULL);
|
---|
169 | CHECK("VDMerge()");
|
---|
170 |
|
---|
171 | VDDumpImages(pVD);
|
---|
172 |
|
---|
173 | VDCloseAll(pVD);
|
---|
174 | #undef CHECK
|
---|
175 | return 0;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | int main()
|
---|
180 | {
|
---|
181 | int rc;
|
---|
182 |
|
---|
183 | RTR3Init();
|
---|
184 | RTPrintf("tstVD: TESTING...\n");
|
---|
185 |
|
---|
186 | /*
|
---|
187 | * Clean up potential leftovers from previous unsuccessful runs.
|
---|
188 | */
|
---|
189 | RTFileDelete("tmpVDCreate.vdi");
|
---|
190 | RTFileDelete("tmpVDCreate.vmdk");
|
---|
191 | RTFileDelete("tmpVDBase.vdi");
|
---|
192 | RTFileDelete("tmpVDDiff.vdi");
|
---|
193 | RTFileDelete("tmpVDBase.vmdk");
|
---|
194 | RTFileDelete("tmpVDDiff.vmdk");
|
---|
195 |
|
---|
196 | rc = tstVDCreateDelete("VDI", "tmpVDCreate.vdi", 2 * _4G,
|
---|
197 | VD_IMAGE_TYPE_NORMAL, VD_IMAGE_FLAGS_NONE,
|
---|
198 | true);
|
---|
199 | if (VBOX_FAILURE(rc))
|
---|
200 | {
|
---|
201 | RTPrintf("tstVD: dynamic VDI create test failed! rc=%Vrc\n", rc);
|
---|
202 | g_cErrors++;
|
---|
203 | }
|
---|
204 | rc = tstVDCreateDelete("VDI", "tmpVDCreate.vdi", 2 * _4G,
|
---|
205 | VD_IMAGE_TYPE_FIXED, VD_IMAGE_FLAGS_NONE,
|
---|
206 | true);
|
---|
207 | if (VBOX_FAILURE(rc))
|
---|
208 | {
|
---|
209 | RTPrintf("tstVD: fixed VDI create test failed! rc=%Vrc\n", rc);
|
---|
210 | g_cErrors++;
|
---|
211 | }
|
---|
212 | rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
|
---|
213 | VD_IMAGE_TYPE_NORMAL, VD_IMAGE_FLAGS_NONE,
|
---|
214 | true);
|
---|
215 | if (VBOX_FAILURE(rc))
|
---|
216 | {
|
---|
217 | RTPrintf("tstVD: dynamic VMDK create test failed! rc=%Vrc\n", rc);
|
---|
218 | g_cErrors++;
|
---|
219 | }
|
---|
220 | rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
|
---|
221 | VD_IMAGE_TYPE_NORMAL, VD_VMDK_IMAGE_FLAGS_SPLIT_2G,
|
---|
222 | true);
|
---|
223 | if (VBOX_FAILURE(rc))
|
---|
224 | {
|
---|
225 | RTPrintf("tstVD: dynamic split VMDK create test failed! rc=%Vrc\n", rc);
|
---|
226 | g_cErrors++;
|
---|
227 | }
|
---|
228 | rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
|
---|
229 | VD_IMAGE_TYPE_FIXED, VD_IMAGE_FLAGS_NONE,
|
---|
230 | true);
|
---|
231 | if (VBOX_FAILURE(rc))
|
---|
232 | {
|
---|
233 | RTPrintf("tstVD: fixed VMDK create test failed! rc=%Vrc\n", rc);
|
---|
234 | g_cErrors++;
|
---|
235 | }
|
---|
236 | rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
|
---|
237 | VD_IMAGE_TYPE_FIXED, VD_VMDK_IMAGE_FLAGS_SPLIT_2G,
|
---|
238 | true);
|
---|
239 | if (VBOX_FAILURE(rc))
|
---|
240 | {
|
---|
241 | RTPrintf("tstVD: fixed split VMDK create test failed! rc=%Vrc\n", rc);
|
---|
242 | g_cErrors++;
|
---|
243 | }
|
---|
244 |
|
---|
245 | rc = tstVDOpenCreateWriteMerge("VDI", "tmpVDBase.vdi", "tmpVDDiff.vdi");
|
---|
246 | if (VBOX_FAILURE(rc))
|
---|
247 | {
|
---|
248 | RTPrintf("tstVD: VDI test failed (new image)! rc=%Vrc\n", rc);
|
---|
249 | g_cErrors++;
|
---|
250 | }
|
---|
251 | rc = tstVDOpenCreateWriteMerge("VDI", "tmpVDBase.vdi", "tmpVDDiff.vdi");
|
---|
252 | if (VBOX_FAILURE(rc))
|
---|
253 | {
|
---|
254 | RTPrintf("tstVD: VDI test failed (existing image)! rc=%Vrc\n", rc);
|
---|
255 | g_cErrors++;
|
---|
256 | }
|
---|
257 | rc = tstVDOpenCreateWriteMerge("VMDK", "tmpVDBase.vmdk", "tmpVDDiff.vmdk");
|
---|
258 | if (VBOX_FAILURE(rc))
|
---|
259 | {
|
---|
260 | RTPrintf("tstVD: VMDK test failed (new image)! rc=%Vrc\n", rc);
|
---|
261 | g_cErrors++;
|
---|
262 | }
|
---|
263 | rc = tstVDOpenCreateWriteMerge("VMDK", "tmpVDBase.vmdk", "tmpVDDiff.vmdk");
|
---|
264 | if (VBOX_FAILURE(rc))
|
---|
265 | {
|
---|
266 | RTPrintf("tstVD: VMDK test failed (existing image)! rc=%Vrc\n", rc);
|
---|
267 | g_cErrors++;
|
---|
268 | }
|
---|
269 |
|
---|
270 | /*
|
---|
271 | * Clean up any leftovers.
|
---|
272 | */
|
---|
273 | RTFileDelete("tmpVDCreate.vdi");
|
---|
274 | RTFileDelete("tmpVDCreate.vmdk");
|
---|
275 | RTFileDelete("tmpVDBase.vdi");
|
---|
276 | RTFileDelete("tmpVDDiff.vdi");
|
---|
277 | RTFileDelete("tmpVDBase.vmdk");
|
---|
278 | RTFileDelete("tmpVDDiff.vmdk");
|
---|
279 |
|
---|
280 | /*
|
---|
281 | * Summary
|
---|
282 | */
|
---|
283 | if (!g_cErrors)
|
---|
284 | RTPrintf("tstVD: SUCCESS\n");
|
---|
285 | else
|
---|
286 | RTPrintf("tstVD: FAILURE - %d errors\n", g_cErrors);
|
---|
287 |
|
---|
288 | return !!g_cErrors;
|
---|
289 | }
|
---|
290 |
|
---|