VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/test.cpp@ 60244

Last change on this file since 60244 was 60244, checked in by vboxsync, 9 years ago

Runtime/test: typos

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 54.8 KB
Line 
1/* $Id: test.cpp 60244 2016-03-29 14:38:07Z vboxsync $ */
2/** @file
3 * IPRT - Testcase Framework.
4 */
5
6/*
7 * Copyright (C) 2009-2015 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 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/test.h>
32
33#include <iprt/asm.h>
34#include <iprt/critsect.h>
35#include <iprt/env.h>
36#include <iprt/err.h>
37#include <iprt/file.h>
38#include <iprt/initterm.h>
39#include <iprt/mem.h>
40#include <iprt/once.h>
41#include <iprt/param.h>
42#include <iprt/pipe.h>
43#include <iprt/string.h>
44#include <iprt/stream.h>
45
46#include "internal/magics.h"
47
48
49/*********************************************************************************************************************************
50* Structures and Typedefs *
51*********************************************************************************************************************************/
52/**
53 * Guarded memory allocation record.
54 */
55typedef struct RTTESTGUARDEDMEM
56{
57 /** Pointer to the next record. */
58 struct RTTESTGUARDEDMEM *pNext;
59 /** The address we return to the user. */
60 void *pvUser;
61 /** The base address of the allocation. */
62 void *pvAlloc;
63 /** The size of the allocation. */
64 size_t cbAlloc;
65 /** Guards. */
66 struct
67 {
68 /** The guard address. */
69 void *pv;
70 /** The guard size. */
71 size_t cb;
72 } aGuards[2];
73} RTTESTGUARDEDMEM;
74/** Pointer to an guarded memory allocation. */
75typedef RTTESTGUARDEDMEM *PRTTESTGUARDEDMEM;
76
77/**
78 * Test instance structure.
79 */
80typedef struct RTTESTINT
81{
82 /** Magic. */
83 uint32_t u32Magic;
84 /** The number of errors. */
85 volatile uint32_t cErrors;
86 /** The test name. */
87 const char *pszTest;
88 /** The length of the test name. */
89 size_t cchTest;
90 /** The size of a guard. Multiple of PAGE_SIZE. */
91 uint32_t cbGuard;
92 /** The verbosity level. */
93 RTTESTLVL enmMaxLevel;
94 /** The creation flags. */
95 uint32_t fFlags;
96
97
98 /** Critical section serializing output. */
99 RTCRITSECT OutputLock;
100 /** The output stream. */
101 PRTSTREAM pOutStrm;
102 /** Whether we're currently at a newline. */
103 bool fNewLine;
104
105
106 /** Critical section serializing access to the members following it. */
107 RTCRITSECT Lock;
108
109 /** The list of guarded memory allocations. */
110 PRTTESTGUARDEDMEM pGuardedMem;
111
112 /** The current sub-test. */
113 const char *pszSubTest;
114 /** The length of the sub-test name. */
115 size_t cchSubTest;
116 /** Whether the current subtest should figure as 'SKIPPED'. */
117 bool fSubTestSkipped;
118 /** Whether we've reported the sub-test result or not. */
119 bool fSubTestReported;
120 /** The start error count of the current subtest. */
121 uint32_t cSubTestAtErrors;
122
123 /** The number of sub tests. */
124 uint32_t cSubTests;
125 /** The number of sub tests that failed. */
126 uint32_t cSubTestsFailed;
127
128 /** Set if XML output is enabled. */
129 bool fXmlEnabled;
130 /** Set if we omit the top level test in the XML report. */
131 bool fXmlOmitTopTest;
132 /** Set if we've reported the top test (for RTTEST_C_XML_DELAY_TOP_TEST). */
133 bool fXmlTopTestDone;
134 enum {
135 kXmlPos_ValueStart,
136 kXmlPos_Value,
137 kXmlPos_ElementEnd
138 } eXmlState;
139 /** Test pipe for the XML output stream going to the server. */
140 RTPIPE hXmlPipe;
141 /** File where the XML output stream might be directed. */
142 RTFILE hXmlFile;
143 /** The number of XML elements on the stack. */
144 size_t cXmlElements;
145 /** XML element stack. */
146 const char *apszXmlElements[10];
147} RTTESTINT;
148/** Pointer to a test instance. */
149typedef RTTESTINT *PRTTESTINT;
150
151
152/*********************************************************************************************************************************
153* Defined Constants And Macros *
154*********************************************************************************************************************************/
155/** Validate a test instance. */
156#define RTTEST_VALID_RETURN(pTest) \
157 do { \
158 AssertPtrReturn(pTest, VERR_INVALID_HANDLE); \
159 AssertReturn(pTest->u32Magic == RTTESTINT_MAGIC, VERR_INVALID_HANDLE); \
160 } while (0)
161
162/** Gets and validates a test instance.
163 * If the handle is nil, we will try retrieve it from the test TLS entry.
164 */
165#define RTTEST_GET_VALID_RETURN(pTest) \
166 do { \
167 if (pTest == NIL_RTTEST) \
168 pTest = (PRTTESTINT)RTTlsGet(g_iTestTls); \
169 AssertPtrReturn(pTest, VERR_INVALID_HANDLE); \
170 AssertReturn(pTest->u32Magic == RTTESTINT_MAGIC, VERR_INVALID_MAGIC); \
171 } while (0)
172
173
174/** Gets and validates a test instance.
175 * If the handle is nil, we will try retrieve it from the test TLS entry.
176 */
177#define RTTEST_GET_VALID_RETURN_RC(pTest, rc) \
178 do { \
179 if (pTest == NIL_RTTEST) \
180 pTest = (PRTTESTINT)RTTlsGet(g_iTestTls); \
181 AssertPtrReturn(pTest, (rc)); \
182 AssertReturn(pTest->u32Magic == RTTESTINT_MAGIC, (rc)); \
183 } while (0)
184
185
186/*********************************************************************************************************************************
187* Internal Functions *
188*********************************************************************************************************************************/
189static void rtTestGuardedFreeOne(PRTTESTGUARDEDMEM pMem);
190static int rtTestPrintf(PRTTESTINT pTest, const char *pszFormat, ...);
191static void rtTestXmlStart(PRTTESTINT pTest, const char *pszTest);
192static void rtTestXmlElemV(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, va_list va);
193static void rtTestXmlElem(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, ...);
194static void rtTestXmlElemStartV(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, va_list va);
195static void rtTestXmlElemStart(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, ...);
196static void rtTestXmlElemEnd(PRTTESTINT pTest, const char *pszTag);
197static void rtTestXmlEnd(PRTTESTINT pTest);
198
199
200/*********************************************************************************************************************************
201* Global Variables *
202*********************************************************************************************************************************/
203/** For serializing TLS init. */
204static RTONCE g_TestInitOnce = RTONCE_INITIALIZER;
205/** Our TLS entry. */
206static RTTLS g_iTestTls = NIL_RTTLS;
207
208
209
210/**
211 * Init TLS index once.
212 *
213 * @returns IPRT status code.
214 * @param pvUser Ignored.
215 */
216static DECLCALLBACK(int32_t) rtTestInitOnce(void *pvUser)
217{
218 NOREF(pvUser);
219 return RTTlsAllocEx(&g_iTestTls, NULL);
220}
221
222
223RTR3DECL(int) RTTestCreateEx(const char *pszTest, uint32_t fFlags, RTTESTLVL enmMaxLevel,
224 RTHCINTPTR iNativeTestPipe, const char *pszXmlFile, PRTTEST phTest)
225{
226 AssertReturn(!(fFlags & ~RTTEST_C_VALID_MASK), VERR_INVALID_PARAMETER);
227 AssertPtrNull(phTest);
228 AssertPtrNull(pszXmlFile);
229 /* RTTESTLVL_INVALID is valid! */
230 AssertReturn(enmMaxLevel >= RTTESTLVL_INVALID && enmMaxLevel < RTTESTLVL_END, VERR_INVALID_PARAMETER);
231
232 /*
233 * Global init.
234 */
235 int rc = RTOnce(&g_TestInitOnce, rtTestInitOnce, NULL);
236 if (RT_FAILURE(rc))
237 return rc;
238
239 /*
240 * Create the instance.
241 */
242 PRTTESTINT pTest = (PRTTESTINT)RTMemAllocZ(sizeof(*pTest));
243 if (!pTest)
244 return VERR_NO_MEMORY;
245 pTest->u32Magic = RTTESTINT_MAGIC;
246 pTest->pszTest = RTStrDup(pszTest);
247 pTest->cchTest = strlen(pszTest);
248 pTest->cbGuard = PAGE_SIZE * 7;
249 pTest->enmMaxLevel = enmMaxLevel == RTTESTLVL_INVALID ? RTTESTLVL_INFO : enmMaxLevel;
250 pTest->fFlags = fFlags;
251
252 pTest->pOutStrm = g_pStdOut;
253 pTest->fNewLine = true;
254
255 pTest->pGuardedMem = NULL;
256
257 pTest->pszSubTest = NULL;
258 pTest->cchSubTest = 0;
259 pTest->fSubTestSkipped = false;
260 pTest->fSubTestReported = true;
261 pTest->cSubTestAtErrors = 0;
262 pTest->cSubTests = 0;
263 pTest->cSubTestsFailed = 0;
264
265 pTest->fXmlEnabled = false;
266 pTest->fXmlTopTestDone = false;
267 pTest->eXmlState = RTTESTINT::kXmlPos_ElementEnd;
268 pTest->hXmlPipe = NIL_RTPIPE;
269 pTest->hXmlFile = NIL_RTFILE;
270 pTest->cXmlElements = 0;
271
272 rc = RTCritSectInit(&pTest->Lock);
273 if (RT_SUCCESS(rc))
274 {
275 rc = RTCritSectInit(&pTest->OutputLock);
276 if (RT_SUCCESS(rc))
277 {
278 /*
279 * Associate it with our TLS entry unless there is already
280 * an instance there.
281 */
282 if ( !(fFlags & RTTEST_C_NO_TLS)
283 && !RTTlsGet(g_iTestTls))
284 rc = RTTlsSet(g_iTestTls, pTest);
285 if (RT_SUCCESS(rc))
286 {
287 /*
288 * Output level override?
289 */
290 char szEnvVal[RTPATH_MAX];
291 if ((fFlags & RTTEST_C_USE_ENV) && enmMaxLevel == RTTESTLVL_INVALID)
292 {
293 rc = RTEnvGetEx(RTENV_DEFAULT, "IPRT_TEST_MAX_LEVEL", szEnvVal, sizeof(szEnvVal), NULL);
294 if (RT_SUCCESS(rc))
295 {
296 char *pszMaxLevel = RTStrStrip(szEnvVal);
297 if (!strcmp(pszMaxLevel, "all"))
298 pTest->enmMaxLevel = RTTESTLVL_DEBUG;
299 if (!strcmp(pszMaxLevel, "quiet"))
300 pTest->enmMaxLevel = RTTESTLVL_FAILURE;
301 else if (!strcmp(pszMaxLevel, "debug"))
302 pTest->enmMaxLevel = RTTESTLVL_DEBUG;
303 else if (!strcmp(pszMaxLevel, "info"))
304 pTest->enmMaxLevel = RTTESTLVL_INFO;
305 else if (!strcmp(pszMaxLevel, "sub_test"))
306 pTest->enmMaxLevel = RTTESTLVL_SUB_TEST;
307 else if (!strcmp(pszMaxLevel, "failure"))
308 pTest->enmMaxLevel = RTTESTLVL_FAILURE;
309 }
310 else if (rc != VERR_ENV_VAR_NOT_FOUND)
311 RTStrmPrintf(g_pStdErr, "%s: test pipe error: RTEnvGetEx(IPRT_TEST_MAX_LEVEL) -> %Rrc\n", pszTest, rc);
312 }
313
314 /*
315 * Any test driver we are connected or should connect to?
316 */
317 if (!(fFlags & RTTEST_C_NO_XML_REPORTING_PIPE))
318 {
319 if ( (fFlags & RTTEST_C_USE_ENV)
320 && iNativeTestPipe == -1)
321 {
322 rc = RTEnvGetEx(RTENV_DEFAULT, "IPRT_TEST_PIPE", szEnvVal, sizeof(szEnvVal), NULL);
323 if (RT_SUCCESS(rc))
324 {
325#if ARCH_BITS == 64
326 rc = RTStrToInt64Full(szEnvVal, 0, &iNativeTestPipe);
327#else
328 rc = RTStrToInt32Full(szEnvVal, 0, &iNativeTestPipe);
329#endif
330 if (RT_FAILURE(rc))
331 {
332 RTStrmPrintf(g_pStdErr, "%s: test pipe error: RTStrToInt32Full(\"%s\") -> %Rrc\n",
333 pszTest, szEnvVal, rc);
334 iNativeTestPipe = -1;
335 }
336 }
337 else if (rc != VERR_ENV_VAR_NOT_FOUND)
338 RTStrmPrintf(g_pStdErr, "%s: test pipe error: RTEnvGetEx(IPRT_TEST_PIPE) -> %Rrc\n", pszTest, rc);
339 }
340 if (iNativeTestPipe != -1)
341 {
342 rc = RTPipeFromNative(&pTest->hXmlPipe, iNativeTestPipe, RTPIPE_N_WRITE);
343 if (RT_SUCCESS(rc))
344 pTest->fXmlEnabled = true;
345 else
346 {
347 RTStrmPrintf(g_pStdErr, "%s: test pipe error: RTPipeFromNative(,%p,WRITE) -> %Rrc\n",
348 pszTest, iNativeTestPipe, rc);
349 pTest->hXmlPipe = NIL_RTPIPE;
350 }
351 }
352 }
353
354 /*
355 * Any test file we should write the test report to?
356 */
357 if (!(fFlags & RTTEST_C_NO_XML_REPORTING_FILE))
358 {
359 if ((fFlags & RTTEST_C_USE_ENV) && pszXmlFile == NULL)
360 {
361 rc = RTEnvGetEx(RTENV_DEFAULT, "IPRT_TEST_FILE", szEnvVal, sizeof(szEnvVal), NULL);
362 if (RT_SUCCESS(rc))
363 pszXmlFile = szEnvVal;
364 else if (rc != VERR_ENV_VAR_NOT_FOUND)
365 RTStrmPrintf(g_pStdErr, "%s: test file error: RTEnvGetEx(IPRT_TEST_MAX_LEVEL) -> %Rrc\n", pszTest, rc);
366 }
367 if (pszXmlFile && *pszXmlFile)
368 {
369 rc = RTFileOpen(&pTest->hXmlFile, pszXmlFile,
370 RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE);
371 if (RT_SUCCESS(rc))
372 pTest->fXmlEnabled = true;
373 else
374 {
375 RTStrmPrintf(g_pStdErr, "%s: test file error: RTFileOpen(,\"%s\",) -> %Rrc\n",
376 pszTest, pszXmlFile, rc);
377 pTest->hXmlFile = NIL_RTFILE;
378 }
379 }
380 }
381
382 /*
383 * What do we report in the XML stream/file.?
384 */
385 pTest->fXmlOmitTopTest = (fFlags & RTTEST_C_XML_OMIT_TOP_TEST)
386 || ( (fFlags & RTTEST_C_USE_ENV)
387 && RTEnvExistEx(RTENV_DEFAULT, "IPRT_TEST_OMIT_TOP_TEST"));
388
389 /*
390 * Tell the test driver that we're up to.
391 */
392 rtTestXmlStart(pTest, pszTest);
393
394 *phTest = pTest;
395 return VINF_SUCCESS;
396 }
397
398 /* bail out. */
399 RTCritSectDelete(&pTest->OutputLock);
400 }
401 RTCritSectDelete(&pTest->Lock);
402 }
403 pTest->u32Magic = 0;
404 RTStrFree((char *)pTest->pszTest);
405 RTMemFree(pTest);
406 return rc;
407}
408
409
410RTR3DECL(int) RTTestCreate(const char *pszTest, PRTTEST phTest)
411{
412 return RTTestCreateEx(pszTest, RTTEST_C_USE_ENV, RTTESTLVL_INVALID, -1 /*iNativeTestPipe*/, NULL /*pszXmlFile*/, phTest);
413}
414
415
416RTR3DECL(int) RTTestCreateChild(const char *pszTest, PRTTEST phTest)
417{
418 return RTTestCreateEx(pszTest, RTTEST_C_USE_ENV | RTTEST_C_NO_XML_REPORTING,
419 RTTESTLVL_INVALID, -1 /*iNativeTestPipe*/, NULL /*pszXmlFile*/, phTest);
420}
421
422
423RTR3DECL(RTEXITCODE) RTTestInitAndCreate(const char *pszTest, PRTTEST phTest)
424{
425 int rc = RTR3InitExeNoArguments(0);
426 if (RT_FAILURE(rc))
427 {
428 RTStrmPrintf(g_pStdErr, "%s: fatal error: RTR3InitExeNoArguments failed with rc=%Rrc\n", pszTest, rc);
429 return RTEXITCODE_INIT;
430 }
431
432 rc = RTTestCreate(pszTest, phTest);
433 if (RT_FAILURE(rc))
434 {
435 RTStrmPrintf(g_pStdErr, "%s: fatal error: RTTestCreate failed with rc=%Rrc\n", pszTest, rc);
436 return RTEXITCODE_INIT;
437 }
438 return RTEXITCODE_SUCCESS;
439}
440
441
442RTR3DECL(RTEXITCODE) RTTestInitExAndCreate(int cArgs, char ***ppapszArgs, uint32_t fRtInit, const char *pszTest, PRTTEST phTest)
443{
444 int rc;
445 if (cArgs <= 0 && ppapszArgs == NULL)
446 rc = RTR3InitExeNoArguments(fRtInit);
447 else
448 rc = RTR3InitExe(cArgs, ppapszArgs, fRtInit);
449 if (RT_FAILURE(rc))
450 {
451 RTStrmPrintf(g_pStdErr, "%s: fatal error: RTR3InitExe(,,%#x) failed with rc=%Rrc\n", pszTest, fRtInit, rc);
452 return RTEXITCODE_INIT;
453 }
454
455 rc = RTTestCreate(pszTest, phTest);
456 if (RT_FAILURE(rc))
457 {
458 RTStrmPrintf(g_pStdErr, "%s: fatal error: RTTestCreate failed with rc=%Rrc\n", pszTest, rc);
459 return RTEXITCODE_INIT;
460 }
461 return RTEXITCODE_SUCCESS;
462}
463
464
465/**
466 * Destroys a test instance previously created by RTTestCreate.
467 *
468 * @returns IPRT status code.
469 * @param hTest The test handle. NIL_RTTEST is ignored.
470 */
471RTR3DECL(int) RTTestDestroy(RTTEST hTest)
472{
473 /*
474 * Validate
475 */
476 if (hTest == NIL_RTTEST)
477 return VINF_SUCCESS;
478 RTTESTINT *pTest = hTest;
479 RTTEST_VALID_RETURN(pTest);
480
481 /*
482 * Make sure we end with a new line and have finished up the XML.
483 */
484 if (!pTest->fNewLine)
485 rtTestPrintf(pTest, "\n");
486 rtTestXmlEnd(pTest);
487
488 /*
489 * Clean up.
490 */
491 if ((RTTESTINT *)RTTlsGet(g_iTestTls) == pTest)
492 RTTlsSet(g_iTestTls, NULL);
493
494 ASMAtomicWriteU32(&pTest->u32Magic, ~RTTESTINT_MAGIC);
495 RTCritSectDelete(&pTest->Lock);
496 RTCritSectDelete(&pTest->OutputLock);
497
498 /* free guarded memory. */
499 PRTTESTGUARDEDMEM pMem = pTest->pGuardedMem;
500 pTest->pGuardedMem = NULL;
501 while (pMem)
502 {
503 PRTTESTGUARDEDMEM pFree = pMem;
504 pMem = pMem->pNext;
505 rtTestGuardedFreeOne(pFree);
506 }
507
508 RTStrFree((char *)pTest->pszSubTest);
509 pTest->pszSubTest = NULL;
510 RTStrFree((char *)pTest->pszTest);
511 pTest->pszTest = NULL;
512 RTMemFree(pTest);
513 return VINF_SUCCESS;
514}
515
516
517/**
518 * Changes the default test instance for the calling thread.
519 *
520 * @returns IPRT status code.
521 *
522 * @param hNewDefaultTest The new default test. NIL_RTTEST is fine.
523 * @param phOldTest Where to store the old test handle. Optional.
524 */
525RTR3DECL(int) RTTestSetDefault(RTTEST hNewDefaultTest, PRTTEST phOldTest)
526{
527 if (phOldTest)
528 *phOldTest = (RTTEST)RTTlsGet(g_iTestTls);
529 return RTTlsSet(g_iTestTls, hNewDefaultTest);
530}
531
532
533RTR3DECL(int) RTTestChangeName(RTTEST hTest, const char *pszName)
534{
535 PRTTESTINT pTest = hTest;
536 RTTEST_GET_VALID_RETURN(pTest);
537 AssertPtrReturn(pszName, VERR_INVALID_POINTER);
538 AssertReturn(*pszName, VERR_INVALID_PARAMETER);
539
540 size_t cchName = strlen(pszName);
541 AssertReturn(cchName < 128, VERR_INVALID_PARAMETER);
542 char *pszDupName = RTStrDup(pszName);
543 if (!pszDupName)
544 return VERR_NO_STR_MEMORY;
545
546 RTCritSectEnter(&pTest->Lock);
547 RTCritSectEnter(&pTest->OutputLock);
548
549 char *pszOldName = (char *)pTest->pszTest;
550 pTest->pszTest = pszDupName;
551 pTest->cchTest = cchName;
552
553 RTCritSectLeave(&pTest->OutputLock);
554 RTCritSectLeave(&pTest->Lock);
555
556 RTStrFree(pszOldName);
557 return VINF_SUCCESS;
558}
559
560
561/**
562 * Allocate a block of guarded memory.
563 *
564 * @returns IPRT status code.
565 * @param hTest The test handle. If NIL_RTTEST we'll use the one
566 * associated with the calling thread.
567 * @param cb The amount of memory to allocate.
568 * @param cbAlign The alignment of the returned block.
569 * @param fHead Head or tail optimized guard.
570 * @param ppvUser Where to return the pointer to the block.
571 */
572RTR3DECL(int) RTTestGuardedAlloc(RTTEST hTest, size_t cb, uint32_t cbAlign, bool fHead, void **ppvUser)
573{
574 PRTTESTINT pTest = hTest;
575 RTTEST_GET_VALID_RETURN(pTest);
576 if (cbAlign == 0)
577 cbAlign = 1;
578 AssertReturn(cbAlign <= PAGE_SIZE, VERR_INVALID_PARAMETER);
579 AssertReturn(cbAlign == (UINT32_C(1) << (ASMBitFirstSetU32(cbAlign) - 1)), VERR_INVALID_PARAMETER);
580
581 /*
582 * Allocate the record and block and initialize them.
583 */
584 int rc = VERR_NO_MEMORY;
585 PRTTESTGUARDEDMEM pMem = (PRTTESTGUARDEDMEM)RTMemAlloc(sizeof(*pMem));
586 if (RT_LIKELY(pMem))
587 {
588 size_t const cbAligned = RT_ALIGN_Z(cb, PAGE_SIZE);
589 pMem->aGuards[0].cb = pMem->aGuards[1].cb = pTest->cbGuard;
590 pMem->cbAlloc = pMem->aGuards[0].cb + pMem->aGuards[1].cb + cbAligned;
591 pMem->pvAlloc = RTMemPageAlloc(pMem->cbAlloc);
592 if (pMem->pvAlloc)
593 {
594 pMem->aGuards[0].pv = pMem->pvAlloc;
595 pMem->pvUser = (uint8_t *)pMem->pvAlloc + pMem->aGuards[0].cb;
596 pMem->aGuards[1].pv = (uint8_t *)pMem->pvUser + cbAligned;
597 if (!fHead)
598 {
599 size_t off = cb & PAGE_OFFSET_MASK;
600 if (off)
601 {
602 off = PAGE_SIZE - RT_ALIGN_Z(off, cbAlign);
603 pMem->pvUser = (uint8_t *)pMem->pvUser + off;
604 }
605 }
606
607 /*
608 * Set up the guards and link the record.
609 */
610 ASMMemFill32(pMem->aGuards[0].pv, pMem->aGuards[0].cb, 0xdeadbeef);
611 ASMMemFill32(pMem->aGuards[1].pv, pMem->aGuards[1].cb, 0xdeadbeef);
612 rc = RTMemProtect(pMem->aGuards[0].pv, pMem->aGuards[0].cb, RTMEM_PROT_NONE);
613 if (RT_SUCCESS(rc))
614 {
615 rc = RTMemProtect(pMem->aGuards[1].pv, pMem->aGuards[1].cb, RTMEM_PROT_NONE);
616 if (RT_SUCCESS(rc))
617 {
618 *ppvUser = pMem->pvUser;
619
620 RTCritSectEnter(&pTest->Lock);
621 pMem->pNext = pTest->pGuardedMem;
622 pTest->pGuardedMem = pMem;
623 RTCritSectLeave(&pTest->Lock);
624
625 return VINF_SUCCESS;
626 }
627
628 RTMemProtect(pMem->aGuards[0].pv, pMem->aGuards[0].cb, RTMEM_PROT_WRITE | RTMEM_PROT_READ);
629 }
630
631 RTMemPageFree(pMem->pvAlloc, pMem->cbAlloc);
632 }
633 RTMemFree(pMem);
634 }
635 return rc;
636}
637
638
639/**
640 * Allocates a block of guarded memory where the guarded is immediately after
641 * the user memory.
642 *
643 * @returns Pointer to the allocated memory. NULL on failure.
644 * @param hTest The test handle. If NIL_RTTEST we'll use the one
645 * associated with the calling thread.
646 * @param cb The amount of memory to allocate.
647 */
648RTR3DECL(void *) RTTestGuardedAllocTail(RTTEST hTest, size_t cb)
649{
650 void *pvUser;
651 int rc = RTTestGuardedAlloc(hTest, cb, 1 /* cbAlign */, false /* fHead */, &pvUser);
652 if (RT_SUCCESS(rc))
653 return pvUser;
654 return NULL;
655}
656
657
658/**
659 * Allocates a block of guarded memory where the guarded is right in front of
660 * the user memory.
661 *
662 * @returns Pointer to the allocated memory. NULL on failure.
663 * @param hTest The test handle. If NIL_RTTEST we'll use the one
664 * associated with the calling thread.
665 * @param cb The amount of memory to allocate.
666 */
667RTR3DECL(void *) RTTestGuardedAllocHead(RTTEST hTest, size_t cb)
668{
669 void *pvUser;
670 int rc = RTTestGuardedAlloc(hTest, cb, 1 /* cbAlign */, true /* fHead */, &pvUser);
671 if (RT_SUCCESS(rc))
672 return pvUser;
673 return NULL;
674}
675
676
677/**
678 * Frees one block of guarded memory.
679 *
680 * The caller is responsible for unlinking it.
681 *
682 * @param pMem The memory record.
683 */
684static void rtTestGuardedFreeOne(PRTTESTGUARDEDMEM pMem)
685{
686 int rc;
687 rc = RTMemProtect(pMem->aGuards[0].pv, pMem->aGuards[0].cb, RTMEM_PROT_WRITE | RTMEM_PROT_READ); AssertRC(rc);
688 rc = RTMemProtect(pMem->aGuards[1].pv, pMem->aGuards[1].cb, RTMEM_PROT_WRITE | RTMEM_PROT_READ); AssertRC(rc);
689 RTMemPageFree(pMem->pvAlloc, pMem->cbAlloc);
690 RTMemFree(pMem);
691}
692
693
694/**
695 * Frees a block of guarded memory.
696 *
697 * @returns IPRT status code.
698 * @param hTest The test handle. If NIL_RTTEST we'll use the one
699 * associated with the calling thread.
700 * @param pv The memory. NULL is ignored.
701 */
702RTR3DECL(int) RTTestGuardedFree(RTTEST hTest, void *pv)
703{
704 PRTTESTINT pTest = hTest;
705 RTTEST_GET_VALID_RETURN(pTest);
706 if (!pv)
707 return VINF_SUCCESS;
708
709 /*
710 * Find it.
711 */
712 int rc = VERR_INVALID_POINTER;
713 PRTTESTGUARDEDMEM pPrev = NULL;
714
715 RTCritSectEnter(&pTest->Lock);
716 for (PRTTESTGUARDEDMEM pMem = pTest->pGuardedMem; pMem; pMem = pMem->pNext)
717 {
718 if (pMem->pvUser == pv)
719 {
720 if (pPrev)
721 pPrev->pNext = pMem->pNext;
722 else
723 pTest->pGuardedMem = pMem->pNext;
724 rtTestGuardedFreeOne(pMem);
725 rc = VINF_SUCCESS;
726 break;
727 }
728 pPrev = pMem;
729 }
730 RTCritSectLeave(&pTest->Lock);
731
732 return rc;
733}
734
735
736/**
737 * Outputs the formatted XML.
738 *
739 * @param pTest The test instance.
740 * @param pszFormat The format string.
741 * @param va The format arguments.
742 */
743static void rtTestXmlOutputV(PRTTESTINT pTest, const char *pszFormat, va_list va)
744{
745 if (pTest->fXmlEnabled)
746 {
747 char *pszStr;
748 ssize_t cchStr = RTStrAPrintfV(&pszStr, pszFormat, va);
749 if (pszStr)
750 {
751 if (pTest->hXmlPipe != NIL_RTPIPE)
752 RTPipeWriteBlocking(pTest->hXmlPipe, pszStr, cchStr, NULL);
753 if (pTest->hXmlFile != NIL_RTFILE)
754 RTFileWrite(pTest->hXmlFile, pszStr, cchStr, NULL);
755 RTStrFree(pszStr);
756 }
757 }
758}
759
760
761/**
762 * Outputs the formatted XML.
763 *
764 * @param pTest The test instance.
765 * @param pszFormat The format string.
766 * @param ... The format arguments.
767 */
768static void rtTestXmlOutput(PRTTESTINT pTest, const char *pszFormat, ...)
769{
770 va_list va;
771 va_start(va, pszFormat);
772 rtTestXmlOutputV(pTest, pszFormat, va);
773 va_end(va);
774}
775
776
777/**
778 * Starts the XML stream.
779 *
780 * @param pTest The test instance.
781 * @param pszTest The test name.
782 */
783static void rtTestXmlStart(PRTTESTINT pTest, const char *pszTest)
784{
785 pTest->cXmlElements = 0;
786 if (pTest->fXmlEnabled)
787 {
788 rtTestXmlOutput(pTest, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
789 pTest->eXmlState = RTTESTINT::kXmlPos_ElementEnd;
790 pTest->fXmlTopTestDone = !(pTest->fFlags & RTTEST_C_XML_DELAY_TOP_TEST) || pTest->fXmlOmitTopTest;
791 if (pTest->fXmlTopTestDone && !pTest->fXmlOmitTopTest)
792 rtTestXmlElemStart(pTest, "Test", "name=%RMas", pszTest);
793 }
794}
795
796/**
797 * Emit an XML element that doesn't have any value and instead ends immediately.
798 *
799 * The caller must own the instance lock.
800 *
801 * @param pTest The test instance.
802 * @param pszTag The element tag.
803 * @param pszAttrFmt The element attributes as a format string. Use
804 * NULL if none.
805 * @param va Format string arguments.
806 */
807static void rtTestXmlElemV(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, va_list va)
808{
809 if (pTest->fXmlEnabled)
810 {
811 RTTIMESPEC TimeSpec;
812 RTTIME Time;
813 char szTS[80];
814 RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&TimeSpec)), szTS, sizeof(szTS));
815
816 if (pTest->eXmlState != RTTESTINT::kXmlPos_ElementEnd)
817 rtTestXmlOutput(pTest, "\n");
818
819 if (!pszAttrFmt || !*pszAttrFmt)
820 rtTestXmlOutput(pTest, "%*s<%s timestamp=%RMas/>\n",
821 pTest->cXmlElements * 2, "", pszTag, szTS);
822 else
823 {
824 va_list va2;
825 va_copy(va2, va);
826 rtTestXmlOutput(pTest, "%*s<%s timestamp=%RMas %N/>\n",
827 pTest->cXmlElements * 2, "", pszTag, szTS, pszAttrFmt, &va2);
828 va_end(va2);
829 }
830 pTest->eXmlState = RTTESTINT::kXmlPos_ElementEnd;
831 }
832}
833
834/**
835 * Wrapper around rtTestXmlElemV.
836 */
837static void rtTestXmlElem(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, ...)
838{
839 va_list va;
840 va_start(va, pszAttrFmt);
841 rtTestXmlElemV(pTest, pszTag, pszAttrFmt, va);
842 va_end(va);
843}
844
845
846/**
847 * Starts a new XML element.
848 *
849 * The caller must own the instance lock.
850 *
851 * @param pTest The test instance.
852 * @param pszTag The element tag.
853 * @param pszAttrFmt The element attributes as a format string. Use
854 * NULL if none.
855 * @param va Format string arguments.
856 */
857static void rtTestXmlElemStartV(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, va_list va)
858{
859 /* Push it onto the stack. */
860 size_t i = pTest->cXmlElements;
861 AssertReturnVoid(i < RT_ELEMENTS(pTest->apszXmlElements));
862 pTest->apszXmlElements[i] = pszTag;
863 pTest->cXmlElements = i + 1;
864
865 if (pTest->fXmlEnabled)
866 {
867 RTTIMESPEC TimeSpec;
868 RTTIME Time;
869 char szTS[80];
870 RTTimeToString(RTTimeExplode(&Time, RTTimeNow(&TimeSpec)), szTS, sizeof(szTS));
871
872 if (pTest->eXmlState != RTTESTINT::kXmlPos_ElementEnd)
873 rtTestXmlOutput(pTest, "\n");
874
875 if (!pszAttrFmt || !*pszAttrFmt)
876 rtTestXmlOutput(pTest, "%*s<%s timestamp=%RMas>",
877 i * 2, "", pszTag, szTS);
878 else
879 {
880 va_list va2;
881 va_copy(va2, va);
882 rtTestXmlOutput(pTest, "%*s<%s timestamp=%RMas %N>",
883 i * 2, "", pszTag, szTS, pszAttrFmt, &va2);
884 va_end(va2);
885 }
886 pTest->eXmlState = RTTESTINT::kXmlPos_ValueStart;
887 }
888}
889
890
891/**
892 * Wrapper around rtTestXmlElemStartV.
893 */
894static void rtTestXmlElemStart(PRTTESTINT pTest, const char *pszTag, const char *pszAttrFmt, ...)
895{
896 va_list va;
897 va_start(va, pszAttrFmt);
898 rtTestXmlElemStartV(pTest, pszTag, pszAttrFmt, va);
899 va_end(va);
900}
901
902
903/**
904 * Ends the current element.
905 *
906 * The caller must own the instance lock.
907 *
908 * @param pTest The test instance.
909 * @param pszTag The tag we're ending (chiefly for sanity
910 * checking).
911 */
912static void rtTestXmlElemEnd(PRTTESTINT pTest, const char *pszTag)
913{
914 /* pop the element */
915 size_t i = pTest->cXmlElements;
916 AssertReturnVoid(i > 0);
917 i--;
918 AssertReturnVoid(!strcmp(pszTag, pTest->apszXmlElements[i]));
919 pTest->cXmlElements = i;
920
921 /* Do the closing. */
922 if (pTest->fXmlEnabled)
923 {
924 if (pTest->eXmlState == RTTESTINT::kXmlPos_ValueStart)
925 rtTestXmlOutput(pTest, "\n%*s</%s>\n", i * 2, "", pszTag);
926 else if (pTest->eXmlState == RTTESTINT::kXmlPos_ElementEnd)
927 rtTestXmlOutput(pTest, "%*s</%s>\n", i * 2, "", pszTag);
928 else
929 rtTestXmlOutput(pTest, "</%s>\n", pszTag);
930 pTest->eXmlState = RTTESTINT::kXmlPos_ElementEnd;
931 }
932}
933
934
935/**
936 * Ends the XML stream, closing all open elements.
937 *
938 * The caller must own the instance lock.
939 *
940 * @param pTest The test instance.
941 */
942static void rtTestXmlEnd(PRTTESTINT pTest)
943{
944 if (pTest->fXmlEnabled)
945 {
946 /*
947 * Close all the elements and add the final TestEnd one to get a
948 * final timestamp and some certainty that the XML is valid.
949 */
950 size_t i = pTest->cXmlElements;
951 AssertReturnVoid(i > 0 || pTest->fXmlOmitTopTest || !pTest->fXmlTopTestDone);
952 while (i-- > 1)
953 {
954 const char *pszTag = pTest->apszXmlElements[pTest->cXmlElements];
955 if (pTest->eXmlState == RTTESTINT::kXmlPos_ValueStart)
956 rtTestXmlOutput(pTest, "\n%*s</%s>\n", i * 2, "", pszTag);
957 else if (pTest->eXmlState == RTTESTINT::kXmlPos_ElementEnd)
958 rtTestXmlOutput(pTest, "%*s</%s>\n", i * 2, "", pszTag);
959 else
960 rtTestXmlOutput(pTest, "</%s>\n", pszTag);
961 pTest->eXmlState = RTTESTINT::kXmlPos_ElementEnd;
962 }
963
964 if (!pTest->fXmlOmitTopTest && pTest->fXmlTopTestDone)
965 {
966 rtTestXmlElem(pTest, "End", "SubTests=\"%u\" SubTestsFailed=\"%u\" errors=\"%u\"",
967 pTest->cSubTests, pTest->cSubTestsFailed, pTest->cErrors);
968 rtTestXmlOutput(pTest, "</Test>\n");
969 }
970
971 /*
972 * Close the XML outputs.
973 */
974 if (pTest->hXmlPipe != NIL_RTPIPE)
975 {
976 RTPipeClose(pTest->hXmlPipe);
977 pTest->hXmlPipe = NIL_RTPIPE;
978 }
979 if (pTest->hXmlFile != NIL_RTFILE)
980 {
981 RTFileClose(pTest->hXmlFile);
982 pTest->hXmlFile = NIL_RTFILE;
983 }
984 pTest->fXmlEnabled = false;
985 pTest->eXmlState = RTTESTINT::kXmlPos_ElementEnd;
986 }
987 pTest->cXmlElements = 0;
988}
989
990/**
991 * Output callback.
992 *
993 * @returns number of bytes written.
994 * @param pvArg User argument.
995 * @param pachChars Pointer to an array of utf-8 characters.
996 * @param cbChars Number of bytes in the character array pointed to by pachChars.
997 */
998static DECLCALLBACK(size_t) rtTestPrintfOutput(void *pvArg, const char *pachChars, size_t cbChars)
999{
1000 size_t cch = 0;
1001 PRTTESTINT pTest = (PRTTESTINT)pvArg;
1002 if (cbChars)
1003 {
1004 do
1005 {
1006 /* insert prefix if at a newline. */
1007 if (pTest->fNewLine)
1008 {
1009 RTStrmWrite(pTest->pOutStrm, pTest->pszTest, pTest->cchTest);
1010 RTStrmWrite(pTest->pOutStrm, ": ", 2);
1011 cch += 2 + pTest->cchTest;
1012 }
1013
1014 /* look for newline and write the stuff. */
1015 const char *pchEnd = (const char *)memchr(pachChars, '\n', cbChars);
1016 if (!pchEnd)
1017 {
1018 pTest->fNewLine = false;
1019 RTStrmWrite(pTest->pOutStrm, pachChars, cbChars);
1020 cch += cbChars;
1021 break;
1022 }
1023
1024 pTest->fNewLine = true;
1025 size_t const cchPart = pchEnd - pachChars + 1;
1026 RTStrmWrite(pTest->pOutStrm, pachChars, cchPart);
1027 cch += cchPart;
1028 pachChars += cchPart;
1029 cbChars -= cchPart;
1030 } while (cbChars);
1031 }
1032 else
1033 RTStrmFlush(pTest->pOutStrm);
1034 return cch;
1035}
1036
1037
1038/**
1039 * Internal output worker.
1040 *
1041 * Caller takes the lock.
1042 *
1043 * @returns Number of chars printed.
1044 * @param pTest The test instance.
1045 * @param pszFormat The message.
1046 * @param va The arguments.
1047 */
1048static int rtTestPrintfV(PRTTESTINT pTest, const char *pszFormat, va_list va)
1049{
1050 return (int)RTStrFormatV(rtTestPrintfOutput, pTest, NULL, NULL, pszFormat, va);
1051}
1052
1053
1054/**
1055 * Internal output worker.
1056 *
1057 * Caller takes the lock.
1058 *
1059 * @returns Number of chars printed.
1060 * @param pTest The test instance.
1061 * @param pszFormat The message.
1062 * @param ... The arguments.
1063 */
1064static int rtTestPrintf(PRTTESTINT pTest, const char *pszFormat, ...)
1065{
1066 va_list va;
1067
1068 va_start(va, pszFormat);
1069 int cch = rtTestPrintfV(pTest, pszFormat, va);
1070 va_end(va);
1071
1072 return cch;
1073}
1074
1075
1076/**
1077 * Test vprintf making sure the output starts on a new line.
1078 *
1079 * @returns Number of chars printed.
1080 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1081 * associated with the calling thread.
1082 * @param enmLevel Message importance level.
1083 * @param pszFormat The message.
1084 * @param va Arguments.
1085 */
1086RTR3DECL(int) RTTestPrintfNlV(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, va_list va)
1087{
1088 PRTTESTINT pTest = hTest;
1089 RTTEST_GET_VALID_RETURN_RC(pTest, -1);
1090
1091 RTCritSectEnter(&pTest->OutputLock);
1092
1093 int cch = 0;
1094 if (enmLevel <= pTest->enmMaxLevel)
1095 {
1096 if (!pTest->fNewLine)
1097 cch += rtTestPrintf(pTest, "\n");
1098 cch += rtTestPrintfV(pTest, pszFormat, va);
1099 }
1100
1101 RTCritSectLeave(&pTest->OutputLock);
1102
1103 return cch;
1104}
1105
1106
1107/**
1108 * Test printf making sure the output starts on a new line.
1109 *
1110 * @returns Number of chars printed.
1111 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1112 * associated with the calling thread.
1113 * @param enmLevel Message importance level.
1114 * @param pszFormat The message.
1115 * @param ... Arguments.
1116 */
1117RTR3DECL(int) RTTestPrintfNl(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, ...)
1118{
1119 va_list va;
1120
1121 va_start(va, pszFormat);
1122 int cch = RTTestPrintfNlV(hTest, enmLevel, pszFormat, va);
1123 va_end(va);
1124
1125 return cch;
1126}
1127
1128
1129/**
1130 * Test vprintf, makes sure lines are prefixed and so forth.
1131 *
1132 * @returns Number of chars printed.
1133 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1134 * associated with the calling thread.
1135 * @param enmLevel Message importance level.
1136 * @param pszFormat The message.
1137 * @param va Arguments.
1138 */
1139RTR3DECL(int) RTTestPrintfV(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, va_list va)
1140{
1141 PRTTESTINT pTest = hTest;
1142 RTTEST_GET_VALID_RETURN_RC(pTest, -1);
1143
1144 RTCritSectEnter(&pTest->OutputLock);
1145 int cch = 0;
1146 if (enmLevel <= pTest->enmMaxLevel)
1147 cch += rtTestPrintfV(pTest, pszFormat, va);
1148 RTCritSectLeave(&pTest->OutputLock);
1149
1150 return cch;
1151}
1152
1153
1154/**
1155 * Test printf, makes sure lines are prefixed and so forth.
1156 *
1157 * @returns Number of chars printed.
1158 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1159 * associated with the calling thread.
1160 * @param enmLevel Message importance level.
1161 * @param pszFormat The message.
1162 * @param ... Arguments.
1163 */
1164RTR3DECL(int) RTTestPrintf(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, ...)
1165{
1166 va_list va;
1167
1168 va_start(va, pszFormat);
1169 int cch = RTTestPrintfV(hTest, enmLevel, pszFormat, va);
1170 va_end(va);
1171
1172 return cch;
1173}
1174
1175
1176/**
1177 * Prints the test banner.
1178 *
1179 * @returns Number of chars printed.
1180 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1181 * associated with the calling thread.
1182 */
1183RTR3DECL(int) RTTestBanner(RTTEST hTest)
1184{
1185 return RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "TESTING...\n");
1186}
1187
1188
1189/**
1190 * Prints the result of a sub-test if necessary.
1191 *
1192 * @returns Number of chars printed.
1193 * @param pTest The test instance.
1194 * @remarks Caller own the test Lock.
1195 */
1196static int rtTestSubTestReport(PRTTESTINT pTest)
1197{
1198 int cch = 0;
1199 if ( !pTest->fSubTestReported
1200 && pTest->pszSubTest)
1201 {
1202 pTest->fSubTestReported = true;
1203 uint32_t cErrors = ASMAtomicUoReadU32(&pTest->cErrors) - pTest->cSubTestAtErrors;
1204 if (!cErrors)
1205 {
1206 if (!pTest->fSubTestSkipped)
1207 {
1208 rtTestXmlElem(pTest, "Passed", NULL);
1209 rtTestXmlElemEnd(pTest, "Test");
1210 cch += RTTestPrintfNl(pTest, RTTESTLVL_SUB_TEST, "%-50s: PASSED\n", pTest->pszSubTest);
1211 }
1212 else
1213 {
1214 rtTestXmlElem(pTest, "Skipped", NULL);
1215 rtTestXmlElemEnd(pTest, "Test");
1216 cch += RTTestPrintfNl(pTest, RTTESTLVL_SUB_TEST, "%-50s: SKIPPED\n", pTest->pszSubTest);
1217 }
1218 }
1219 else
1220 {
1221 pTest->cSubTestsFailed++;
1222 rtTestXmlElem(pTest, "Failed", "errors=\"%u\"", cErrors);
1223 rtTestXmlElemEnd(pTest, "Test");
1224 cch += RTTestPrintfNl(pTest, RTTESTLVL_SUB_TEST, "%-50s: FAILED (%u errors)\n",
1225 pTest->pszSubTest, cErrors);
1226 }
1227 }
1228 return cch;
1229}
1230
1231
1232/**
1233 * RTTestSub and RTTestSubDone worker that cleans up the current (if any)
1234 * sub test.
1235 *
1236 * @returns Number of chars printed.
1237 * @param pTest The test instance.
1238 * @remarks Caller own the test Lock.
1239 */
1240static int rtTestSubCleanup(PRTTESTINT pTest)
1241{
1242 int cch = 0;
1243 if (pTest->pszSubTest)
1244 {
1245 cch += rtTestSubTestReport(pTest);
1246
1247 RTStrFree((char *)pTest->pszSubTest);
1248 pTest->pszSubTest = NULL;
1249 pTest->fSubTestReported = true;
1250 }
1251 return cch;
1252}
1253
1254
1255/**
1256 * Summaries the test, destroys the test instance and return an exit code.
1257 *
1258 * @returns Test program exit code.
1259 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1260 * associated with the calling thread.
1261 */
1262RTR3DECL(RTEXITCODE) RTTestSummaryAndDestroy(RTTEST hTest)
1263{
1264 PRTTESTINT pTest = hTest;
1265 RTTEST_GET_VALID_RETURN_RC(pTest, RTEXITCODE_FAILURE);
1266
1267 RTCritSectEnter(&pTest->Lock);
1268 rtTestSubTestReport(pTest);
1269 RTCritSectLeave(&pTest->Lock);
1270
1271 RTEXITCODE enmExitCode;
1272 if (!pTest->cErrors)
1273 {
1274 RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "SUCCESS\n");
1275 enmExitCode = RTEXITCODE_SUCCESS;
1276 }
1277 else
1278 {
1279 RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "FAILURE - %u errors\n", pTest->cErrors);
1280 enmExitCode = RTEXITCODE_FAILURE;
1281 }
1282
1283 RTTestDestroy(pTest);
1284 return enmExitCode;
1285}
1286
1287
1288RTR3DECL(RTEXITCODE) RTTestSkipAndDestroyV(RTTEST hTest, const char *pszReasonFmt, va_list va)
1289{
1290 PRTTESTINT pTest = hTest;
1291 RTTEST_GET_VALID_RETURN_RC(pTest, RTEXITCODE_SKIPPED);
1292
1293 RTCritSectEnter(&pTest->Lock);
1294 rtTestSubTestReport(pTest);
1295 RTCritSectLeave(&pTest->Lock);
1296
1297 RTEXITCODE enmExitCode;
1298 if (!pTest->cErrors)
1299 {
1300 if (pszReasonFmt)
1301 RTTestPrintfNlV(hTest, RTTESTLVL_FAILURE, pszReasonFmt, va);
1302 RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "SKIPPED\n");
1303 enmExitCode = RTEXITCODE_SKIPPED;
1304 }
1305 else
1306 {
1307 RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "FAILURE - %u errors\n", pTest->cErrors);
1308 enmExitCode = RTEXITCODE_FAILURE;
1309 }
1310
1311 RTTestDestroy(pTest);
1312 return enmExitCode;
1313}
1314
1315
1316RTR3DECL(RTEXITCODE) RTTestSkipAndDestroy(RTTEST hTest, const char *pszReasonFmt, ...)
1317{
1318 va_list va;
1319 va_start(va, pszReasonFmt);
1320 RTEXITCODE enmExitCode = RTTestSkipAndDestroyV(hTest, pszReasonFmt, va);
1321 va_end(va);
1322 return enmExitCode;
1323}
1324
1325
1326/**
1327 * Starts a sub-test.
1328 *
1329 * This will perform an implicit RTTestSubDone() call if that has not been done
1330 * since the last RTTestSub call.
1331 *
1332 * @returns Number of chars printed.
1333 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1334 * associated with the calling thread.
1335 * @param pszSubTest The sub-test name
1336 */
1337RTR3DECL(int) RTTestSub(RTTEST hTest, const char *pszSubTest)
1338{
1339 PRTTESTINT pTest = hTest;
1340 RTTEST_GET_VALID_RETURN_RC(pTest, -1);
1341
1342 RTCritSectEnter(&pTest->Lock);
1343
1344 /* Cleanup, reporting if necessary previous sub test. */
1345 rtTestSubCleanup(pTest);
1346
1347 /* Start new sub test. */
1348 pTest->cSubTests++;
1349 pTest->cSubTestAtErrors = ASMAtomicUoReadU32(&pTest->cErrors);
1350 pTest->pszSubTest = RTStrDup(pszSubTest);
1351 pTest->cchSubTest = strlen(pszSubTest);
1352 pTest->fSubTestSkipped = false;
1353 pTest->fSubTestReported = false;
1354
1355 int cch = 0;
1356 if (pTest->enmMaxLevel >= RTTESTLVL_DEBUG)
1357 cch = RTTestPrintfNl(hTest, RTTESTLVL_DEBUG, "debug: Starting sub-test '%s'\n", pszSubTest);
1358
1359 if (!pTest->fXmlTopTestDone)
1360 {
1361 pTest->fXmlTopTestDone = true;
1362 rtTestXmlElemStart(pTest, "Test", "name=%RMas", pTest->pszTest);
1363 }
1364
1365 rtTestXmlElemStart(pTest, "Test", "name=%RMas", pszSubTest);
1366
1367 RTCritSectLeave(&pTest->Lock);
1368
1369 return cch;
1370}
1371
1372
1373/**
1374 * Format string version of RTTestSub.
1375 *
1376 * See RTTestSub for details.
1377 *
1378 * @returns Number of chars printed.
1379 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1380 * associated with the calling thread.
1381 * @param pszSubTestFmt The sub-test name format string.
1382 * @param ... Arguments.
1383 */
1384RTR3DECL(int) RTTestSubF(RTTEST hTest, const char *pszSubTestFmt, ...)
1385{
1386 va_list va;
1387 va_start(va, pszSubTestFmt);
1388 int cch = RTTestSubV(hTest, pszSubTestFmt, va);
1389 va_end(va);
1390 return cch;
1391}
1392
1393
1394/**
1395 * Format string version of RTTestSub.
1396 *
1397 * See RTTestSub for details.
1398 *
1399 * @returns Number of chars printed.
1400 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1401 * associated with the calling thread.
1402 * @param pszSubTestFmt The sub-test name format string.
1403 * @param ... Arguments.
1404 */
1405RTR3DECL(int) RTTestSubV(RTTEST hTest, const char *pszSubTestFmt, va_list va)
1406{
1407 char *pszSubTest;
1408 RTStrAPrintfV(&pszSubTest, pszSubTestFmt, va);
1409 if (pszSubTest)
1410 {
1411 int cch = RTTestSub(hTest, pszSubTest);
1412 RTStrFree(pszSubTest);
1413 return cch;
1414 }
1415 return 0;
1416}
1417
1418
1419/**
1420 * Completes a sub-test.
1421 *
1422 * @returns Number of chars printed.
1423 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1424 * associated with the calling thread.
1425 */
1426RTR3DECL(int) RTTestSubDone(RTTEST hTest)
1427{
1428 PRTTESTINT pTest = hTest;
1429 RTTEST_GET_VALID_RETURN_RC(pTest, VERR_INVALID_HANDLE);
1430
1431 RTCritSectEnter(&pTest->Lock);
1432 int cch = rtTestSubCleanup(pTest);
1433 RTCritSectLeave(&pTest->Lock);
1434
1435 return cch;
1436}
1437
1438/**
1439 * Prints an extended PASSED message, optional.
1440 *
1441 * This does not conclude the sub-test, it could be used to report the passing
1442 * of a sub-sub-to-the-power-of-N-test.
1443 *
1444 * @returns IPRT status code.
1445 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1446 * associated with the calling thread.
1447 * @param pszFormat The message. No trailing newline.
1448 * @param va The arguments.
1449 */
1450RTR3DECL(int) RTTestPassedV(RTTEST hTest, const char *pszFormat, va_list va)
1451{
1452 PRTTESTINT pTest = hTest;
1453 AssertPtr(pszFormat);
1454 RTTEST_GET_VALID_RETURN_RC(pTest, VERR_INVALID_HANDLE);
1455
1456 int cch = 0;
1457 if (pTest->enmMaxLevel >= RTTESTLVL_INFO)
1458 {
1459 va_list va2;
1460 va_copy(va2, va);
1461
1462 RTCritSectEnter(&pTest->OutputLock);
1463 cch += rtTestPrintf(pTest, "%N\n", pszFormat, &va2);
1464 RTCritSectLeave(&pTest->OutputLock);
1465
1466 va_end(va2);
1467 }
1468
1469 return cch;
1470}
1471
1472
1473/**
1474 * Prints an extended PASSED message, optional.
1475 *
1476 * This does not conclude the sub-test, it could be used to report the passing
1477 * of a sub-sub-to-the-power-of-N-test.
1478 *
1479 * @returns IPRT status code.
1480 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1481 * associated with the calling thread.
1482 * @param pszFormat The message. No trailing newline.
1483 * @param ... The arguments.
1484 */
1485RTR3DECL(int) RTTestPassed(RTTEST hTest, const char *pszFormat, ...)
1486{
1487 va_list va;
1488
1489 va_start(va, pszFormat);
1490 int cch = RTTestPassedV(hTest, pszFormat, va);
1491 va_end(va);
1492
1493 return cch;
1494}
1495
1496
1497RTR3DECL(int) RTTestSkippedV(RTTEST hTest, const char *pszFormat, va_list va)
1498{
1499 PRTTESTINT pTest = hTest;
1500 AssertPtrNull(pszFormat);
1501 RTTEST_GET_VALID_RETURN_RC(pTest, VERR_INVALID_HANDLE);
1502
1503 pTest->fSubTestSkipped = true;
1504
1505 int cch = 0;
1506 if (pszFormat && *pszFormat && pTest->enmMaxLevel >= RTTESTLVL_INFO)
1507 {
1508 va_list va2;
1509 va_copy(va2, va);
1510
1511 RTCritSectEnter(&pTest->OutputLock);
1512 cch += rtTestPrintf(pTest, "%N\n", pszFormat, &va2);
1513 RTCritSectLeave(&pTest->OutputLock);
1514
1515 va_end(va2);
1516 }
1517
1518 return cch;
1519}
1520
1521
1522RTR3DECL(int) RTTestSkipped(RTTEST hTest, const char *pszFormat, ...)
1523{
1524 va_list va;
1525
1526 va_start(va, pszFormat);
1527 int cch = RTTestSkippedV(hTest, pszFormat, va);
1528 va_end(va);
1529
1530 return cch;
1531}
1532
1533
1534
1535/**
1536 * Gets the unit name.
1537 *
1538 * @returns Unit name.
1539 * @param enmUnit The unit.
1540 */
1541static const char *rtTestUnitName(RTTESTUNIT enmUnit)
1542{
1543 switch (enmUnit)
1544 {
1545 case RTTESTUNIT_PCT: return "%";
1546 case RTTESTUNIT_BYTES: return "bytes";
1547 case RTTESTUNIT_BYTES_PER_SEC: return "bytes/s";
1548 case RTTESTUNIT_KILOBYTES: return "KB";
1549 case RTTESTUNIT_KILOBYTES_PER_SEC: return "KB/s";
1550 case RTTESTUNIT_MEGABYTES: return "MB";
1551 case RTTESTUNIT_MEGABYTES_PER_SEC: return "MB/s";
1552 case RTTESTUNIT_PACKETS: return "packets";
1553 case RTTESTUNIT_PACKETS_PER_SEC: return "packets/s";
1554 case RTTESTUNIT_FRAMES: return "frames";
1555 case RTTESTUNIT_FRAMES_PER_SEC: return "frames/s";
1556 case RTTESTUNIT_OCCURRENCES: return "occurrences";
1557 case RTTESTUNIT_OCCURRENCES_PER_SEC: return "occurrences/s";
1558 case RTTESTUNIT_ROUND_TRIP: return "roundtrips";
1559 case RTTESTUNIT_CALLS: return "calls";
1560 case RTTESTUNIT_CALLS_PER_SEC: return "calls/s";
1561 case RTTESTUNIT_SECS: return "s";
1562 case RTTESTUNIT_MS: return "ms";
1563 case RTTESTUNIT_NS: return "ns";
1564 case RTTESTUNIT_NS_PER_CALL: return "ns/call";
1565 case RTTESTUNIT_NS_PER_FRAME: return "ns/frame";
1566 case RTTESTUNIT_NS_PER_OCCURRENCE: return "ns/occurrence";
1567 case RTTESTUNIT_NS_PER_PACKET: return "ns/packet";
1568 case RTTESTUNIT_NS_PER_ROUND_TRIP: return "ns/roundtrip";
1569 case RTTESTUNIT_INSTRS: return "ins";
1570 case RTTESTUNIT_INSTRS_PER_SEC: return "ins/sec";
1571 case RTTESTUNIT_NONE: return "";
1572 case RTTESTUNIT_PP1K: return "pp1k";
1573 case RTTESTUNIT_PP10K: return "pp10k";
1574 case RTTESTUNIT_PPM: return "ppm";
1575 case RTTESTUNIT_PPB: return "ppb";
1576
1577 /* No default so gcc helps us keep this up to date. */
1578 case RTTESTUNIT_INVALID:
1579 case RTTESTUNIT_END:
1580 break;
1581 }
1582 AssertMsgFailed(("%d\n", enmUnit));
1583 return "unknown";
1584}
1585
1586
1587RTR3DECL(int) RTTestValue(RTTEST hTest, const char *pszName, uint64_t u64Value, RTTESTUNIT enmUnit)
1588{
1589 PRTTESTINT pTest = hTest;
1590 RTTEST_GET_VALID_RETURN(pTest);
1591
1592 const char *pszUnit = rtTestUnitName(enmUnit);
1593
1594 RTCritSectEnter(&pTest->Lock);
1595 rtTestXmlElem(pTest, "Value", "name=%RMas unit=%RMas value=\"%llu\"", pszName, pszUnit, u64Value);
1596 RTCritSectLeave(&pTest->Lock);
1597
1598 RTCritSectEnter(&pTest->OutputLock);
1599 rtTestPrintf(pTest, " %-48s: %'16llu %s\n", pszName, u64Value, pszUnit);
1600 RTCritSectLeave(&pTest->OutputLock);
1601
1602 return VINF_SUCCESS;
1603}
1604
1605
1606RTR3DECL(int) RTTestValueF(RTTEST hTest, uint64_t u64Value, RTTESTUNIT enmUnit, const char *pszNameFmt, ...)
1607{
1608 va_list va;
1609 va_start(va, pszNameFmt);
1610 int rc = RTTestValueV(hTest, u64Value, enmUnit, pszNameFmt, va);
1611 va_end(va);
1612 return rc;
1613}
1614
1615
1616RTR3DECL(int) RTTestValueV(RTTEST hTest, uint64_t u64Value, RTTESTUNIT enmUnit, const char *pszNameFmt, va_list va)
1617{
1618 char *pszName;
1619 RTStrAPrintfV(&pszName, pszNameFmt, va);
1620 if (!pszName)
1621 return VERR_NO_MEMORY;
1622 int rc = RTTestValue(hTest, pszName, u64Value, enmUnit);
1623 RTStrFree(pszName);
1624 return rc;
1625}
1626
1627
1628/**
1629 * Increments the error counter.
1630 *
1631 * @returns IPRT status code.
1632 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1633 * associated with the calling thread.
1634 */
1635RTR3DECL(int) RTTestErrorInc(RTTEST hTest)
1636{
1637 PRTTESTINT pTest = hTest;
1638 RTTEST_GET_VALID_RETURN(pTest);
1639
1640 ASMAtomicIncU32(&pTest->cErrors);
1641
1642 return VINF_SUCCESS;
1643}
1644
1645
1646
1647/**
1648 * Get the current error count.
1649 *
1650 * @returns The error counter, UINT32_MAX if no valid test handle.
1651 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1652 * associated with the calling thread.
1653 */
1654RTR3DECL(uint32_t) RTTestErrorCount(RTTEST hTest)
1655{
1656 PRTTESTINT pTest = hTest;
1657 RTTEST_GET_VALID_RETURN_RC(pTest, UINT32_MAX);
1658
1659 return ASMAtomicReadU32(&pTest->cErrors);
1660}
1661
1662
1663RTR3DECL(uint32_t) RTTestSubErrorCount(RTTEST hTest)
1664{
1665 PRTTESTINT pTest = hTest;
1666 RTTEST_GET_VALID_RETURN_RC(pTest, UINT32_MAX);
1667
1668 return ASMAtomicReadU32(&pTest->cErrors) - pTest->cSubTestAtErrors;
1669}
1670
1671
1672/**
1673 * Increments the error counter and prints a failure message.
1674 *
1675 * @returns IPRT status code.
1676 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1677 * associated with the calling thread.
1678 * @param pszFormat The message. No trailing newline.
1679 * @param va The arguments.
1680 */
1681RTR3DECL(int) RTTestFailedV(RTTEST hTest, const char *pszFormat, va_list va)
1682{
1683 PRTTESTINT pTest = hTest;
1684 RTTEST_GET_VALID_RETURN_RC(pTest, VERR_INVALID_HANDLE);
1685
1686 RTTestErrorInc(pTest);
1687
1688 int cch = 0;
1689 if (pTest->enmMaxLevel >= RTTESTLVL_FAILURE)
1690 {
1691 va_list va2;
1692 va_copy(va2, va);
1693
1694 const char *pszEnd = strchr(pszFormat, '\0');
1695 bool fHasNewLine = pszFormat != pszEnd
1696 && pszEnd[-1] == '\n';
1697
1698 RTCritSectEnter(&pTest->OutputLock);
1699 cch += rtTestPrintf(pTest, fHasNewLine ? "%N" : "%N\n", pszFormat, &va2);
1700 RTCritSectLeave(&pTest->OutputLock);
1701
1702 va_end(va2);
1703 }
1704
1705 return cch;
1706}
1707
1708
1709/**
1710 * Increments the error counter and prints a failure message.
1711 *
1712 * @returns IPRT status code.
1713 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1714 * associated with the calling thread.
1715 * @param pszFormat The message. No trailing newline.
1716 * @param ... The arguments.
1717 */
1718RTR3DECL(int) RTTestFailed(RTTEST hTest, const char *pszFormat, ...)
1719{
1720 va_list va;
1721
1722 va_start(va, pszFormat);
1723 int cch = RTTestFailedV(hTest, pszFormat, va);
1724 va_end(va);
1725
1726 return cch;
1727}
1728
1729
1730/**
1731 * Same as RTTestPrintfV with RTTESTLVL_FAILURE.
1732 *
1733 * @returns Number of chars printed.
1734 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1735 * associated with the calling thread.
1736 * @param pszFormat The message.
1737 * @param va Arguments.
1738 */
1739RTR3DECL(int) RTTestFailureDetailsV(RTTEST hTest, const char *pszFormat, va_list va)
1740{
1741 return RTTestPrintfV(hTest, RTTESTLVL_FAILURE, pszFormat, va);
1742}
1743
1744
1745/**
1746 * Same as RTTestPrintf with RTTESTLVL_FAILURE.
1747 *
1748 * @returns Number of chars printed.
1749 * @param hTest The test handle. If NIL_RTTEST we'll use the one
1750 * associated with the calling thread.
1751 * @param pszFormat The message.
1752 * @param ... Arguments.
1753 */
1754RTR3DECL(int) RTTestFailureDetails(RTTEST hTest, const char *pszFormat, ...)
1755{
1756 va_list va;
1757 va_start(va, pszFormat);
1758 int cch = RTTestFailureDetailsV(hTest, pszFormat, va);
1759 va_end(va);
1760 return cch;
1761}
1762
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