VirtualBox

source: vbox/trunk/src/VBox/Storage/testcase/tstVD.cpp@ 59710

Last change on this file since 59710 was 57415, checked in by vboxsync, 9 years ago

More DECLCALLBACK fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.9 KB
Line 
1/* $Id: tstVD.cpp 57415 2015-08-18 10:58:19Z vboxsync $ */
2/** @file
3 * Simple VBox HDD container test utility.
4 */
5
6/*
7 * Copyright (C) 2006-2011 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#include <VBox/vd.h>
23#include <VBox/err.h>
24#include <VBox/log.h>
25#include <iprt/asm-amd64-x86.h>
26#include <iprt/dir.h>
27#include <iprt/string.h>
28#include <iprt/stream.h>
29#include <iprt/file.h>
30#include <iprt/mem.h>
31#include <iprt/initterm.h>
32#include <iprt/rand.h>
33#include "stdio.h"
34#include "stdlib.h"
35
36#define VHD_TEST
37#define VDI_TEST
38#define VMDK_TEST
39
40
41/*********************************************************************************************************************************
42* Global Variables *
43*********************************************************************************************************************************/
44/** The error count. */
45unsigned g_cErrors = 0;
46
47
48static DECLCALLBACK(void) tstVDError(void *pvUser, int rc, RT_SRC_POS_DECL, const char *pszFormat, va_list va)
49{
50 g_cErrors++;
51 RTPrintf("tstVD: Error %Rrc at %s:%u (%s): ", rc, RT_SRC_POS_ARGS);
52 RTPrintfV(pszFormat, va);
53 RTPrintf("\n");
54}
55
56static DECLCALLBACK(int) tstVDMessage(void *pvUser, const char *pszFormat, va_list va)
57{
58 RTPrintf("tstVD: ");
59 RTPrintfV(pszFormat, va);
60 return VINF_SUCCESS;
61}
62
63static int tstVDCreateDelete(const char *pszBackend, const char *pszFilename,
64 uint64_t cbSize, unsigned uFlags, bool fDelete)
65{
66 int rc;
67 PVBOXHDD pVD = NULL;
68 VDGEOMETRY PCHS = { 0, 0, 0 };
69 VDGEOMETRY LCHS = { 0, 0, 0 };
70 PVDINTERFACE pVDIfs = NULL;
71 VDINTERFACEERROR VDIfError;
72
73#define CHECK(str) \
74 do \
75 { \
76 RTPrintf("%s rc=%Rrc\n", str, rc); \
77 if (RT_FAILURE(rc)) \
78 { \
79 VDDestroy(pVD); \
80 return rc; \
81 } \
82 } while (0)
83
84 /* Create error interface. */
85 VDIfError.pfnError = tstVDError;
86 VDIfError.pfnMessage = tstVDMessage;
87
88 rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
89 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
90 AssertRC(rc);
91
92 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
93 CHECK("VDCreate()");
94
95 rc = VDCreateBase(pVD, pszBackend, pszFilename, cbSize,
96 uFlags, "Test image", &PCHS, &LCHS, NULL,
97 VD_OPEN_FLAGS_NORMAL, NULL, NULL);
98 CHECK("VDCreateBase()");
99
100 VDDumpImages(pVD);
101
102 VDClose(pVD, fDelete);
103 if (fDelete)
104 {
105 RTFILE File;
106 rc = RTFileOpen(&File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
107 if (RT_SUCCESS(rc))
108 {
109 RTFileClose(File);
110 return VERR_INTERNAL_ERROR;
111 }
112 }
113
114 VDDestroy(pVD);
115#undef CHECK
116 return 0;
117}
118
119static int tstVDOpenDelete(const char *pszBackend, const char *pszFilename)
120{
121 int rc;
122 PVBOXHDD pVD = NULL;
123 VDGEOMETRY PCHS = { 0, 0, 0 };
124 VDGEOMETRY LCHS = { 0, 0, 0 };
125 PVDINTERFACE pVDIfs = NULL;
126 VDINTERFACEERROR VDIfError;
127
128#define CHECK(str) \
129 do \
130 { \
131 RTPrintf("%s rc=%Rrc\n", str, rc); \
132 if (RT_FAILURE(rc)) \
133 { \
134 VDDestroy(pVD); \
135 return rc; \
136 } \
137 } while (0)
138
139 /* Create error interface. */
140 VDIfError.pfnError = tstVDError;
141 VDIfError.pfnMessage = tstVDMessage;
142
143 rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
144 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
145 AssertRC(rc);
146
147
148 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
149 CHECK("VDCreate()");
150
151 rc = VDOpen(pVD, pszBackend, pszFilename, VD_OPEN_FLAGS_NORMAL, NULL);
152 CHECK("VDOpen()");
153
154 VDDumpImages(pVD);
155
156 VDClose(pVD, true);
157 RTFILE File;
158 rc = RTFileOpen(&File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
159 if (RT_SUCCESS(rc))
160 {
161 RTFileClose(File);
162 return VERR_INTERNAL_ERROR;
163 }
164
165 VDDestroy(pVD);
166#undef CHECK
167 return 0;
168}
169
170
171#undef RTDECL
172#define RTDECL(x) static x
173
174/* Start of IPRT code */
175
176/**
177 * The following code is based on the work of George Marsaglia
178 * taken from
179 * http://groups.google.ws/group/comp.sys.sun.admin/msg/7c667186f6cbf354
180 * and
181 * http://groups.google.ws/group/comp.lang.c/msg/0e170777c6e79e8d
182 */
183
184/*
185A C version of a very very good 64-bit RNG is given below.
186You should be able to adapt it to your particular needs.
187
188It is based on the complimentary-multiple-with-carry
189sequence
190 x(n)=a*x(n-4)+carry mod 2^64-1,
191which works as follows:
192Assume a certain multiplier 'a' and a base 'b'.
193Given a current x value and a current carry 'c',
194form: t=a*x+c
195Then the new carry is c=floor(t/b)
196and the new x value is x = b-1-(t mod b).
197
198
199Ordinarily, for 32-bit mwc or cmwc sequences, the
200value t=a*x+c can be formed in 64 bits, then the new c
201is the top and the new x the bottom 32 bits (with a little
202fiddling when b=2^32-1 and cmwc rather than mwc.)
203
204
205To generate 64-bit x's, it is difficult to form
206t=a*x+c in 128 bits then get the new c and new x
207from the top and bottom halves.
208But if 'a' has a special form, for example,
209a=2^62+2^47+2 and b=2^64-1, then the new c and
210the new x can be formed with shifts, tests and +/-'s,
211again with a little fiddling because b=2^64-1 rather
212than 2^64. (The latter is not an optimal choice because,
213being a square, it cannot be a primitive root of the
214prime a*b^k+1, where 'k' is the 'lag':
215 x(n)=a*x(n-k)+carry mod b.)
216But the multiplier a=2^62+2^47+2 makes a*b^4+1 a prime for
217which b=2^64-1 is a primitive root, and getting the new x and
218new c can be done with arithmetic on integers the size of x.
219*/
220
221struct RndCtx
222{
223 uint64_t x;
224 uint64_t y;
225 uint64_t z;
226 uint64_t w;
227 uint64_t c;
228 uint32_t u32x;
229 uint32_t u32y;
230};
231typedef struct RndCtx RNDCTX;
232typedef RNDCTX *PRNDCTX;
233
234/**
235 * Initialize seeds.
236 *
237 * @remarks You should choose ANY 4 random 64-bit
238 * seeds x,y,z,w < 2^64-1 and a random seed c in
239 * 0<= c < a = 2^62+2^47+2.
240 * There are P=(2^62+2^46+2)*(2^64-1)^4 > 2^318 possible choices
241 * for seeds, the period of the RNG.
242 */
243RTDECL(int) RTPRandInit(PRNDCTX pCtx, uint32_t u32Seed)
244{
245 if (u32Seed == 0)
246 u32Seed = (uint32_t)(ASMReadTSC() >> 8);
247 /* Zero is not a good seed. */
248 if (u32Seed == 0)
249 u32Seed = 362436069;
250 pCtx->x = u32Seed;
251 pCtx->y = 17280675555674358941ULL;
252 pCtx->z = 6376492577913983186ULL;
253 pCtx->w = 9064188857900113776ULL;
254 pCtx->c = 123456789;
255 pCtx->u32x = 2282008;
256 pCtx->u32y = u32Seed;
257 return VINF_SUCCESS;
258}
259
260RTDECL(uint32_t) RTPRandGetSeedInfo(PRNDCTX pCtx)
261{
262 return pCtx->u32y;
263}
264
265/**
266 * Generate a 64-bit unsigned random number.
267 *
268 * @returns The pseudo random number.
269 */
270RTDECL(uint64_t) RTPRandU64(PRNDCTX pCtx)
271{
272 uint64_t t;
273 t = (pCtx->x<<47) + (pCtx->x<<62) + (pCtx->x<<1);
274 t += pCtx->c; t+= (t < pCtx->c);
275 pCtx->c = (t<pCtx->c) + (pCtx->x>>17) + (pCtx->x>>2) + (pCtx->x>>63);
276 pCtx->x = pCtx->y; pCtx->y = pCtx->z ; pCtx->z = pCtx->w;
277 return (pCtx->w = ~(t + pCtx->c)-1);
278}
279
280/**
281 * Generate a 64-bit unsigned pseudo random number in the set
282 * [u64First..u64Last].
283 *
284 * @returns The pseudo random number.
285 * @param u64First First number in the set.
286 * @param u64Last Last number in the set.
287 */
288RTDECL(uint64_t) RTPRandU64Ex(PRNDCTX pCtx, uint64_t u64First, uint64_t u64Last)
289{
290 if (u64First == 0 && u64Last == UINT64_MAX)
291 return RTPRandU64(pCtx);
292
293 uint64_t u64Tmp;
294 uint64_t u64Range = u64Last - u64First + 1;
295 uint64_t u64Scale = UINT64_MAX / u64Range;
296
297 do
298 {
299 u64Tmp = RTPRandU64(pCtx) / u64Scale;
300 } while (u64Tmp >= u64Range);
301 return u64First + u64Tmp;
302}
303
304/**
305 * Generate a 32-bit unsigned random number.
306 *
307 * @returns The pseudo random number.
308 */
309RTDECL(uint32_t) RTPRandU32(PRNDCTX pCtx)
310{
311 return ( pCtx->u32x = 69069 * pCtx->u32x + 123,
312 pCtx->u32y ^= pCtx->u32y<<13,
313 pCtx->u32y ^= pCtx->u32y>>17,
314 pCtx->u32y ^= pCtx->u32y<<5,
315 pCtx->u32x + pCtx->u32y );
316}
317
318/**
319 * Generate a 32-bit unsigned pseudo random number in the set
320 * [u32First..u32Last].
321 *
322 * @returns The pseudo random number.
323 * @param u32First First number in the set.
324 * @param u32Last Last number in the set.
325 */
326RTDECL(uint32_t) RTPRandU32Ex(PRNDCTX pCtx, uint32_t u32First, uint32_t u32Last)
327{
328 if (u32First == 0 && u32Last == UINT32_MAX)
329 return RTPRandU32(pCtx);
330
331 uint32_t u32Tmp;
332 uint32_t u32Range = u32Last - u32First + 1;
333 uint32_t u32Scale = UINT32_MAX / u32Range;
334
335 do
336 {
337 u32Tmp = RTPRandU32(pCtx) / u32Scale;
338 } while (u32Tmp >= u32Range);
339 return u32First + u32Tmp;
340}
341
342/* End of IPRT code */
343
344struct Segment
345{
346 uint64_t u64Offset;
347 uint32_t u32Length;
348 uint32_t u8Value;
349};
350typedef struct Segment *PSEGMENT;
351
352static void initializeRandomGenerator(PRNDCTX pCtx, uint32_t u32Seed)
353{
354 int rc = RTPRandInit(pCtx, u32Seed);
355 if (RT_FAILURE(rc))
356 RTPrintf("ERROR: Failed to initialize random generator. RC=%Rrc\n", rc);
357 else
358 {
359 RTPrintf("INFO: Random generator seed used: %x\n", RTPRandGetSeedInfo(pCtx));
360 RTLogPrintf("INFO: Random generator seed used: %x\n", RTPRandGetSeedInfo(pCtx));
361 }
362}
363
364static int compareSegments(const void *left, const void *right)
365{
366 /* Note that no duplicates are allowed in the array being sorted. */
367 return ((PSEGMENT)left)->u64Offset < ((PSEGMENT)right)->u64Offset ? -1 : 1;
368}
369
370static void generateRandomSegments(PRNDCTX pCtx, PSEGMENT pSegment, uint32_t nSegments, uint32_t u32MaxSegmentSize, uint64_t u64DiskSize, uint32_t u32SectorSize, uint8_t u8ValueLow, uint8_t u8ValueHigh)
371{
372 uint32_t i;
373 /* Generate segment offsets. */
374 for (i = 0; i < nSegments; i++)
375 {
376 bool fDuplicateFound;
377 do
378 {
379 pSegment[i].u64Offset = RTPRandU64Ex(pCtx, 0, u64DiskSize / u32SectorSize - 1) * u32SectorSize;
380 fDuplicateFound = false;
381 for (uint32_t j = 0; j < i; j++)
382 if (pSegment[i].u64Offset == pSegment[j].u64Offset)
383 {
384 fDuplicateFound = true;
385 break;
386 }
387 } while (fDuplicateFound);
388 }
389 /* Sort in offset-ascending order. */
390 qsort(pSegment, nSegments, sizeof(*pSegment), compareSegments);
391 /* Put a sentinel at the end. */
392 pSegment[nSegments].u64Offset = u64DiskSize;
393 pSegment[nSegments].u32Length = 0;
394 /* Generate segment lengths and values. */
395 for (i = 0; i < nSegments; i++)
396 {
397 pSegment[i].u32Length = RTPRandU32Ex(pCtx, 1, RT_MIN(pSegment[i+1].u64Offset - pSegment[i].u64Offset,
398 u32MaxSegmentSize) / u32SectorSize) * u32SectorSize;
399 Assert(pSegment[i].u32Length <= u32MaxSegmentSize);
400 pSegment[i].u8Value = RTPRandU32Ex(pCtx, (uint32_t)u8ValueLow, (uint32_t)u8ValueHigh);
401 }
402}
403
404static void mergeSegments(PSEGMENT pBaseSegment, PSEGMENT pDiffSegment, PSEGMENT pMergeSegment, uint32_t u32MaxLength)
405{
406 while (pBaseSegment->u32Length > 0 || pDiffSegment->u32Length > 0)
407 {
408 if (pBaseSegment->u64Offset < pDiffSegment->u64Offset)
409 {
410 *pMergeSegment = *pBaseSegment;
411 if (pMergeSegment->u64Offset + pMergeSegment->u32Length <= pDiffSegment->u64Offset)
412 pBaseSegment++;
413 else
414 {
415 pMergeSegment->u32Length = pDiffSegment->u64Offset - pMergeSegment->u64Offset;
416 Assert(pMergeSegment->u32Length <= u32MaxLength);
417 if (pBaseSegment->u64Offset + pBaseSegment->u32Length >
418 pDiffSegment->u64Offset + pDiffSegment->u32Length)
419 {
420 pBaseSegment->u32Length -= pDiffSegment->u64Offset + pDiffSegment->u32Length - pBaseSegment->u64Offset;
421 Assert(pBaseSegment->u32Length <= u32MaxLength);
422 pBaseSegment->u64Offset = pDiffSegment->u64Offset + pDiffSegment->u32Length;
423 }
424 else
425 pBaseSegment++;
426 }
427 pMergeSegment++;
428 }
429 else
430 {
431 *pMergeSegment = *pDiffSegment;
432 if (pMergeSegment->u64Offset + pMergeSegment->u32Length <= pBaseSegment->u64Offset)
433 {
434 pDiffSegment++;
435 pMergeSegment++;
436 }
437 else
438 {
439 if (pBaseSegment->u64Offset + pBaseSegment->u32Length > pDiffSegment->u64Offset + pDiffSegment->u32Length)
440 {
441 pBaseSegment->u32Length -= pDiffSegment->u64Offset + pDiffSegment->u32Length - pBaseSegment->u64Offset;
442 Assert(pBaseSegment->u32Length <= u32MaxLength);
443 pBaseSegment->u64Offset = pDiffSegment->u64Offset + pDiffSegment->u32Length;
444 pDiffSegment++;
445 pMergeSegment++;
446 }
447 else
448 pBaseSegment++;
449 }
450 }
451 }
452}
453
454static void writeSegmentsToDisk(PVBOXHDD pVD, void *pvBuf, PSEGMENT pSegment)
455{
456 while (pSegment->u32Length)
457 {
458 //memset((uint8_t*)pvBuf + pSegment->u64Offset, pSegment->u8Value, pSegment->u32Length);
459 memset(pvBuf, pSegment->u8Value, pSegment->u32Length);
460 VDWrite(pVD, pSegment->u64Offset, pvBuf, pSegment->u32Length);
461 pSegment++;
462 }
463}
464
465static int readAndCompareSegments(PVBOXHDD pVD, void *pvBuf, PSEGMENT pSegment)
466{
467 while (pSegment->u32Length)
468 {
469 int rc = VDRead(pVD, pSegment->u64Offset, pvBuf, pSegment->u32Length);
470 if (RT_FAILURE(rc))
471 {
472 RTPrintf("ERROR: Failed to read from virtual disk\n");
473 return rc;
474 }
475 else
476 {
477 for (unsigned i = 0; i < pSegment->u32Length; i++)
478 if (((uint8_t*)pvBuf)[i] != pSegment->u8Value)
479 {
480 RTPrintf("ERROR: Segment at %Lx of %x bytes is corrupt at offset %x (found %x instead of %x)\n",
481 pSegment->u64Offset, pSegment->u32Length, i, ((uint8_t*)pvBuf)[i],
482 pSegment->u8Value);
483 RTLogPrintf("ERROR: Segment at %Lx of %x bytes is corrupt at offset %x (found %x instead of %x)\n",
484 pSegment->u64Offset, pSegment->u32Length, i, ((uint8_t*)pvBuf)[i],
485 pSegment->u8Value);
486 return VERR_INTERNAL_ERROR;
487 }
488 }
489 pSegment++;
490 }
491
492 return VINF_SUCCESS;
493}
494
495static int tstVDOpenCreateWriteMerge(const char *pszBackend,
496 const char *pszBaseFilename,
497 const char *pszDiffFilename,
498 uint32_t u32Seed)
499{
500 int rc;
501 PVBOXHDD pVD = NULL;
502 char *pszFormat;
503 VDTYPE enmType = VDTYPE_INVALID;
504 VDGEOMETRY PCHS = { 0, 0, 0 };
505 VDGEOMETRY LCHS = { 0, 0, 0 };
506 uint64_t u64DiskSize = 1000 * _1M;
507 uint32_t u32SectorSize = 512;
508 PVDINTERFACE pVDIfs = NULL;
509 VDINTERFACEERROR VDIfError;
510
511#define CHECK(str) \
512 do \
513 { \
514 RTPrintf("%s rc=%Rrc\n", str, rc); \
515 if (RT_FAILURE(rc)) \
516 { \
517 if (pvBuf) \
518 RTMemFree(pvBuf); \
519 VDDestroy(pVD); \
520 return rc; \
521 } \
522 } while (0)
523
524 void *pvBuf = RTMemAlloc(_1M);
525
526 /* Create error interface. */
527 VDIfError.pfnError = tstVDError;
528 VDIfError.pfnMessage = tstVDMessage;
529
530 rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
531 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
532 AssertRC(rc);
533
534
535 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
536 CHECK("VDCreate()");
537
538 RTFILE File;
539 rc = RTFileOpen(&File, pszBaseFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
540 if (RT_SUCCESS(rc))
541 {
542 RTFileClose(File);
543 rc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
544 pszBaseFilename, &pszFormat, &enmType);
545 RTPrintf("VDGetFormat() pszFormat=%s rc=%Rrc\n", pszFormat, rc);
546 if (RT_SUCCESS(rc) && strcmp(pszFormat, pszBackend))
547 {
548 rc = VERR_GENERAL_FAILURE;
549 RTPrintf("VDGetFormat() returned incorrect backend name\n");
550 }
551 RTStrFree(pszFormat);
552 CHECK("VDGetFormat()");
553
554 rc = VDOpen(pVD, pszBackend, pszBaseFilename, VD_OPEN_FLAGS_NORMAL,
555 NULL);
556 CHECK("VDOpen()");
557 }
558 else
559 {
560 rc = VDCreateBase(pVD, pszBackend, pszBaseFilename, u64DiskSize,
561 VD_IMAGE_FLAGS_NONE, "Test image",
562 &PCHS, &LCHS, NULL, VD_OPEN_FLAGS_NORMAL,
563 NULL, NULL);
564 CHECK("VDCreateBase()");
565 }
566
567 int nSegments = 100;
568 /* Allocate one extra element for a sentinel. */
569 PSEGMENT paBaseSegments = (PSEGMENT)RTMemAllocZ(sizeof(struct Segment) * (nSegments + 1));
570 PSEGMENT paDiffSegments = (PSEGMENT)RTMemAllocZ(sizeof(struct Segment) * (nSegments + 1));
571 PSEGMENT paMergeSegments = (PSEGMENT)RTMemAllocZ(sizeof(struct Segment) * (nSegments + 1) * 3);
572
573 RNDCTX ctx;
574 initializeRandomGenerator(&ctx, u32Seed);
575 generateRandomSegments(&ctx, paBaseSegments, nSegments, _1M, u64DiskSize, u32SectorSize, 0u, 127u);
576 generateRandomSegments(&ctx, paDiffSegments, nSegments, _1M, u64DiskSize, u32SectorSize, 128u, 255u);
577
578 /*PSEGMENT pSegment;
579 RTPrintf("Base segments:\n");
580 for (pSegment = paBaseSegments; pSegment->u32Length; pSegment++)
581 RTPrintf("off: %08Lx len: %05x val: %02x\n", pSegment->u64Offset, pSegment->u32Length, pSegment->u8Value);*/
582 writeSegmentsToDisk(pVD, pvBuf, paBaseSegments);
583
584 rc = VDCreateDiff(pVD, pszBackend, pszDiffFilename,
585 VD_IMAGE_FLAGS_NONE, "Test diff image", NULL, NULL,
586 VD_OPEN_FLAGS_NORMAL, NULL, NULL);
587 CHECK("VDCreateDiff()");
588
589 /*RTPrintf("\nDiff segments:\n");
590 for (pSegment = paDiffSegments; pSegment->u32Length; pSegment++)
591 RTPrintf("off: %08Lx len: %05x val: %02x\n", pSegment->u64Offset, pSegment->u32Length, pSegment->u8Value);*/
592 writeSegmentsToDisk(pVD, pvBuf, paDiffSegments);
593
594 VDDumpImages(pVD);
595
596 RTPrintf("Merging diff into base..\n");
597 rc = VDMerge(pVD, VD_LAST_IMAGE, 0, NULL);
598 CHECK("VDMerge()");
599
600 mergeSegments(paBaseSegments, paDiffSegments, paMergeSegments, _1M);
601 /*RTPrintf("\nMerged segments:\n");
602 for (pSegment = paMergeSegments; pSegment->u32Length; pSegment++)
603 RTPrintf("off: %08Lx len: %05x val: %02x\n", pSegment->u64Offset, pSegment->u32Length, pSegment->u8Value);*/
604 rc = readAndCompareSegments(pVD, pvBuf, paMergeSegments);
605 CHECK("readAndCompareSegments()");
606
607 RTMemFree(paMergeSegments);
608 RTMemFree(paDiffSegments);
609 RTMemFree(paBaseSegments);
610
611 VDDumpImages(pVD);
612
613 VDDestroy(pVD);
614 if (pvBuf)
615 RTMemFree(pvBuf);
616#undef CHECK
617 return 0;
618}
619
620static int tstVDCreateWriteOpenRead(const char *pszBackend,
621 const char *pszFilename,
622 uint32_t u32Seed)
623{
624 int rc;
625 PVBOXHDD pVD = NULL;
626 VDGEOMETRY PCHS = { 0, 0, 0 };
627 VDGEOMETRY LCHS = { 0, 0, 0 };
628 uint64_t u64DiskSize = 1000 * _1M;
629 uint32_t u32SectorSize = 512;
630 PVDINTERFACE pVDIfs = NULL;
631 VDINTERFACEERROR VDIfError;
632
633#define CHECK(str) \
634 do \
635 { \
636 RTPrintf("%s rc=%Rrc\n", str, rc); \
637 if (RT_FAILURE(rc)) \
638 { \
639 if (pvBuf) \
640 RTMemFree(pvBuf); \
641 VDDestroy(pVD); \
642 return rc; \
643 } \
644 } while (0)
645
646 void *pvBuf = RTMemAlloc(_1M);
647
648 /* Create error interface. */
649 VDIfError.pfnError = tstVDError;
650 VDIfError.pfnMessage = tstVDMessage;
651
652 rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
653 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
654 AssertRC(rc);
655
656 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
657 CHECK("VDCreate()");
658
659 RTFILE File;
660 rc = RTFileOpen(&File, pszFilename, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
661 if (RT_SUCCESS(rc))
662 {
663 RTFileClose(File);
664 RTFileDelete(pszFilename);
665 }
666
667 rc = VDCreateBase(pVD, pszBackend, pszFilename, u64DiskSize,
668 VD_IMAGE_FLAGS_NONE, "Test image",
669 &PCHS, &LCHS, NULL, VD_OPEN_FLAGS_NORMAL,
670 NULL, NULL);
671 CHECK("VDCreateBase()");
672
673 int nSegments = 100;
674 /* Allocate one extra element for a sentinel. */
675 PSEGMENT paSegments = (PSEGMENT)RTMemAllocZ(sizeof(struct Segment) * (nSegments + 1));
676
677 RNDCTX ctx;
678 initializeRandomGenerator(&ctx, u32Seed);
679 generateRandomSegments(&ctx, paSegments, nSegments, _1M, u64DiskSize, u32SectorSize, 0u, 127u);
680 /*for (PSEGMENT pSegment = paSegments; pSegment->u32Length; pSegment++)
681 RTPrintf("off: %08Lx len: %05x val: %02x\n", pSegment->u64Offset, pSegment->u32Length, pSegment->u8Value);*/
682
683 writeSegmentsToDisk(pVD, pvBuf, paSegments);
684
685 VDCloseAll(pVD);
686
687 rc = VDOpen(pVD, pszBackend, pszFilename, VD_OPEN_FLAGS_NORMAL, NULL);
688 CHECK("VDOpen()");
689 rc = readAndCompareSegments(pVD, pvBuf, paSegments);
690 CHECK("readAndCompareSegments()");
691
692 RTMemFree(paSegments);
693
694 VDDestroy(pVD);
695 if (pvBuf)
696 RTMemFree(pvBuf);
697#undef CHECK
698 return 0;
699}
700
701static int tstVmdkRename(const char *src, const char *dst)
702{
703 int rc;
704 PVBOXHDD pVD = NULL;
705 PVDINTERFACE pVDIfs = NULL;
706 VDINTERFACEERROR VDIfError;
707
708#define CHECK(str) \
709 do \
710 { \
711 RTPrintf("%s rc=%Rrc\n", str, rc); \
712 if (RT_FAILURE(rc)) \
713 { \
714 VDDestroy(pVD); \
715 return rc; \
716 } \
717 } while (0)
718
719 /* Create error interface. */
720 VDIfError.pfnError = tstVDError;
721 VDIfError.pfnMessage = tstVDMessage;
722
723 rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
724 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
725 AssertRC(rc);
726
727 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
728 CHECK("VDCreate()");
729
730 rc = VDOpen(pVD, "VMDK", src, VD_OPEN_FLAGS_NORMAL, NULL);
731 CHECK("VDOpen()");
732 rc = VDCopy(pVD, 0, pVD, "VMDK", dst, true, 0, VD_IMAGE_FLAGS_NONE, NULL,
733 VD_OPEN_FLAGS_NORMAL, NULL, NULL, NULL);
734 CHECK("VDCopy()");
735
736 VDDestroy(pVD);
737#undef CHECK
738 return 0;
739}
740
741static int tstVmdkCreateRenameOpen(const char *src, const char *dst,
742 uint64_t cbSize, unsigned uFlags)
743{
744 int rc = tstVDCreateDelete("VMDK", src, cbSize, uFlags, false);
745 if (RT_FAILURE(rc))
746 return rc;
747
748 rc = tstVmdkRename(src, dst);
749 if (RT_FAILURE(rc))
750 return rc;
751
752 PVBOXHDD pVD = NULL;
753 PVDINTERFACE pVDIfs = NULL;
754 VDINTERFACEERROR VDIfError;
755
756#define CHECK(str) \
757 do \
758 { \
759 RTPrintf("%s rc=%Rrc\n", str, rc); \
760 if (RT_FAILURE(rc)) \
761 { \
762 VDCloseAll(pVD); \
763 return rc; \
764 } \
765 } while (0)
766
767 /* Create error interface. */
768 VDIfError.pfnError = tstVDError;
769 VDIfError.pfnMessage = tstVDMessage;
770
771 rc = VDInterfaceAdd(&VDIfError.Core, "tstVD_Error", VDINTERFACETYPE_ERROR,
772 NULL, sizeof(VDINTERFACEERROR), &pVDIfs);
773 AssertRC(rc);
774
775 rc = VDCreate(pVDIfs, VDTYPE_HDD, &pVD);
776 CHECK("VDCreate()");
777
778 rc = VDOpen(pVD, "VMDK", dst, VD_OPEN_FLAGS_NORMAL, NULL);
779 CHECK("VDOpen()");
780
781 VDClose(pVD, true);
782 CHECK("VDClose()");
783 VDDestroy(pVD);
784#undef CHECK
785 return rc;
786}
787
788#if defined(RT_OS_OS2) || defined(RT_OS_WINDOWS)
789#define DST_PATH "tmp\\tmpVDRename.vmdk"
790#else
791#define DST_PATH "tmp/tmpVDRename.vmdk"
792#endif
793
794static void tstVmdk()
795{
796 int rc = tstVmdkCreateRenameOpen("tmpVDCreate.vmdk", "tmpVDRename.vmdk", _4G,
797 VD_IMAGE_FLAGS_NONE);
798 if (RT_FAILURE(rc))
799 {
800 RTPrintf("tstVD: VMDK rename (single extent, embedded descriptor, same dir) test failed! rc=%Rrc\n", rc);
801 g_cErrors++;
802 }
803 rc = tstVmdkCreateRenameOpen("tmpVDCreate.vmdk", "tmpVDRename.vmdk", _4G,
804 VD_VMDK_IMAGE_FLAGS_SPLIT_2G);
805 if (RT_FAILURE(rc))
806 {
807 RTPrintf("tstVD: VMDK rename (multiple extent, separate descriptor, same dir) test failed! rc=%Rrc\n", rc);
808 g_cErrors++;
809 }
810 rc = tstVmdkCreateRenameOpen("tmpVDCreate.vmdk", DST_PATH, _4G,
811 VD_IMAGE_FLAGS_NONE);
812 if (RT_FAILURE(rc))
813 {
814 RTPrintf("tstVD: VMDK rename (single extent, embedded descriptor, another dir) test failed! rc=%Rrc\n", rc);
815 g_cErrors++;
816 }
817 rc = tstVmdkCreateRenameOpen("tmpVDCreate.vmdk", DST_PATH, _4G,
818 VD_VMDK_IMAGE_FLAGS_SPLIT_2G);
819 if (RT_FAILURE(rc))
820 {
821 RTPrintf("tstVD: VMDK rename (multiple extent, separate descriptor, another dir) test failed! rc=%Rrc\n", rc);
822 g_cErrors++;
823 }
824
825 RTFILE File;
826 rc = RTFileOpen(&File, DST_PATH, RTFILE_O_WRITE | RTFILE_O_CREATE | RTFILE_O_DENY_NONE);
827 if (RT_SUCCESS(rc))
828 RTFileClose(File);
829
830 rc = tstVmdkCreateRenameOpen("tmpVDCreate.vmdk", DST_PATH, _4G,
831 VD_VMDK_IMAGE_FLAGS_SPLIT_2G);
832 if (RT_SUCCESS(rc))
833 {
834 RTPrintf("tstVD: VMDK rename (multiple extent, separate descriptor, another dir, already exists) test failed!\n");
835 g_cErrors++;
836 }
837 RTFileDelete(DST_PATH);
838 RTFileDelete("tmpVDCreate.vmdk");
839 RTFileDelete("tmpVDCreate-s001.vmdk");
840 RTFileDelete("tmpVDCreate-s002.vmdk");
841 RTFileDelete("tmpVDCreate-s003.vmdk");
842}
843
844int main(int argc, char *argv[])
845{
846 RTR3InitExe(argc, &argv, 0);
847 int rc;
848
849 uint32_t u32Seed = 0; // Means choose random
850
851 if (argc > 1)
852 if (sscanf(argv[1], "%x", &u32Seed) != 1)
853 {
854 RTPrintf("ERROR: Invalid parameter %s. Valid usage is %s <32-bit seed>.\n",
855 argv[1], argv[0]);
856 return 1;
857 }
858
859 RTPrintf("tstVD: TESTING...\n");
860
861 /*
862 * Clean up potential leftovers from previous unsuccessful runs.
863 */
864 RTFileDelete("tmpVDCreate.vdi");
865 RTFileDelete("tmpVDCreate.vmdk");
866 RTFileDelete("tmpVDCreate.vhd");
867 RTFileDelete("tmpVDBase.vdi");
868 RTFileDelete("tmpVDDiff.vdi");
869 RTFileDelete("tmpVDBase.vmdk");
870 RTFileDelete("tmpVDDiff.vmdk");
871 RTFileDelete("tmpVDBase.vhd");
872 RTFileDelete("tmpVDDiff.vhd");
873 RTFileDelete("tmpVDCreate-s001.vmdk");
874 RTFileDelete("tmpVDCreate-s002.vmdk");
875 RTFileDelete("tmpVDCreate-s003.vmdk");
876 RTFileDelete("tmpVDRename.vmdk");
877 RTFileDelete("tmpVDRename-s001.vmdk");
878 RTFileDelete("tmpVDRename-s002.vmdk");
879 RTFileDelete("tmpVDRename-s003.vmdk");
880 RTFileDelete("tmp/tmpVDRename.vmdk");
881 RTFileDelete("tmp/tmpVDRename-s001.vmdk");
882 RTFileDelete("tmp/tmpVDRename-s002.vmdk");
883 RTFileDelete("tmp/tmpVDRename-s003.vmdk");
884
885 if (!RTDirExists("tmp"))
886 {
887 rc = RTDirCreate("tmp", RTFS_UNIX_IRWXU, 0);
888 if (RT_FAILURE(rc))
889 {
890 RTPrintf("tstVD: Failed to create 'tmp' directory! rc=%Rrc\n", rc);
891 g_cErrors++;
892 }
893 }
894
895#ifdef VMDK_TEST
896 rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
897 VD_IMAGE_FLAGS_NONE, true);
898 if (RT_FAILURE(rc))
899 {
900 RTPrintf("tstVD: dynamic VMDK create test failed! rc=%Rrc\n", rc);
901 g_cErrors++;
902 }
903 rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
904 VD_IMAGE_FLAGS_NONE, false);
905 if (RT_FAILURE(rc))
906 {
907 RTPrintf("tstVD: dynamic VMDK create test failed! rc=%Rrc\n", rc);
908 g_cErrors++;
909 }
910 rc = tstVDOpenDelete("VMDK", "tmpVDCreate.vmdk");
911 if (RT_FAILURE(rc))
912 {
913 RTPrintf("tstVD: VMDK delete test failed! rc=%Rrc\n", rc);
914 g_cErrors++;
915 }
916
917 tstVmdk();
918#endif /* VMDK_TEST */
919#ifdef VDI_TEST
920 rc = tstVDCreateDelete("VDI", "tmpVDCreate.vdi", 2 * _4G,
921 VD_IMAGE_FLAGS_NONE, true);
922 if (RT_FAILURE(rc))
923 {
924 RTPrintf("tstVD: dynamic VDI create test failed! rc=%Rrc\n", rc);
925 g_cErrors++;
926 }
927 rc = tstVDCreateDelete("VDI", "tmpVDCreate.vdi", 2 * _4G,
928 VD_IMAGE_FLAGS_NONE, true);
929 if (RT_FAILURE(rc))
930 {
931 RTPrintf("tstVD: fixed VDI create test failed! rc=%Rrc\n", rc);
932 g_cErrors++;
933 }
934#endif /* VDI_TEST */
935#ifdef VMDK_TEST
936 rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
937 VD_IMAGE_FLAGS_NONE, true);
938 if (RT_FAILURE(rc))
939 {
940 RTPrintf("tstVD: dynamic VMDK create test failed! rc=%Rrc\n", rc);
941 g_cErrors++;
942 }
943 rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
944 VD_VMDK_IMAGE_FLAGS_SPLIT_2G, true);
945 if (RT_FAILURE(rc))
946 {
947 RTPrintf("tstVD: dynamic split VMDK create test failed! rc=%Rrc\n", rc);
948 g_cErrors++;
949 }
950 rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
951 VD_IMAGE_FLAGS_FIXED, true);
952 if (RT_FAILURE(rc))
953 {
954 RTPrintf("tstVD: fixed VMDK create test failed! rc=%Rrc\n", rc);
955 g_cErrors++;
956 }
957 rc = tstVDCreateDelete("VMDK", "tmpVDCreate.vmdk", 2 * _4G,
958 VD_IMAGE_FLAGS_FIXED | VD_VMDK_IMAGE_FLAGS_SPLIT_2G,
959 true);
960 if (RT_FAILURE(rc))
961 {
962 RTPrintf("tstVD: fixed split VMDK create test failed! rc=%Rrc\n", rc);
963 g_cErrors++;
964 }
965#endif /* VMDK_TEST */
966#ifdef VHD_TEST
967 rc = tstVDCreateDelete("VHD", "tmpVDCreate.vhd", 2 * _4G,
968 VD_IMAGE_FLAGS_NONE, true);
969 if (RT_FAILURE(rc))
970 {
971 RTPrintf("tstVD: dynamic VHD create test failed! rc=%Rrc\n", rc);
972 g_cErrors++;
973 }
974 rc = tstVDCreateDelete("VHD", "tmpVDCreate.vhd", 2 * _4G,
975 VD_IMAGE_FLAGS_FIXED, true);
976 if (RT_FAILURE(rc))
977 {
978 RTPrintf("tstVD: fixed VHD create test failed! rc=%Rrc\n", rc);
979 g_cErrors++;
980 }
981#endif /* VHD_TEST */
982#ifdef VDI_TEST
983 rc = tstVDOpenCreateWriteMerge("VDI", "tmpVDBase.vdi", "tmpVDDiff.vdi", u32Seed);
984 if (RT_FAILURE(rc))
985 {
986 RTPrintf("tstVD: VDI test failed (new image)! rc=%Rrc\n", rc);
987 g_cErrors++;
988 }
989 rc = tstVDOpenCreateWriteMerge("VDI", "tmpVDBase.vdi", "tmpVDDiff.vdi", u32Seed);
990 if (RT_FAILURE(rc))
991 {
992 RTPrintf("tstVD: VDI test failed (existing image)! rc=%Rrc\n", rc);
993 g_cErrors++;
994 }
995#endif /* VDI_TEST */
996#ifdef VMDK_TEST
997 rc = tstVDOpenCreateWriteMerge("VMDK", "tmpVDBase.vmdk", "tmpVDDiff.vmdk", u32Seed);
998 if (RT_FAILURE(rc))
999 {
1000 RTPrintf("tstVD: VMDK test failed (new image)! rc=%Rrc\n", rc);
1001 g_cErrors++;
1002 }
1003 rc = tstVDOpenCreateWriteMerge("VMDK", "tmpVDBase.vmdk", "tmpVDDiff.vmdk", u32Seed);
1004 if (RT_FAILURE(rc))
1005 {
1006 RTPrintf("tstVD: VMDK test failed (existing image)! rc=%Rrc\n", rc);
1007 g_cErrors++;
1008 }
1009#endif /* VMDK_TEST */
1010#ifdef VHD_TEST
1011 rc = tstVDCreateWriteOpenRead("VHD", "tmpVDCreate.vhd", u32Seed);
1012 if (RT_FAILURE(rc))
1013 {
1014 RTPrintf("tstVD: VHD test failed (creating image)! rc=%Rrc\n", rc);
1015 g_cErrors++;
1016 }
1017
1018 rc = tstVDOpenCreateWriteMerge("VHD", "tmpVDBase.vhd", "tmpVDDiff.vhd", u32Seed);
1019 if (RT_FAILURE(rc))
1020 {
1021 RTPrintf("tstVD: VHD test failed (existing image)! rc=%Rrc\n", rc);
1022 g_cErrors++;
1023 }
1024#endif /* VHD_TEST */
1025
1026 /*
1027 * Clean up any leftovers.
1028 */
1029 RTFileDelete("tmpVDCreate.vdi");
1030 RTFileDelete("tmpVDCreate.vmdk");
1031 RTFileDelete("tmpVDCreate.vhd");
1032 RTFileDelete("tmpVDBase.vdi");
1033 RTFileDelete("tmpVDDiff.vdi");
1034 RTFileDelete("tmpVDBase.vmdk");
1035 RTFileDelete("tmpVDDiff.vmdk");
1036 RTFileDelete("tmpVDBase.vhd");
1037 RTFileDelete("tmpVDDiff.vhd");
1038 RTFileDelete("tmpVDCreate-s001.vmdk");
1039 RTFileDelete("tmpVDCreate-s002.vmdk");
1040 RTFileDelete("tmpVDCreate-s003.vmdk");
1041 RTFileDelete("tmpVDRename.vmdk");
1042 RTFileDelete("tmpVDRename-s001.vmdk");
1043 RTFileDelete("tmpVDRename-s002.vmdk");
1044 RTFileDelete("tmpVDRename-s003.vmdk");
1045
1046 rc = VDShutdown();
1047 if (RT_FAILURE(rc))
1048 {
1049 RTPrintf("tstVD: unloading backends failed! rc=%Rrc\n", rc);
1050 g_cErrors++;
1051 }
1052 /*
1053 * Summary
1054 */
1055 if (!g_cErrors)
1056 RTPrintf("tstVD: SUCCESS\n");
1057 else
1058 RTPrintf("tstVD: FAILURE - %d errors\n", g_cErrors);
1059
1060 return !!g_cErrors;
1061}
1062
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette