6739cf90e4aa3b3344768b8da241802f80ff455cvboxsync/* $Id$ */
72e560433c691132e8f21f463955515d4511c34bvboxsync/** @file
72e560433c691132e8f21f463955515d4511c34bvboxsync *
72e560433c691132e8f21f463955515d4511c34bvboxsync * tstOVF - testcases for OVF import and export
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/*
438870735e3606c923d51fce19d9fc3b6ebffde1vboxsync * Copyright (C) 2010-2014 Oracle Corporation
72e560433c691132e8f21f463955515d4511c34bvboxsync *
72e560433c691132e8f21f463955515d4511c34bvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
72e560433c691132e8f21f463955515d4511c34bvboxsync * available from http://www.virtualbox.org. This file is free software;
72e560433c691132e8f21f463955515d4511c34bvboxsync * you can redistribute it and/or modify it under the terms of the GNU
72e560433c691132e8f21f463955515d4511c34bvboxsync * General Public License (GPL) as published by the Free Software
72e560433c691132e8f21f463955515d4511c34bvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
72e560433c691132e8f21f463955515d4511c34bvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
72e560433c691132e8f21f463955515d4511c34bvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <VBox/com/VirtualBox.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <VBox/com/com.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <VBox/com/array.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <VBox/com/string.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <VBox/com/ErrorInfo.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <VBox/com/errorprint.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/initterm.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/stream.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/file.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/path.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <iprt/param.h>
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync#include <list>
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsyncusing namespace com;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync// main
72e560433c691132e8f21f463955515d4511c34bvboxsync///////////////////////////////////////////////////////////////////////////////
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/**
72e560433c691132e8f21f463955515d4511c34bvboxsync * Quick hack exception structure.
72e560433c691132e8f21f463955515d4511c34bvboxsync *
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsyncstruct MyError
72e560433c691132e8f21f463955515d4511c34bvboxsync{
72e560433c691132e8f21f463955515d4511c34bvboxsync MyError(HRESULT rc,
72e560433c691132e8f21f463955515d4511c34bvboxsync const char *pcsz,
72e560433c691132e8f21f463955515d4511c34bvboxsync IProgress *pProgress = NULL)
72e560433c691132e8f21f463955515d4511c34bvboxsync : m_rc(rc)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync m_str = "ERROR: ";
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync m_str += pcsz;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync if (pProgress)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync com::ProgressErrorInfo info(pProgress);
72e560433c691132e8f21f463955515d4511c34bvboxsync com::GluePrintErrorInfo(info);
72e560433c691132e8f21f463955515d4511c34bvboxsync }
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync else if (rc)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync com::ErrorInfo info;
72e560433c691132e8f21f463955515d4511c34bvboxsync if (!info.isFullAvailable() && !info.isBasicAvailable())
72e560433c691132e8f21f463955515d4511c34bvboxsync com::GluePrintRCMessage(rc);
72e560433c691132e8f21f463955515d4511c34bvboxsync else
72e560433c691132e8f21f463955515d4511c34bvboxsync com::GluePrintErrorInfo(info);
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync Utf8Str m_str;
72e560433c691132e8f21f463955515d4511c34bvboxsync HRESULT m_rc;
72e560433c691132e8f21f463955515d4511c34bvboxsync};
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/**
72e560433c691132e8f21f463955515d4511c34bvboxsync * Imports the given OVF file, with all bells and whistles.
72e560433c691132e8f21f463955515d4511c34bvboxsync * Throws MyError on errors.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * @param pcszPrefix Descriptive short prefix string for console output.
72e560433c691132e8f21f463955515d4511c34bvboxsync * @param pVirtualBox VirtualBox instance.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * @param pcszOVF0 File to import.
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync * @param llMachinesCreated out: UUIDs of machines that were created so that caller can clean up.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsyncvoid importOVF(const char *pcszPrefix,
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync ComPtr<IVirtualBox> &pVirtualBox,
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync const char *pcszOVF0,
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync std::list<Guid> &llMachinesCreated)
72e560433c691132e8f21f463955515d4511c34bvboxsync{
72e560433c691132e8f21f463955515d4511c34bvboxsync char szAbsOVF[RTPATH_MAX];
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync RTPathExecDir(szAbsOVF, sizeof(szAbsOVF));
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync RTPathAppend(szAbsOVF, sizeof(szAbsOVF), pcszOVF0);
72e560433c691132e8f21f463955515d4511c34bvboxsync
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync RTPrintf("%s: reading appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
72e560433c691132e8f21f463955515d4511c34bvboxsync ComPtr<IAppliance> pAppl;
72e560433c691132e8f21f463955515d4511c34bvboxsync HRESULT rc = pVirtualBox->CreateAppliance(pAppl.asOutParam());
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "failed to create appliance\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync ComPtr<IProgress> pProgress;
1207f59aa62006952dbb0bf7700decf34d8caeb2vboxsync rc = pAppl->Read(Bstr(szAbsOVF).raw(), pProgress.asOutParam());
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "Appliance::Read() failed\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = pProgress->WaitForCompletion(-1);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync LONG rc2;
72e560433c691132e8f21f463955515d4511c34bvboxsync pProgress->COMGETTER(ResultCode)(&rc2);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc2)) throw MyError(rc2, "Appliance::Read() failed\n", pProgress);
72e560433c691132e8f21f463955515d4511c34bvboxsync
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync RTPrintf("%s: interpreting appliance \"%s\"...\n", pcszPrefix, szAbsOVF);
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = pAppl->Interpret();
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "Appliance::Interpret() failed\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync com::SafeIfaceArray<IVirtualSystemDescription> aDescriptions;
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = pAppl->COMGETTER(VirtualSystemDescriptions)(ComSafeArrayAsOutParam(aDescriptions));
72e560433c691132e8f21f463955515d4511c34bvboxsync for (uint32_t u = 0;
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync u < aDescriptions.size();
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync ++u)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync ComPtr<IVirtualSystemDescription> pVSys = aDescriptions[u];
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync com::SafeArray<VirtualSystemDescriptionType_T> aTypes;
72e560433c691132e8f21f463955515d4511c34bvboxsync com::SafeArray<BSTR> aRefs;
72e560433c691132e8f21f463955515d4511c34bvboxsync com::SafeArray<BSTR> aOvfValues;
1468b39c8bba83821c9a88039ead6315de21da51vboxsync com::SafeArray<BSTR> aVBoxValues;
72e560433c691132e8f21f463955515d4511c34bvboxsync com::SafeArray<BSTR> aExtraConfigValues;
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = pVSys->GetDescription(ComSafeArrayAsOutParam(aTypes),
72e560433c691132e8f21f463955515d4511c34bvboxsync ComSafeArrayAsOutParam(aRefs),
72e560433c691132e8f21f463955515d4511c34bvboxsync ComSafeArrayAsOutParam(aOvfValues),
1468b39c8bba83821c9a88039ead6315de21da51vboxsync ComSafeArrayAsOutParam(aVBoxValues),
72e560433c691132e8f21f463955515d4511c34bvboxsync ComSafeArrayAsOutParam(aExtraConfigValues));
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "VirtualSystemDescription::GetDescription() failed\n");
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync for (uint32_t u2 = 0;
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync u2 < aTypes.size();
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync ++u2)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync const char *pcszType;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync VirtualSystemDescriptionType_T t = aTypes[u2];
72e560433c691132e8f21f463955515d4511c34bvboxsync switch (t)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_OS:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "ostype";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_Name:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "name";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_Product:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "product";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_ProductUrl:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "producturl";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_Vendor:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "vendor";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_VendorUrl:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "vendorurl";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_Version:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "version";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_Description:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "description";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_License:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "license";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_CPU:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "cpu";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_Memory:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "memory";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_HardDiskControllerIDE:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "ide";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_HardDiskControllerSATA:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "sata";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_HardDiskControllerSAS:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "sas";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_HardDiskControllerSCSI:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "scsi";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_HardDiskImage:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "hd";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_CDROM:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "cdrom";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_Floppy:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "floppy";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_NetworkAdapter:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "net";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_USBController:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "usb";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync case VirtualSystemDescriptionType_SoundCard:
72e560433c691132e8f21f463955515d4511c34bvboxsync pcszType = "sound";
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync default:
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync throw MyError(E_UNEXPECTED, "Invalid VirtualSystemDescriptionType\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync break;
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPrintf(" vsys %2u item %2u: type %2d (%s), ovf: \"%ls\", vbox: \"%ls\", extra: \"%ls\"\n",
72e560433c691132e8f21f463955515d4511c34bvboxsync u, u2, t, pcszType,
72e560433c691132e8f21f463955515d4511c34bvboxsync aOvfValues[u2],
1468b39c8bba83821c9a88039ead6315de21da51vboxsync aVBoxValues[u2],
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync aExtraConfigValues[u2]);
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync RTPrintf("%s: importing %d machine(s)...\n", pcszPrefix, aDescriptions.size());
8ae161fca703d669e1306fafee128914f831f72bvboxsync SafeArray<ImportOptions_T> sfaOptions;
8ae161fca703d669e1306fafee128914f831f72bvboxsync rc = pAppl->ImportMachines(ComSafeArrayAsInParam(sfaOptions), pProgress.asOutParam());
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "Appliance::ImportMachines() failed\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = pProgress->WaitForCompletion(-1);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync pProgress->COMGETTER(ResultCode)(&rc2);
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync if (FAILED(rc2)) throw MyError(rc2, "Appliance::ImportMachines() failed\n", pProgress);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync com::SafeArray<BSTR> aMachineUUIDs;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync rc = pAppl->COMGETTER(Machines)(ComSafeArrayAsOutParam(aMachineUUIDs));
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "Appliance::GetMachines() failed\n");
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync for (size_t u = 0;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync u < aMachineUUIDs.size();
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync ++u)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync {
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync RTPrintf("%s: created machine %u: %ls\n", pcszPrefix, u, aMachineUUIDs[u]);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync llMachinesCreated.push_back(Guid(Bstr(aMachineUUIDs[u])));
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync }
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync RTPrintf("%s: success!\n", pcszPrefix);
72e560433c691132e8f21f463955515d4511c34bvboxsync}
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/**
72e560433c691132e8f21f463955515d4511c34bvboxsync * Copies ovf-testcases/ovf-dummy.vmdk to the given target and appends that
72e560433c691132e8f21f463955515d4511c34bvboxsync * target as a string to the given list so that the caller can delete it
72e560433c691132e8f21f463955515d4511c34bvboxsync * again later.
72e560433c691132e8f21f463955515d4511c34bvboxsync * @param llFiles2Delete List of strings to append the target file path to.
72e560433c691132e8f21f463955515d4511c34bvboxsync * @param pcszDest Target for dummy VMDK.
72e560433c691132e8f21f463955515d4511c34bvboxsync */
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsyncvoid copyDummyDiskImage(const char *pcszPrefix,
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync std::list<Utf8Str> &llFiles2Delete,
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync const char *pcszDest)
72e560433c691132e8f21f463955515d4511c34bvboxsync{
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync char szSrc[RTPATH_MAX];
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync char szDst[RTPATH_MAX];
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync RTPathExecDir(szSrc, sizeof(szSrc));
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync RTPathAppend(szSrc, sizeof(szSrc), "ovf-testcases/ovf-dummy.vmdk");
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync RTPathExecDir(szDst, sizeof(szDst));
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync RTPathAppend(szDst, sizeof(szDst), pcszDest);
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync RTPrintf("%s: copying ovf-dummy.vmdk to \"%s\"...\n", pcszPrefix, pcszDest);
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync int vrc = RTFileCopy(szSrc, szDst);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (RT_FAILURE(vrc)) throw MyError(0, Utf8StrFmt("Cannot copy ovf-dummy.vmdk to %s: %Rra\n", pcszDest, vrc).c_str());
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync llFiles2Delete.push_back(szDst);
72e560433c691132e8f21f463955515d4511c34bvboxsync}
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync/**
72e560433c691132e8f21f463955515d4511c34bvboxsync *
72e560433c691132e8f21f463955515d4511c34bvboxsync * @param argc
72e560433c691132e8f21f463955515d4511c34bvboxsync * @param argv[]
72e560433c691132e8f21f463955515d4511c34bvboxsync * @return
72e560433c691132e8f21f463955515d4511c34bvboxsync */
72e560433c691132e8f21f463955515d4511c34bvboxsyncint main(int argc, char *argv[])
72e560433c691132e8f21f463955515d4511c34bvboxsync{
230bd8589bba39933ac5ec21482d6186d675e604vboxsync RTR3InitExe(argc, &argv, 0);
72e560433c691132e8f21f463955515d4511c34bvboxsync
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync RTEXITCODE rcExit = RTEXITCODE_SUCCESS;
72e560433c691132e8f21f463955515d4511c34bvboxsync HRESULT rc = S_OK;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync std::list<Utf8Str> llFiles2Delete;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync std::list<Guid> llMachinesCreated;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync ComPtr<IVirtualBox> pVirtualBox;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync try
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPrintf("Initializing COM...\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = com::Initialize();
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "failed to initialize COM!\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync ComPtr<ISession> pSession;
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPrintf("Creating VirtualBox object...\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = pVirtualBox.createLocalObject(CLSID_VirtualBox);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "failed to create the VirtualBox object!\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = pSession.createInprocObject(CLSID_Session);
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync if (FAILED(rc)) throw MyError(rc, "failed to create a session object!\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync // for each testcase, we will copy the dummy VMDK image to the subdirectory with the OVF testcase
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync // so that the import will find the disks it expects; this is just for testing the import since
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync // the imported machines will obviously not be usable.
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync // llFiles2Delete receives the paths of all the files that we need to clean up later.
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync // testcase 1: import ovf-joomla-0.9/joomla-1.1.4-ovf.ovf
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-0.vmdk");
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync copyDummyDiskImage("joomla-0.9", llFiles2Delete, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf-1.vmdk");
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync importOVF("joomla-0.9", pVirtualBox, "ovf-testcases/ovf-joomla-0.9/joomla-1.1.4-ovf.ovf", llMachinesCreated);
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync // testcase 2: import ovf-winxp-vbox-sharedfolders/winxp.ovf
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/Windows 5.1 XP 1 merged.vmdk");
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync copyDummyDiskImage("winxp-vbox-sharedfolders", llFiles2Delete, "ovf-testcases/ovf-winxp-vbox-sharedfolders/smallvdi.vmdk");
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync importOVF("winxp-vbox-sharedfolders", pVirtualBox, "ovf-testcases/ovf-winxp-vbox-sharedfolders/winxp.ovf", llMachinesCreated);
72e560433c691132e8f21f463955515d4511c34bvboxsync
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync // testcase 3: import ovf-winxp-vbox-sharedfolders/winxp.ovf
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync importOVF("winhost-audio-nodisks", pVirtualBox, "ovf-testcases/ovf-winhost-audio-nodisks/WinXP.ovf", llMachinesCreated);
2534dba6160d58bc4f081f2cd8ebf06b523b8713vboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync RTPrintf("Machine imports done, no errors. Cleaning up...\n");
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync }
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync catch (MyError &e)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync {
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync rc = e.m_rc;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync RTPrintf("%s", e.m_str.c_str());
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync rcExit = RTEXITCODE_FAILURE;
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync try
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync {
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync // clean up the machines created
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync for (std::list<Guid>::const_iterator it = llMachinesCreated.begin();
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync it != llMachinesCreated.end();
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync ++it)
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync {
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync const Guid &uuid = *it;
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync Bstr bstrUUID(uuid.toUtf16());
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync ComPtr<IMachine> pMachine;
508452243fd3328f7b9e0405d39fb9dc004e31b8vboxsync rc = pVirtualBox->FindMachine(bstrUUID.raw(), pMachine.asOutParam());
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync if (FAILED(rc)) throw MyError(rc, "VirtualBox::FindMachine() failed\n");
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync RTPrintf(" Deleting machine %ls...\n", bstrUUID.raw());
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync SafeIfaceArray<IMedium> sfaMedia;
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync rc = pMachine->Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync ComSafeArrayAsOutParam(sfaMedia));
2d7e2a75f9939e8bfd6f19cfb507bb57d86b3be1vboxsync if (FAILED(rc)) throw MyError(rc, "Machine::Unregister() failed\n");
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync ComPtr<IProgress> pProgress;
48b14aaf804d764e2c7bec8243c379f7ff489ebbvboxsync rc = pMachine->DeleteConfig(ComSafeArrayAsInParam(sfaMedia), pProgress.asOutParam());
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync if (FAILED(rc)) throw MyError(rc, "Machine::DeleteSettings() failed\n");
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync rc = pProgress->WaitForCompletion(-1);
cc9b7957f36eef072546fb6e528a6e437b8e1c77vboxsync if (FAILED(rc)) throw MyError(rc, "Progress::WaitForCompletion() failed\n");
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync catch (MyError &e)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync rc = e.m_rc;
d8b658a6b59104fdf0a35e569136f9b42fcf672dvboxsync RTPrintf("%s", e.m_str.c_str());
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync rcExit = RTEXITCODE_FAILURE;
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync // clean up the VMDK copies that we made in copyDummyDiskImage()
72e560433c691132e8f21f463955515d4511c34bvboxsync for (std::list<Utf8Str>::const_iterator it = llFiles2Delete.begin();
72e560433c691132e8f21f463955515d4511c34bvboxsync it != llFiles2Delete.end();
72e560433c691132e8f21f463955515d4511c34bvboxsync ++it)
72e560433c691132e8f21f463955515d4511c34bvboxsync {
72e560433c691132e8f21f463955515d4511c34bvboxsync const Utf8Str &strFile = *it;
ceba829c00a67e9dcf7b27a986735a6764bb785evboxsync RTPrintf("Deleting file %s...\n", strFile.c_str());
72e560433c691132e8f21f463955515d4511c34bvboxsync RTFileDelete(strFile.c_str());
72e560433c691132e8f21f463955515d4511c34bvboxsync }
72e560433c691132e8f21f463955515d4511c34bvboxsync
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync pVirtualBox.setNull();
22ea904a6fe5c95f54c4374502747cc844ce8204vboxsync
72e560433c691132e8f21f463955515d4511c34bvboxsync RTPrintf("Shutting down COM...\n");
72e560433c691132e8f21f463955515d4511c34bvboxsync com::Shutdown();
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync RTPrintf("tstOVF all done: %s\n", rcExit ? "ERROR" : "SUCCESS");
72e560433c691132e8f21f463955515d4511c34bvboxsync
561a60db0f5f745ce80967b0867e5f605893fd29vboxsync return rcExit;
72e560433c691132e8f21f463955515d4511c34bvboxsync}
72e560433c691132e8f21f463955515d4511c34bvboxsync