1 | /* $Id: tstRTR0Common.h 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT R0 Testcase - Common header.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 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 | #ifndef ___testcase_tstRTR0Common_h
|
---|
29 | #define ___testcase_tstRTR0Common_h
|
---|
30 |
|
---|
31 | #include <iprt/stdarg.h>
|
---|
32 | #include <iprt/string.h>
|
---|
33 | #include "tstRTR0CommonReq.h"
|
---|
34 |
|
---|
35 |
|
---|
36 | /*******************************************************************************
|
---|
37 | * Global Variables *
|
---|
38 | *******************************************************************************/
|
---|
39 | /** Global error buffer used by macros and inline functions in this file. */
|
---|
40 | static char g_szErr[2048];
|
---|
41 | /** The number of errors reported in this g_szErr. */
|
---|
42 | static uint32_t volatile g_cErrors;
|
---|
43 |
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Service request handler prolog.
|
---|
47 | *
|
---|
48 | * Returns if the input is invalid. Initializes the return packet as well as
|
---|
49 | * the globals (g_szErr, g_cErrors).
|
---|
50 | *
|
---|
51 | * @param pReqHdr The request packet header.
|
---|
52 | */
|
---|
53 | #define RTR0TESTR0_SRV_REQ_PROLOG_RET(pReqHdr) \
|
---|
54 | do \
|
---|
55 | { \
|
---|
56 | if (!VALID_PTR(pReqHdr)) \
|
---|
57 | return VERR_INVALID_PARAMETER; \
|
---|
58 | \
|
---|
59 | PRTTSTR0REQ pReq = (PRTTSTR0REQ)(pReqHdr); \
|
---|
60 | size_t cchErr = pReqHdr->cbReq - sizeof(pReq->Hdr); \
|
---|
61 | if (cchErr < 32 || cchErr >= 0x10000) \
|
---|
62 | return VERR_INVALID_PARAMETER; \
|
---|
63 | pReq->szMsg[0] = '\0'; \
|
---|
64 | \
|
---|
65 | /* Initialize the global buffer. */ \
|
---|
66 | memset(&g_szErr[0], 0, sizeof(g_szErr)); \
|
---|
67 | ASMAtomicWriteU32(&g_cErrors, 0); \
|
---|
68 | } while (0)
|
---|
69 |
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Service request handler epilog.
|
---|
73 | *
|
---|
74 | * Copies any errors or messages into the request packet.
|
---|
75 | *
|
---|
76 | * @param pReqHdr The request packet header.
|
---|
77 | */
|
---|
78 | #define RTR0TESTR0_SRV_REQ_EPILOG(pReqHdr) \
|
---|
79 | do \
|
---|
80 | { \
|
---|
81 | PRTTSTR0REQ pReq = (PRTTSTR0REQ)(pReqHdr); \
|
---|
82 | size_t cbErr = pReqHdr->cbReq - sizeof(pReq->Hdr); \
|
---|
83 | if (g_szErr[0] && pReq->szMsg[0] != '!') \
|
---|
84 | RTStrCopyEx(pReq->szMsg, (cbErr), g_szErr, sizeof(g_szErr) - 1); \
|
---|
85 | } while (0)
|
---|
86 |
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Implement the sanity check switch-cases of a service request handler.
|
---|
90 | */
|
---|
91 | #define RTR0TESTR0_IMPLEMENT_SANITY_CASES() \
|
---|
92 | case RTTSTR0REQ_SANITY_OK: \
|
---|
93 | break; \
|
---|
94 | case RTTSTR0REQ_SANITY_FAILURE: \
|
---|
95 | RTR0TestR0Error("42failure42%4096s", ""); \
|
---|
96 | break
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Implements the default switch-case of a service request handler.
|
---|
100 | * @param uOperation The operation.
|
---|
101 | */
|
---|
102 | #define RTR0TESTR0_IMPLEMENT_DEFAULT_CASE(uOperation) \
|
---|
103 | default: \
|
---|
104 | RTR0TestR0Error("Unknown test #%d", (uOperation)); \
|
---|
105 | break
|
---|
106 |
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Macro for checking the return code of an API in the ring-0 testcase.
|
---|
110 | *
|
---|
111 | * Similar to RTTESTI_CHECK_RC.
|
---|
112 | *
|
---|
113 | * @param rcExpr The expression producing the return code. Only
|
---|
114 | * evaluated once.
|
---|
115 | * @param rcExpect The expected result. Evaluated multiple times.
|
---|
116 | */
|
---|
117 | #define RTR0TESTR0_CHECK_RC(rcExpr, rcExpect) \
|
---|
118 | do { \
|
---|
119 | int rcCheck = (rcExpr); \
|
---|
120 | if (rcCheck != (rcExpect)) \
|
---|
121 | RTR0TestR0Error("line %u: %s: expected %Rrc, got %Rrc", __LINE__, #rcExpr, (rcExpect), rcCheck); \
|
---|
122 | } while (0)
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Same as RTR0TESTR0_CHECK_RC + break.
|
---|
126 | */
|
---|
127 | #define RTR0TESTR0_CHECK_RC_BREAK(rcExpr, rcExpect) \
|
---|
128 | if (1) \
|
---|
129 | { \
|
---|
130 | int rcCheck = (rcExpr); \
|
---|
131 | if (rcCheck != (rcExpect)) \
|
---|
132 | { \
|
---|
133 | RTR0TestR0Error("line %u: %s: expected %Rrc, got %Rrc", __LINE__, #rcExpr, (rcExpect), rcCheck); \
|
---|
134 | break; \
|
---|
135 | } \
|
---|
136 | } else do { } while (0)
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Macro for checking the return code of an API in the ring-0 testcase.
|
---|
140 | *
|
---|
141 | * Similar to RTTESTI_CHECK_MSG
|
---|
142 | *
|
---|
143 | * @param expr The expression to evaluate.
|
---|
144 | * @param DetailsArgs Format string + arguments - in parenthesis.
|
---|
145 | */
|
---|
146 | #define RTR0TESTR0_CHECK_MSG(expr, DetailsArgs) \
|
---|
147 | do { \
|
---|
148 | if (!(expr)) \
|
---|
149 | { \
|
---|
150 | RTR0TestR0Error("line %u: expression failed: %s - ", __LINE__, #expr); \
|
---|
151 | RTR0TestR0AppendDetails DetailsArgs; \
|
---|
152 | } \
|
---|
153 | } while (0)
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Same as RTR0TESTR0_CHECK_MSG + break.
|
---|
157 | */
|
---|
158 | #define RTR0TESTR0_CHECK_MSG_BREAK(expr, DetailsArgs) \
|
---|
159 | if (!(expr)) \
|
---|
160 | { \
|
---|
161 | RTR0TestR0Error("line %u: expression failed: %s - ", __LINE__, #expr); \
|
---|
162 | RTR0TestR0AppendDetails DetailsArgs; \
|
---|
163 | break; \
|
---|
164 | } else do { } while (0)
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Same as RTR0TESTR0_CHECK_MSG + return @a rcRete.
|
---|
168 | */
|
---|
169 | #define RTR0TESTR0_CHECK_MSG_RET(expr, DetailsArgs, rcRet) \
|
---|
170 | do { \
|
---|
171 | if (!(expr)) \
|
---|
172 | { \
|
---|
173 | RTR0TestR0Error("line %u: expression failed: %s - ", __LINE__, #expr); \
|
---|
174 | RTR0TestR0AppendDetails DetailsArgs; \
|
---|
175 | return (rcRet); \
|
---|
176 | } \
|
---|
177 | } while (0)
|
---|
178 |
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Report an error.
|
---|
182 | */
|
---|
183 | void RTR0TestR0Error(const char *pszFormat, ...)
|
---|
184 | {
|
---|
185 | size_t off = RTStrNLen(g_szErr, sizeof(g_szErr) - 1);
|
---|
186 | size_t cbLeft = sizeof(g_szErr) - off;
|
---|
187 | if (cbLeft > 10)
|
---|
188 | {
|
---|
189 | char *psz = &g_szErr[off];
|
---|
190 | if (off)
|
---|
191 | {
|
---|
192 | *psz++ = '\n';
|
---|
193 | *psz++ = '\n';
|
---|
194 | cbLeft -= 2;
|
---|
195 | }
|
---|
196 | *psz++ = '!';
|
---|
197 | cbLeft--;
|
---|
198 |
|
---|
199 | va_list va;
|
---|
200 | va_start(va, pszFormat);
|
---|
201 | RTStrPrintfV(psz, cbLeft, pszFormat, va);
|
---|
202 | va_end(va);
|
---|
203 | }
|
---|
204 | ASMAtomicIncU32(&g_cErrors);
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Append error details.
|
---|
210 | */
|
---|
211 | void RTR0TestR0AppendDetails(const char *pszFormat, ...)
|
---|
212 | {
|
---|
213 | size_t off = RTStrNLen(g_szErr, sizeof(g_szErr) - 1);
|
---|
214 | va_list va;
|
---|
215 | va_start(va, pszFormat);
|
---|
216 | RTStrPrintfV(&g_szErr[off], sizeof(g_szErr) - off, pszFormat, va);
|
---|
217 | va_end(va);
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * Informational message.
|
---|
223 | */
|
---|
224 | void RTR0TestR0Info(const char *pszFormat, ...)
|
---|
225 | {
|
---|
226 | size_t off = RTStrNLen(g_szErr, sizeof(g_szErr) - 1);
|
---|
227 | size_t cbLeft = sizeof(g_szErr) - off;
|
---|
228 | if (cbLeft > 10)
|
---|
229 | {
|
---|
230 | char *psz = &g_szErr[off];
|
---|
231 | if (off)
|
---|
232 | {
|
---|
233 | *psz++ = '\n';
|
---|
234 | *psz++ = '\n';
|
---|
235 | cbLeft -= 2;
|
---|
236 | }
|
---|
237 | *psz++ = '?';
|
---|
238 | cbLeft--;
|
---|
239 |
|
---|
240 | va_list va;
|
---|
241 | va_start(va, pszFormat);
|
---|
242 | RTStrPrintfV(psz, cbLeft, pszFormat, va);
|
---|
243 | va_end(va);
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 | /**
|
---|
248 | * Checks if we have any error reports.
|
---|
249 | *
|
---|
250 | * @returns true if there are errors, false if none.
|
---|
251 | */
|
---|
252 | bool RTR0TestR0HaveErrors(void)
|
---|
253 | {
|
---|
254 | return ASMAtomicUoReadU32(&g_cErrors) > 0;
|
---|
255 | }
|
---|
256 |
|
---|
257 | #endif
|
---|
258 |
|
---|