VirtualBox

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

Last change on this file since 93349 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property filesplitter.c set to Makefile.kmk
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 23.5 KB
Line 
1/* $Id: webtest.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * webtest.cpp:
4 * demo webservice client in C++. This mimics some of the
5 * functionality of VBoxManage for testing purposes.
6 */
7/*
8 * Copyright (C) 2006-2022 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19// gSOAP headers (must come after vbox includes because it checks for conflicting defs)
20#include "soapStub.h"
21
22// include generated namespaces table
23#include "vboxwebsrv.nsmap"
24
25#include <iostream>
26#include <iprt/sanitized/sstream>
27#include <iprt/sanitized/string>
28
29#include <iprt/initterm.h>
30#include <iprt/message.h>
31#include <iprt/errcore.h>
32
33
34static void usage(int exitcode)
35{
36 std::cout <<
37 "webtest: VirtualBox webservice testcase.\n"
38 "\nUsage: webtest [options] [command]...\n"
39 "\nSupported options:\n"
40 " -h: print this help message and exit.\n"
41 " -c URL: specify the webservice server URL (default http://localhost:18083/).\n"
42 "\nSupported commands:\n"
43 " - IWebsessionManager:\n"
44 " - webtest logon <user> <pass>: IWebsessionManager::logon().\n"
45 " - webtest getsession <vboxref>: IWebsessionManager::getSessionObject().\n"
46 " - webtest logoff <vboxref>: IWebsessionManager::logoff().\n"
47 " - IVirtualBox:\n"
48 " - webtest version <vboxref>: IVirtualBox::getVersion().\n"
49 " - webtest gethost <vboxref>: IVirtualBox::getHost().\n"
50 " - webtest getpc <vboxref>: IVirtualBox::getPerformanceCollector().\n"
51 " - webtest getmachines <vboxref>: IVirtualBox::getMachines().\n"
52 " - webtest createmachine <vboxref> <settingsPath> <name>: IVirtualBox::createMachine().\n"
53 " - webtest registermachine <vboxref> <machineref>: IVirtualBox::registerMachine().\n"
54 " - IHost:\n"
55 " - webtest getdvddrives <hostref>: IHost::getDVDDrives.\n"
56 " - IHostDVDDrive:\n"
57 " - webtest getdvdname <dvdref>: IHostDVDDrive::getname.\n"
58 " - IMachine:\n"
59 " - webtest getname <machineref>: IMachine::getName().\n"
60 " - webtest getid <machineref>: IMachine::getId().\n"
61 " - webtest getostype <machineref>: IMachine::getGuestOSType().\n"
62 " - webtest savesettings <machineref>: IMachine::saveSettings().\n"
63 " - IPerformanceCollector:\n"
64 " - webtest setupmetrics <pcref>: IPerformanceCollector::setupMetrics()\n"
65 " - webtest querymetricsdata <pcref>: IPerformanceCollector::QueryMetricsData()\n"
66 " - IVirtualBoxErrorInfo:\n"
67 " - webtest errorinfo <eiref>: various IVirtualBoxErrorInfo getters\n"
68 " - All managed object references:\n"
69 " - webtest getif <ref>: report interface of object.\n"
70 " - webtest release <ref>: IUnknown::Release().\n";
71 exit(exitcode);
72}
73
74/**
75 *
76 * @param argc
77 * @param argv[]
78 * @return
79 */
80int main(int argc, char* argv[])
81{
82 bool fSSL = false;
83 const char *pcszArgEndpoint = "http://localhost:18083/";
84
85 /* SSL callbacks drag in IPRT sem/thread use, so make sure it is ready. */
86 int rc = RTR3InitExe(argc, &argv, 0);
87 if (RT_FAILURE(rc))
88 return RTMsgInitFailure(rc);
89
90 int ap;
91 for (ap = 1; ap < argc; ap++)
92 {
93 if (argv[ap][0] == '-')
94 {
95 if (!strcmp(argv[ap], "-h"))
96 usage(0);
97 else if (!strcmp(argv[ap], "-c"))
98 {
99 ap++;
100 if (ap >= argc)
101 usage(1);
102 pcszArgEndpoint = argv[ap];
103 fSSL = !strncmp(pcszArgEndpoint, "https://", 8);
104 }
105 else
106 usage(1);
107 }
108 else
109 break;
110 }
111
112 if (argc < 1 + ap)
113 usage(1);
114
115#ifdef WITH_OPENSSL
116 if (fSSL)
117 soap_ssl_init();
118#endif /* WITH_OPENSSL */
119
120 struct soap soap; // gSOAP runtime environment
121 soap_init(&soap); // initialize runtime environment (only once)
122#ifdef WITH_OPENSSL
123 // Use SOAP_SSL_NO_AUTHENTICATION here to accept broken server configs.
124 // In a real world setup please use at least SOAP_SSL_DEFAULT and provide
125 // the necessary CA certificate for validating the server's certificate.
126 if (fSSL && soap_ssl_client_context(&soap, SOAP_SSL_NO_AUTHENTICATION | SOAP_TLSv1,
127 NULL /*clientkey*/, NULL /*password*/,
128 NULL /*cacert*/, NULL /*capath*/,
129 NULL /*randfile*/))
130 {
131 soap_print_fault(&soap, stderr);
132 exit(1);
133 }
134#endif /* WITH_OPENSSL */
135
136 const char *pcszMode = argv[ap];
137 int soaprc = SOAP_SVR_FAULT;
138
139 if (!strcmp(pcszMode, "logon"))
140 {
141 if (argc < 3 + ap)
142 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
143 else
144 {
145 _vbox__IWebsessionManager_USCORElogon req;
146 req.username = argv[ap + 1];
147 req.password = argv[ap + 2];
148 _vbox__IWebsessionManager_USCORElogonResponse resp;
149
150 if (!(soaprc = soap_call___vbox__IWebsessionManager_USCORElogon(&soap,
151 pcszArgEndpoint,
152 NULL,
153 &req,
154 &resp)))
155 std::cout << "VirtualBox objref: \"" << resp.returnval << "\"\n";
156 }
157 }
158 else if (!strcmp(pcszMode, "getsession"))
159 {
160 if (argc < 2 + ap)
161 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
162 else
163 {
164 _vbox__IWebsessionManager_USCOREgetSessionObject req;
165 req.refIVirtualBox = argv[ap + 1];
166 _vbox__IWebsessionManager_USCOREgetSessionObjectResponse resp;
167
168 if (!(soaprc = soap_call___vbox__IWebsessionManager_USCOREgetSessionObject(&soap,
169 pcszArgEndpoint,
170 NULL,
171 &req,
172 &resp)))
173 std::cout << "session: \"" << resp.returnval << "\"\n";
174 }
175 }
176 else if (!strcmp(pcszMode, "logoff"))
177 {
178 if (argc < 2 + ap)
179 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
180 else
181 {
182 _vbox__IWebsessionManager_USCORElogoff req;
183 req.refIVirtualBox = argv[ap + 1];
184 _vbox__IWebsessionManager_USCORElogoffResponse resp;
185
186 if (!(soaprc = soap_call___vbox__IWebsessionManager_USCORElogoff(&soap,
187 pcszArgEndpoint,
188 NULL,
189 &req,
190 &resp)))
191 {
192 ;
193 }
194 }
195 }
196 else if (!strcmp(pcszMode, "version"))
197 {
198 if (argc < 2 + ap)
199 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
200 else
201 {
202 _vbox__IVirtualBox_USCOREgetVersion req;
203 req._USCOREthis = argv[ap + 1];
204 _vbox__IVirtualBox_USCOREgetVersionResponse resp;
205
206 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetVersion(&soap,
207 pcszArgEndpoint,
208 NULL,
209 &req,
210 &resp)))
211 std::cout << "version: \"" << resp.returnval << "\"\n";
212 }
213 }
214 else if (!strcmp(pcszMode, "gethost"))
215 {
216 if (argc < 2 + ap)
217 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
218 else
219 {
220 _vbox__IVirtualBox_USCOREgetHost req;
221 req._USCOREthis = argv[ap + 1];
222 _vbox__IVirtualBox_USCOREgetHostResponse resp;
223
224 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetHost(&soap,
225 pcszArgEndpoint,
226 NULL,
227 &req,
228 &resp)))
229 {
230 std::cout << "Host objref " << resp.returnval << "\n";
231 }
232 }
233 }
234 else if (!strcmp(pcszMode, "getpc"))
235 {
236 if (argc < 2 + ap)
237 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
238 else
239 {
240 _vbox__IVirtualBox_USCOREgetPerformanceCollector req;
241 req._USCOREthis = argv[ap + 1];
242 _vbox__IVirtualBox_USCOREgetPerformanceCollectorResponse resp;
243
244 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetPerformanceCollector(&soap,
245 pcszArgEndpoint,
246 NULL,
247 &req,
248 &resp)))
249 {
250 std::cout << "Performance collector objref " << resp.returnval << "\n";
251 }
252 }
253 }
254 else if (!strcmp(pcszMode, "getmachines"))
255 {
256 if (argc < 2 + ap)
257 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
258 else
259 {
260 _vbox__IVirtualBox_USCOREgetMachines req;
261 req._USCOREthis = argv[ap + 1];
262 _vbox__IVirtualBox_USCOREgetMachinesResponse resp;
263
264 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREgetMachines(&soap,
265 pcszArgEndpoint,
266 NULL,
267 &req,
268 &resp)))
269 {
270 size_t c = resp.returnval.size();
271 for (size_t i = 0;
272 i < c;
273 ++i)
274 {
275 std::cout << "Machine " << i << ": objref " << resp.returnval[i] << "\n";
276 }
277 }
278 }
279 }
280 else if (!strcmp(pcszMode, "createmachine"))
281 {
282 if (argc < 4 + ap)
283 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
284 else
285 {
286 _vbox__IVirtualBox_USCOREcreateMachine req;
287 req._USCOREthis = argv[ap + 1];
288 req.settingsFile = argv[ap + 2];
289 req.name = argv[ap + 3];
290 std::cout << "createmachine: settingsFile = \"" << req.settingsFile << "\", name = \"" << req.name << "\"\n";
291 _vbox__IVirtualBox_USCOREcreateMachineResponse resp;
292
293 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREcreateMachine(&soap,
294 pcszArgEndpoint,
295 NULL,
296 &req,
297 &resp)))
298 std::cout << "Machine created: managed object reference ID is " << resp.returnval << "\n";
299 }
300 }
301 else if (!strcmp(pcszMode, "registermachine"))
302 {
303 if (argc < 3 + ap)
304 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
305 else
306 {
307 _vbox__IVirtualBox_USCOREregisterMachine req;
308 req._USCOREthis = argv[ap + 1];
309 req.machine = argv[ap + 2];
310 _vbox__IVirtualBox_USCOREregisterMachineResponse resp;
311 if (!(soaprc = soap_call___vbox__IVirtualBox_USCOREregisterMachine(&soap,
312 pcszArgEndpoint,
313 NULL,
314 &req,
315 &resp)))
316 std::cout << "Machine registered.\n";
317 }
318 }
319 else if (!strcmp(pcszMode, "getdvddrives"))
320 {
321 if (argc < 2 + ap)
322 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
323 else
324 {
325 _vbox__IHost_USCOREgetDVDDrives req;
326 req._USCOREthis = argv[ap + 1];
327 _vbox__IHost_USCOREgetDVDDrivesResponse resp;
328 if (!(soaprc = soap_call___vbox__IHost_USCOREgetDVDDrives(&soap,
329 pcszArgEndpoint,
330 NULL,
331 &req,
332 &resp)))
333 {
334 size_t c = resp.returnval.size();
335 for (size_t i = 0;
336 i < c;
337 ++i)
338 {
339 std::cout << "DVD drive " << i << ": objref " << resp.returnval[i] << "\n";
340 }
341 }
342 }
343 }
344 else if (!strcmp(pcszMode, "getname"))
345 {
346 if (argc < 2 + ap)
347 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
348 else
349 {
350 _vbox__IMachine_USCOREgetName req;
351 req._USCOREthis = argv[ap + 1];
352 _vbox__IMachine_USCOREgetNameResponse resp;
353 if (!(soaprc = soap_call___vbox__IMachine_USCOREgetName(&soap,
354 pcszArgEndpoint,
355 NULL,
356 &req,
357 &resp)))
358 printf("Name is: %s\n", resp.returnval.c_str());
359 }
360 }
361 else if (!strcmp(pcszMode, "getid"))
362 {
363 if (argc < 2 + ap)
364 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
365 else
366 {
367 _vbox__IMachine_USCOREgetId req;
368 req._USCOREthis = argv[ap + 1];
369 _vbox__IMachine_USCOREgetIdResponse resp;
370 if (!(soaprc = soap_call___vbox__IMachine_USCOREgetId(&soap,
371 pcszArgEndpoint,
372 NULL,
373 &req,
374 &resp)))
375 std::cout << "UUID is: " << resp.returnval << "\n";;
376 }
377 }
378 else if (!strcmp(pcszMode, "getostypeid"))
379 {
380 if (argc < 2 + ap)
381 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
382 else
383 {
384 _vbox__IMachine_USCOREgetOSTypeId req;
385 req._USCOREthis = argv[ap + 1];
386 _vbox__IMachine_USCOREgetOSTypeIdResponse resp;
387 if (!(soaprc = soap_call___vbox__IMachine_USCOREgetOSTypeId(&soap,
388 pcszArgEndpoint,
389 NULL,
390 &req,
391 &resp)))
392 std::cout << "Guest OS type is: " << resp.returnval << "\n";
393 }
394 }
395 else if (!strcmp(pcszMode, "savesettings"))
396 {
397 if (argc < 2 + ap)
398 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
399 else
400 {
401 _vbox__IMachine_USCOREsaveSettings req;
402 req._USCOREthis = argv[ap + 1];
403 _vbox__IMachine_USCOREsaveSettingsResponse resp;
404 if (!(soaprc = soap_call___vbox__IMachine_USCOREsaveSettings(&soap,
405 pcszArgEndpoint,
406 NULL,
407 &req,
408 &resp)))
409 std::cout << "Settings saved\n";
410 }
411 }
412 else if (!strcmp(pcszMode, "setupmetrics"))
413 {
414 if (argc < 2 + ap)
415 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
416 else
417 {
418 _vbox__IPerformanceCollector_USCOREsetupMetrics req;
419 req._USCOREthis = argv[ap + 1];
420// req.metricNames[0] = "*";
421// req.objects
422 req.period = 1; // seconds
423 req.count = 100;
424 _vbox__IPerformanceCollector_USCOREsetupMetricsResponse resp;
425 if (!(soaprc = soap_call___vbox__IPerformanceCollector_USCOREsetupMetrics(&soap,
426 pcszArgEndpoint,
427 NULL,
428 &req,
429 &resp)))
430 {
431 size_t c = resp.returnval.size();
432 for (size_t i = 0;
433 i < c;
434 ++i)
435 {
436 std::cout << "Metric " << i << ": objref " << resp.returnval[i] << "\n";
437 }
438 }
439 }
440 }
441 else if (!strcmp(pcszMode, "querymetricsdata"))
442 {
443 if (argc < 2 + ap)
444 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
445 else
446 {
447 _vbox__IPerformanceCollector_USCOREqueryMetricsData req;
448 req._USCOREthis = argv[ap + 1];
449// req.metricNames[0] = "*";
450// req.objects
451 _vbox__IPerformanceCollector_USCOREqueryMetricsDataResponse resp;
452 if (!(soaprc = soap_call___vbox__IPerformanceCollector_USCOREqueryMetricsData(&soap,
453 pcszArgEndpoint,
454 NULL,
455 &req,
456 &resp)))
457 {
458 size_t c = resp.returnval.size();
459 for (size_t i = 0;
460 i < c;
461 ++i)
462 {
463 std::cout << "long " << i << ": " << resp.returnval[i] << "\n";
464 }
465 }
466 }
467 }
468 else if (!strcmp(pcszMode, "errorinfo"))
469 {
470 if (argc < 2 + ap)
471 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
472 else
473 {
474 _vbox__IVirtualBoxErrorInfo_USCOREgetResultCode req;
475 req._USCOREthis = argv[ap + 1];
476 _vbox__IVirtualBoxErrorInfo_USCOREgetResultCodeResponse resp;
477 if (!(soaprc = soap_call___vbox__IVirtualBoxErrorInfo_USCOREgetResultCode(&soap,
478 pcszArgEndpoint,
479 NULL,
480 &req,
481 &resp)))
482 {
483 std::cout << "ErrorInfo ResultCode: " << std::hex << resp.returnval << "\n";
484
485 _vbox__IVirtualBoxErrorInfo_USCOREgetText req2;
486 req2._USCOREthis = argv[ap + 1];
487 _vbox__IVirtualBoxErrorInfo_USCOREgetTextResponse resp2;
488 if (!(soaprc = soap_call___vbox__IVirtualBoxErrorInfo_USCOREgetText(&soap,
489 pcszArgEndpoint,
490 NULL,
491 &req2,
492 &resp2)))
493 {
494 std::cout << "ErrorInfo Text: " << resp2.returnval << "\n";
495
496 _vbox__IVirtualBoxErrorInfo_USCOREgetNext req3;
497 req3._USCOREthis = argv[ap + 1];
498 _vbox__IVirtualBoxErrorInfo_USCOREgetNextResponse resp3;
499 if (!(soaprc = soap_call___vbox__IVirtualBoxErrorInfo_USCOREgetNext(&soap,
500 pcszArgEndpoint,
501 NULL,
502 &req3,
503 &resp3)))
504 std::cout << "Next ErrorInfo: " << resp3.returnval << "\n";
505 }
506 }
507 }
508 }
509 else if (!strcmp(pcszMode, "release"))
510 {
511 if (argc < 2 + ap)
512 std::cout << "Not enough arguments for \"" << pcszMode << "\" mode.\n";
513 else
514 {
515 _vbox__IManagedObjectRef_USCORErelease req;
516 req._USCOREthis = argv[ap + 1];
517 _vbox__IManagedObjectRef_USCOREreleaseResponse resp;
518 if (!(soaprc = soap_call___vbox__IManagedObjectRef_USCORErelease(&soap,
519 pcszArgEndpoint,
520 NULL,
521 &req,
522 &resp)))
523 std::cout << "Managed object reference " << req._USCOREthis << " released.\n";
524 }
525 }
526 else
527 std::cout << "Unknown mode parameter \"" << pcszMode << "\".\n";
528
529 if (soaprc)
530 {
531 if ( (soap.fault)
532 && (soap.fault->detail)
533 )
534 {
535 // generic fault message whether the fault is known or not
536 std::cerr << "Generic fault message:\n";
537 soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream
538
539 if (soap.fault->detail->vbox__InvalidObjectFault)
540 {
541 std::cerr << "Bad object ID: " << soap.fault->detail->vbox__InvalidObjectFault->badObjectID << "\n";
542 }
543 else if (soap.fault->detail->vbox__RuntimeFault)
544 {
545 std::cerr << "Result code: 0x" << std::hex << soap.fault->detail->vbox__RuntimeFault->resultCode << "\n";
546 std::cerr << "ErrorInfo: " << soap.fault->detail->vbox__RuntimeFault->returnval << "\n";
547 }
548 }
549 else
550 {
551 std::cerr << "Invalid fault data, fault message:\n";
552 soap_print_fault(&soap, stderr); // display the SOAP fault message on the stderr stream
553 }
554 }
555
556 soap_destroy(&soap); // delete deserialized class instances (for C++ only)
557 soap_end(&soap); // remove deserialized data and clean up
558 soap_done(&soap); // detach the gSOAP environment
559
560 return soaprc;
561}
562
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