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