VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstOVF.cpp@ 108379

最後變更 在這個檔案從108379是 107716,由 vboxsync 提交於 2 月 前

Main/testcase/tstOVF.cpp: Missing error check, bugref:3409

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 15.5 KB
 
1/* $Id: tstOVF.cpp 107716 2025-01-13 16:51:14Z vboxsync $ */
2/** @file
3 *
4 * tstOVF - testcases for OVF import and export
5 */
6
7/*
8 * Copyright (C) 2010-2024 Oracle and/or its affiliates.
9 *
10 * This file is part of VirtualBox base platform packages, as
11 * available from https://www.alldomusa.eu.org.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation, in version 3 of the
16 * License.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <https://www.gnu.org/licenses>.
25 *
26 * SPDX-License-Identifier: GPL-3.0-only
27 */
28
29#include <VBox/com/VirtualBox.h>
30
31#include <VBox/com/com.h>
32#include <VBox/com/array.h>
33#include <VBox/com/string.h>
34#include <VBox/com/ErrorInfo.h>
35#include <VBox/com/errorprint.h>
36
37#include <iprt/initterm.h>
38#include <iprt/stream.h>
39#include <iprt/file.h>
40#include <iprt/path.h>
41#include <iprt/param.h>
42
43#include <list>
44
45using namespace com;
46
47// main
48///////////////////////////////////////////////////////////////////////////////
49
50/**
51 * Quick hack exception structure.
52 *
53 */
54struct MyError
55{
56 MyError(HRESULT rc,
57 const char *pcsz,
58 IProgress *pProgress = NULL)
59 : m_rc(rc)
60 {
61 m_str = "ERROR: ";
62 m_str += pcsz;
63
64 if (pProgress)
65 {
66 com::ProgressErrorInfo info(pProgress);
67 com::GluePrintErrorInfo(info);
68 }
69 else if (rc != S_OK)
70 {
71 com::ErrorInfo info;
72 if (!info.isFullAvailable() && !info.isBasicAvailable())
73 com::GluePrintRCMessage(rc);
74 else
75 com::GluePrintErrorInfo(info);
76 }
77 }
78
79 Utf8Str m_str;
80 HRESULT m_rc;
81};
82
83/**
84 * Imports the given OVF file, with all bells and whistles.
85 * Throws MyError on errors.
86 * @param pcszPrefix Descriptive short prefix string for console output.
87 * @param pVirtualBox VirtualBox instance.
88 * @param pcszOVF0 File to import.
89 * @param llMachinesCreated out: UUIDs of machines that were created so that caller can clean up.
90 */
91static void importOVF(const char *pcszPrefix,
92 ComPtr<IVirtualBox> &pVirtualBox,
93 const char *pcszOVF0,
94 std::list<Guid> &llMachinesCreated)
95{
96 char szAbsOVF[RTPATH_MAX];
97 RTPathExecDir(szAbsOVF, sizeof(szAbsOVF));
98 RTPathAppend(szAbsOVF, sizeof(szAbsOVF), pcszOVF0);
99
100 RTPrintf("%s: reading appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
101 ComPtr<IAppliance> pAppl;
102 HRESULT hrc = pVirtualBox->CreateAppliance(pAppl.asOutParam());
103 if (FAILED(hrc)) throw MyError(hrc, "failed to create appliance\n");
104
105 ComPtr<IProgress> pProgress;
106 hrc = pAppl->Read(Bstr(szAbsOVF).raw(), pProgress.asOutParam());
107 if (FAILED(hrc)) throw MyError(hrc, "Appliance::Read() failed\n");
108 hrc = pProgress->WaitForCompletion(-1);
109 if (FAILED(hrc)) throw MyError(hrc, "Progress::WaitForCompletion() failed\n");
110 LONG rc2;
111 pProgress->COMGETTER(ResultCode)(&rc2);
112 if (FAILED(rc2)) throw MyError(rc2, "Appliance::Read() failed\n", pProgress);
113
114 RTPrintf("%s: interpreting appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
115 hrc = pAppl->Interpret();
116 if (FAILED(hrc)) throw MyError(hrc, "Appliance::Interpret() failed\n");
117
118 com::SafeIfaceArray<IVirtualSystemDescription> aDescriptions;
119 hrc = pAppl->COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aDescriptions));
120 if (FAILED(hrc)) throw MyError(hrc, "Appliance::GetVirtualSystemDescriptions() failed\n");
121
122 for (uint32_t u = 0;
123 u < aDescriptions.size();
124 ++u)
125 {
126 ComPtr<IVirtualSystemDescription> pVSys = aDescriptions[u];
127
128 com::SafeArray<VirtualSystemDescriptionType_T> aTypes;
129 com::SafeArray<BSTR> aRefs;
130 com::SafeArray<BSTR> aOvfValues;
131 com::SafeArray<BSTR> aVBoxValues;
132 com::SafeArray<BSTR> aExtraConfigValues;
133 hrc = pVSys->GetDescription(ComSafeArrayAsOutParam(aTypes),
134 ComSafeArrayAsOutParam(aRefs),
135 ComSafeArrayAsOutParam(aOvfValues),
136 ComSafeArrayAsOutParam(aVBoxValues),
137 ComSafeArrayAsOutParam(aExtraConfigValues));
138 if (FAILED(hrc)) throw MyError(hrc, "VirtualSystemDescription::GetDescription() failed\n");
139
140 for (uint32_t u2 = 0;
141 u2 < aTypes.size();
142 ++u2)
143 {
144 const char *pcszType;
145
146 VirtualSystemDescriptionType_T t = aTypes[u2];
147 switch (t)
148 {
149 case VirtualSystemDescriptionType_OS:
150 pcszType = "ostype";
151 break;
152
153 case VirtualSystemDescriptionType_Name:
154 pcszType = "name";
155 break;
156
157 case VirtualSystemDescriptionType_Product:
158 pcszType = "product";
159 break;
160
161 case VirtualSystemDescriptionType_ProductUrl:
162 pcszType = "producturl";
163 break;
164
165 case VirtualSystemDescriptionType_Vendor:
166 pcszType = "vendor";
167 break;
168
169 case VirtualSystemDescriptionType_VendorUrl:
170 pcszType = "vendorurl";
171 break;
172
173 case VirtualSystemDescriptionType_Version:
174 pcszType = "version";
175 break;
176
177 case VirtualSystemDescriptionType_Description:
178 pcszType = "description";
179 break;
180
181 case VirtualSystemDescriptionType_License:
182 pcszType = "license";
183 break;
184
185 case VirtualSystemDescriptionType_CPU:
186 pcszType = "cpu";
187 break;
188
189 case VirtualSystemDescriptionType_Memory:
190 pcszType = "memory";
191 break;
192
193 case VirtualSystemDescriptionType_HardDiskControllerIDE:
194 pcszType = "ide";
195 break;
196
197 case VirtualSystemDescriptionType_HardDiskControllerSATA:
198 pcszType = "sata";
199 break;
200
201 case VirtualSystemDescriptionType_HardDiskControllerSAS:
202 pcszType = "sas";
203 break;
204
205 case VirtualSystemDescriptionType_HardDiskControllerSCSI:
206 pcszType = "scsi";
207 break;
208
209 case VirtualSystemDescriptionType_HardDiskControllerVirtioSCSI:
210 pcszType = "virtio-scsi";
211 break;
212
213 case VirtualSystemDescriptionType_HardDiskControllerNVMe:
214 pcszType = "nvme";
215 break;
216
217 case VirtualSystemDescriptionType_HardDiskImage:
218 pcszType = "hd";
219 break;
220
221 case VirtualSystemDescriptionType_CDROM:
222 pcszType = "cdrom";
223 break;
224
225 case VirtualSystemDescriptionType_Floppy:
226 pcszType = "floppy";
227 break;
228
229 case VirtualSystemDescriptionType_NetworkAdapter:
230 pcszType = "net";
231 break;
232
233 case VirtualSystemDescriptionType_USBController:
234 pcszType = "usb";
235 break;
236
237 case VirtualSystemDescriptionType_SoundCard:
238 pcszType = "sound";
239 break;
240
241 case VirtualSystemDescriptionType_SettingsFile:
242 pcszType = "settings";
243 break;
244
245 case VirtualSystemDescriptionType_BaseFolder:
246 pcszType = "basefolder";
247 break;
248
249 case VirtualSystemDescriptionType_PrimaryGroup:
250 pcszType = "primarygroup";
251 break;
252
253 default:
254 throw MyError(E_UNEXPECTED, Utf8StrFmt("Invalid VirtualSystemDescriptionType (enum=%d)\n", t).c_str());
255 break;
256 }
257
258 RTPrintf(" vsys %2u item %2u: type %2d (%s), ovf: \"%ls\", vbox: \"%ls\", extra: \"%ls\"\n",
259 u, u2, t, pcszType,
260 aOvfValues[u2],
261 aVBoxValues[u2],
262 aExtraConfigValues[u2]);
263 }
264 }
265
266 RTPrintf("%s: importing %d machine(s)...\n", pcszPrefix, aDescriptions.size());
267 SafeArray<ImportOptions_T> sfaOptions;
268 hrc = pAppl->ImportMachines(ComSafeArrayAsInParam(sfaOptions), pProgress.asOutParam());
269 if (FAILED(hrc)) throw MyError(hrc, "Appliance::ImportMachines() failed\n");
270 hrc = pProgress->WaitForCompletion(-1);
271 if (FAILED(hrc)) throw MyError(hrc, "Progress::WaitForCompletion() failed\n");
272 pProgress->COMGETTER(ResultCode)(&rc2);
273 if (FAILED(rc2)) throw MyError(rc2, "Appliance::ImportMachines() failed\n", pProgress);
274
275 com::SafeArray<BSTR> aMachineUUIDs;
276 hrc = pAppl->COMGETTER(Machines)(ComSafeArrayAsOutParam(aMachineUUIDs));
277 if (FAILED(hrc)) throw MyError(hrc, "Appliance::GetMachines() failed\n");
278
279 for (size_t u = 0;
280 u < aMachineUUIDs.size();
281 ++u)
282 {
283 RTPrintf("%s: created machine %u: %ls\n", pcszPrefix, u, aMachineUUIDs[u]);
284 llMachinesCreated.push_back(Guid(Bstr(aMachineUUIDs[u])));
285 }
286
287 RTPrintf("%s: success!\n", pcszPrefix);
288}
289
290/**
291 * Copies ovf-testcases/ovf-dummy.vmdk to the given target and appends that
292 * target as a string to the given list so that the caller can delete it
293 * again later.
294 * @param llFiles2Delete List of strings to append the target file path to.
295 * @param pcszDest Target for dummy VMDK.
296 */
297static void copyDummyDiskImage(const char *pcszPrefix,
298 std::list<Utf8Str> &llFiles2Delete,
299 const char *pcszDest)
300{
301 char szSrc[RTPATH_MAX];
302 RTPathExecDir(szSrc, sizeof(szSrc));
303 RTPathAppend(szSrc, sizeof(szSrc), "ovf-testcases/ovf-dummy.vmdk");
304
305 char szDst[RTPATH_MAX];
306 RTPathExecDir(szDst, sizeof(szDst));
307 RTPathAppend(szDst, sizeof(szDst), pcszDest);
308 RTPrintf("%s: copying ovf-dummy.vmdk to \"%s\"...\n", pcszPrefix, pcszDest);
309
310 /* Delete the destination file if it exists or RTFileCopy will fail. */
311 if (RTFileExists(szDst))
312 {
313 RTPrintf("Deleting file %s...\n", szDst);
314 RTFileDelete(szDst);
315 }
316
317 int vrc = RTFileCopy(szSrc, szDst);
318 if (RT_FAILURE(vrc)) throw MyError(0, Utf8StrFmt("Cannot copy ovf-dummy.vmdk to %s: %Rra\n", pcszDest, vrc).c_str());
319 llFiles2Delete.push_back(szDst);
320}
321
322/**
323 *
324 * @param argc
325 * @param argv[]
326 * @return
327 */
328int main(int argc, char *argv[])
329{
330 RTR3InitExe(argc, &argv, 0);
331
332 /** @todo r=aeichner: This should work for x86 machines on ARM platforms as well.
333 * Currently we get:
334 * tstOVF: error: x86-specific platform settings are not available on this platform
335 * tstOVF: error: Details: code VBOX_E_PLATFORM_ARCH_NOT_SUPPORTED (0x80bb0012), component PlatformWrap, interface IPlatform
336 * */
337
338 RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
339 HRESULT hrc = S_OK;
340
341 std::list<Utf8Str> llFiles2Delete;
342 std::list<Guid> llMachinesCreated;
343
344 ComPtr<IVirtualBoxClient> pVirtualBoxClient;
345 ComPtr<IVirtualBox> pVirtualBox;
346
347 try
348 {
349 RTPrintf("Initializing COM...\n");
350 hrc = com::Initialize();
351 if (FAILED(hrc)) throw MyError(hrc, "failed to initialize COM!\n");
352
353 ComPtr<ISession> pSession;
354
355 RTPrintf("Creating VirtualBox object...\n");
356 hrc = pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
357 if (SUCCEEDED(hrc))
358 hrc = pVirtualBoxClient->COMGETTER(VirtualBox)(pVirtualBox.asOutParam());
359 if (FAILED(hrc)) throw MyError(hrc, "failed to create the VirtualBox object!\n");
360
361 hrc = pSession.createInprocObject(CLSID_Session);
362 if (FAILED(hrc)) throw MyError(hrc, "failed to create a session object!\n");
363
364 // for each testcase, we will copy the dummy VMDK image to the subdirectory with the OVF testcase
365 // so that the import will find the disks it expects; this is just for testing the import since
366 // the imported machines will obviously not be usable.
367 // llFiles2Delete receives the paths of all the files that we need to clean up later.
368
369 // testcase 1: import ovf-joomla-0.9/joomla-1.1.4-ovf.ovf
370 copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
371 copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
372 importOVF("joomla-0.9", pVirtualBox, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf.ovf", llMachinesCreated);
373
374 // testcase 2: import ovf-winxp-vbox-sharedfolders/winxp.ovf
375 copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/Windows 5.1 XP 1 merged.vmdk");
376 copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/smallvdi.vmdk");
377 importOVF("winxp-vbox-sharedfolders", pVirtualBox, "ovf-testcases/ovf-winxp-vbox-sharedfolders/winxp.ovf", llMachinesCreated);
378
379 // testcase 3: import ovf-winxp-vbox-sharedfolders/winxp.ovf
380 importOVF("winhost-audio-nodisks", pVirtualBox, "ovf-testcases/ovf-winhost-audio-nodisks/WinXP.ovf", llMachinesCreated);
381
382 RTPrintf("Machine imports done, no errors. Cleaning up...\n");
383 }
384 catch (MyError &e)
385 {
386 RTPrintf("%s", e.m_str.c_str());
387 rcExit = RTEXITCODE_FAILURE;
388 }
389
390 try
391 {
392 // clean up the machines created
393 for (std::list<Guid>::const_iterator it = llMachinesCreated.begin();
394 it != llMachinesCreated.end();
395 ++it)
396 {
397 const Guid &uuid = *it;
398 Bstr bstrUUID(uuid.toUtf16());
399 ComPtr<IMachine> pMachine;
400 hrc = pVirtualBox->FindMachine(bstrUUID.raw(), pMachine.asOutParam());
401 if (FAILED(hrc)) throw MyError(hrc, "VirtualBox::FindMachine() failed\n");
402
403 RTPrintf(" Deleting machine %ls...\n", bstrUUID.raw());
404 SafeIfaceArray<IMedium> sfaMedia;
405 hrc = pMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
406 ComSafeArrayAsOutParam(sfaMedia));
407 if (FAILED(hrc)) throw MyError(hrc, "Machine::Unregister() failed\n");
408
409 ComPtr<IProgress> pProgress;
410 hrc = pMachine->DeleteConfig(ComSafeArrayAsInParam(sfaMedia), pProgress.asOutParam());
411 if (FAILED(hrc)) throw MyError(hrc, "Machine::DeleteSettings() failed\n");
412 hrc = pProgress->WaitForCompletion(-1);
413 if (FAILED(hrc)) throw MyError(hrc, "Progress::WaitForCompletion() failed\n");
414 }
415 }
416 catch (MyError &e)
417 {
418 RTPrintf("%s", e.m_str.c_str());
419 rcExit = RTEXITCODE_FAILURE;
420 }
421
422 // clean up the VMDK copies that we made in copyDummyDiskImage()
423 for (std::list<Utf8Str>::const_iterator it = llFiles2Delete.begin();
424 it != llFiles2Delete.end();
425 ++it)
426 {
427 const Utf8Str &strFile = *it;
428 RTPrintf("Deleting file %s...\n", strFile.c_str());
429 RTFileDelete(strFile.c_str());
430 }
431
432 pVirtualBox.setNull();
433 pVirtualBoxClient.setNull();
434
435 RTPrintf("Shutting down COM...\n");
436 com::Shutdown();
437 RTPrintf("tstOVF all done: %s\n", rcExit ? "ERROR" : "SUCCESS");
438
439 return rcExit;
440}
441
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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