VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/utils/audio/vkatCmdSelfTest.cpp@ 92195

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

Audio/Validation Kit: Use the RTTEST macros in selftest mode. ​bugref:10008

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1/* $Id: vkatCmdSelfTest.cpp 92051 2021-10-25 17:40:49Z 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/rand.h>
50#include <iprt/test.h>
51
52#include "Audio/AudioHlp.h"
53#include "Audio/AudioTest.h"
54#include "Audio/AudioTestService.h"
55#include "Audio/AudioTestServiceClient.h"
56
57#include "vkatInternal.h"
58
59
60/*********************************************************************************************************************************
61* Internal structures *
62*********************************************************************************************************************************/
63
64/**
65 * Structure for keeping a VKAT self test context.
66 */
67typedef struct SELFTESTCTX
68{
69 /** Common tag for guest and host side. */
70 char szTag[AUDIOTEST_TAG_MAX];
71 /** Whether to use DrvAudio in the driver stack or not. */
72 bool fWithDrvAudio;
73 AUDIOTESTDRVSTACK DrvStack;
74 /** Audio driver to use.
75 * Defaults to the platform's default driver. */
76 PCPDMDRVREG pDrvReg;
77 struct
78 {
79 AUDIOTESTENV TstEnv;
80 /** Where to bind the address of the guest ATS instance to.
81 * Defaults to localhost (127.0.0.1) if empty. */
82 char szAtsAddr[64];
83 /** Port of the guest ATS instance.
84 * Defaults to ATS_ALT_PORT if not set. */
85 uint32_t uAtsPort;
86 } Guest;
87 struct
88 {
89 AUDIOTESTENV TstEnv;
90 /** Address of the guest ATS instance.
91 * Defaults to localhost (127.0.0.1) if not set. */
92 char szGuestAtsAddr[64];
93 /** Port of the guest ATS instance.
94 * Defaults to ATS_DEFAULT_PORT if not set. */
95 uint32_t uGuestAtsPort;
96 /** Address of the Validation Kit audio driver ATS instance.
97 * Defaults to localhost (127.0.0.1) if not set. */
98 char szValKitAtsAddr[64];
99 /** Port of the Validation Kit audio driver ATS instance.
100 * Defaults to ATS_ALT_PORT if not set. */
101 uint32_t uValKitAtsPort;
102 } Host;
103} SELFTESTCTX;
104/** Pointer to a VKAT self test context. */
105typedef SELFTESTCTX *PSELFTESTCTX;
106
107
108/*********************************************************************************************************************************
109* Global Variables *
110*********************************************************************************************************************************/
111
112/** The global self-text context. */
113static SELFTESTCTX g_Ctx;
114
115
116/*********************************************************************************************************************************
117* Driver stack self-test implementation *
118*********************************************************************************************************************************/
119
120/**
121 * Performs a (quick) audio driver stack self test.
122 *
123 * Local only, no guest/host communication involved.
124 *
125 * @returns VBox status code.
126 */
127int AudioTestDriverStackPerformSelftest(void)
128{
129 PCPDMDRVREG pDrvReg = AudioTestGetDefaultBackend();
130
131 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Testing driver stack started\n");
132
133 AUDIOTESTDRVSTACK DrvStack;
134 int rc = audioTestDriverStackProbe(&DrvStack, pDrvReg,
135 true /* fEnabledIn */, true /* fEnabledOut */, false /* fWithDrvAudio */);
136 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
137
138 AUDIOTESTIOOPTS IoOpts;
139 audioTestIoOptsInitDefaults(&IoOpts);
140
141 PPDMAUDIOSTREAM pStream;
142 PDMAUDIOSTREAMCFG CfgAcq;
143 rc = audioTestDriverStackStreamCreateOutput(&DrvStack, &IoOpts.Props,
144 IoOpts.cMsBufferSize, IoOpts.cMsPreBuffer, IoOpts.cMsSchedulingHint,
145 &pStream, &CfgAcq);
146 AssertRCReturn(rc, rc);
147
148 rc = audioTestDriverStackStreamEnable(&DrvStack, pStream);
149 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
150
151 RTTEST_CHECK_RET(g_hTest, audioTestDriverStackStreamIsOkay(&DrvStack, pStream), VERR_AUDIO_STREAM_NOT_READY);
152
153 uint8_t abBuf[_4K];
154 memset(abBuf, 0x42, sizeof(abBuf));
155
156 uint32_t cbWritten;
157 rc = audioTestDriverStackStreamPlay(&DrvStack, pStream, abBuf, sizeof(abBuf), &cbWritten);
158 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
159 RTTEST_CHECK_RET(g_hTest, cbWritten == sizeof(abBuf), VERR_AUDIO_STREAM_NOT_READY);
160
161 audioTestDriverStackStreamDrain(&DrvStack, pStream, true /* fSync */);
162 audioTestDriverStackStreamDestroy(&DrvStack, pStream);
163
164 audioTestDriverStackDelete(&DrvStack);
165
166 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Testing driver stack ended with %Rrc\n", rc);
167 return rc;
168}
169
170
171/*********************************************************************************************************************************
172* Self-test implementation *
173*********************************************************************************************************************************/
174
175/**
176 * Thread callback for mocking the guest (VM) side of things.
177 *
178 * @returns VBox status code.
179 * @param hThread Thread handle.
180 * @param pvUser Pointer to user-supplied data.
181 */
182static DECLCALLBACK(int) audioTestSelftestGuestAtsThread(RTTHREAD hThread, void *pvUser)
183{
184 RT_NOREF(hThread);
185 PSELFTESTCTX pCtx = (PSELFTESTCTX)pvUser;
186
187 PAUDIOTESTENV pTstEnvGst = &pCtx->Guest.TstEnv;
188
189 audioTestEnvInit(pTstEnvGst);
190
191 /* Flag the environment for self test mode. */
192 pTstEnvGst->fSelftest = true;
193
194 /* Tweak the address the guest ATS is trying to connect to the host if anything else is specified.
195 * Note: The host also runs on the same host (this self-test is completely self-contained and does not need a VM). */
196 if (!pTstEnvGst->TcpOpts.szConnectAddr[0])
197 RTStrCopy(pTstEnvGst->TcpOpts.szConnectAddr, sizeof(pTstEnvGst->TcpOpts.szConnectAddr), "127.0.0.1");
198
199 /* Generate tag for guest side. */
200 int rc = RTStrCopy(pTstEnvGst->szTag, sizeof(pTstEnvGst->szTag), pCtx->szTag);
201 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
202
203 rc = AudioTestPathCreateTemp(pTstEnvGst->szPathTemp, sizeof(pTstEnvGst->szPathTemp), "selftest-guest");
204 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
205
206 rc = AudioTestPathCreateTemp(pTstEnvGst->szPathOut, sizeof(pTstEnvGst->szPathOut), "selftest-out");
207 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
208
209 pTstEnvGst->enmMode = AUDIOTESTMODE_GUEST;
210
211 rc = audioTestEnvCreate(pTstEnvGst, &pCtx->DrvStack);
212 if (RT_SUCCESS(rc))
213 {
214 RTThreadUserSignal(hThread);
215
216 rc = audioTestWorker(pTstEnvGst);
217 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, rc);
218
219 audioTestEnvDestroy(pTstEnvGst);
220 }
221
222 return rc;
223}
224
225/**
226 * Main function for performing the self test.
227 *
228 * @returns RTEXITCODE
229 * @param pCtx Self test context to use.
230 */
231RTEXITCODE audioTestDoSelftest(PSELFTESTCTX pCtx)
232{
233 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Running self test ...\n");
234
235 /* Generate a common tag for guest and host side. */
236 int rc = AudioTestGenTag(pCtx->szTag, sizeof(pCtx->szTag));
237 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE);
238
239 PAUDIOTESTENV pTstEnvHst = &pCtx->Host.TstEnv;
240
241 audioTestEnvInit(pTstEnvHst);
242
243 /* Flag the environment for self test mode. */
244 pTstEnvHst->fSelftest = true;
245
246 /* One test iteration with a 5s maximum test tone is enough for a (quick) self test. */
247 pTstEnvHst->cIterations = 1;
248 pTstEnvHst->ToneParms.msDuration = RTRandU32Ex(500, RT_MS_5SEC);
249
250 /* Generate tag for host side. */
251 rc = RTStrCopy(pTstEnvHst->szTag, sizeof(pTstEnvHst->szTag), pCtx->szTag);
252 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE);
253
254 rc = AudioTestPathCreateTemp(pTstEnvHst->szPathTemp, sizeof(pTstEnvHst->szPathTemp), "selftest-tmp");
255 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE);
256
257 rc = AudioTestPathCreateTemp(pTstEnvHst->szPathOut, sizeof(pTstEnvHst->szPathOut), "selftest-out");
258 RTTEST_CHECK_RC_OK_RET(g_hTest, rc, RTEXITCODE_FAILURE);
259
260 /*
261 * Step 1.
262 */
263 RTTHREAD hThreadGstAts = NIL_RTTHREAD;
264
265 bool const fStartGuestAts = RTStrNLen(pCtx->Host.szGuestAtsAddr, sizeof(pCtx->Host.szGuestAtsAddr)) == 0;
266 if (fStartGuestAts)
267 {
268 /* Step 1b. */
269 rc = RTThreadCreate(&hThreadGstAts, audioTestSelftestGuestAtsThread, pCtx, 0, RTTHREADTYPE_IO, RTTHREADFLAGS_WAITABLE,
270 "VKATGstAts");
271 if (RT_SUCCESS(rc))
272 rc = RTThreadUserWait(hThreadGstAts, RT_MS_30SEC);
273 }
274
275 RTThreadSleep(2000); /* Fudge: Wait until guest ATS is up. 2 seconds should be enough (tm). */
276
277 if (RT_SUCCESS(rc))
278 {
279 /*
280 * Steps 2 + 3.
281 */
282 pTstEnvHst->enmMode = AUDIOTESTMODE_HOST;
283
284 rc = audioTestEnvCreate(pTstEnvHst, &pCtx->DrvStack);
285 if (RT_SUCCESS(rc))
286 {
287 /*
288 * Step 4.
289 */
290 rc = audioTestWorker(pTstEnvHst);
291 RTTEST_CHECK_RC_OK(g_hTest, rc);
292
293 audioTestEnvDestroy(pTstEnvHst);
294 }
295 }
296
297 /*
298 * Shutting down.
299 */
300 RTTestPrintf(g_hTest, RTTESTLVL_ALWAYS, "Shutting down self test\n");
301
302 /* If we started the guest ATS ourselves, wait for it to terminate properly. */
303 if (fStartGuestAts)
304 {
305 int rcThread;
306 int rc2 = RTThreadWait(hThreadGstAts, RT_MS_30SEC, &rcThread);
307 if (RT_SUCCESS(rc2))
308 rc2 = rcThread;
309 if (RT_FAILURE(rc2))
310 RTTestFailed(g_hTest, "Shutting down guest ATS failed with %Rrc\n", rc2);
311 if (RT_SUCCESS(rc))
312 rc = rc2;
313 }
314
315 if (RT_FAILURE(rc))
316 RTTestFailed(g_hTest, "Self test failed with %Rrc\n", rc);
317
318 return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
319}
320
321
322/*********************************************************************************************************************************
323* Command: selftest *
324*********************************************************************************************************************************/
325
326/**
327 * Command line parameters for self-test mode.
328 */
329static const RTGETOPTDEF s_aCmdSelftestOptions[] =
330{
331 { "--exclude-all", 'a', RTGETOPT_REQ_NOTHING },
332 { "--backend", 'b', RTGETOPT_REQ_STRING },
333 { "--with-drv-audio", 'd', RTGETOPT_REQ_NOTHING },
334 { "--exclude", 'e', RTGETOPT_REQ_UINT32 },
335 { "--include", 'i', RTGETOPT_REQ_UINT32 }
336};
337
338/** the 'selftest' command option help. */
339static DECLCALLBACK(const char *) audioTestCmdSelftestHelp(PCRTGETOPTDEF pOpt)
340{
341 switch (pOpt->iShort)
342 {
343 case 'a': return "Exclude all tests from the list (useful to enable single tests later with --include)";
344 case 'b': return "The audio backend to use";
345 case 'd': return "Go via DrvAudio instead of directly interfacing with the backend";
346 case 'e': return "Exclude the given test id from the list";
347 case 'i': return "Include the given test id in the list";
348 default: return NULL;
349 }
350}
351
352/**
353 * The 'selftest' command handler.
354 *
355 * @returns Program exit code.
356 * @param pGetState RTGetOpt state.
357 */
358DECLCALLBACK(RTEXITCODE) audioTestCmdSelftestHandler(PRTGETOPTSTATE pGetState)
359{
360 RT_ZERO(g_Ctx);
361
362 /* Argument processing loop: */
363 int rc;
364 RTGETOPTUNION ValueUnion;
365 while ((rc = RTGetOpt(pGetState, &ValueUnion)) != 0)
366 {
367 switch (rc)
368 {
369 case 'a':
370 for (unsigned i = 0; i < g_cTests; i++)
371 g_aTests[i].fExcluded = true;
372 break;
373
374 case 'b':
375 g_Ctx.pDrvReg = AudioTestFindBackendOpt(ValueUnion.psz);
376 if (g_Ctx.pDrvReg == NULL)
377 return RTEXITCODE_SYNTAX;
378 break;
379
380 case 'd':
381 g_Ctx.fWithDrvAudio = true;
382 break;
383
384 case 'e':
385 if (ValueUnion.u32 >= g_cTests)
386 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --exclude", ValueUnion.u32);
387 g_aTests[ValueUnion.u32].fExcluded = true;
388 break;
389
390 case 'i':
391 if (ValueUnion.u32 >= g_cTests)
392 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Invalid test number %u passed to --include", ValueUnion.u32);
393 g_aTests[ValueUnion.u32].fExcluded = false;
394 break;
395
396 AUDIO_TEST_COMMON_OPTION_CASES(ValueUnion);
397
398 default:
399 return RTGetOptPrintError(rc, &ValueUnion);
400 }
401 }
402
403 rc = AudioTestDriverStackPerformSelftest();
404 if (RT_FAILURE(rc))
405 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Testing driver stack failed: %Rrc\n", rc);
406
407 /* Go with the Validation Kit audio backend if nothing else is specified. */
408 if (g_Ctx.pDrvReg == NULL)
409 g_Ctx.pDrvReg = AudioTestFindBackendOpt("valkit");
410
411 /*
412 * In self-test mode the guest and the host side have to share the same driver stack,
413 * as we don't have any device emulation between the two sides.
414 *
415 * This is necessary to actually get the played/recorded audio to from/to the guest
416 * and host respectively.
417 *
418 * Choosing any other backend than the Validation Kit above *will* break this self-test!
419 */
420 rc = audioTestDriverStackInitEx(&g_Ctx.DrvStack, g_Ctx.pDrvReg,
421 true /* fEnabledIn */, true /* fEnabledOut */, g_Ctx.fWithDrvAudio);
422 if (RT_FAILURE(rc))
423 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unable to init driver stack: %Rrc\n", rc);
424
425 /*
426 * Start testing.
427 */
428 RTTestBanner(g_hTest);
429
430 int rc2 = audioTestDoSelftest(&g_Ctx);
431 if (RT_FAILURE(rc2))
432 RTTestFailed(g_hTest, "Self test failed with rc=%Rrc", rc2);
433
434 audioTestDriverStackDelete(&g_Ctx.DrvStack);
435
436 /*
437 * Print summary and exit.
438 */
439 return RTTestSummaryAndDestroy(g_hTest);
440}
441
442/**
443 * Command table entry for 'selftest'.
444 */
445const VKATCMD g_CmdSelfTest =
446{
447 "selftest",
448 audioTestCmdSelftestHandler,
449 "Performs self-tests.",
450 s_aCmdSelftestOptions,
451 RT_ELEMENTS(s_aCmdSelftestOptions),
452 audioTestCmdSelftestHelp,
453 true /* fNeedsTransport */
454};
455
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