VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstVBoxAPI.cpp@ 100841

Last change on this file since 100841 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.1 KB
Line 
1/* $Id: tstVBoxAPI.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * tstVBoxAPI - Checks VirtualBox API.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <VBox/com/com.h>
33#include <VBox/com/string.h>
34#include <VBox/com/array.h>
35#include <VBox/com/Guid.h>
36#include <VBox/com/ErrorInfo.h>
37#include <VBox/com/errorprint.h>
38#include <VBox/com/VirtualBox.h>
39#include <VBox/sup.h>
40
41#include <iprt/test.h>
42#include <iprt/time.h>
43
44using namespace com;
45
46
47/*********************************************************************************************************************************
48* Global Variables *
49*********************************************************************************************************************************/
50static RTTEST g_hTest;
51static Bstr tstMachineName = "tstVBoxAPI test VM";
52
53
54/** Worker for TST_COM_EXPR(). */
55static HRESULT tstComExpr(HRESULT hrc, const char *pszOperation, int iLine)
56{
57 if (FAILED(hrc))
58 RTTestFailed(g_hTest, "%s failed on line %u with hrc=%Rhrc", pszOperation, iLine, hrc);
59 return hrc;
60}
61
62/** Macro that executes the given expression and report any failure.
63 * The expression must return a HRESULT. */
64#define TST_COM_EXPR(expr) tstComExpr(expr, #expr, __LINE__)
65
66
67static BOOL tstApiIVirtualBox(IVirtualBox *pVBox)
68{
69 HRESULT hrc;
70 Bstr bstrTmp;
71 ULONG ulTmp;
72
73 RTTestSub(g_hTest, "IVirtualBox::version");
74 CHECK_ERROR(pVBox, COMGETTER(Version)(bstrTmp.asOutParam()));
75 if (SUCCEEDED(hrc))
76 RTTestPassed(g_hTest, "IVirtualBox::version");
77 else
78 RTTestFailed(g_hTest, "%d: IVirtualBox::version failed", __LINE__);
79
80 RTTestSub(g_hTest, "IVirtualBox::versionNormalized");
81 CHECK_ERROR(pVBox, COMGETTER(VersionNormalized)(bstrTmp.asOutParam()));
82 if (SUCCEEDED(hrc))
83 RTTestPassed(g_hTest, "IVirtualBox::versionNormalized");
84 else
85 RTTestFailed(g_hTest, "%d: IVirtualBox::versionNormalized failed", __LINE__);
86
87 RTTestSub(g_hTest, "IVirtualBox::revision");
88 CHECK_ERROR(pVBox, COMGETTER(Revision)(&ulTmp));
89 if (SUCCEEDED(hrc))
90 RTTestPassed(g_hTest, "IVirtualBox::revision");
91 else
92 RTTestFailed(g_hTest, "%d: IVirtualBox::revision failed", __LINE__);
93
94 RTTestSub(g_hTest, "IVirtualBox::packageType");
95 CHECK_ERROR(pVBox, COMGETTER(PackageType)(bstrTmp.asOutParam()));
96 if (SUCCEEDED(hrc))
97 RTTestPassed(g_hTest, "IVirtualBox::packageType");
98 else
99 RTTestFailed(g_hTest, "%d: IVirtualBox::packageType failed", __LINE__);
100
101 RTTestSub(g_hTest, "IVirtualBox::APIVersion");
102 CHECK_ERROR(pVBox, COMGETTER(APIVersion)(bstrTmp.asOutParam()));
103 if (SUCCEEDED(hrc))
104 RTTestPassed(g_hTest, "IVirtualBox::APIVersion");
105 else
106 RTTestFailed(g_hTest, "%d: IVirtualBox::APIVersion failed", __LINE__);
107
108 RTTestSub(g_hTest, "IVirtualBox::homeFolder");
109 CHECK_ERROR(pVBox, COMGETTER(HomeFolder)(bstrTmp.asOutParam()));
110 if (SUCCEEDED(hrc))
111 RTTestPassed(g_hTest, "IVirtualBox::homeFolder");
112 else
113 RTTestFailed(g_hTest, "%d: IVirtualBox::homeFolder failed", __LINE__);
114
115 RTTestSub(g_hTest, "IVirtualBox::settingsFilePath");
116 CHECK_ERROR(pVBox, COMGETTER(SettingsFilePath)(bstrTmp.asOutParam()));
117 if (SUCCEEDED(hrc))
118 RTTestPassed(g_hTest, "IVirtualBox::settingsFilePath");
119 else
120 RTTestFailed(g_hTest, "%d: IVirtualBox::settingsFilePath failed", __LINE__);
121
122 com::SafeIfaceArray<IGuestOSType> guestOSTypes;
123 RTTestSub(g_hTest, "IVirtualBox::guestOSTypes");
124 CHECK_ERROR(pVBox, COMGETTER(GuestOSTypes)(ComSafeArrayAsOutParam(guestOSTypes)));
125 if (SUCCEEDED(hrc))
126 RTTestPassed(g_hTest, "IVirtualBox::guestOSTypes");
127 else
128 RTTestFailed(g_hTest, "%d: IVirtualBox::guestOSTypes failed", __LINE__);
129
130 /** Create VM */
131 RTTestSub(g_hTest, "IVirtualBox::CreateMachine");
132 ComPtr<IMachine> ptrMachine;
133 com::SafeArray<BSTR> groups;
134 /** Default VM settings */
135 CHECK_ERROR(pVBox, CreateMachine(NULL, /** Settings */
136 tstMachineName.raw(), /** Name */
137 ComSafeArrayAsInParam(groups), /** Groups */
138 NULL, /** OS Type */
139 NULL, /** Create flags */
140 NULL, /** Cipher */
141 NULL, /** Password id */
142 NULL, /** Password */
143 ptrMachine.asOutParam())); /** Machine */
144 if (SUCCEEDED(hrc))
145 RTTestPassed(g_hTest, "IVirtualBox::CreateMachine");
146 else
147 {
148 RTTestFailed(g_hTest, "%d: IVirtualBox::CreateMachine failed", __LINE__);
149 return FALSE;
150 }
151
152 RTTestSub(g_hTest, "IVirtualBox::RegisterMachine");
153 CHECK_ERROR(pVBox, RegisterMachine(ptrMachine));
154 if (SUCCEEDED(hrc))
155 RTTestPassed(g_hTest, "IVirtualBox::RegisterMachine");
156 else
157 {
158 RTTestFailed(g_hTest, "%d: IVirtualBox::RegisterMachine failed", __LINE__);
159 return FALSE;
160 }
161
162 ComPtr<IHost> host;
163 RTTestSub(g_hTest, "IVirtualBox::host");
164 CHECK_ERROR(pVBox, COMGETTER(Host)(host.asOutParam()));
165 if (SUCCEEDED(hrc))
166 {
167 /** @todo Add IHost testing here. */
168 RTTestPassed(g_hTest, "IVirtualBox::host");
169 }
170 else
171 RTTestFailed(g_hTest, "%d: IVirtualBox::host failed", __LINE__);
172
173 ComPtr<ISystemProperties> sysprop;
174 RTTestSub(g_hTest, "IVirtualBox::systemProperties");
175 CHECK_ERROR(pVBox, COMGETTER(SystemProperties)(sysprop.asOutParam()));
176 if (SUCCEEDED(hrc))
177 {
178 /** @todo Add ISystemProperties testing here. */
179 RTTestPassed(g_hTest, "IVirtualBox::systemProperties");
180 }
181 else
182 RTTestFailed(g_hTest, "%d: IVirtualBox::systemProperties failed", __LINE__);
183
184 com::SafeIfaceArray<IMachine> machines;
185 RTTestSub(g_hTest, "IVirtualBox::machines");
186 CHECK_ERROR(pVBox, COMGETTER(Machines)(ComSafeArrayAsOutParam(machines)));
187 if (SUCCEEDED(hrc))
188 {
189 bool bFound = FALSE;
190 for (size_t i = 0; i < machines.size(); ++i)
191 {
192 if (machines[i])
193 {
194 Bstr tmpName;
195 hrc = machines[i]->COMGETTER(Name)(tmpName.asOutParam());
196 if (SUCCEEDED(hrc))
197 {
198 if (tmpName == tstMachineName)
199 {
200 bFound = TRUE;
201 break;
202 }
203 }
204 }
205 }
206
207 if (bFound)
208 RTTestPassed(g_hTest, "IVirtualBox::machines");
209 else
210 RTTestFailed(g_hTest, "%d: IVirtualBox::machines failed. No created machine found", __LINE__);
211 }
212 else
213 RTTestFailed(g_hTest, "%d: IVirtualBox::machines failed", __LINE__);
214
215#if 0 /** Not yet implemented */
216 com::SafeIfaceArray<ISharedFolder> sharedFolders;
217 RTTestSub(g_hTest, "IVirtualBox::sharedFolders");
218 CHECK_ERROR(pVBox, COMGETTER(SharedFolders)(ComSafeArrayAsOutParam(sharedFolders)));
219 if (SUCCEEDED(rc))
220 {
221 /** @todo Add ISharedFolders testing here. */
222 RTTestPassed(g_hTest, "IVirtualBox::sharedFolders");
223 }
224 else
225 RTTestFailed(g_hTest, "%d: IVirtualBox::sharedFolders failed", __LINE__);
226#endif
227
228 com::SafeIfaceArray<IMedium> hardDisks;
229 RTTestSub(g_hTest, "IVirtualBox::hardDisks");
230 CHECK_ERROR(pVBox, COMGETTER(HardDisks)(ComSafeArrayAsOutParam(hardDisks)));
231 if (SUCCEEDED(hrc))
232 {
233 /** @todo Add hardDisks testing here. */
234 RTTestPassed(g_hTest, "IVirtualBox::hardDisks");
235 }
236 else
237 RTTestFailed(g_hTest, "%d: IVirtualBox::hardDisks failed", __LINE__);
238
239 com::SafeIfaceArray<IMedium> DVDImages;
240 RTTestSub(g_hTest, "IVirtualBox::DVDImages");
241 CHECK_ERROR(pVBox, COMGETTER(DVDImages)(ComSafeArrayAsOutParam(DVDImages)));
242 if (SUCCEEDED(hrc))
243 {
244 /** @todo Add DVDImages testing here. */
245 RTTestPassed(g_hTest, "IVirtualBox::DVDImages");
246 }
247 else
248 RTTestFailed(g_hTest, "%d: IVirtualBox::DVDImages failed", __LINE__);
249
250 com::SafeIfaceArray<IMedium> floppyImages;
251 RTTestSub(g_hTest, "IVirtualBox::floppyImages");
252 CHECK_ERROR(pVBox, COMGETTER(FloppyImages)(ComSafeArrayAsOutParam(floppyImages)));
253 if (SUCCEEDED(hrc))
254 {
255 /** @todo Add floppyImages testing here. */
256 RTTestPassed(g_hTest, "IVirtualBox::floppyImages");
257 }
258 else
259 RTTestFailed(g_hTest, "%d: IVirtualBox::floppyImages failed", __LINE__);
260
261 com::SafeIfaceArray<IProgress> progressOperations;
262 RTTestSub(g_hTest, "IVirtualBox::progressOperations");
263 CHECK_ERROR(pVBox, COMGETTER(ProgressOperations)(ComSafeArrayAsOutParam(progressOperations)));
264 if (SUCCEEDED(hrc))
265 {
266 /** @todo Add IProgress testing here. */
267 RTTestPassed(g_hTest, "IVirtualBox::progressOperations");
268 }
269 else
270 RTTestFailed(g_hTest, "%d: IVirtualBox::progressOperations failed", __LINE__);
271
272 ComPtr<IPerformanceCollector> performanceCollector;
273 RTTestSub(g_hTest, "IVirtualBox::performanceCollector");
274 CHECK_ERROR(pVBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()));
275 if (SUCCEEDED(hrc))
276 {
277 /** @todo Add IPerformanceCollector testing here. */
278 RTTestPassed(g_hTest, "IVirtualBox::performanceCollector");
279 }
280 else
281 RTTestFailed(g_hTest, "%d: IVirtualBox::performanceCollector failed", __LINE__);
282
283 com::SafeIfaceArray<IDHCPServer> DHCPServers;
284 RTTestSub(g_hTest, "IVirtualBox::DHCPServers");
285 CHECK_ERROR(pVBox, COMGETTER(DHCPServers)(ComSafeArrayAsOutParam(DHCPServers)));
286 if (SUCCEEDED(hrc))
287 {
288 /** @todo Add IDHCPServers testing here. */
289 RTTestPassed(g_hTest, "IVirtualBox::DHCPServers");
290 }
291 else
292 RTTestFailed(g_hTest, "%d: IVirtualBox::DHCPServers failed", __LINE__);
293
294 com::SafeIfaceArray<INATNetwork> NATNetworks;
295 RTTestSub(g_hTest, "IVirtualBox::NATNetworks");
296 CHECK_ERROR(pVBox, COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(NATNetworks)));
297 if (SUCCEEDED(hrc))
298 {
299 /** @todo Add INATNetworks testing here. */
300 RTTestPassed(g_hTest, "IVirtualBox::NATNetworks");
301 }
302 else
303 RTTestFailed(g_hTest, "%d: IVirtualBox::NATNetworks failed", __LINE__);
304
305 ComPtr<IEventSource> eventSource;
306 RTTestSub(g_hTest, "IVirtualBox::eventSource");
307 CHECK_ERROR(pVBox, COMGETTER(EventSource)(eventSource.asOutParam()));
308 if (SUCCEEDED(hrc))
309 {
310 /** @todo Add IEventSource testing here. */
311 RTTestPassed(g_hTest, "IVirtualBox::eventSource");
312 }
313 else
314 RTTestFailed(g_hTest, "%d: IVirtualBox::eventSource failed", __LINE__);
315
316 ComPtr<IExtPackManager> extensionPackManager;
317 RTTestSub(g_hTest, "IVirtualBox::extensionPackManager");
318 CHECK_ERROR(pVBox, COMGETTER(ExtensionPackManager)(extensionPackManager.asOutParam()));
319 if (SUCCEEDED(hrc))
320 {
321 /** @todo Add IExtPackManager testing here. */
322 RTTestPassed(g_hTest, "IVirtualBox::extensionPackManager");
323 }
324 else
325 RTTestFailed(g_hTest, "%d: IVirtualBox::extensionPackManager failed", __LINE__);
326
327 com::SafeArray<BSTR> internalNetworks;
328 RTTestSub(g_hTest, "IVirtualBox::internalNetworks");
329 CHECK_ERROR(pVBox, COMGETTER(InternalNetworks)(ComSafeArrayAsOutParam(internalNetworks)));
330 if (SUCCEEDED(hrc))
331 {
332 RTTestPassed(g_hTest, "IVirtualBox::internalNetworks");
333 }
334 else
335 RTTestFailed(g_hTest, "%d: IVirtualBox::internalNetworks failed", __LINE__);
336
337 com::SafeArray<BSTR> genericNetworkDrivers;
338 RTTestSub(g_hTest, "IVirtualBox::genericNetworkDrivers");
339 CHECK_ERROR(pVBox, COMGETTER(GenericNetworkDrivers)(ComSafeArrayAsOutParam(genericNetworkDrivers)));
340 if (SUCCEEDED(hrc))
341 {
342 RTTestPassed(g_hTest, "IVirtualBox::genericNetworkDrivers");
343 }
344 else
345 RTTestFailed(g_hTest, "%d: IVirtualBox::genericNetworkDrivers failed", __LINE__);
346
347 return TRUE;
348}
349
350
351static BOOL tstApiClean(IVirtualBox *pVBox)
352{
353 HRESULT hrc;
354
355 /** Delete created VM and its files */
356 ComPtr<IMachine> machine;
357 CHECK_ERROR_RET(pVBox, FindMachine(Bstr(tstMachineName).raw(), machine.asOutParam()), FALSE);
358 SafeIfaceArray<IMedium> media;
359 CHECK_ERROR_RET(machine, Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
360 ComSafeArrayAsOutParam(media)), FALSE);
361 ComPtr<IProgress> progress;
362 CHECK_ERROR_RET(machine, DeleteConfig(ComSafeArrayAsInParam(media), progress.asOutParam()), FALSE);
363 CHECK_ERROR_RET(progress, WaitForCompletion(-1), FALSE);
364
365 return TRUE;
366}
367
368
369int main()
370{
371 /*
372 * Initialization.
373 */
374 RTEXITCODE rcExit = RTTestInitAndCreate("tstVBoxAPI", &g_hTest);
375 if (rcExit != RTEXITCODE_SUCCESS)
376 return rcExit;
377 SUPR3Init(NULL); /* Better time support. */
378 RTTestBanner(g_hTest);
379
380 RTTestSub(g_hTest, "Initializing COM and singletons");
381 HRESULT hrc = com::Initialize();
382 if (SUCCEEDED(hrc))
383 {
384 ComPtr<IVirtualBoxClient> ptrVBoxClient;
385 ComPtr<IVirtualBox> ptrVBox;
386 hrc = TST_COM_EXPR(ptrVBoxClient.createInprocObject(CLSID_VirtualBoxClient));
387 if (SUCCEEDED(hrc))
388 hrc = TST_COM_EXPR(ptrVBoxClient->COMGETTER(VirtualBox)(ptrVBox.asOutParam()));
389 if (SUCCEEDED(hrc))
390 {
391 ComPtr<ISession> ptrSession;
392 hrc = TST_COM_EXPR(ptrSession.createInprocObject(CLSID_Session));
393 if (SUCCEEDED(hrc))
394 {
395 RTTestSubDone(g_hTest);
396
397 /*
398 * Call test functions.
399 */
400
401 /** Test IVirtualBox interface */
402 tstApiIVirtualBox(ptrVBox);
403
404
405 /** Clean files/configs */
406 tstApiClean(ptrVBox);
407 }
408 }
409
410 ptrVBox.setNull();
411 ptrVBoxClient.setNull();
412 com::Shutdown();
413 }
414 else
415 RTTestIFailed("com::Initialize failed with hrc=%Rhrc", hrc);
416 return RTTestSummaryAndDestroy(g_hTest);
417}
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