webtest.cpp revision f6fd812d35e1100931d67e2f37b0e54ec3e91c6a
355b4669e025ff377602b6fc7caaf30dbc218371jacobs/*
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * webtest.cpp:
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * demo webservice client in C++. This mimics some of the
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * functionality of VBoxManage for testing purposes.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Copyright (C) 2006-2009 Sun Microsystems, Inc.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * This file is part of VirtualBox Open Source Edition (OSE), as
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * available from http://www.virtualbox.org. This file is free software;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * you can redistribute it and/or modify it under the terms of the GNU
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * General Public License (GPL) as published by the Free Software
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Foundation, in version 2 as it comes in the "COPYING" file of the
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs *
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * Clara, CA 95054 USA or visit http://www.sun.com if you need
355b4669e025ff377602b6fc7caaf30dbc218371jacobs * additional information or have any questions.
355b4669e025ff377602b6fc7caaf30dbc218371jacobs */
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs// gSOAP headers (must come after vbox includes because it checks for conflicting defs)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include "soapStub.h"
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami
355b4669e025ff377602b6fc7caaf30dbc218371jacobs// include generated namespaces table
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include "vboxwebsrv.nsmap"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <iostream>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <sstream>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs#include <string>
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami/**
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami *
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami * @param argc
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami * @param argv[]
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami * @return
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami */
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahramiint main(int argc, char* argv[])
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami{
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami struct soap soap; // gSOAP runtime environment
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami soap_init(&soap); // initialize runtime environment (only once)
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami
bfed486ad8de8b8ebc6345a8e10accae08bf2f45Ali Bahrami if (argc < 2)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs std::cout <<
355b4669e025ff377602b6fc7caaf30dbc218371jacobs "webtest: VirtualBox webservice testcase.\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs "Usage:\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - IWebsessionManager:\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest logon <user> <pass>: IWebsessionManager::logon().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest getsession <vboxref>: IWebsessionManager::getSessionObject().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest logoff <vboxref>: IWebsessionManager::logoff().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - IVirtualBox:\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest version <vboxref>: IVirtualBox::getVersion().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest gethost <vboxref>: IVirtualBox::getHost().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest getmachines <vboxref>: IVirtualBox::getMachines().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest createmachine <vboxref> <baseFolder> <name>: IVirtualBox::createMachine().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest registermachine <vboxref> <machineref>: IVirtualBox::registerMachine().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - IHost:\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest getdvddrives <hostref>: IHost::getDVDDrives.\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - IHostDVDDrive:\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest getdvdname <dvdref>: IHostDVDDrive::getname.\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - IMachine:\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest getname <machineref>: IMachine::getName().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest getid <machineref>: IMachine::getId().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest getostype <machineref>: IMachine::getGuestOSType().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest savesettings <machineref>: IMachine::saveSettings().\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - All managed object references:\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest getif <ref>: report interface of object.\n"
355b4669e025ff377602b6fc7caaf30dbc218371jacobs " - webtest release <ref>: IUnknown::Release().\n";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs exit(1);
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs const char *pcszArgEndpoint = "localhost:18083";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs const char *pcszMode = argv[1];
355b4669e025ff377602b6fc7caaf30dbc218371jacobs int soaprc = 2;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (!strcmp(pcszMode, "logon"))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (argc < 4)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs else
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IWebsessionManager_USCORElogon req;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs req.username = argv[2];
355b4669e025ff377602b6fc7caaf30dbc218371jacobs req.password = argv[3];
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IWebsessionManager_USCORElogonResponse resp;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (!(soaprc = soap_call___vbox__IWebsessionManager_USCORElogon(&soap,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs pcszArgEndpoint,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs NULL,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &req,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &resp)))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs std::cout << "VirtualBox objref: \"" << resp.returnval << "\"\n";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs else if (!strcmp(pcszMode, "getsession"))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (argc < 3)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs else
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IWebsessionManager_USCOREgetSessionObject req;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs req.refIVirtualBox = argv[2];
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IWebsessionManager_USCOREgetSessionObjectResponse resp;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (!(soaprc = soap_call___vbox__IWebsessionManager_USCOREgetSessionObject(&soap,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs pcszArgEndpoint,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs NULL,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &req,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &resp)))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs std::cout << "session: \"" << resp.returnval << "\"\n";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs else if (!strcmp(pcszMode, "logoff"))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (argc < 3)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs else
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IWebsessionManager_USCORElogoff req;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs req.refIVirtualBox = argv[2];
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IWebsessionManager_USCORElogoffResponse resp;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (!(soaprc = soap_call___vbox__IWebsessionManager_USCORElogoff(&soap,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs pcszArgEndpoint,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs NULL,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &req,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &resp)))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs ;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs else if (!strcmp(pcszMode, "version"))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (argc < 3)
355b4669e025ff377602b6fc7caaf30dbc218371jacobs std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs else
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IVirtualBox_USCOREgetVersion req;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs req._USCOREthis = argv[2];
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IVirtualBox_USCOREgetVersionResponse resp;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetVersion(&soap,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs pcszArgEndpoint,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs NULL,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &req,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &resp)))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs std::cout << "version: \"" << resp.returnval << "\"\n";
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs }
355b4669e025ff377602b6fc7caaf30dbc218371jacobs else if (!strcmp(pcszMode, "gethost"))
355b4669e025ff377602b6fc7caaf30dbc218371jacobs {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (argc < 3)
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
0a44ef6d9afbfe052a7e975f55ea0d2954b62a82jacobs else
a18dc42fc967d11feba9b8be61c6727dc6c56b48ps {
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IVirtualBox_USCOREgetHost req;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs req._USCOREthis = argv[2];
355b4669e025ff377602b6fc7caaf30dbc218371jacobs _vbox__IVirtualBox_USCOREgetHostResponse resp;
355b4669e025ff377602b6fc7caaf30dbc218371jacobs
355b4669e025ff377602b6fc7caaf30dbc218371jacobs if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetHost(&soap,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs pcszArgEndpoint,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs NULL,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &req,
355b4669e025ff377602b6fc7caaf30dbc218371jacobs &resp)))
{
std::cout << "Host objref " << resp.returnval << "\n";
}
}
}
else if (!strcmp(pcszMode, "getmachines"))
{
if (argc < 3)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IVirtualBox_USCOREgetMachines req;
req._USCOREthis = argv[2];
_vbox__IVirtualBox_USCOREgetMachinesResponse resp;
if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetMachines(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
{
size_t c = resp.returnval.size();
for (size_t i = 0;
i < c;
++i)
{
std::cout << "Machine " << i << ": objref " << resp.returnval[i] << "\n";
}
}
}
}
else if (!strcmp(pcszMode, "createmachine"))
{
if (argc < 5)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IVirtualBox_USCOREcreateMachine req;
req._USCOREthis = argv[2];
req.baseFolder = argv[3];
req.name = argv[4];
std::cout << "createmachine: baseFolder = \"" << req.baseFolder << "\", name = \"" << req.name << "\"\n";
_vbox__IVirtualBox_USCOREcreateMachineResponse resp;
if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREcreateMachine(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
std::cout << "Machine created: managed object reference ID is " << resp.returnval << "\n";
}
}
else if (!strcmp(pcszMode, "registermachine"))
{
if (argc < 4)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IVirtualBox_USCOREregisterMachine req;
req._USCOREthis = argv[2];
req.machine = argv[3];
_vbox__IVirtualBox_USCOREregisterMachineResponse resp;
if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREregisterMachine(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
std::cout << "Machine registered.\n";
}
}
else if (!strcmp(pcszMode, "getdvddrives"))
{
if (argc < 3)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IHost_USCOREgetDVDDrives req;
req._USCOREthis = argv[2];
_vbox__IHost_USCOREgetDVDDrivesResponse resp;
if (!(soaprc = soap_call___vbox__IHost_USCOREgetDVDDrives(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
{
size_t c = resp.returnval.size();
for (size_t i = 0;
i < c;
++i)
{
std::cout << "DVD drive " << i << ": objref " << resp.returnval[i] << "\n";
}
}
}
}
else if (!strcmp(pcszMode, "getdvdname"))
{
if (argc < 3)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IHostDVDDrive_USCOREgetName req;
req._USCOREthis = argv[2];
_vbox__IHostDVDDrive_USCOREgetNameResponse resp;
if (!(soaprc = soap_call___vbox__IHostDVDDrive_USCOREgetName(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
std::cout << "Name is: \"" << resp.returnval << "\"\n";
}
}
else if (!strcmp(pcszMode, "getname"))
{
if (argc < 3)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IMachine_USCOREgetName req;
req._USCOREthis = argv[2];
_vbox__IMachine_USCOREgetNameResponse resp;
if (!(soaprc = soap_call___vbox__IMachine_USCOREgetName(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
printf("Name is: %s\n", resp.returnval.c_str());
}
}
else if (!strcmp(pcszMode, "getid"))
{
if (argc < 3)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IMachine_USCOREgetId req;
req._USCOREthis = argv[2];
_vbox__IMachine_USCOREgetIdResponse resp;
if (!(soaprc = soap_call___vbox__IMachine_USCOREgetId(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
std::cout << "UUID is: " << resp.returnval << "\n";;
}
}
else if (!strcmp(pcszMode, "getostypeid"))
{
if (argc < 3)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IMachine_USCOREgetOSTypeId req;
req._USCOREthis = argv[2];
_vbox__IMachine_USCOREgetOSTypeIdResponse resp;
if (!(soaprc = soap_call___vbox__IMachine_USCOREgetOSTypeId(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
std::cout << "Guest OS type is: " << resp.returnval << "\n";
}
}
else if (!strcmp(pcszMode, "savesettings"))
{
if (argc < 3)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IMachine_USCOREsaveSettings req;
req._USCOREthis = argv[2];
_vbox__IMachine_USCOREsaveSettingsResponse resp;
if (!(soaprc = soap_call___vbox__IMachine_USCOREsaveSettings(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
std::cout << "Settings saved\n";
}
}
else if (!strcmp(pcszMode, "release"))
{
if (argc < 3)
std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
else
{
_vbox__IManagedObjectRef_USCORErelease req;
req._USCOREthis = argv[2];
_vbox__IManagedObjectRef_USCOREreleaseResponse resp;
if (!(soaprc = soap_call___vbox__IManagedObjectRef_USCORErelease(&soap,
pcszArgEndpoint,
NULL,
&req,
&resp)))
std::cout << "Managed object reference " << req._USCOREthis << " released.\n";
}
}
else
std::cout << "Unknown mode parameter \"" << pcszMode << "\".\n";
if (soaprc)
{
if ( (soap.fault)
&& (soap.fault->detail)
)
{
if (soap.fault->detail->vbox__InvalidObjectFault)
{
std::cout << "Bad object ID: " << soap.fault->detail->vbox__InvalidObjectFault->badObjectID << "\n";
}
if (soap.fault->detail->vbox__RuntimeFault)
{
std::cout << "Result code: 0x" << std::hex << soap.fault->detail->vbox__RuntimeFault->resultCode << "\n";
std::cout << "Text: " << std::hex << soap.fault->detail->vbox__RuntimeFault->text << "\n";
std::cout << "Component: " << std::hex << soap.fault->detail->vbox__RuntimeFault->component << "\n";
std::cout << "Interface ID: " << std::hex << soap.fault->detail->vbox__RuntimeFault->interfaceID << "\n";
}
}
else
{
std::cerr << "Invalid fault data, fault message:\n";
soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream
}
}
soap_destroy(&soap); // delete deserialized class instances (for C++ only)
soap_end(&soap); // remove deserialized data and clean up
soap_done(&soap); // detach the gSOAP environment
return soaprc;
}