VirtualBox

source: vbox/trunk/src/VBox/Storage/testcase/tstVDCopy.cpp@ 33889

Last change on this file since 33889 was 33567, checked in by vboxsync, 14 years ago

VD: Move the generic virtual disk framework + backends to src/VBox/Storage and rename the files to get rid of the HDD part because it supports floppy and DVD images too

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/** @file
2 *
3 * Simple VBox HDD container test utility. Compares two images and prints
4 * differences. Mainly used to check cloning of disk images.
5 */
6
7/*
8 * Copyright (C) 2006-2010 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#include <VBox/err.h>
20#include <VBox/vd.h>
21#include <iprt/string.h>
22#include <iprt/stream.h>
23#include <iprt/file.h>
24#include <iprt/mem.h>
25#include <iprt/initterm.h>
26#include <iprt/rand.h>
27#include "stdio.h"
28#include "stdlib.h"
29
30/* from VBoxHDD.cpp */
31#define VD_MERGE_BUFFER_SIZE (16 * _1M)
32
33/*******************************************************************************
34* Global Variables *
35*******************************************************************************/
36/** The error count. */
37unsigned g_cErrors = 0;
38
39
40static void tstVDError(void *pvUser, int rc, RT_SRC_POS_DECL,
41 const char *pszFormat, va_list va)
42{
43 g_cErrors++;
44 RTPrintf("tstVD: Error %Rrc at %s:%u (%s): ", rc, RT_SRC_POS_ARGS);
45 RTPrintfV(pszFormat, va);
46 RTPrintf("\n");
47}
48
49int main(int argc, char *argv[])
50{
51 int rc;
52
53 RTR3Init();
54
55 if (argc != 3)
56 {
57 RTPrintf("Usage: ./tstVDCopy <hdd1> <hdd2>\n");
58 return 1;
59 }
60
61 RTPrintf("tstVDCopy: TESTING...\n");
62
63 PVBOXHDD pVD1 = NULL;
64 PVBOXHDD pVD2 = NULL;
65 PVDINTERFACE pVDIfs = NULL;
66 VDINTERFACE VDIError;
67 VDINTERFACEERROR VDIErrorCallbacks;
68 char *pszVD1 = NULL;
69 char *pszVD2 = NULL;
70 char *pbBuf1 = NULL;
71 char *pbBuf2 = NULL;
72 VDTYPE enmTypeVD1 = VDTYPE_INVALID;
73 VDTYPE enmTypeVD2 = VDTYPE_INVALID;
74
75#define CHECK(str) \
76 do \
77 { \
78 if (RT_FAILURE(rc)) \
79 { \
80 RTPrintf("%s rc=%Rrc\n", str, rc); \
81 if (pVD1) \
82 VDCloseAll(pVD1); \
83 if (pVD2) \
84 VDCloseAll(pVD2); \
85 return rc; \
86 } \
87 } while (0)
88
89 pbBuf1 = (char *)RTMemAllocZ(VD_MERGE_BUFFER_SIZE);
90 pbBuf2 = (char *)RTMemAllocZ(VD_MERGE_BUFFER_SIZE);
91
92 /* Create error interface. */
93 VDIErrorCallbacks.cbSize = sizeof(VDINTERFACEERROR);
94 VDIErrorCallbacks.enmInterface = VDINTERFACETYPE_ERROR;
95 VDIErrorCallbacks.pfnError = tstVDError;
96
97 rc = VDInterfaceAdd(&VDIError, "tstVD_Error", VDINTERFACETYPE_ERROR, &VDIErrorCallbacks,
98 NULL, &pVDIfs);
99 AssertRC(rc);
100
101 rc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
102 argv[1], &pszVD1, &enmTypeVD1);
103 CHECK("VDGetFormat() hdd1");
104
105 rc = VDGetFormat(NULL /* pVDIfsDisk */, NULL /* pVDIfsImage */,
106 argv[2], &pszVD2, &enmTypeVD2);
107 CHECK("VDGetFormat() hdd2");
108
109 rc = VDCreate(&VDIError, VDTYPE_HDD, &pVD1);
110 CHECK("VDCreate() hdd1");
111
112 rc = VDCreate(&VDIError, VDTYPE_HDD, &pVD2);
113 CHECK("VDCreate() hdd1");
114
115 rc = VDOpen(pVD1, pszVD1, argv[1], VD_OPEN_FLAGS_NORMAL, NULL);
116 CHECK("VDOpen() hdd1");
117
118 rc = VDOpen(pVD2, pszVD2, argv[2], VD_OPEN_FLAGS_NORMAL, NULL);
119 CHECK("VDOpen() hdd2");
120
121 uint64_t cbSize1 = 0;
122 uint64_t cbSize2 = 0;
123
124 cbSize1 = VDGetSize(pVD1, 0);
125 Assert(cbSize1 != 0);
126 cbSize2 = VDGetSize(pVD1, 0);
127 Assert(cbSize1 != 0);
128
129 if (cbSize1 == cbSize2)
130 {
131 uint64_t uOffCurr = 0;
132
133 /* Compare block by block. */
134 while (uOffCurr < cbSize1)
135 {
136 size_t cbRead = RT_MIN((cbSize1 - uOffCurr), VD_MERGE_BUFFER_SIZE);
137
138 rc = VDRead(pVD1, uOffCurr, pbBuf1, cbRead);
139 CHECK("VDRead() hdd1");
140
141 rc = VDRead(pVD2, uOffCurr, pbBuf2, cbRead);
142 CHECK("VDRead() hdd2");
143
144 if (memcmp(pbBuf1, pbBuf2, cbRead))
145 {
146 RTPrintf("tstVDCopy: Images differ uOffCurr=%llu\n", uOffCurr);
147 /* Do byte by byte comparison. */
148 for (size_t i = 0; i < cbRead; i++)
149 {
150 if (pbBuf1[i] != pbBuf2[i])
151 {
152 RTPrintf("tstVDCopy: First different byte is at offset %llu\n", uOffCurr + i);
153 break;
154 }
155 }
156 break;
157 }
158
159 uOffCurr += cbRead;
160 }
161 }
162 else
163 RTPrintf("tstVDCopy: Images have different size hdd1=%llu hdd2=%llu\n", cbSize1, cbSize2);
164
165 VDClose(pVD1, false);
166 CHECK("VDClose() hdd1");
167
168 VDClose(pVD2, false);
169 CHECK("VDClose() hdd2");
170
171 VDDestroy(pVD1);
172 VDDestroy(pVD2);
173 RTMemFree(pbBuf1);
174 RTMemFree(pbBuf2);
175#undef CHECK
176
177 rc = VDShutdown();
178 if (RT_FAILURE(rc))
179 {
180 RTPrintf("tstVDCopy: unloading backends failed! rc=%Rrc\n", rc);
181 g_cErrors++;
182 }
183 /*
184 * Summary
185 */
186 if (!g_cErrors)
187 RTPrintf("tstVDCopy: SUCCESS\n");
188 else
189 RTPrintf("tstVDCopy: FAILURE - %d errors\n", g_cErrors);
190
191 return !!g_cErrors;
192}
193
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