1 | /* $Id: tstVBoxAPI.cpp 69500 2017-10-28 15:14:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * tstVBoxAPI - Checks VirtualBox API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 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 |
|
---|
34 | using namespace com;
|
---|
35 |
|
---|
36 |
|
---|
37 | /*********************************************************************************************************************************
|
---|
38 | * Global Variables *
|
---|
39 | *********************************************************************************************************************************/
|
---|
40 | static RTTEST g_hTest;
|
---|
41 | static Bstr tstMachineName = "tstVBoxAPI test VM";
|
---|
42 |
|
---|
43 |
|
---|
44 | /** Worker for TST_COM_EXPR(). */
|
---|
45 | static 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 |
|
---|
57 | static BOOL tstApiIVirtualBox(IVirtualBox *pVBox)
|
---|
58 | {
|
---|
59 | HRESULT rc;
|
---|
60 | Bstr bstrTmp;
|
---|
61 | ULONG ulTmp;
|
---|
62 |
|
---|
63 | RTTestSub(g_hTest, "IVirtualBox::version");
|
---|
64 | CHECK_ERROR(pVBox, COMGETTER(Version)(bstrTmp.asOutParam()));
|
---|
65 | if (SUCCEEDED(rc))
|
---|
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(rc))
|
---|
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(rc))
|
---|
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(rc))
|
---|
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(rc))
|
---|
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(rc))
|
---|
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(rc))
|
---|
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(rc))
|
---|
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 | ptrMachine.asOutParam())); /** Machine */
|
---|
131 | if (SUCCEEDED(rc))
|
---|
132 | RTTestPassed(g_hTest, "IVirtualBox::CreateMachine");
|
---|
133 | else
|
---|
134 | {
|
---|
135 | RTTestFailed(g_hTest, "%d: IVirtualBox::CreateMachine failed", __LINE__);
|
---|
136 | return FALSE;
|
---|
137 | }
|
---|
138 |
|
---|
139 | RTTestSub(g_hTest, "IVirtualBox::RegisterMachine");
|
---|
140 | CHECK_ERROR(pVBox, RegisterMachine(ptrMachine));
|
---|
141 | if (SUCCEEDED(rc))
|
---|
142 | RTTestPassed(g_hTest, "IVirtualBox::RegisterMachine");
|
---|
143 | else
|
---|
144 | {
|
---|
145 | RTTestFailed(g_hTest, "%d: IVirtualBox::RegisterMachine failed", __LINE__);
|
---|
146 | return FALSE;
|
---|
147 | }
|
---|
148 |
|
---|
149 | ComPtr<IHost> host;
|
---|
150 | RTTestSub(g_hTest, "IVirtualBox::host");
|
---|
151 | CHECK_ERROR(pVBox, COMGETTER(Host)(host.asOutParam()));
|
---|
152 | if (SUCCEEDED(rc))
|
---|
153 | {
|
---|
154 | /** @todo Add IHost testing here. */
|
---|
155 | RTTestPassed(g_hTest, "IVirtualBox::host");
|
---|
156 | }
|
---|
157 | else
|
---|
158 | RTTestFailed(g_hTest, "%d: IVirtualBox::host failed", __LINE__);
|
---|
159 |
|
---|
160 | ComPtr<ISystemProperties> sysprop;
|
---|
161 | RTTestSub(g_hTest, "IVirtualBox::systemProperties");
|
---|
162 | CHECK_ERROR(pVBox, COMGETTER(SystemProperties)(sysprop.asOutParam()));
|
---|
163 | if (SUCCEEDED(rc))
|
---|
164 | {
|
---|
165 | /** @todo Add ISystemProperties testing here. */
|
---|
166 | RTTestPassed(g_hTest, "IVirtualBox::systemProperties");
|
---|
167 | }
|
---|
168 | else
|
---|
169 | RTTestFailed(g_hTest, "%d: IVirtualBox::systemProperties failed", __LINE__);
|
---|
170 |
|
---|
171 | com::SafeIfaceArray<IMachine> machines;
|
---|
172 | RTTestSub(g_hTest, "IVirtualBox::machines");
|
---|
173 | CHECK_ERROR(pVBox, COMGETTER(Machines)(ComSafeArrayAsOutParam(machines)));
|
---|
174 | if (SUCCEEDED(rc))
|
---|
175 | {
|
---|
176 | bool bFound = FALSE;
|
---|
177 | for (size_t i = 0; i < machines.size(); ++i)
|
---|
178 | {
|
---|
179 | if (machines[i])
|
---|
180 | {
|
---|
181 | Bstr tmpName;
|
---|
182 | rc = machines[i]->COMGETTER(Name)(tmpName.asOutParam());
|
---|
183 | if (SUCCEEDED(rc))
|
---|
184 | {
|
---|
185 | if (tmpName == tstMachineName)
|
---|
186 | {
|
---|
187 | bFound = TRUE;
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | if (bFound)
|
---|
195 | RTTestPassed(g_hTest, "IVirtualBox::machines");
|
---|
196 | else
|
---|
197 | RTTestFailed(g_hTest, "%d: IVirtualBox::machines failed. No created machine found", __LINE__);
|
---|
198 | }
|
---|
199 | else
|
---|
200 | RTTestFailed(g_hTest, "%d: IVirtualBox::machines failed", __LINE__);
|
---|
201 |
|
---|
202 | #if 0 /** Not yet implemented */
|
---|
203 | com::SafeIfaceArray<ISharedFolder> sharedFolders;
|
---|
204 | RTTestSub(g_hTest, "IVirtualBox::sharedFolders");
|
---|
205 | CHECK_ERROR(pVBox, COMGETTER(SharedFolders)(ComSafeArrayAsOutParam(sharedFolders)));
|
---|
206 | if (SUCCEEDED(rc))
|
---|
207 | {
|
---|
208 | /** @todo Add ISharedFolders testing here. */
|
---|
209 | RTTestPassed(g_hTest, "IVirtualBox::sharedFolders");
|
---|
210 | }
|
---|
211 | else
|
---|
212 | RTTestFailed(g_hTest, "%d: IVirtualBox::sharedFolders failed", __LINE__);
|
---|
213 | #endif
|
---|
214 |
|
---|
215 | com::SafeIfaceArray<IMedium> hardDisks;
|
---|
216 | RTTestSub(g_hTest, "IVirtualBox::hardDisks");
|
---|
217 | CHECK_ERROR(pVBox, COMGETTER(HardDisks)(ComSafeArrayAsOutParam(hardDisks)));
|
---|
218 | if (SUCCEEDED(rc))
|
---|
219 | {
|
---|
220 | /** @todo Add hardDisks testing here. */
|
---|
221 | RTTestPassed(g_hTest, "IVirtualBox::hardDisks");
|
---|
222 | }
|
---|
223 | else
|
---|
224 | RTTestFailed(g_hTest, "%d: IVirtualBox::hardDisks failed", __LINE__);
|
---|
225 |
|
---|
226 | com::SafeIfaceArray<IMedium> DVDImages;
|
---|
227 | RTTestSub(g_hTest, "IVirtualBox::DVDImages");
|
---|
228 | CHECK_ERROR(pVBox, COMGETTER(DVDImages)(ComSafeArrayAsOutParam(DVDImages)));
|
---|
229 | if (SUCCEEDED(rc))
|
---|
230 | {
|
---|
231 | /** @todo Add DVDImages testing here. */
|
---|
232 | RTTestPassed(g_hTest, "IVirtualBox::DVDImages");
|
---|
233 | }
|
---|
234 | else
|
---|
235 | RTTestFailed(g_hTest, "%d: IVirtualBox::DVDImages failed", __LINE__);
|
---|
236 |
|
---|
237 | com::SafeIfaceArray<IMedium> floppyImages;
|
---|
238 | RTTestSub(g_hTest, "IVirtualBox::floppyImages");
|
---|
239 | CHECK_ERROR(pVBox, COMGETTER(FloppyImages)(ComSafeArrayAsOutParam(floppyImages)));
|
---|
240 | if (SUCCEEDED(rc))
|
---|
241 | {
|
---|
242 | /** @todo Add floppyImages testing here. */
|
---|
243 | RTTestPassed(g_hTest, "IVirtualBox::floppyImages");
|
---|
244 | }
|
---|
245 | else
|
---|
246 | RTTestFailed(g_hTest, "%d: IVirtualBox::floppyImages failed", __LINE__);
|
---|
247 |
|
---|
248 | com::SafeIfaceArray<IProgress> progressOperations;
|
---|
249 | RTTestSub(g_hTest, "IVirtualBox::progressOperations");
|
---|
250 | CHECK_ERROR(pVBox, COMGETTER(ProgressOperations)(ComSafeArrayAsOutParam(progressOperations)));
|
---|
251 | if (SUCCEEDED(rc))
|
---|
252 | {
|
---|
253 | /** @todo Add IProgress testing here. */
|
---|
254 | RTTestPassed(g_hTest, "IVirtualBox::progressOperations");
|
---|
255 | }
|
---|
256 | else
|
---|
257 | RTTestFailed(g_hTest, "%d: IVirtualBox::progressOperations failed", __LINE__);
|
---|
258 |
|
---|
259 | ComPtr<IPerformanceCollector> performanceCollector;
|
---|
260 | RTTestSub(g_hTest, "IVirtualBox::performanceCollector");
|
---|
261 | CHECK_ERROR(pVBox, COMGETTER(PerformanceCollector)(performanceCollector.asOutParam()));
|
---|
262 | if (SUCCEEDED(rc))
|
---|
263 | {
|
---|
264 | /** @todo Add IPerformanceCollector testing here. */
|
---|
265 | RTTestPassed(g_hTest, "IVirtualBox::performanceCollector");
|
---|
266 | }
|
---|
267 | else
|
---|
268 | RTTestFailed(g_hTest, "%d: IVirtualBox::performanceCollector failed", __LINE__);
|
---|
269 |
|
---|
270 | com::SafeIfaceArray<IDHCPServer> DHCPServers;
|
---|
271 | RTTestSub(g_hTest, "IVirtualBox::DHCPServers");
|
---|
272 | CHECK_ERROR(pVBox, COMGETTER(DHCPServers)(ComSafeArrayAsOutParam(DHCPServers)));
|
---|
273 | if (SUCCEEDED(rc))
|
---|
274 | {
|
---|
275 | /** @todo Add IDHCPServers testing here. */
|
---|
276 | RTTestPassed(g_hTest, "IVirtualBox::DHCPServers");
|
---|
277 | }
|
---|
278 | else
|
---|
279 | RTTestFailed(g_hTest, "%d: IVirtualBox::DHCPServers failed", __LINE__);
|
---|
280 |
|
---|
281 | com::SafeIfaceArray<INATNetwork> NATNetworks;
|
---|
282 | RTTestSub(g_hTest, "IVirtualBox::NATNetworks");
|
---|
283 | CHECK_ERROR(pVBox, COMGETTER(NATNetworks)(ComSafeArrayAsOutParam(NATNetworks)));
|
---|
284 | if (SUCCEEDED(rc))
|
---|
285 | {
|
---|
286 | /** @todo Add INATNetworks testing here. */
|
---|
287 | RTTestPassed(g_hTest, "IVirtualBox::NATNetworks");
|
---|
288 | }
|
---|
289 | else
|
---|
290 | RTTestFailed(g_hTest, "%d: IVirtualBox::NATNetworks failed", __LINE__);
|
---|
291 |
|
---|
292 | ComPtr<IEventSource> eventSource;
|
---|
293 | RTTestSub(g_hTest, "IVirtualBox::eventSource");
|
---|
294 | CHECK_ERROR(pVBox, COMGETTER(EventSource)(eventSource.asOutParam()));
|
---|
295 | if (SUCCEEDED(rc))
|
---|
296 | {
|
---|
297 | /** @todo Add IEventSource testing here. */
|
---|
298 | RTTestPassed(g_hTest, "IVirtualBox::eventSource");
|
---|
299 | }
|
---|
300 | else
|
---|
301 | RTTestFailed(g_hTest, "%d: IVirtualBox::eventSource failed", __LINE__);
|
---|
302 |
|
---|
303 | ComPtr<IExtPackManager> extensionPackManager;
|
---|
304 | RTTestSub(g_hTest, "IVirtualBox::extensionPackManager");
|
---|
305 | CHECK_ERROR(pVBox, COMGETTER(ExtensionPackManager)(extensionPackManager.asOutParam()));
|
---|
306 | if (SUCCEEDED(rc))
|
---|
307 | {
|
---|
308 | /** @todo Add IExtPackManager testing here. */
|
---|
309 | RTTestPassed(g_hTest, "IVirtualBox::extensionPackManager");
|
---|
310 | }
|
---|
311 | else
|
---|
312 | RTTestFailed(g_hTest, "%d: IVirtualBox::extensionPackManager failed", __LINE__);
|
---|
313 |
|
---|
314 | com::SafeArray<BSTR> internalNetworks;
|
---|
315 | RTTestSub(g_hTest, "IVirtualBox::internalNetworks");
|
---|
316 | CHECK_ERROR(pVBox, COMGETTER(InternalNetworks)(ComSafeArrayAsOutParam(internalNetworks)));
|
---|
317 | if (SUCCEEDED(rc))
|
---|
318 | {
|
---|
319 | RTTestPassed(g_hTest, "IVirtualBox::internalNetworks");
|
---|
320 | }
|
---|
321 | else
|
---|
322 | RTTestFailed(g_hTest, "%d: IVirtualBox::internalNetworks failed", __LINE__);
|
---|
323 |
|
---|
324 | com::SafeArray<BSTR> genericNetworkDrivers;
|
---|
325 | RTTestSub(g_hTest, "IVirtualBox::genericNetworkDrivers");
|
---|
326 | CHECK_ERROR(pVBox, COMGETTER(GenericNetworkDrivers)(ComSafeArrayAsOutParam(genericNetworkDrivers)));
|
---|
327 | if (SUCCEEDED(rc))
|
---|
328 | {
|
---|
329 | RTTestPassed(g_hTest, "IVirtualBox::genericNetworkDrivers");
|
---|
330 | }
|
---|
331 | else
|
---|
332 | RTTestFailed(g_hTest, "%d: IVirtualBox::genericNetworkDrivers failed", __LINE__);
|
---|
333 |
|
---|
334 | return TRUE;
|
---|
335 | }
|
---|
336 |
|
---|
337 |
|
---|
338 | static BOOL tstApiClean(IVirtualBox *pVBox)
|
---|
339 | {
|
---|
340 | HRESULT rc;
|
---|
341 |
|
---|
342 | /** Delete created VM and its files */
|
---|
343 | ComPtr<IMachine> machine;
|
---|
344 | CHECK_ERROR_RET(pVBox, FindMachine(Bstr(tstMachineName).raw(), machine.asOutParam()), FALSE);
|
---|
345 | SafeIfaceArray<IMedium> media;
|
---|
346 | CHECK_ERROR_RET(machine, Unregister(CleanupMode_DetachAllReturnHardDisksOnly,
|
---|
347 | ComSafeArrayAsOutParam(media)), FALSE);
|
---|
348 | ComPtr<IProgress> progress;
|
---|
349 | CHECK_ERROR_RET(machine, DeleteConfig(ComSafeArrayAsInParam(media), progress.asOutParam()), FALSE);
|
---|
350 | CHECK_ERROR_RET(progress, WaitForCompletion(-1), FALSE);
|
---|
351 |
|
---|
352 | return TRUE;
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | int main()
|
---|
357 | {
|
---|
358 | /*
|
---|
359 | * Initialization.
|
---|
360 | */
|
---|
361 | RTEXITCODE rcExit = RTTestInitAndCreate("tstVBoxAPI", &g_hTest);
|
---|
362 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
363 | return rcExit;
|
---|
364 | SUPR3Init(NULL); /* Better time support. */
|
---|
365 | RTTestBanner(g_hTest);
|
---|
366 |
|
---|
367 | RTTestSub(g_hTest, "Initializing COM and singletons");
|
---|
368 | HRESULT hrc = com::Initialize();
|
---|
369 | if (SUCCEEDED(hrc))
|
---|
370 | {
|
---|
371 | ComPtr<IVirtualBoxClient> ptrVBoxClient;
|
---|
372 | ComPtr<IVirtualBox> ptrVBox;
|
---|
373 | hrc = TST_COM_EXPR(ptrVBoxClient.createInprocObject(CLSID_VirtualBoxClient));
|
---|
374 | if (SUCCEEDED(hrc))
|
---|
375 | hrc = TST_COM_EXPR(ptrVBoxClient->COMGETTER(VirtualBox)(ptrVBox.asOutParam()));
|
---|
376 | if (SUCCEEDED(hrc))
|
---|
377 | {
|
---|
378 | ComPtr<ISession> ptrSession;
|
---|
379 | hrc = TST_COM_EXPR(ptrSession.createInprocObject(CLSID_Session));
|
---|
380 | if (SUCCEEDED(hrc))
|
---|
381 | {
|
---|
382 | RTTestSubDone(g_hTest);
|
---|
383 |
|
---|
384 | /*
|
---|
385 | * Call test functions.
|
---|
386 | */
|
---|
387 |
|
---|
388 | /** Test IVirtualBox interface */
|
---|
389 | tstApiIVirtualBox(ptrVBox);
|
---|
390 |
|
---|
391 |
|
---|
392 | /** Clean files/configs */
|
---|
393 | tstApiClean(ptrVBox);
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | ptrVBox.setNull();
|
---|
398 | ptrVBoxClient.setNull();
|
---|
399 | com::Shutdown();
|
---|
400 | }
|
---|
401 | else
|
---|
402 | RTTestIFailed("com::Initialize failed with hrc=%Rhrc", hrc);
|
---|
403 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
404 | }
|
---|