1 | /*
|
---|
2 | * webtest.cpp:
|
---|
3 | * demo webservice client in C++. This mimics some of the
|
---|
4 | * functionality of VBoxManage for testing purposes.
|
---|
5 | *
|
---|
6 | * Copyright (C) 2006-2009 Sun Microsystems, Inc.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
17 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
18 | * additional information or have any questions.
|
---|
19 | */
|
---|
20 |
|
---|
21 | // gSOAP headers (must come after vbox includes because it checks for conflicting defs)
|
---|
22 | #include "soapStub.h"
|
---|
23 |
|
---|
24 | // include generated namespaces table
|
---|
25 | #include "vboxwebsrv.nsmap"
|
---|
26 |
|
---|
27 | #include <iostream>
|
---|
28 | #include <sstream>
|
---|
29 | #include <string>
|
---|
30 |
|
---|
31 |
|
---|
32 | /**
|
---|
33 | *
|
---|
34 | * @param argc
|
---|
35 | * @param argv[]
|
---|
36 | * @return
|
---|
37 | */
|
---|
38 | int main(int argc, char* argv[])
|
---|
39 | {
|
---|
40 | struct soap soap; // gSOAP runtime environment
|
---|
41 | soap_init(&soap); // initialize runtime environment (only once)
|
---|
42 |
|
---|
43 | if (argc < 2)
|
---|
44 | {
|
---|
45 | std::cout <<
|
---|
46 | "webtest: VirtualBox webservice testcase.\n"
|
---|
47 | "Usage:\n"
|
---|
48 | " - IWebsessionManager:\n"
|
---|
49 | " - webtest logon <user> <pass>: IWebsessionManage::logon().\n"
|
---|
50 | " - webtest getsession <vboxref>: IWebsessionManage::getSessionObject().\n"
|
---|
51 | " - IVirtualBox:\n"
|
---|
52 | " - webtest version <vboxref>: IVirtualBox::getVersion().\n"
|
---|
53 | " - webtest gethost <vboxref>: IVirtualBox::getHost().\n"
|
---|
54 | " - webtest getmachines <vboxref>: IVirtualBox::getMachines().\n"
|
---|
55 | " - webtest createmachine <vboxref> <baseFolder> <name>: IVirtualBox::createMachine().\n"
|
---|
56 | " - webtest registermachine <vboxref> <machineref>: IVirtualBox::registerMachine().\n"
|
---|
57 | " - IHost:\n"
|
---|
58 | " - webtest getdvddrives <hostref>: IHost::getDVDDrives.\n"
|
---|
59 | " - IHostDVDDrive:\n"
|
---|
60 | " - webtest getdvdname <dvdref>: IHostDVDDrive::getname.\n"
|
---|
61 | " - IMachine:\n"
|
---|
62 | " - webtest getname <machineref>: IMachine::getName().\n"
|
---|
63 | " - webtest getid <machineref>: IMachine::getId().\n"
|
---|
64 | " - webtest getostype <machineref>: IMachine::getGuestOSType().\n"
|
---|
65 | " - webtest savesettings <machineref>: IMachine::saveSettings().\n"
|
---|
66 | " - All managed object references:\n"
|
---|
67 | " - webtest getif <ref>: report interface of object.\n"
|
---|
68 | " - webtest release <ref>: IUnknown::Release().\n";
|
---|
69 | exit(1);
|
---|
70 | }
|
---|
71 |
|
---|
72 | const char *pcszArgEndpoint = "localhost:18083";
|
---|
73 |
|
---|
74 | const char *pcszMode = argv[1];
|
---|
75 | int soaprc = 2;
|
---|
76 |
|
---|
77 | if (!strcmp(pcszMode, "logon"))
|
---|
78 | {
|
---|
79 | if (argc < 4)
|
---|
80 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
81 | else
|
---|
82 | {
|
---|
83 | _vbox__IWebsessionManager_USCORElogon req;
|
---|
84 | req.username = argv[2];
|
---|
85 | req.password = argv[3];
|
---|
86 | std::cout << "logon: user = \"" << req.username << "\", pass = \"" << req.password << "\"\n";
|
---|
87 | _vbox__IWebsessionManager_USCORElogonResponse resp;
|
---|
88 |
|
---|
89 | if (!(soaprc = soap_call___vbox__IWebsessionManager_USCORElogon(&soap,
|
---|
90 | pcszArgEndpoint,
|
---|
91 | NULL,
|
---|
92 | &req,
|
---|
93 | &resp)))
|
---|
94 | std::cout << "VirtualBox objref: \"" << resp.returnval << "\"\n";
|
---|
95 | }
|
---|
96 | }
|
---|
97 | else if (!strcmp(pcszMode, "getsession"))
|
---|
98 | {
|
---|
99 | if (argc < 3)
|
---|
100 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
101 | else
|
---|
102 | {
|
---|
103 | _vbox__IWebsessionManager_USCOREgetSessionObject req;
|
---|
104 | req.refIVirtualBox = argv[2];
|
---|
105 | _vbox__IWebsessionManager_USCOREgetSessionObjectResponse resp;
|
---|
106 |
|
---|
107 | if (!(soaprc = soap_call___vbox__IWebsessionManager_USCOREgetSessionObject(&soap,
|
---|
108 | pcszArgEndpoint,
|
---|
109 | NULL,
|
---|
110 | &req,
|
---|
111 | &resp)))
|
---|
112 | std::cout << "session: \"" << resp.returnval << "\"\n";
|
---|
113 | }
|
---|
114 | }
|
---|
115 | else if (!strcmp(pcszMode, "version"))
|
---|
116 | {
|
---|
117 | if (argc < 3)
|
---|
118 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
119 | else
|
---|
120 | {
|
---|
121 | _vbox__IVirtualBox_USCOREgetVersion req;
|
---|
122 | req._USCOREthis = argv[2];
|
---|
123 | _vbox__IVirtualBox_USCOREgetVersionResponse resp;
|
---|
124 |
|
---|
125 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetVersion(&soap,
|
---|
126 | pcszArgEndpoint,
|
---|
127 | NULL,
|
---|
128 | &req,
|
---|
129 | &resp)))
|
---|
130 | std::cout << "version: \"" << resp.returnval << "\"\n";
|
---|
131 | }
|
---|
132 | }
|
---|
133 | else if (!strcmp(pcszMode, "gethost"))
|
---|
134 | {
|
---|
135 | if (argc < 3)
|
---|
136 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
137 | else
|
---|
138 | {
|
---|
139 | _vbox__IVirtualBox_USCOREgetHost req;
|
---|
140 | req._USCOREthis = argv[2];
|
---|
141 | _vbox__IVirtualBox_USCOREgetHostResponse resp;
|
---|
142 |
|
---|
143 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetHost(&soap,
|
---|
144 | pcszArgEndpoint,
|
---|
145 | NULL,
|
---|
146 | &req,
|
---|
147 | &resp)))
|
---|
148 | {
|
---|
149 | std::cout << "Host objref " << resp.returnval << "\n";
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | else if (!strcmp(pcszMode, "getmachines"))
|
---|
154 | {
|
---|
155 | if (argc < 3)
|
---|
156 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
157 | else
|
---|
158 | {
|
---|
159 | _vbox__IVirtualBox_USCOREgetMachines req;
|
---|
160 | req._USCOREthis = argv[2];
|
---|
161 | _vbox__IVirtualBox_USCOREgetMachinesResponse resp;
|
---|
162 |
|
---|
163 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetMachines(&soap,
|
---|
164 | pcszArgEndpoint,
|
---|
165 | NULL,
|
---|
166 | &req,
|
---|
167 | &resp)))
|
---|
168 | {
|
---|
169 | size_t c = resp.returnval.size();
|
---|
170 | for (size_t i = 0;
|
---|
171 | i < c;
|
---|
172 | ++i)
|
---|
173 | {
|
---|
174 | std::cout << "Machine " << i << ": objref " << resp.returnval[i] << "\n";
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | }
|
---|
179 | else if (!strcmp(pcszMode, "createmachine"))
|
---|
180 | {
|
---|
181 | if (argc < 5)
|
---|
182 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
183 | else
|
---|
184 | {
|
---|
185 | _vbox__IVirtualBox_USCOREcreateMachine req;
|
---|
186 | req._USCOREthis = argv[2];
|
---|
187 | req.baseFolder = argv[3];
|
---|
188 | req.name = argv[4];
|
---|
189 | std::cout << "createmachine: baseFolder = \"" << req.baseFolder << "\", name = \"" << req.name << "\"\n";
|
---|
190 | _vbox__IVirtualBox_USCOREcreateMachineResponse resp;
|
---|
191 |
|
---|
192 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREcreateMachine(&soap,
|
---|
193 | pcszArgEndpoint,
|
---|
194 | NULL,
|
---|
195 | &req,
|
---|
196 | &resp)))
|
---|
197 | std::cout << "Machine created: managed object reference ID is " << resp.returnval << "\n";
|
---|
198 | }
|
---|
199 | }
|
---|
200 | else if (!strcmp(pcszMode, "registermachine"))
|
---|
201 | {
|
---|
202 | if (argc < 4)
|
---|
203 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
204 | else
|
---|
205 | {
|
---|
206 | _vbox__IVirtualBox_USCOREregisterMachine req;
|
---|
207 | req._USCOREthis = argv[2];
|
---|
208 | req.machine = argv[3];
|
---|
209 | _vbox__IVirtualBox_USCOREregisterMachineResponse resp;
|
---|
210 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREregisterMachine(&soap,
|
---|
211 | pcszArgEndpoint,
|
---|
212 | NULL,
|
---|
213 | &req,
|
---|
214 | &resp)))
|
---|
215 | std::cout << "Machine registered.\n";
|
---|
216 | }
|
---|
217 | }
|
---|
218 | else if (!strcmp(pcszMode, "getdvddrives"))
|
---|
219 | {
|
---|
220 | if (argc < 3)
|
---|
221 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
222 | else
|
---|
223 | {
|
---|
224 | _vbox__IHost_USCOREgetDVDDrives req;
|
---|
225 | req._USCOREthis = argv[2];
|
---|
226 | _vbox__IHost_USCOREgetDVDDrivesResponse resp;
|
---|
227 | if (!(soaprc = soap_call___vbox__IHost_USCOREgetDVDDrives(&soap,
|
---|
228 | pcszArgEndpoint,
|
---|
229 | NULL,
|
---|
230 | &req,
|
---|
231 | &resp)))
|
---|
232 | {
|
---|
233 | size_t c = resp.returnval.size();
|
---|
234 | for (size_t i = 0;
|
---|
235 | i < c;
|
---|
236 | ++i)
|
---|
237 | {
|
---|
238 | std::cout << "DVD drive " << i << ": objref " << resp.returnval[i] << "\n";
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | else if (!strcmp(pcszMode, "getdvdname"))
|
---|
244 | {
|
---|
245 | if (argc < 3)
|
---|
246 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
247 | else
|
---|
248 | {
|
---|
249 | _vbox__IHostDVDDrive_USCOREgetName req;
|
---|
250 | req._USCOREthis = argv[2];
|
---|
251 | _vbox__IHostDVDDrive_USCOREgetNameResponse resp;
|
---|
252 | if (!(soaprc = soap_call___vbox__IHostDVDDrive_USCOREgetName(&soap,
|
---|
253 | pcszArgEndpoint,
|
---|
254 | NULL,
|
---|
255 | &req,
|
---|
256 | &resp)))
|
---|
257 | std::cout << "Name is: \"" << resp.returnval << "\"\n";
|
---|
258 | }
|
---|
259 | }
|
---|
260 | else if (!strcmp(pcszMode, "getname"))
|
---|
261 | {
|
---|
262 | if (argc < 3)
|
---|
263 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
264 | else
|
---|
265 | {
|
---|
266 | _vbox__IMachine_USCOREgetName req;
|
---|
267 | req._USCOREthis = argv[2];
|
---|
268 | _vbox__IMachine_USCOREgetNameResponse resp;
|
---|
269 | if (!(soaprc = soap_call___vbox__IMachine_USCOREgetName(&soap,
|
---|
270 | pcszArgEndpoint,
|
---|
271 | NULL,
|
---|
272 | &req,
|
---|
273 | &resp)))
|
---|
274 | printf("Name is: %s\n", resp.returnval.c_str());
|
---|
275 | }
|
---|
276 | }
|
---|
277 | else if (!strcmp(pcszMode, "getid"))
|
---|
278 | {
|
---|
279 | if (argc < 3)
|
---|
280 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
281 | else
|
---|
282 | {
|
---|
283 | _vbox__IMachine_USCOREgetId req;
|
---|
284 | req._USCOREthis = argv[2];
|
---|
285 | _vbox__IMachine_USCOREgetIdResponse resp;
|
---|
286 | if (!(soaprc = soap_call___vbox__IMachine_USCOREgetId(&soap,
|
---|
287 | pcszArgEndpoint,
|
---|
288 | NULL,
|
---|
289 | &req,
|
---|
290 | &resp)))
|
---|
291 | std::cout << "UUID is: " << resp.returnval << "\n";;
|
---|
292 | }
|
---|
293 | }
|
---|
294 | else if (!strcmp(pcszMode, "getostypeid"))
|
---|
295 | {
|
---|
296 | if (argc < 3)
|
---|
297 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
298 | else
|
---|
299 | {
|
---|
300 | _vbox__IMachine_USCOREgetOSTypeId req;
|
---|
301 | req._USCOREthis = argv[2];
|
---|
302 | _vbox__IMachine_USCOREgetOSTypeIdResponse resp;
|
---|
303 | if (!(soaprc = soap_call___vbox__IMachine_USCOREgetOSTypeId(&soap,
|
---|
304 | pcszArgEndpoint,
|
---|
305 | NULL,
|
---|
306 | &req,
|
---|
307 | &resp)))
|
---|
308 | std::cout << "Guest OS type is: " << resp.returnval << "\n";
|
---|
309 | }
|
---|
310 | }
|
---|
311 | else if (!strcmp(pcszMode, "savesettings"))
|
---|
312 | {
|
---|
313 | if (argc < 3)
|
---|
314 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
315 | else
|
---|
316 | {
|
---|
317 | _vbox__IMachine_USCOREsaveSettings req;
|
---|
318 | req._USCOREthis = argv[2];
|
---|
319 | _vbox__IMachine_USCOREsaveSettingsResponse resp;
|
---|
320 | if (!(soaprc = soap_call___vbox__IMachine_USCOREsaveSettings(&soap,
|
---|
321 | pcszArgEndpoint,
|
---|
322 | NULL,
|
---|
323 | &req,
|
---|
324 | &resp)))
|
---|
325 | std::cout << "Settings saved\n";
|
---|
326 | }
|
---|
327 | }
|
---|
328 | else if (!strcmp(pcszMode, "release"))
|
---|
329 | {
|
---|
330 | if (argc < 3)
|
---|
331 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
332 | else
|
---|
333 | {
|
---|
334 | _vbox__IManagedObjectRef_USCORErelease req;
|
---|
335 | req._USCOREthis = argv[2];
|
---|
336 | _vbox__IManagedObjectRef_USCOREreleaseResponse resp;
|
---|
337 | if (!(soaprc = soap_call___vbox__IManagedObjectRef_USCORErelease(&soap,
|
---|
338 | pcszArgEndpoint,
|
---|
339 | NULL,
|
---|
340 | &req,
|
---|
341 | &resp)))
|
---|
342 | std::cout << "Managed object reference " << req._USCOREthis << " released.\n";
|
---|
343 | }
|
---|
344 | }
|
---|
345 | else
|
---|
346 | std::cout << "Unknown mode parameter \"" << pcszMode << "\".\n";
|
---|
347 |
|
---|
348 | if (soaprc)
|
---|
349 | {
|
---|
350 | if ( (soap.fault)
|
---|
351 | && (soap.fault->detail)
|
---|
352 | )
|
---|
353 | {
|
---|
354 | if (soap.fault->detail->vbox__InvalidObjectFault)
|
---|
355 | {
|
---|
356 | std::cout << "Bad object ID: " << soap.fault->detail->vbox__InvalidObjectFault->badObjectID << "\n";
|
---|
357 | }
|
---|
358 | if (soap.fault->detail->vbox__RuntimeFault)
|
---|
359 | {
|
---|
360 | std::cout << "Result code: 0x" << std::hex << soap.fault->detail->vbox__RuntimeFault->resultCode << "\n";
|
---|
361 | std::cout << "Text: " << std::hex << soap.fault->detail->vbox__RuntimeFault->text << "\n";
|
---|
362 | std::cout << "Component: " << std::hex << soap.fault->detail->vbox__RuntimeFault->component << "\n";
|
---|
363 | std::cout << "Interface ID: " << std::hex << soap.fault->detail->vbox__RuntimeFault->interfaceID << "\n";
|
---|
364 | }
|
---|
365 | }
|
---|
366 | else
|
---|
367 | {
|
---|
368 | std::cerr << "Invalid fault data, fault message:\n";
|
---|
369 | soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | soap_destroy(&soap); // delete deserialized class instances (for C++ only)
|
---|
374 | soap_end(&soap); // remove deserialized data and clean up
|
---|
375 | soap_done(&soap); // detach the gSOAP environment
|
---|
376 |
|
---|
377 | return soaprc;
|
---|
378 | }
|
---|
379 |
|
---|