1 | /* $Id: vkatCmdSelfTest.cpp 90918 2021-08-26 15:29:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Validation Kit Audio Test (VKAT) - Self test.
|
---|
4 | *
|
---|
5 | * Self-test which does a complete audio testing framework run without the need
|
---|
6 | * of a VM or other infrastructure, i.e. all required parts are running locally
|
---|
7 | * on the same machine.
|
---|
8 | *
|
---|
9 | * This self-test does the following:
|
---|
10 | * - 1. Creates a separate thread for the guest side VKAT and connects to the ATS instance on
|
---|
11 | * the host side at port 6052 (ATS_TCP_DEF_BIND_PORT_HOST).
|
---|
12 | * - 2. Uses the Validation Kit audio backend, which in turn creates an ATS instance
|
---|
13 | * listening at port 6062 (ATS_TCP_DEF_BIND_PORT_VALKIT).
|
---|
14 | * - 3. Uses the host test environment which creates an ATS instance
|
---|
15 | * listening at port 6052 (ATS_TCP_DEF_BIND_PORT_HOST).
|
---|
16 | * - 4. Executes a complete test run locally (e.g. without any guest (VM) involved).
|
---|
17 | */
|
---|
18 |
|
---|
19 | /*
|
---|
20 | * Copyright (C) 2021 Oracle Corporation
|
---|
21 | *
|
---|
22 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
23 | * available from http://www.virtualbox.org. This file is free software;
|
---|
24 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
25 | * General Public License (GPL) as published by the Free Software
|
---|
26 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
27 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
28 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
29 | *
|
---|
30 | * The contents of this file may alternatively be used under the terms
|
---|
31 | * of the Common Development and Distribution License Version 1.0
|
---|
32 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
33 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
34 | * CDDL are applicable instead of those of the GPL.
|
---|
35 | *
|
---|
36 | * You may elect to license modified versions of this file under the
|
---|
37 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
38 | */
|
---|
39 |
|
---|
40 |
|
---|
41 | /*********************************************************************************************************************************
|
---|
42 | * Header Files *
|
---|
43 | *********************************************************************************************************************************/
|
---|
44 |
|
---|
45 | #include <iprt/ctype.h>
|
---|
46 | #include <iprt/errcore.h>
|
---|
47 | #include <iprt/getopt.h>
|
---|
48 | #include <iprt/message.h>
|
---|
49 | #include <iprt/test.h>
|
---|
50 |
|
---|
51 | #include "Audio/AudioHlp.h"
|
---|
52 | #include "Audio/AudioTest.h"
|
---|
53 | #include "Audio/AudioTestService.h"
|
---|
54 | #include "Audio/AudioTestServiceClient.h"
|
---|
55 |
|
---|
56 | #include "vkatInternal.h"
|
---|
57 |
|
---|
58 |
|
---|
59 | /*********************************************************************************************************************************
|
---|
60 | * Internal structures *
|
---|
61 | *********************************************************************************************************************************/
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Structure for keeping a VKAT self test context.
|
---|
65 | */
|
---|
66 | typedef struct SELFTESTCTX
|
---|
67 | {
|
---|
68 | /** Common tag for guest and host side. */
|
---|
69 | char szTag[AUDIOTEST_TAG_MAX];
|
---|
70 | /** Whether to use DrvAudio in the driver stack or not. */
|
---|
71 | bool fWithDrvAudio;
|
---|
72 | AUDIOTESTDRVSTACK DrvStack;
|
---|
73 | /** Audio driver to use.
|
---|
74 | * Defaults to the platform's default driver. */
|
---|
75 | PCPDMDRVREG pDrvReg;
|
---|
76 | struct
|
---|
77 | {
|
---|
78 | AUDIOTESTENV TstEnv;
|
---|
79 | /** Where to bind the address of the guest ATS instance to.
|
---|
80 | * Defaults to localhost (127.0.0.1) if empty. */
|
---|
81 | char szAtsAddr[64];
|
---|
82 | /** Port of the guest ATS instance.
|
---|
83 | * Defaults to ATS_ALT_PORT if not set. */
|
---|
84 | uint32_t uAtsPort;
|
---|
85 | } Guest;
|
---|
86 | struct
|
---|
87 | {
|
---|
88 | AUDIOTESTENV TstEnv;
|
---|
89 | /** Address of the guest ATS instance.
|
---|
90 | * Defaults to localhost (127.0.0.1) if not set. */
|
---|
91 | char szGuestAtsAddr[64];
|
---|
92 | /** Port of the guest ATS instance.
|
---|
93 | * Defaults to ATS_DEFAULT_PORT if not set. */
|
---|
94 | uint32_t uGuestAtsPort;
|
---|
95 | /** Address of the Validation Kit audio driver ATS instance.
|
---|
96 | * Defaults to localhost (127.0.0.1) if not set. */
|
---|
97 | char szValKitAtsAddr[64];
|
---|
98 | /** Port of the Validation Kit audio driver ATS instance.
|
---|
99 | * Defaults to ATS_ALT_PORT if not set. */
|
---|
100 | uint32_t uValKitAtsPort;
|
---|
101 | } Host;
|
---|
102 | } SELFTESTCTX;
|
---|
103 | /** Pointer to a VKAT self test context. */
|
---|
104 | typedef SELFTESTCTX *PSELFTESTCTX;
|
---|
105 |
|
---|
106 |
|
---|
107 | /*********************************************************************************************************************************
|
---|
108 | * Global Variables *
|
---|
109 | *********************************************************************************************************************************/
|
---|
110 |
|
---|
111 | /** The global self-text context. */
|
---|
112 | static SELFTESTCTX g_Ctx;
|
---|
113 |
|
---|
114 |
|
---|
115 | /*********************************************************************************************************************************
|
---|
116 | * Self-test implementation *
|
---|
117 | *********************************************************************************************************************************/
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Thread callback for mocking the guest (VM) side of things.
|
---|
121 | *
|
---|
122 | * @returns VBox status code.
|
---|
123 | * @param hThread Thread handle.
|
---|
124 | * @param pvUser Pointer to user-supplied data.
|
---|
125 | */
|
---|
126 | static DECLCALLBACK(int) audioTestSelftestGuestAtsThread(RTTHREAD hThread, void *pvUser)
|
---|
127 | {
|
---|
128 | RT_NOREF(hThread);
|
---|
129 | PSELFTESTCTX pCtx = (PSELFTESTCTX)pvUser;
|
---|
130 |
|
---|
131 | PAUDIOTESTENV pTstEnvGst = &pCtx->Guest.TstEnv;
|
---|
132 |
|
---|
133 | /* Flag the environment for self test mode. */
|
---|
134 | pTstEnvGst->fSelftest = true;
|
---|
135 |
|
---|
136 | /* Tweak the address the guest ATS is trying to connect to the host if anything else is specified.
|
---|
137 | * Note: The host also runs on the same host (this self-test is completely self-contained and does not need a VM). */
|
---|
138 | if (!pTstEnvGst->TcpOpts.szConnectAddr[0])
|
---|
139 | RTStrCopy(pTstEnvGst->TcpOpts.szConnectAddr, sizeof(pTstEnvGst->TcpOpts.szConnectAddr), "127.0.0.1");
|
---|
140 |
|
---|
141 | /* Generate tag for guest side. */
|
---|
142 | int rc = RTStrCopy(pTstEnvGst->szTag, sizeof(pTstEnvGst->szTag), pCtx->szTag);
|
---|
143 | AssertRCReturn(rc, rc);
|
---|
144 |
|
---|
145 | rc = AudioTestPathCreateTemp(pTstEnvGst->szPathTemp, sizeof(pTstEnvGst->szPathTemp), "selftest-guest");
|
---|
146 | AssertRCReturn(rc, rc);
|
---|
147 |
|
---|
148 | rc = AudioTestPathCreateTemp(pTstEnvGst->szPathOut, sizeof(pTstEnvGst->szPathOut), "selftest-out");
|
---|
149 | AssertRCReturn(rc, rc);
|
---|
150 |
|
---|
151 | pTstEnvGst->enmMode = AUDIOTESTMODE_GUEST;
|
---|
152 |
|
---|
153 | /** @todo Make this customizable. */
|
---|
154 | PDMAudioPropsInit(&pTstEnvGst->Props,
|
---|
155 | 2 /* 16-bit */, true /* fSigned */, 2 /* cChannels */, 44100 /* uHz */);
|
---|
156 |
|
---|
157 | rc = audioTestEnvInit(pTstEnvGst, &pCtx->DrvStack);
|
---|
158 | if (RT_SUCCESS(rc))
|
---|
159 | {
|
---|
160 | RTThreadUserSignal(hThread);
|
---|
161 |
|
---|
162 | audioTestWorker(pTstEnvGst);
|
---|
163 | audioTestEnvDestroy(pTstEnvGst);
|
---|
164 | }
|
---|
165 |
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * Main function for performing the self test.
|
---|
171 | *
|
---|
172 | * @returns RTEXITCODE
|
---|
173 | * @param pCtx Self test context to use.
|
---|
174 | */
|
---|
175 | RTEXITCODE audioTestDoSelftest(PSELFTESTCTX pCtx)
|
---|
176 | {
|
---|
177 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Running self test ...\n");
|
---|
178 |
|
---|
179 | /* Generate a common tag for guest and host side. */
|
---|
180 | int rc = AudioTestGenTag(pCtx->szTag, sizeof(pCtx->szTag));
|
---|
181 | AssertRCReturn(rc, RTEXITCODE_FAILURE);
|
---|
182 |
|
---|
183 | PAUDIOTESTENV pTstEnvHst = &pCtx->Host.TstEnv;
|
---|
184 |
|
---|
185 | /* Flag the environment for self test mode. */
|
---|
186 | pTstEnvHst->fSelftest = true;
|
---|
187 |
|
---|
188 | /* Generate tag for host side. */
|
---|
189 | rc = RTStrCopy(pTstEnvHst->szTag, sizeof(pTstEnvHst->szTag), pCtx->szTag);
|
---|
190 | AssertRCReturn(rc, RTEXITCODE_FAILURE);
|
---|
191 |
|
---|
192 | rc = AudioTestPathCreateTemp(pTstEnvHst->szPathTemp, sizeof(pTstEnvHst->szPathTemp), "selftest-tmp");
|
---|
193 | AssertRCReturn(rc, RTEXITCODE_FAILURE);
|
---|
194 |
|
---|
195 | rc = AudioTestPathCreateTemp(pTstEnvHst->szPathOut, sizeof(pTstEnvHst->szPathOut), "selftest-out");
|
---|
196 | AssertRCReturn(rc, RTEXITCODE_FAILURE);
|
---|
197 |
|
---|
198 | /* Initialize the PCM properties to some sane values. */
|
---|
199 | PDMAudioPropsInit(&pTstEnvHst->Props,
|
---|
200 | 2 /* 16-bit */, true /* fPcmSigned */, 2 /* cPcmChannels */, 44100 /* uPcmHz */);
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * Step 1.
|
---|
204 | */
|
---|
205 | RTTHREAD hThreadGstAts = NIL_RTTHREAD;
|
---|
206 |
|
---|
207 | bool const fStartGuestAts = RTStrNLen(pCtx->Host.szGuestAtsAddr, sizeof(pCtx->Host.szGuestAtsAddr)) == 0;
|
---|
208 | if (fStartGuestAts)
|
---|
209 | {
|
---|
210 | /* Step 1b. */
|
---|
211 | rc = RTThreadCreate(&hThreadGstAts, audioTestSelftestGuestAtsThread, pCtx, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE,
|
---|
212 | "VKATGstAts");
|
---|
213 | if (RT_SUCCESS(rc))
|
---|
214 | rc = RTThreadUserWait(hThreadGstAts, RT_MS_30SEC);
|
---|
215 | }
|
---|
216 |
|
---|
217 | RTThreadSleep(2000); /* Fudge: Wait until guest ATS is up. 2 seconds should be enough (tm). */
|
---|
218 |
|
---|
219 | if (RT_SUCCESS(rc))
|
---|
220 | {
|
---|
221 | /*
|
---|
222 | * Steps 2 + 3.
|
---|
223 | */
|
---|
224 | pTstEnvHst->enmMode = AUDIOTESTMODE_HOST;
|
---|
225 |
|
---|
226 | rc = audioTestEnvInit(pTstEnvHst, &pCtx->DrvStack);
|
---|
227 | if (RT_SUCCESS(rc))
|
---|
228 | {
|
---|
229 | /*
|
---|
230 | * Step 4.
|
---|
231 | */
|
---|
232 | rc = audioTestWorker(pTstEnvHst);
|
---|
233 | if (RT_SUCCESS(rc))
|
---|
234 | {
|
---|
235 |
|
---|
236 | }
|
---|
237 |
|
---|
238 | audioTestEnvDestroy(pTstEnvHst);
|
---|
239 | }
|
---|
240 | }
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Shutting down.
|
---|
244 | */
|
---|
245 | RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Shutting down self test\n");
|
---|
246 |
|
---|
247 | /* If we started the guest ATS ourselves, wait for it to terminate properly. */
|
---|
248 | if (fStartGuestAts)
|
---|
249 | {
|
---|
250 | int rcThread;
|
---|
251 | int rc2 = RTThreadWait(hThreadGstAts, RT_MS_30SEC, &rcThread);
|
---|
252 | if (RT_SUCCESS(rc2))
|
---|
253 | rc2 = rcThread;
|
---|
254 | if (RT_FAILURE(rc2))
|
---|
255 | RTTestFailed(g_hTest, "Shutting down guest ATS failed with %Rrc\n", rc2);
|
---|
256 | if (RT_SUCCESS(rc))
|
---|
257 | rc = rc2;
|
---|
258 | }
|
---|
259 |
|
---|
260 | if (RT_FAILURE(rc))
|
---|
261 | RTTestFailed(g_hTest, "Self test failed with %Rrc\n", rc);
|
---|
262 |
|
---|
263 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | /*********************************************************************************************************************************
|
---|
268 | * Command: selftest *
|
---|
269 | *********************************************************************************************************************************/
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Command line parameters for self-test mode.
|
---|
273 | */
|
---|
274 | static const RTGETOPTDEF s_aCmdSelftestOptions[] =
|
---|
275 | {
|
---|
276 | { "--exclude-all", 'a', RTGETOPT_REQ_NOTHING },
|
---|
277 | { "--backend", 'b', RTGETOPT_REQ_STRING },
|
---|
278 | { "--with-drv-audio", 'd', RTGETOPT_REQ_NOTHING },
|
---|
279 | { "--exclude", 'e', RTGETOPT_REQ_UINT32 },
|
---|
280 | { "--include", 'i', RTGETOPT_REQ_UINT32 }
|
---|
281 | };
|
---|
282 |
|
---|
283 | /** the 'selftest' command option help. */
|
---|
284 | static DECLCALLBACK(const char *) audioTestCmdSelftestHelp(PCRTGETOPTDEF pOpt)
|
---|
285 | {
|
---|
286 | switch (pOpt->iShort)
|
---|
287 | {
|
---|
288 | case 'a': return "Exclude all tests from the list (useful to enable single tests later with --include)";
|
---|
289 | case 'b': return "The audio backend to use";
|
---|
290 | case 'd': return "Go via DrvAudio instead of directly interfacing with the backend";
|
---|
291 | case 'e': return "Exclude the given test id from the list";
|
---|
292 | case 'i': return "Include the given test id in the list";
|
---|
293 | default: return NULL;
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * The 'selftest' command handler.
|
---|
299 | *
|
---|
300 | * @returns Program exit code.
|
---|
301 | * @param pGetState RTGetOpt state.
|
---|
302 | */
|
---|
303 | DECLCALLBACK(RTEXITCODE) audioTestCmdSelftestHandler(PRTGETOPTSTATE pGetState)
|
---|
304 | {
|
---|
305 | RT_ZERO(g_Ctx);
|
---|
306 |
|
---|
307 | /* Argument processing loop: */
|
---|
308 | int rc;
|
---|
309 | RTGETOPTUNION ValueUnion;
|
---|
310 | while ((rc = RTGetOpt(pGetState, &ValueUnion)) != 0)
|
---|
311 | {
|
---|
312 | switch (rc)
|
---|
313 | {
|
---|
314 | case 'a':
|
---|
315 | for (unsigned i = 0; i < g_cTests; i++)
|
---|
316 | g_aTests[i].fExcluded = true;
|
---|
317 | break;
|
---|
318 |
|
---|
319 | case 'b':
|
---|
320 | g_Ctx.pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz);
|
---|
321 | if (g_Ctx.pDrvReg == NULL)
|
---|
322 | return RTEXITCODE_SYNTAX;
|
---|
323 | break;
|
---|
324 |
|
---|
325 | case 'd':
|
---|
326 | g_Ctx.fWithDrvAudio = true;
|
---|
327 | break;
|
---|
328 |
|
---|
329 | case 'e':
|
---|
330 | if (ValueUnion.u32 >= g_cTests)
|
---|
331 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --exclude", ValueUnion.u32);
|
---|
332 | g_aTests[ValueUnion.u32].fExcluded = true;
|
---|
333 | break;
|
---|
334 |
|
---|
335 | case 'i':
|
---|
336 | if (ValueUnion.u32 >= g_cTests)
|
---|
337 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --include", ValueUnion.u32);
|
---|
338 | g_aTests[ValueUnion.u32].fExcluded = false;
|
---|
339 | break;
|
---|
340 |
|
---|
341 | AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
|
---|
342 |
|
---|
343 | default:
|
---|
344 | return RTGetOptPrintError(rc, &ValueUnion);
|
---|
345 | }
|
---|
346 | }
|
---|
347 |
|
---|
348 | /* Go with the Validation Kit audio backend if nothing else is specified. */
|
---|
349 | if (g_Ctx.pDrvReg == NULL)
|
---|
350 | g_Ctx.pDrvReg = AudioTestFindBackendOpt("valkit");
|
---|
351 |
|
---|
352 | /*
|
---|
353 | * In self-test mode the guest and the host side have to share the same driver stack,
|
---|
354 | * as we don't have any device emulation between the two sides.
|
---|
355 | *
|
---|
356 | * This is necessary to actually get the played/recorded audio to from/to the guest
|
---|
357 | * and host respectively.
|
---|
358 | *
|
---|
359 | * Choosing any other backend than the Validation Kit above *will* break this self-test!
|
---|
360 | */
|
---|
361 | rc = audioTestDriverStackInitEx(&g_Ctx.DrvStack, g_Ctx.pDrvReg,
|
---|
362 | true /* fEnabledIn */, true /* fEnabledOut */, g_Ctx.fWithDrvAudio);
|
---|
363 | if (RT_FAILURE(rc))
|
---|
364 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unable to init driver stack: %Rrc\n", rc);
|
---|
365 |
|
---|
366 | /*
|
---|
367 | * Start testing.
|
---|
368 | */
|
---|
369 | RTTestBanner(g_hTest);
|
---|
370 |
|
---|
371 | int rc2 = audioTestDoSelftest(&g_Ctx);
|
---|
372 | if (RT_FAILURE(rc2))
|
---|
373 | RTTestFailed(g_hTest, "Self test failed with rc=%Rrc", rc2);
|
---|
374 |
|
---|
375 | audioTestDriverStackDelete(&g_Ctx.DrvStack);
|
---|
376 |
|
---|
377 | /*
|
---|
378 | * Print summary and exit.
|
---|
379 | */
|
---|
380 | return RTTestSummaryAndDestroy(g_hTest);
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * Command table entry for 'selftest'.
|
---|
385 | */
|
---|
386 | const VKATCMD g_CmdSelfTest =
|
---|
387 | {
|
---|
388 | "selftest",
|
---|
389 | audioTestCmdSelftestHandler,
|
---|
390 | "Performs self-tests.",
|
---|
391 | s_aCmdSelftestOptions,
|
---|
392 | RT_ELEMENTS(s_aCmdSelftestOptions),
|
---|
393 | audioTestCmdSelftestHelp,
|
---|
394 | true /* fNeedsTransport */
|
---|
395 | };
|
---|
396 |
|
---|