VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTErr-1.cpp@ 98103

Last change on this file since 98103 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.7 KB
Line 
1/* $Id: tstRTErr-1.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Error Messages.
4 */
5
6/*
7 * Copyright (C) 2020-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/errcore.h>
42
43#include <iprt/test.h>
44#include <iprt/string.h>
45#include <iprt/err.h>
46
47
48
49static void tstIprtStatuses(RTTEST hTest)
50{
51 RTTestSub(hTest, "IPRT status codes");
52
53 char szMsgShort[640];
54 char szMsgFull[sizeof(szMsgShort)];
55 char szMsgAll[sizeof(szMsgShort) + 80];
56 size_t const cbBuf = sizeof(szMsgShort);
57 char * const pszBuf = (char *)RTTestGuardedAllocTail(hTest, cbBuf);
58 RTTESTI_CHECK_RETV(pszBuf);
59
60 static const struct
61 {
62 int rc;
63 const char *pszDefine;
64 } s_aTests[] =
65 {
66 { VINF_SUCCESS, "VINF_SUCCESS" },
67 { VERR_INVALID_PARAMETER, "VERR_INVALID_PARAMETER" },
68 { VERR_NOT_IMPLEMENTED, "VERR_NOT_IMPLEMENTED" },
69 { VERR_NUMBER_TOO_BIG, "VERR_NUMBER_TOO_BIG" },
70 { VWRN_NUMBER_TOO_BIG, "VWRN_NUMBER_TOO_BIG" },
71 { VERR_CANCELLED, "VERR_CANCELLED" },
72 { VERR_ISOMK_IMPORT_BOOT_CAT_DEF_ENTRY_INVALID_BOOT_IND, "VERR_ISOMK_IMPORT_BOOT_CAT_DEF_ENTRY_INVALID_BOOT_IND" },
73 { VERR_CR_CIPHER_INVALID_INITIALIZATION_VECTOR_LENGTH, "VERR_CR_CIPHER_INVALID_INITIALIZATION_VECTOR_LENGTH" },
74 };
75 for (size_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
76 {
77 int const rc = s_aTests[i].rc;
78 const char * const pszDefine = s_aTests[i].pszDefine;
79 size_t const cchDefine = strlen(pszDefine);
80
81 if (RTErrIsKnown(rc) != true)
82 RTTestFailed(hTest, "RTErrIsKnown(%s) did not return true", pszDefine);
83
84 RTTestDisableAssertions(hTest);
85 size_t cchMsgShort = ~(size_t)0;
86 size_t cchMsgFull = ~(size_t)0;
87 size_t cchMsgAll = ~(size_t)0;
88 size_t cbBuf2 = cbBuf - 1;
89 while (cbBuf2-- > 0)
90 {
91#define CHECK_TEST_RESULT(a_szFunction, a_pszExpect, a_cchExpect) do { \
92 if (cbBuf2 > (a_cchExpect) && cchRet != (ssize_t)(a_cchExpect)) \
93 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %zd", a_szFunction, pszDefine, cbBuf2, \
94 cchRet, (a_cchExpect)); \
95 else if (cbBuf2 <= (a_cchExpect) && cchRet != VERR_BUFFER_OVERFLOW) \
96 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %d", a_szFunction, pszDefine, cbBuf2, \
97 cchRet, VERR_BUFFER_OVERFLOW); \
98 else if (cbBuf2 > (a_cchExpect) && memcmp(pszBuf2, (a_pszExpect), (a_cchExpect) + 1) != 0) \
99 RTTestFailed(hTest, "%s(%s, , %#x) -> '%.*s', expected '%s'", a_szFunction, pszDefine, cbBuf2, \
100 cbBuf2, pszBuf2, (a_pszExpect)); \
101 /* Only check that it's terminated. Compression may exit before it's quite full. */ \
102 else if (cbBuf2 > 0 && RTStrNLen(pszBuf2, cbBuf2) >= cbBuf2) \
103 RTTestFailed(hTest, "%s(%s, , %#x) -> result not terminated", a_szFunction, pszDefine, cbBuf2); \
104 } while (0)
105
106 /* RTErrQueryDefine: */
107 memset(pszBuf, '?', cbBuf);
108 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
109 ssize_t cchRet = RTErrQueryDefine(rc, pszBuf2, cbBuf2, false);
110 CHECK_TEST_RESULT("RTErrQueryDefine", pszDefine, cchDefine);
111
112 /* RTErrQueryMsgShort: */
113 memset(pszBuf, '?', cbBuf);
114 cchRet = RTErrQueryMsgShort(rc, pszBuf2, cbBuf2, false);
115 if (cchMsgShort == ~(size_t)0)
116 {
117 cchMsgShort = (size_t)cchRet;
118 memcpy(szMsgShort, pszBuf2, cchMsgShort);
119 szMsgShort[cchMsgShort] = '\0';
120 }
121 CHECK_TEST_RESULT("RTErrQueryMsgShort", szMsgShort, cchMsgShort);
122
123 /* RTErrQueryMsgFull: */
124 memset(pszBuf, '?', cbBuf);
125 cchRet = RTErrQueryMsgFull(rc, pszBuf2, cbBuf2, false);
126 if (cchMsgFull == ~(size_t)0)
127 {
128 cchMsgFull = (size_t)cchRet;
129 memcpy(szMsgFull, pszBuf2, cchMsgFull);
130 szMsgFull[cchMsgFull] = '\0';
131 }
132 CHECK_TEST_RESULT("RTErrQueryMsgFull", szMsgFull, cchMsgFull);
133
134 /* Same thru the string formatter. */
135#define CHECK_TEST_RESULT2(a_szFunction, a_pszExpect, a_cchExpect) do { \
136 ssize_t const cchLocalExpect = cbBuf2 > (a_cchExpect) ? (ssize_t)(a_cchExpect) : -(ssize_t)(a_cchExpect) - 1; \
137 if (cchRet != cchLocalExpect && cchRet > 0) \
138 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %zd ('%s' vs '%s')", a_szFunction, pszDefine, cbBuf2, \
139 cchRet, cchLocalExpect, pszBuf2, (a_pszExpect)); \
140 else if (cchRet != cchLocalExpect && cchRet <= 0) \
141 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %zd", a_szFunction, pszDefine, cbBuf2, \
142 cchRet, cchLocalExpect); \
143 else if (cbBuf2 > 0 && memcmp(pszBuf2, (a_pszExpect), RT_MIN(cbBuf2 - 1, (a_cchExpect) + 1)) != 0) \
144 RTTestFailed(hTest, "%s(%s, , %#x) -> '%.*s', expected '%s'", a_szFunction, pszDefine, cbBuf2, \
145 cbBuf2, pszBuf2, (a_pszExpect)); \
146 else if (cbBuf2 > 0 && cbBuf2 <= (a_cchExpect) && pszBuf2[cbBuf2 - 1] != '\0') \
147 RTTestFailed(hTest, "%s(%s, , %#x) -> result not terminated", a_szFunction, pszDefine, cbBuf2); \
148 } while (0)
149 memset(pszBuf, '?', cbBuf);
150 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrc", rc);
151 CHECK_TEST_RESULT2("RTErrFormatDefine/%Rrc", pszDefine, cchDefine);
152
153 memset(pszBuf, '?', cbBuf);
154 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrs", rc);
155 CHECK_TEST_RESULT2("RTErrFormatMsgShort/%Rrs", szMsgShort, cchMsgShort);
156
157 memset(pszBuf, '?', cbBuf);
158 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrf", rc);
159 CHECK_TEST_RESULT2("RTErrFormatMsgFull/%Rrf", szMsgFull, cchMsgFull);
160
161 if (cchMsgAll == ~(size_t)0)
162 cchMsgAll = RTStrPrintf(szMsgAll, sizeof(szMsgAll), "%s (%d) - %s", pszDefine, rc, szMsgFull);
163 memset(pszBuf, '?', cbBuf);
164 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rra", rc);
165 CHECK_TEST_RESULT2("RTErrFormatMsgAll/%Rra", szMsgAll, cchMsgAll);
166 }
167 RTTestRestoreAssertions(hTest);
168 }
169
170 /*
171 * Same but for an unknown status code.
172 */
173 static const int s_arcUnknowns[] = { -270, 270, -88888888, 88888888, };
174 for (size_t i = 0; i < RT_ELEMENTS(s_arcUnknowns); i++)
175 {
176 int const rc = s_arcUnknowns[i];
177
178 if (RTErrIsKnown(rc) != false)
179 RTTestFailed(hTest, "RTErrIsKnown(%d) did not return false", rc);
180
181 size_t const cchDefine = RTStrPrintf(szMsgFull, sizeof(szMsgFull), "%d", rc);
182 const char * const pszDefine = szMsgFull;
183 size_t const cchMsg = RTStrPrintf(szMsgShort, sizeof(szMsgShort), "Unknown Status %d (%#x)", rc, rc);
184 const char * const pszMsg = szMsgShort;
185
186 RTTestDisableAssertions(hTest);
187 size_t cbBuf2 = cbBuf - 1;
188 while (cbBuf2-- > 0)
189 {
190 /* RTErrQueryDefine: */
191 memset(pszBuf, '?', cbBuf);
192 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
193 ssize_t cchRet = RTErrQueryDefine(rc, pszBuf2, cbBuf2, false);
194 CHECK_TEST_RESULT("RTErrQueryDefine", pszDefine, cchDefine);
195 RTTEST_CHECK(hTest, RTErrQueryDefine(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
196
197
198 /* RTErrQueryMsgShort: */
199 memset(pszBuf, '?', cbBuf);
200 cchRet = RTErrQueryMsgShort(rc, pszBuf2, cbBuf2, false);
201 CHECK_TEST_RESULT("RTErrQueryMsgShort", pszMsg, cchMsg);
202 RTTEST_CHECK(hTest, RTErrQueryMsgShort(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
203
204 /* RTErrQueryMsgFull: */
205 memset(pszBuf, '?', cbBuf);
206 cchRet = RTErrQueryMsgFull(rc, pszBuf2, cbBuf2, false);
207 CHECK_TEST_RESULT("RTErrQueryMsgFull", pszMsg, cchMsg);
208 RTTEST_CHECK(hTest, RTErrQueryMsgFull(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
209
210 /* Same thru the string formatter. */
211 memset(pszBuf, '?', cbBuf);
212 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrc", rc);
213 CHECK_TEST_RESULT2("RTErrFormatDefine/%Rrc", pszDefine, cchDefine);
214
215 memset(pszBuf, '?', cbBuf);
216 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrs", rc);
217 CHECK_TEST_RESULT2("RTErrFormatMsgShort/%Rrs", pszMsg, cchMsg);
218
219 memset(pszBuf, '?', cbBuf);
220 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrf", rc);
221 CHECK_TEST_RESULT2("RTErrFormatMsgFull/%Rrf", pszMsg, cchMsg);
222
223 memset(pszBuf, '?', cbBuf);
224 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rra", rc);
225 CHECK_TEST_RESULT2("RTErrFormatMsgAll/%Rra", pszMsg, cchMsg);
226 }
227 RTTestRestoreAssertions(hTest);
228 }
229
230 RTTestGuardedFree(hTest, pszBuf);
231}
232
233
234#ifdef RT_OS_WINDOWS
235static void tstWinComStatuses(RTTEST hTest)
236{
237 RTTestSub(hTest, "COM/Win status codes");
238
239 char szMsg[640];
240 char szMsgAll[sizeof(szMsg) + 80];
241 size_t const cbBuf = sizeof(szMsg);
242 char * const pszBuf = (char *)RTTestGuardedAllocTail(hTest, cbBuf);
243 RTTESTI_CHECK_RETV(pszBuf);
244
245 static const struct
246 {
247 int32_t rc;
248 const char *pszDefine;
249 } s_aTests[] =
250 {
251 { (int32_t)0x00000000, "ERROR_SUCCESS" },
252 { (int32_t)0x0000000e, "ERROR_OUTOFMEMORY" },
253 { (int32_t)0x8007000e, "E_OUTOFMEMORY" },
254 { (int32_t)0x00000057, "ERROR_INVALID_PARAMETER" },
255 { (int32_t)0x80070057, "E_INVALIDARG" },
256 { (int32_t)0x80004005, "E_FAIL" },
257 { (int32_t)0x00000783, "RPC_S_NOT_ALL_OBJS_EXPORTED" },
258
259 };
260 for (size_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
261 {
262 int32_t const rc = s_aTests[i].rc;
263 const char * const pszDefine = s_aTests[i].pszDefine;
264 size_t const cchDefine = strlen(pszDefine);
265
266 if (RTErrWinIsKnown(rc) != true)
267 RTTestFailed(hTest, "RTErrIsKnown(%s) did not return true", pszDefine);
268
269 RTTestDisableAssertions(hTest);
270 size_t cchMsg = ~(size_t)0;
271 size_t cchMsgAll = ~(size_t)0;
272 size_t cbBuf2 = cbBuf - 1;
273 while (cbBuf2-- > 0)
274 {
275 /* RTErrQueryDefine: */
276 memset(pszBuf, '?', cbBuf);
277 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
278 ssize_t cchRet = RTErrWinQueryDefine(rc, pszBuf2, cbBuf2, false);
279 CHECK_TEST_RESULT("RTErrWinQueryDefine", pszDefine, cchDefine);
280
281 /* Thru the string formatter. */
282 memset(pszBuf, '?', cbBuf);
283 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwc", rc);
284 CHECK_TEST_RESULT2("RTErrWinFormatDefine/%Rwc", pszDefine, cchDefine);
285
286 memset(pszBuf, '?', cbBuf);
287 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhrc", rc);
288 CHECK_TEST_RESULT2("RTErrWinFormatDefine/%Rhrc", pszDefine, cchDefine);
289
290 memset(pszBuf, '?', cbBuf);
291 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwf", rc);
292 if (cchMsg == ~(size_t)0)
293 {
294 cchMsg = (size_t)cchRet;
295 memcpy(szMsg, pszBuf2, cchMsg + 1);
296 }
297 CHECK_TEST_RESULT2("RTErrWinFormatMsg/%Rwf", szMsg, cchMsg);
298
299 memset(pszBuf, '?', cbBuf);
300 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhrf", rc);
301 CHECK_TEST_RESULT2("RTErrWinFormatMsg/%Rhrf", szMsg, cchMsg);
302
303 if (cchMsgAll == ~(size_t)0)
304 cchMsgAll = RTStrPrintf(szMsgAll, sizeof(szMsgAll), "%s (%#x)", pszDefine, rc);
305 memset(pszBuf, '?', cbBuf);
306 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwa", rc);
307 CHECK_TEST_RESULT2("RTErrWinFormatMsgAll/%Rwa", szMsgAll, cchMsgAll);
308
309 memset(pszBuf, '?', cbBuf);
310 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhra", rc);
311 CHECK_TEST_RESULT2("RTErrWinFormatMsgAll/%Rhra", szMsgAll, cchMsgAll);
312 }
313 RTTestRestoreAssertions(hTest);
314 }
315
316 /*
317 * Same but for an unknown status code.
318 */
319 static const int32_t s_arcUnknowns[] = { (int32_t)0xff88ff88, 0x0f88ff88, };
320 for (size_t i = 0; i < RT_ELEMENTS(s_arcUnknowns); i++)
321 {
322 int32_t const rc = s_arcUnknowns[i];
323
324 if (RTErrIsKnown(rc) != false)
325 RTTestFailed(hTest, "RTErrIsKnown(%d) did not return false", rc);
326
327 size_t const cchDefine = RTStrPrintf(szMsg, sizeof(szMsg), "%#x", rc);
328 const char * const pszDefine = szMsg;
329 size_t const cchMsg = RTStrPrintf(szMsgAll, sizeof(szMsgAll), "Unknown Status %#x", rc);
330 const char * const pszMsg = szMsgAll;
331
332 RTTestDisableAssertions(hTest);
333 size_t cbBuf2 = cbBuf - 1;
334 while (cbBuf2-- > 0)
335 {
336 /* RTErrWinQueryDefine: */
337 memset(pszBuf, '?', cbBuf);
338 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
339 ssize_t cchRet = RTErrWinQueryDefine(rc, pszBuf2, cbBuf2, false);
340 CHECK_TEST_RESULT("RTErrWinQueryDefine", pszDefine, cchDefine);
341 RTTEST_CHECK(hTest, RTErrWinQueryDefine(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
342
343 /* Thru the string formatter. */
344 memset(pszBuf, '?', cbBuf);
345 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwc", rc);
346 CHECK_TEST_RESULT2("RTErrWinFormatDefine/%Rwc", pszDefine, cchDefine);
347
348 memset(pszBuf, '?', cbBuf);
349 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhrc", rc);
350 CHECK_TEST_RESULT2("RTErrWinFormatDefine/%Rhrc", pszDefine, cchDefine);
351
352 memset(pszBuf, '?', cbBuf);
353 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwf", rc);
354 CHECK_TEST_RESULT2("RTErrWinFormatMsg/%Rwf", pszDefine, cchDefine);
355
356 memset(pszBuf, '?', cbBuf);
357 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhrf", rc);
358 CHECK_TEST_RESULT2("RTErrWinFormatMsg/%Rhrf", pszDefine, cchDefine);
359
360 memset(pszBuf, '?', cbBuf);
361 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwa", rc);
362 CHECK_TEST_RESULT2("RTErrWinFormatMsgAll/%Rwa", pszMsg, cchMsg);
363
364 memset(pszBuf, '?', cbBuf);
365 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhra", rc);
366 CHECK_TEST_RESULT2("RTErrWinFormatMsgAll/%Rhra", pszMsg, cchMsg);
367 }
368 RTTestRestoreAssertions(hTest);
369 }
370}
371#endif /* RT_OS_WINDOWS */
372
373
374int main(int argc, char **argv)
375{
376 RT_NOREF2(argc, argv);
377 RTTEST hTest;
378 int rc = RTTestInitAndCreate("tstRTErr-1", &hTest);
379 if (rc)
380 return rc;
381 RTTestBanner(hTest);
382
383 tstIprtStatuses(hTest);
384#ifdef RT_OS_WINDOWS
385 tstWinComStatuses(hTest);
386#else
387#endif
388
389 /*
390 * Summary
391 */
392 return RTTestSummaryAndDestroy(hTest);
393}
394
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