VirtualBox

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

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

Frontends + Main: Adjust to the new rules wrt. to rc -> hrc,vrc usage. This also fixes quite a few bugs wrt shadow variables, wrong return values and output error translations / exit codes. Also see @todos added. ​​bugref:10223

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