VirtualBox

source: vbox/trunk/src/VBox/Main/webservice/webtest.cpp@ 40130

Last change on this file since 40130 was 40130, checked in by vboxsync, 13 years ago

Main/webservice+doc/manual: Add SSL support to the webservice, and also add an optional parameter which ensures the authentication setting is correct. Update manual and SDK reference.

  • Property filesplitter.c set to Makefile.kmk
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 20.9 KB
Line 
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-2012 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
28static void usage(int exitcode)
29{
30 std::cout <<
31 "webtest: VirtualBox webservice testcase.\n"
32 "\nUsage: webtest [options] [command]...\n"
33 "\nSupported options:\n"
34 " -h: print this help message and exit.\n"
35 " -c URL: specify the webservice server URL (default http://localhost:18083/).\n"
36 "\nSupported commands:\n"
37 " - IWebsessionManager:\n"
38 " - webtest logon <user> <pass>: IWebsessionManager::logon().\n"
39 " - webtest getsession <vboxref>: IWebsessionManager::getSessionObject().\n"
40 " - webtest logoff <vboxref>: IWebsessionManager::logoff().\n"
41 " - IVirtualBox:\n"
42 " - webtest version <vboxref>: IVirtualBox::getVersion().\n"
43 " - webtest gethost <vboxref>: IVirtualBox::getHost().\n"
44 " - webtest getpc <vboxref>: IVirtualBox::getPerformanceCollector().\n"
45 " - webtest getmachines <vboxref>: IVirtualBox::getMachines().\n"
46 " - webtest createmachine <vboxref> <settingsPath> <name>: IVirtualBox::createMachine().\n"
47 " - webtest registermachine <vboxref> <machineref>: IVirtualBox::registerMachine().\n"
48 " - IHost:\n"
49 " - webtest getdvddrives <hostref>: IHost::getDVDDrives.\n"
50 " - IHostDVDDrive:\n"
51 " - webtest getdvdname <dvdref>: IHostDVDDrive::getname.\n"
52 " - IMachine:\n"
53 " - webtest getname <machineref>: IMachine::getName().\n"
54 " - webtest getid <machineref>: IMachine::getId().\n"
55 " - webtest getostype <machineref>: IMachine::getGuestOSType().\n"
56 " - webtest savesettings <machineref>: IMachine::saveSettings().\n"
57 " - IPerformanceCollector:\n"
58 " - webtest setupmetrics <pcref>: IPerformanceCollector::setupMetrics()\n"
59 " - webtest querymetricsdata <pcref>: IPerformanceCollector::QueryMetricsData()\n"
60 " - All managed object references:\n"
61 " - webtest getif <ref>: report interface of object.\n"
62 " - webtest release <ref>: IUnknown::Release().\n";
63 exit(exitcode);
64}
65
66/**
67 *
68 * @param argc
69 * @param argv[]
70 * @return
71 */
72int main(int argc, char* argv[])
73{
74 bool fSSL = false;
75 const char *pcszArgEndpoint = "http://localhost:18083/";
76
77 int ap;
78 for (ap = 1; ap <= argc; ap++)
79 {
80 if (argv[ap][0] == '-')
81 {
82 if (!strcmp(argv[ap], "-h"))
83 usage(0);
84 else if (!strcmp(argv[ap], "-c"))
85 {
86 ap++;
87 if (ap > argc)
88 usage(1);
89 pcszArgEndpoint = argv[ap];
90 fSSL = !strncmp(pcszArgEndpoint, "https://", 8);
91 }
92 else
93 usage(1);
94 }
95 else
96 break;
97 }
98
99 if (argc < 1 + ap)
100 usage(1);
101
102#ifdef WITH_OPENSSL
103 if (fSSL)
104 soap_ssl_init();
105#endif /* WITH_OPENSSL */
106
107 struct soap soap; // gSOAP runtime environment
108 soap_init(&soap); // initialize runtime environment (only once)
109#ifdef WITH_OPENSSL
110 // Use SOAP_SSL_NO_AUTHENTICATION here to accept broken server configs.
111 // In a real world setup please use at least SOAP_SSL_DEFAULT and provide
112 // the necessary CA certificate for validating the server's certificate.
113 if (fSSL && soap_ssl_client_context(&soap, SOAP_SSL_NO_AUTHENTICATION,
114 NULL /*clientkey*/, NULL /*password*/,
115 NULL /*cacert*/, NULL /*capath*/,
116 NULL /*randfile*/))
117 {
118 soap_print_fault(&soap, stderr);
119 exit(1);
120 }
121#endif /* WITH_OPENSSL */
122
123 const char *pcszMode = argv[ap];
124 int soaprc = SOAP_SVR_FAULT;
125
126 if (!strcmp(pcszMode, "logon"))
127 {
128 if (argc < 3 + ap)
129 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
130 else
131 {
132 _vbox__IWebsessionManager_USCORElogon req;
133 req.username = argv[ap + 1];
134 req.password = argv[ap + 2];
135 _vbox__IWebsessionManager_USCORElogonResponse resp;
136
137 if (!(soaprc = soap_call___vbox__IWebsessionManager_USCORElogon(&soap,
138 pcszArgEndpoint,
139 NULL,
140 &req,
141 &resp)))
142 std::cout << "VirtualBox objref: \"" << resp.returnval << "\"\n";
143 }
144 }
145 else if (!strcmp(pcszMode, "getsession"))
146 {
147 if (argc < 2 + ap)
148 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
149 else
150 {
151 _vbox__IWebsessionManager_USCOREgetSessionObject req;
152 req.refIVirtualBox = argv[ap + 1];
153 _vbox__IWebsessionManager_USCOREgetSessionObjectResponse resp;
154
155 if (!(soaprc = soap_call___vbox__IWebsessionManager_USCOREgetSessionObject(&soap,
156 pcszArgEndpoint,
157 NULL,
158 &req,
159 &resp)))
160 std::cout << "session: \"" << resp.returnval << "\"\n";
161 }
162 }
163 else if (!strcmp(pcszMode, "logoff"))
164 {
165 if (argc < 2 + ap)
166 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
167 else
168 {
169 _vbox__IWebsessionManager_USCORElogoff req;
170 req.refIVirtualBox = argv[ap + 1];
171 _vbox__IWebsessionManager_USCORElogoffResponse resp;
172
173 if (!(soaprc = soap_call___vbox__IWebsessionManager_USCORElogoff(&soap,
174 pcszArgEndpoint,
175 NULL,
176 &req,
177 &resp)))
178 {
179 ;
180 }
181 }
182 }
183 else if (!strcmp(pcszMode, "version"))
184 {
185 if (argc < 2 + ap)
186 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
187 else
188 {
189 _vbox__IVirtualBox_USCOREgetVersion req;
190 req._USCOREthis = argv[ap + 1];
191 _vbox__IVirtualBox_USCOREgetVersionResponse resp;
192
193 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetVersion(&soap,
194 pcszArgEndpoint,
195 NULL,
196 &req,
197 &resp)))
198 std::cout << "version: \"" << resp.returnval << "\"\n";
199 }
200 }
201 else if (!strcmp(pcszMode, "gethost"))
202 {
203 if (argc < 2 + ap)
204 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
205 else
206 {
207 _vbox__IVirtualBox_USCOREgetHost req;
208 req._USCOREthis = argv[ap + 1];
209 _vbox__IVirtualBox_USCOREgetHostResponse resp;
210
211 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetHost(&soap,
212 pcszArgEndpoint,
213 NULL,
214 &req,
215 &resp)))
216 {
217 std::cout << "Host objref " << resp.returnval << "\n";
218 }
219 }
220 }
221 else if (!strcmp(pcszMode, "getpc"))
222 {
223 if (argc < 2 + ap)
224 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
225 else
226 {
227 _vbox__IVirtualBox_USCOREgetPerformanceCollector req;
228 req._USCOREthis = argv[ap + 1];
229 _vbox__IVirtualBox_USCOREgetPerformanceCollectorResponse resp;
230
231 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetPerformanceCollector(&soap,
232 pcszArgEndpoint,
233 NULL,
234 &req,
235 &resp)))
236 {
237 std::cout << "Performance collector objref " << resp.returnval << "\n";
238 }
239 }
240 }
241 else if (!strcmp(pcszMode, "getmachines"))
242 {
243 if (argc < 2 + ap)
244 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
245 else
246 {
247 _vbox__IVirtualBox_USCOREgetMachines req;
248 req._USCOREthis = argv[ap + 1];
249 _vbox__IVirtualBox_USCOREgetMachinesResponse resp;
250
251 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetMachines(&soap,
252 pcszArgEndpoint,
253 NULL,
254 &req,
255 &resp)))
256 {
257 size_t c = resp.returnval.size();
258 for (size_t i = 0;
259 i < c;
260 ++i)
261 {
262 std::cout << "Machine " << i << ": objref " << resp.returnval[i] << "\n";
263 }
264 }
265 }
266 }
267 else if (!strcmp(pcszMode, "createmachine"))
268 {
269 if (argc < 4 + ap)
270 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
271 else
272 {
273 _vbox__IVirtualBox_USCOREcreateMachine req;
274 req._USCOREthis = argv[ap + 1];
275 req.settingsFile = argv[ap + 2];
276 req.name = argv[ap + 3];
277 std::cout << "createmachine: settingsFile = \"" << req.settingsFile << "\", name = \"" << req.name << "\"\n";
278 _vbox__IVirtualBox_USCOREcreateMachineResponse resp;
279
280 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREcreateMachine(&soap,
281 pcszArgEndpoint,
282 NULL,
283 &req,
284 &resp)))
285 std::cout << "Machine created: managed object reference ID is " << resp.returnval << "\n";
286 }
287 }
288 else if (!strcmp(pcszMode, "registermachine"))
289 {
290 if (argc < 3 + ap)
291 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
292 else
293 {
294 _vbox__IVirtualBox_USCOREregisterMachine req;
295 req._USCOREthis = argv[ap + 1];
296 req.machine = argv[ap + 2];
297 _vbox__IVirtualBox_USCOREregisterMachineResponse resp;
298 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREregisterMachine(&soap,
299 pcszArgEndpoint,
300 NULL,
301 &req,
302 &resp)))
303 std::cout << "Machine registered.\n";
304 }
305 }
306 else if (!strcmp(pcszMode, "getdvddrives"))
307 {
308 if (argc < 2 + ap)
309 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
310 else
311 {
312 _vbox__IHost_USCOREgetDVDDrives req;
313 req._USCOREthis = argv[ap + 1];
314 _vbox__IHost_USCOREgetDVDDrivesResponse resp;
315 if (!(soaprc = soap_call___vbox__IHost_USCOREgetDVDDrives(&soap,
316 pcszArgEndpoint,
317 NULL,
318 &req,
319 &resp)))
320 {
321 size_t c = resp.returnval.size();
322 for (size_t i = 0;
323 i < c;
324 ++i)
325 {
326 std::cout << "DVD drive " << i << ": objref " << resp.returnval[i] << "\n";
327 }
328 }
329 }
330 }
331 else if (!strcmp(pcszMode, "getname"))
332 {
333 if (argc < 2 + ap)
334 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
335 else
336 {
337 _vbox__IMachine_USCOREgetName req;
338 req._USCOREthis = argv[ap + 1];
339 _vbox__IMachine_USCOREgetNameResponse resp;
340 if (!(soaprc = soap_call___vbox__IMachine_USCOREgetName(&soap,
341 pcszArgEndpoint,
342 NULL,
343 &req,
344 &resp)))
345 printf("Name is: %s\n", resp.returnval.c_str());
346 }
347 }
348 else if (!strcmp(pcszMode, "getid"))
349 {
350 if (argc < 2 + ap)
351 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
352 else
353 {
354 _vbox__IMachine_USCOREgetId req;
355 req._USCOREthis = argv[ap + 1];
356 _vbox__IMachine_USCOREgetIdResponse resp;
357 if (!(soaprc = soap_call___vbox__IMachine_USCOREgetId(&soap,
358 pcszArgEndpoint,
359 NULL,
360 &req,
361 &resp)))
362 std::cout << "UUID is: " << resp.returnval << "\n";;
363 }
364 }
365 else if (!strcmp(pcszMode, "getostypeid"))
366 {
367 if (argc < 2 + ap)
368 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
369 else
370 {
371 _vbox__IMachine_USCOREgetOSTypeId req;
372 req._USCOREthis = argv[ap + 1];
373 _vbox__IMachine_USCOREgetOSTypeIdResponse resp;
374 if (!(soaprc = soap_call___vbox__IMachine_USCOREgetOSTypeId(&soap,
375 pcszArgEndpoint,
376 NULL,
377 &req,
378 &resp)))
379 std::cout << "Guest OS type is: " << resp.returnval << "\n";
380 }
381 }
382 else if (!strcmp(pcszMode, "savesettings"))
383 {
384 if (argc < 2 + ap)
385 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
386 else
387 {
388 _vbox__IMachine_USCOREsaveSettings req;
389 req._USCOREthis = argv[ap + 1];
390 _vbox__IMachine_USCOREsaveSettingsResponse resp;
391 if (!(soaprc = soap_call___vbox__IMachine_USCOREsaveSettings(&soap,
392 pcszArgEndpoint,
393 NULL,
394 &req,
395 &resp)))
396 std::cout << "Settings saved\n";
397 }
398 }
399 else if (!strcmp(pcszMode, "setupmetrics"))
400 {
401 if (argc < 2 + ap)
402 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
403 else
404 {
405 _vbox__IPerformanceCollector_USCOREsetupMetrics req;
406 req._USCOREthis = argv[ap + 1];
407// req.metricNames[0] = "*";
408// req.objects
409 req.period = 1; // seconds
410 req.count = 100;
411 _vbox__IPerformanceCollector_USCOREsetupMetricsResponse resp;
412 if (!(soaprc = soap_call___vbox__IPerformanceCollector_USCOREsetupMetrics(&soap,
413 pcszArgEndpoint,
414 NULL,
415 &req,
416 &resp)))
417 {
418 size_t c = resp.returnval.size();
419 for (size_t i = 0;
420 i < c;
421 ++i)
422 {
423 std::cout << "Metric " << i << ": objref " << resp.returnval[i] << "\n";
424 }
425 }
426 }
427 }
428 else if (!strcmp(pcszMode, "querymetricsdata"))
429 {
430 if (argc < 2 + ap)
431 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
432 else
433 {
434 _vbox__IPerformanceCollector_USCOREqueryMetricsData req;
435 req._USCOREthis = argv[ap + 1];
436// req.metricNames[0] = "*";
437// req.objects
438 _vbox__IPerformanceCollector_USCOREqueryMetricsDataResponse resp;
439 if (!(soaprc = soap_call___vbox__IPerformanceCollector_USCOREqueryMetricsData(&soap,
440 pcszArgEndpoint,
441 NULL,
442 &req,
443 &resp)))
444 {
445 size_t c = resp.returnval.size();
446 for (size_t i = 0;
447 i < c;
448 ++i)
449 {
450 std::cout << "long " << i << ": " << resp.returnval[i] << "\n";
451 }
452 }
453 }
454 }
455 else if (!strcmp(pcszMode, "release"))
456 {
457 if (argc < 2 + ap)
458 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
459 else
460 {
461 _vbox__IManagedObjectRef_USCORErelease req;
462 req._USCOREthis = argv[ap + 1];
463 _vbox__IManagedObjectRef_USCOREreleaseResponse resp;
464 if (!(soaprc = soap_call___vbox__IManagedObjectRef_USCORErelease(&soap,
465 pcszArgEndpoint,
466 NULL,
467 &req,
468 &resp)))
469 std::cout << "Managed object reference " << req._USCOREthis << " released.\n";
470 }
471 }
472 else
473 std::cout << "Unknown mode parameter \"" << pcszMode << "\".\n";
474
475 if (soaprc)
476 {
477 if ( (soap.fault)
478 && (soap.fault->detail)
479 )
480 {
481 if (soap.fault->detail->vbox__InvalidObjectFault)
482 {
483 std::cout << "Bad object ID: " << soap.fault->detail->vbox__InvalidObjectFault->badObjectID << "\n";
484 }
485 else if (soap.fault->detail->vbox__RuntimeFault)
486 {
487 std::cout << "Result code: 0x" << std::hex << soap.fault->detail->vbox__RuntimeFault->resultCode << "\n";
488 std::cout << "Text: " << std::hex << soap.fault->detail->vbox__RuntimeFault->text << "\n";
489 std::cout << "Component: " << std::hex << soap.fault->detail->vbox__RuntimeFault->component << "\n";
490 std::cout << "Interface ID: " << std::hex << soap.fault->detail->vbox__RuntimeFault->interfaceID << "\n";
491 }
492 else
493 {
494 // generic fault
495 std::cerr << "Generic fault message:\n";
496 soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream
497 }
498 }
499 else
500 {
501 std::cerr << "Invalid fault data, fault message:\n";
502 soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream
503 }
504 }
505
506 soap_destroy(&soap); // delete deserialized class instances (for C++ only)
507 soap_end(&soap); // remove deserialized data and clean up
508 soap_done(&soap); // detach the gSOAP environment
509
510 return soaprc;
511}
512
Note: See TracBrowser for help on using the repository browser.

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