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-2010 Oracle Corporation
|
---|
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 |
|
---|
17 | // gSOAP headers (must come after vbox includes because it checks for conflicting defs)
|
---|
18 | #include "soapStub.h"
|
---|
19 |
|
---|
20 | // include generated namespaces table
|
---|
21 | #include "vboxwebsrv.nsmap"
|
---|
22 |
|
---|
23 | #include <iostream>
|
---|
24 | #include <sstream>
|
---|
25 | #include <string>
|
---|
26 |
|
---|
27 |
|
---|
28 | /**
|
---|
29 | *
|
---|
30 | * @param argc
|
---|
31 | * @param argv[]
|
---|
32 | * @return
|
---|
33 | */
|
---|
34 | int main(int argc, char* argv[])
|
---|
35 | {
|
---|
36 | struct soap soap; // gSOAP runtime environment
|
---|
37 | soap_init(&soap); // initialize runtime environment (only once)
|
---|
38 |
|
---|
39 | if (argc < 2)
|
---|
40 | {
|
---|
41 | std::cout <<
|
---|
42 | "webtest: VirtualBox webservice testcase.\n"
|
---|
43 | "Usage:\n"
|
---|
44 | " - IWebsessionManager:\n"
|
---|
45 | " - webtest logon <user> <pass>: IWebsessionManager::logon().\n"
|
---|
46 | " - webtest getsession <vboxref>: IWebsessionManager::getSessionObject().\n"
|
---|
47 | " - webtest logoff <vboxref>: IWebsessionManager::logoff().\n"
|
---|
48 | " - IVirtualBox:\n"
|
---|
49 | " - webtest version <vboxref>: IVirtualBox::getVersion().\n"
|
---|
50 | " - webtest gethost <vboxref>: IVirtualBox::getHost().\n"
|
---|
51 | " - webtest getpc <vboxref>: IVirtualBox::getPerformanceCollector().\n"
|
---|
52 | " - webtest getmachines <vboxref>: IVirtualBox::getMachines().\n"
|
---|
53 | " - webtest createmachine <vboxref> <baseFolder> <name>: IVirtualBox::createMachine().\n"
|
---|
54 | " - webtest registermachine <vboxref> <machineref>: IVirtualBox::registerMachine().\n"
|
---|
55 | " - IHost:\n"
|
---|
56 | " - webtest getdvddrives <hostref>: IHost::getDVDDrives.\n"
|
---|
57 | " - IHostDVDDrive:\n"
|
---|
58 | " - webtest getdvdname <dvdref>: IHostDVDDrive::getname.\n"
|
---|
59 | " - IMachine:\n"
|
---|
60 | " - webtest getname <machineref>: IMachine::getName().\n"
|
---|
61 | " - webtest getid <machineref>: IMachine::getId().\n"
|
---|
62 | " - webtest getostype <machineref>: IMachine::getGuestOSType().\n"
|
---|
63 | " - webtest savesettings <machineref>: IMachine::saveSettings().\n"
|
---|
64 | " - IPerformanceCollector:\n"
|
---|
65 | " - webtest setupmetrics <pcref>: IPerformanceCollector::setupMetrics()\n"
|
---|
66 | " - webtest querymetricsdata <pcref>: IPerformanceCollector::QueryMetricsData()\n"
|
---|
67 | " - All managed object references:\n"
|
---|
68 | " - webtest getif <ref>: report interface of object.\n"
|
---|
69 | " - webtest release <ref>: IUnknown::Release().\n";
|
---|
70 | exit(1);
|
---|
71 | }
|
---|
72 |
|
---|
73 | const char *pcszArgEndpoint = "localhost:18083";
|
---|
74 |
|
---|
75 | const char *pcszMode = argv[1];
|
---|
76 | int soaprc = 2;
|
---|
77 |
|
---|
78 | if (!strcmp(pcszMode, "logon"))
|
---|
79 | {
|
---|
80 | if (argc < 4)
|
---|
81 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
82 | else
|
---|
83 | {
|
---|
84 | _vbox__IWebsessionManager_USCORElogon req;
|
---|
85 | req.username = argv[2];
|
---|
86 | req.password = argv[3];
|
---|
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, "logoff"))
|
---|
116 | {
|
---|
117 | if (argc < 3)
|
---|
118 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
119 | else
|
---|
120 | {
|
---|
121 | _vbox__IWebsessionManager_USCORElogoff req;
|
---|
122 | req.refIVirtualBox = argv[2];
|
---|
123 | _vbox__IWebsessionManager_USCORElogoffResponse resp;
|
---|
124 |
|
---|
125 | if (!(soaprc = soap_call___vbox__IWebsessionManager_USCORElogoff(&soap,
|
---|
126 | pcszArgEndpoint,
|
---|
127 | NULL,
|
---|
128 | &req,
|
---|
129 | &resp)))
|
---|
130 | {
|
---|
131 | ;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | }
|
---|
135 | else if (!strcmp(pcszMode, "version"))
|
---|
136 | {
|
---|
137 | if (argc < 3)
|
---|
138 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
139 | else
|
---|
140 | {
|
---|
141 | _vbox__IVirtualBox_USCOREgetVersion req;
|
---|
142 | req._USCOREthis = argv[2];
|
---|
143 | _vbox__IVirtualBox_USCOREgetVersionResponse resp;
|
---|
144 |
|
---|
145 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetVersion(&soap,
|
---|
146 | pcszArgEndpoint,
|
---|
147 | NULL,
|
---|
148 | &req,
|
---|
149 | &resp)))
|
---|
150 | std::cout << "version: \"" << resp.returnval << "\"\n";
|
---|
151 | }
|
---|
152 | }
|
---|
153 | else if (!strcmp(pcszMode, "gethost"))
|
---|
154 | {
|
---|
155 | if (argc < 3)
|
---|
156 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
157 | else
|
---|
158 | {
|
---|
159 | _vbox__IVirtualBox_USCOREgetHost req;
|
---|
160 | req._USCOREthis = argv[2];
|
---|
161 | _vbox__IVirtualBox_USCOREgetHostResponse resp;
|
---|
162 |
|
---|
163 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetHost(&soap,
|
---|
164 | pcszArgEndpoint,
|
---|
165 | NULL,
|
---|
166 | &req,
|
---|
167 | &resp)))
|
---|
168 | {
|
---|
169 | std::cout << "Host objref " << resp.returnval << "\n";
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|
173 | else if (!strcmp(pcszMode, "getpc"))
|
---|
174 | {
|
---|
175 | if (argc < 3)
|
---|
176 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
177 | else
|
---|
178 | {
|
---|
179 | _vbox__IVirtualBox_USCOREgetPerformanceCollector req;
|
---|
180 | req._USCOREthis = argv[2];
|
---|
181 | _vbox__IVirtualBox_USCOREgetPerformanceCollectorResponse resp;
|
---|
182 |
|
---|
183 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetPerformanceCollector(&soap,
|
---|
184 | pcszArgEndpoint,
|
---|
185 | NULL,
|
---|
186 | &req,
|
---|
187 | &resp)))
|
---|
188 | {
|
---|
189 | std::cout << "Performance collector objref " << resp.returnval << "\n";
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 | else if (!strcmp(pcszMode, "getmachines"))
|
---|
194 | {
|
---|
195 | if (argc < 3)
|
---|
196 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
197 | else
|
---|
198 | {
|
---|
199 | _vbox__IVirtualBox_USCOREgetMachines req;
|
---|
200 | req._USCOREthis = argv[2];
|
---|
201 | _vbox__IVirtualBox_USCOREgetMachinesResponse resp;
|
---|
202 |
|
---|
203 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetMachines(&soap,
|
---|
204 | pcszArgEndpoint,
|
---|
205 | NULL,
|
---|
206 | &req,
|
---|
207 | &resp)))
|
---|
208 | {
|
---|
209 | size_t c = resp.returnval.size();
|
---|
210 | for (size_t i = 0;
|
---|
211 | i < c;
|
---|
212 | ++i)
|
---|
213 | {
|
---|
214 | std::cout << "Machine " << i << ": objref " << resp.returnval[i] << "\n";
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 | }
|
---|
219 | else if (!strcmp(pcszMode, "createmachine"))
|
---|
220 | {
|
---|
221 | if (argc < 5)
|
---|
222 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
223 | else
|
---|
224 | {
|
---|
225 | _vbox__IVirtualBox_USCOREcreateMachine req;
|
---|
226 | req._USCOREthis = argv[2];
|
---|
227 | req.baseFolder = argv[3];
|
---|
228 | req.name = argv[4];
|
---|
229 | std::cout << "createmachine: baseFolder = \"" << req.baseFolder << "\", name = \"" << req.name << "\"\n";
|
---|
230 | _vbox__IVirtualBox_USCOREcreateMachineResponse resp;
|
---|
231 |
|
---|
232 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREcreateMachine(&soap,
|
---|
233 | pcszArgEndpoint,
|
---|
234 | NULL,
|
---|
235 | &req,
|
---|
236 | &resp)))
|
---|
237 | std::cout << "Machine created: managed object reference ID is " << resp.returnval << "\n";
|
---|
238 | }
|
---|
239 | }
|
---|
240 | else if (!strcmp(pcszMode, "registermachine"))
|
---|
241 | {
|
---|
242 | if (argc < 4)
|
---|
243 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
244 | else
|
---|
245 | {
|
---|
246 | _vbox__IVirtualBox_USCOREregisterMachine req;
|
---|
247 | req._USCOREthis = argv[2];
|
---|
248 | req.machine = argv[3];
|
---|
249 | _vbox__IVirtualBox_USCOREregisterMachineResponse resp;
|
---|
250 | if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREregisterMachine(&soap,
|
---|
251 | pcszArgEndpoint,
|
---|
252 | NULL,
|
---|
253 | &req,
|
---|
254 | &resp)))
|
---|
255 | std::cout << "Machine registered.\n";
|
---|
256 | }
|
---|
257 | }
|
---|
258 | else if (!strcmp(pcszMode, "getdvddrives"))
|
---|
259 | {
|
---|
260 | if (argc < 3)
|
---|
261 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
262 | else
|
---|
263 | {
|
---|
264 | _vbox__IHost_USCOREgetDVDDrives req;
|
---|
265 | req._USCOREthis = argv[2];
|
---|
266 | _vbox__IHost_USCOREgetDVDDrivesResponse resp;
|
---|
267 | if (!(soaprc = soap_call___vbox__IHost_USCOREgetDVDDrives(&soap,
|
---|
268 | pcszArgEndpoint,
|
---|
269 | NULL,
|
---|
270 | &req,
|
---|
271 | &resp)))
|
---|
272 | {
|
---|
273 | size_t c = resp.returnval.size();
|
---|
274 | for (size_t i = 0;
|
---|
275 | i < c;
|
---|
276 | ++i)
|
---|
277 | {
|
---|
278 | std::cout << "DVD drive " << i << ": objref " << resp.returnval[i] << "\n";
|
---|
279 | }
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 | else if (!strcmp(pcszMode, "getname"))
|
---|
284 | {
|
---|
285 | if (argc < 3)
|
---|
286 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
287 | else
|
---|
288 | {
|
---|
289 | _vbox__IMachine_USCOREgetName req;
|
---|
290 | req._USCOREthis = argv[2];
|
---|
291 | _vbox__IMachine_USCOREgetNameResponse resp;
|
---|
292 | if (!(soaprc = soap_call___vbox__IMachine_USCOREgetName(&soap,
|
---|
293 | pcszArgEndpoint,
|
---|
294 | NULL,
|
---|
295 | &req,
|
---|
296 | &resp)))
|
---|
297 | printf("Name is: %s\n", resp.returnval.c_str());
|
---|
298 | }
|
---|
299 | }
|
---|
300 | else if (!strcmp(pcszMode, "getid"))
|
---|
301 | {
|
---|
302 | if (argc < 3)
|
---|
303 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
304 | else
|
---|
305 | {
|
---|
306 | _vbox__IMachine_USCOREgetId req;
|
---|
307 | req._USCOREthis = argv[2];
|
---|
308 | _vbox__IMachine_USCOREgetIdResponse resp;
|
---|
309 | if (!(soaprc = soap_call___vbox__IMachine_USCOREgetId(&soap,
|
---|
310 | pcszArgEndpoint,
|
---|
311 | NULL,
|
---|
312 | &req,
|
---|
313 | &resp)))
|
---|
314 | std::cout << "UUID is: " << resp.returnval << "\n";;
|
---|
315 | }
|
---|
316 | }
|
---|
317 | else if (!strcmp(pcszMode, "getostypeid"))
|
---|
318 | {
|
---|
319 | if (argc < 3)
|
---|
320 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
321 | else
|
---|
322 | {
|
---|
323 | _vbox__IMachine_USCOREgetOSTypeId req;
|
---|
324 | req._USCOREthis = argv[2];
|
---|
325 | _vbox__IMachine_USCOREgetOSTypeIdResponse resp;
|
---|
326 | if (!(soaprc = soap_call___vbox__IMachine_USCOREgetOSTypeId(&soap,
|
---|
327 | pcszArgEndpoint,
|
---|
328 | NULL,
|
---|
329 | &req,
|
---|
330 | &resp)))
|
---|
331 | std::cout << "Guest OS type is: " << resp.returnval << "\n";
|
---|
332 | }
|
---|
333 | }
|
---|
334 | else if (!strcmp(pcszMode, "savesettings"))
|
---|
335 | {
|
---|
336 | if (argc < 3)
|
---|
337 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
338 | else
|
---|
339 | {
|
---|
340 | _vbox__IMachine_USCOREsaveSettings req;
|
---|
341 | req._USCOREthis = argv[2];
|
---|
342 | _vbox__IMachine_USCOREsaveSettingsResponse resp;
|
---|
343 | if (!(soaprc = soap_call___vbox__IMachine_USCOREsaveSettings(&soap,
|
---|
344 | pcszArgEndpoint,
|
---|
345 | NULL,
|
---|
346 | &req,
|
---|
347 | &resp)))
|
---|
348 | std::cout << "Settings saved\n";
|
---|
349 | }
|
---|
350 | }
|
---|
351 | else if (!strcmp(pcszMode, "setupmetrics"))
|
---|
352 | {
|
---|
353 | if (argc < 3)
|
---|
354 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
355 | else
|
---|
356 | {
|
---|
357 | _vbox__IPerformanceCollector_USCOREsetupMetrics req;
|
---|
358 | req._USCOREthis = argv[2];
|
---|
359 | // req.metricNames[0] = "*";
|
---|
360 | // req.objects
|
---|
361 | req.period = 1; // seconds
|
---|
362 | req.count = 100;
|
---|
363 | _vbox__IPerformanceCollector_USCOREsetupMetricsResponse resp;
|
---|
364 | if (!(soaprc = soap_call___vbox__IPerformanceCollector_USCOREsetupMetrics(&soap,
|
---|
365 | pcszArgEndpoint,
|
---|
366 | NULL,
|
---|
367 | &req,
|
---|
368 | &resp)))
|
---|
369 | {
|
---|
370 | size_t c = resp.returnval.size();
|
---|
371 | for (size_t i = 0;
|
---|
372 | i < c;
|
---|
373 | ++i)
|
---|
374 | {
|
---|
375 | std::cout << "Metric " << i << ": objref " << resp.returnval[i] << "\n";
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|
379 | }
|
---|
380 | else if (!strcmp(pcszMode, "querymetricsdata"))
|
---|
381 | {
|
---|
382 | if (argc < 3)
|
---|
383 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
384 | else
|
---|
385 | {
|
---|
386 | _vbox__IPerformanceCollector_USCOREqueryMetricsData req;
|
---|
387 | req._USCOREthis = argv[2];
|
---|
388 | // req.metricNames[0] = "*";
|
---|
389 | // req.objects
|
---|
390 | _vbox__IPerformanceCollector_USCOREqueryMetricsDataResponse resp;
|
---|
391 | if (!(soaprc = soap_call___vbox__IPerformanceCollector_USCOREqueryMetricsData(&soap,
|
---|
392 | pcszArgEndpoint,
|
---|
393 | NULL,
|
---|
394 | &req,
|
---|
395 | &resp)))
|
---|
396 | {
|
---|
397 | size_t c = resp.returnval.size();
|
---|
398 | for (size_t i = 0;
|
---|
399 | i < c;
|
---|
400 | ++i)
|
---|
401 | {
|
---|
402 | std::cout << "long " << i << ": " << resp.returnval[i] << "\n";
|
---|
403 | }
|
---|
404 | }
|
---|
405 | }
|
---|
406 | }
|
---|
407 | else if (!strcmp(pcszMode, "release"))
|
---|
408 | {
|
---|
409 | if (argc < 3)
|
---|
410 | std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
|
---|
411 | else
|
---|
412 | {
|
---|
413 | _vbox__IManagedObjectRef_USCORErelease req;
|
---|
414 | req._USCOREthis = argv[2];
|
---|
415 | _vbox__IManagedObjectRef_USCOREreleaseResponse resp;
|
---|
416 | if (!(soaprc = soap_call___vbox__IManagedObjectRef_USCORErelease(&soap,
|
---|
417 | pcszArgEndpoint,
|
---|
418 | NULL,
|
---|
419 | &req,
|
---|
420 | &resp)))
|
---|
421 | std::cout << "Managed object reference " << req._USCOREthis << " released.\n";
|
---|
422 | }
|
---|
423 | }
|
---|
424 | else
|
---|
425 | std::cout << "Unknown mode parameter \"" << pcszMode << "\".\n";
|
---|
426 |
|
---|
427 | if (soaprc)
|
---|
428 | {
|
---|
429 | if ( (soap.fault)
|
---|
430 | && (soap.fault->detail)
|
---|
431 | )
|
---|
432 | {
|
---|
433 | if (soap.fault->detail->vbox__InvalidObjectFault)
|
---|
434 | {
|
---|
435 | std::cout << "Bad object ID: " << soap.fault->detail->vbox__InvalidObjectFault->badObjectID << "\n";
|
---|
436 | }
|
---|
437 | if (soap.fault->detail->vbox__RuntimeFault)
|
---|
438 | {
|
---|
439 | std::cout << "Result code: 0x" << std::hex << soap.fault->detail->vbox__RuntimeFault->resultCode << "\n";
|
---|
440 | std::cout << "Text: " << std::hex << soap.fault->detail->vbox__RuntimeFault->text << "\n";
|
---|
441 | std::cout << "Component: " << std::hex << soap.fault->detail->vbox__RuntimeFault->component << "\n";
|
---|
442 | std::cout << "Interface ID: " << std::hex << soap.fault->detail->vbox__RuntimeFault->interfaceID << "\n";
|
---|
443 | }
|
---|
444 | }
|
---|
445 | else
|
---|
446 | {
|
---|
447 | std::cerr << "Invalid fault data, fault message:\n";
|
---|
448 | soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream
|
---|
449 | }
|
---|
450 | }
|
---|
451 |
|
---|
452 | soap_destroy(&soap); // delete deserialized class instances (for C++ only)
|
---|
453 | soap_end(&soap); // remove deserialized data and clean up
|
---|
454 | soap_done(&soap); // detach the gSOAP environment
|
---|
455 |
|
---|
456 | return soaprc;
|
---|
457 | }
|
---|
458 |
|
---|