VirtualBox

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

Last change on this file since 85546 was 84069, checked in by vboxsync, 5 years ago

IPRT,++: Compress the windows status code info (errmsgwin.cpp) to save space and reduce load time a tiny bit. [test + fix] bugref:9726

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 16.4 KB
Line 
1/* $Id: tstRTErr-1.cpp 84069 2020-04-28 23:35:00Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Error Messages.
4 */
5
6/*
7 * Copyright (C) 2020 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/errcore.h>
32
33#include <iprt/test.h>
34#include <iprt/string.h>
35#include <iprt/err.h>
36
37
38
39static void tstIprtStatuses(RTTEST hTest)
40{
41 RTTestSub(hTest, "IPRT status codes");
42
43 char szMsgShort[640];
44 char szMsgFull[sizeof(szMsgShort)];
45 char szMsgAll[sizeof(szMsgShort) + 80];
46 size_t const cbBuf = sizeof(szMsgShort);
47 char * const pszBuf = (char *)RTTestGuardedAllocTail(hTest, cbBuf);
48 RTTESTI_CHECK_RETV(pszBuf);
49
50 static const struct
51 {
52 int rc;
53 const char *pszDefine;
54 } s_aTests[] =
55 {
56 { VINF_SUCCESS, "VINF_SUCCESS" },
57 { VERR_INVALID_PARAMETER, "VERR_INVALID_PARAMETER" },
58 { VERR_NOT_IMPLEMENTED, "VERR_NOT_IMPLEMENTED" },
59 { VERR_NUMBER_TOO_BIG, "VERR_NUMBER_TOO_BIG" },
60 { VWRN_NUMBER_TOO_BIG, "VWRN_NUMBER_TOO_BIG" },
61 { VERR_CANCELLED, "VERR_CANCELLED" },
62 { VERR_ISOMK_IMPORT_BOOT_CAT_DEF_ENTRY_INVALID_BOOT_IND, "VERR_ISOMK_IMPORT_BOOT_CAT_DEF_ENTRY_INVALID_BOOT_IND" },
63 { VERR_CR_CIPHER_INVALID_INITIALIZATION_VECTOR_LENGTH, "VERR_CR_CIPHER_INVALID_INITIALIZATION_VECTOR_LENGTH" },
64 };
65 for (size_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
66 {
67 int const rc = s_aTests[i].rc;
68 const char * const pszDefine = s_aTests[i].pszDefine;
69 size_t const cchDefine = strlen(pszDefine);
70
71 if (RTErrIsKnown(rc) != true)
72 RTTestFailed(hTest, "RTErrIsKnown(%s) did not return true", pszDefine);
73
74 RTTestDisableAssertions(hTest);
75 size_t cchMsgShort = ~(size_t)0;
76 size_t cchMsgFull = ~(size_t)0;
77 size_t cchMsgAll = ~(size_t)0;
78 size_t cbBuf2 = cbBuf - 1;
79 while (cbBuf2-- > 0)
80 {
81#define CHECK_TEST_RESULT(a_szFunction, a_pszExpect, a_cchExpect) do { \
82 if (cbBuf2 > (a_cchExpect) && cchRet != (ssize_t)(a_cchExpect)) \
83 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %zd", a_szFunction, pszDefine, cbBuf2, \
84 cchRet, (a_cchExpect)); \
85 else if (cbBuf2 <= (a_cchExpect) && cchRet != VERR_BUFFER_OVERFLOW) \
86 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %d", a_szFunction, pszDefine, cbBuf2, \
87 cchRet, VERR_BUFFER_OVERFLOW); \
88 else if (cbBuf2 > (a_cchExpect) && memcmp(pszBuf2, (a_pszExpect), (a_cchExpect) + 1) != 0) \
89 RTTestFailed(hTest, "%s(%s, , %#x) -> '%.*s', expected '%s'", a_szFunction, pszDefine, cbBuf2, \
90 cbBuf2, pszBuf2, (a_pszExpect)); \
91 /* Only check that it's terminated. Compression may exit before it's quite full. */ \
92 else if (cbBuf2 > 0 && RTStrNLen(pszBuf2, cbBuf2) >= cbBuf2) \
93 RTTestFailed(hTest, "%s(%s, , %#x) -> result not terminated", a_szFunction, pszDefine, cbBuf2); \
94 } while (0)
95
96 /* RTErrQueryDefine: */
97 memset(pszBuf, '?', cbBuf);
98 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
99 ssize_t cchRet = RTErrQueryDefine(rc, pszBuf2, cbBuf2, false);
100 CHECK_TEST_RESULT("RTErrQueryDefine", pszDefine, cchDefine);
101
102 /* RTErrQueryMsgShort: */
103 memset(pszBuf, '?', cbBuf);
104 cchRet = RTErrQueryMsgShort(rc, pszBuf2, cbBuf2, false);
105 if (cchMsgShort == ~(size_t)0)
106 {
107 cchMsgShort = (size_t)cchRet;
108 memcpy(szMsgShort, pszBuf2, cchMsgShort);
109 szMsgShort[cchMsgShort] = '\0';
110 }
111 CHECK_TEST_RESULT("RTErrQueryMsgShort", szMsgShort, cchMsgShort);
112
113 /* RTErrQueryMsgFull: */
114 memset(pszBuf, '?', cbBuf);
115 cchRet = RTErrQueryMsgFull(rc, pszBuf2, cbBuf2, false);
116 if (cchMsgFull == ~(size_t)0)
117 {
118 cchMsgFull = (size_t)cchRet;
119 memcpy(szMsgFull, pszBuf2, cchMsgFull);
120 szMsgFull[cchMsgFull] = '\0';
121 }
122 CHECK_TEST_RESULT("RTErrQueryMsgFull", szMsgFull, cchMsgFull);
123
124 /* Same thru the string formatter. */
125#define CHECK_TEST_RESULT2(a_szFunction, a_pszExpect, a_cchExpect) do { \
126 ssize_t const cchLocalExpect = cbBuf2 > (a_cchExpect) ? (ssize_t)(a_cchExpect) : -(ssize_t)(a_cchExpect) - 1; \
127 if (cchRet != cchLocalExpect && cchRet > 0) \
128 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %zd ('%s' vs '%s')", a_szFunction, pszDefine, cbBuf2, \
129 cchRet, cchLocalExpect, pszBuf2, (a_pszExpect)); \
130 else if (cchRet != cchLocalExpect && cchRet <= 0) \
131 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %zd", a_szFunction, pszDefine, cbBuf2, \
132 cchRet, cchLocalExpect); \
133 else if (cbBuf2 > 0 && memcmp(pszBuf2, (a_pszExpect), RT_MIN(cbBuf2 - 1, (a_cchExpect) + 1)) != 0) \
134 RTTestFailed(hTest, "%s(%s, , %#x) -> '%.*s', expected '%s'", a_szFunction, pszDefine, cbBuf2, \
135 cbBuf2, pszBuf2, (a_pszExpect)); \
136 else if (cbBuf2 > 0 && cbBuf2 <= (a_cchExpect) && pszBuf2[cbBuf2 - 1] != '\0') \
137 RTTestFailed(hTest, "%s(%s, , %#x) -> result not terminated", a_szFunction, pszDefine, cbBuf2); \
138 } while (0)
139 memset(pszBuf, '?', cbBuf);
140 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrc", rc);
141 CHECK_TEST_RESULT2("RTErrFormatDefine/%Rrc", pszDefine, cchDefine);
142
143 memset(pszBuf, '?', cbBuf);
144 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrs", rc);
145 CHECK_TEST_RESULT2("RTErrFormatMsgShort/%Rrs", szMsgShort, cchMsgShort);
146
147 memset(pszBuf, '?', cbBuf);
148 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrf", rc);
149 CHECK_TEST_RESULT2("RTErrFormatMsgFull/%Rrf", szMsgFull, cchMsgFull);
150
151 if (cchMsgAll == ~(size_t)0)
152 cchMsgAll = RTStrPrintf(szMsgAll, sizeof(szMsgAll), "%s (%d) - %s", pszDefine, rc, szMsgFull);
153 memset(pszBuf, '?', cbBuf);
154 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rra", rc);
155 CHECK_TEST_RESULT2("RTErrFormatMsgAll/%Rra", szMsgAll, cchMsgAll);
156 }
157 RTTestRestoreAssertions(hTest);
158 }
159
160 /*
161 * Same but for an unknown status code.
162 */
163 static const int s_arcUnknowns[] = { -270, 270, -88888888, 88888888, };
164 for (size_t i = 0; i < RT_ELEMENTS(s_arcUnknowns); i++)
165 {
166 int const rc = s_arcUnknowns[i];
167
168 if (RTErrIsKnown(rc) != false)
169 RTTestFailed(hTest, "RTErrIsKnown(%d) did not return false", rc);
170
171 size_t const cchDefine = RTStrPrintf(szMsgFull, sizeof(szMsgFull), "%d", rc);
172 const char * const pszDefine = szMsgFull;
173 size_t const cchMsg = RTStrPrintf(szMsgShort, sizeof(szMsgShort), "Unknown Status %d (%#x)", rc, rc);
174 const char * const pszMsg = szMsgShort;
175
176 RTTestDisableAssertions(hTest);
177 size_t cbBuf2 = cbBuf - 1;
178 while (cbBuf2-- > 0)
179 {
180 /* RTErrQueryDefine: */
181 memset(pszBuf, '?', cbBuf);
182 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
183 ssize_t cchRet = RTErrQueryDefine(rc, pszBuf2, cbBuf2, false);
184 CHECK_TEST_RESULT("RTErrQueryDefine", pszDefine, cchDefine);
185 RTTEST_CHECK(hTest, RTErrQueryDefine(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
186
187
188 /* RTErrQueryMsgShort: */
189 memset(pszBuf, '?', cbBuf);
190 cchRet = RTErrQueryMsgShort(rc, pszBuf2, cbBuf2, false);
191 CHECK_TEST_RESULT("RTErrQueryMsgShort", pszMsg, cchMsg);
192 RTTEST_CHECK(hTest, RTErrQueryMsgShort(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
193
194 /* RTErrQueryMsgFull: */
195 memset(pszBuf, '?', cbBuf);
196 cchRet = RTErrQueryMsgFull(rc, pszBuf2, cbBuf2, false);
197 CHECK_TEST_RESULT("RTErrQueryMsgFull", pszMsg, cchMsg);
198 RTTEST_CHECK(hTest, RTErrQueryMsgFull(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
199
200 /* Same thru the string formatter. */
201 memset(pszBuf, '?', cbBuf);
202 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrc", rc);
203 CHECK_TEST_RESULT2("RTErrFormatDefine/%Rrc", pszDefine, cchDefine);
204
205 memset(pszBuf, '?', cbBuf);
206 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrs", rc);
207 CHECK_TEST_RESULT2("RTErrFormatMsgShort/%Rrs", pszMsg, cchMsg);
208
209 memset(pszBuf, '?', cbBuf);
210 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrf", rc);
211 CHECK_TEST_RESULT2("RTErrFormatMsgFull/%Rrf", pszMsg, cchMsg);
212
213 memset(pszBuf, '?', cbBuf);
214 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rra", rc);
215 CHECK_TEST_RESULT2("RTErrFormatMsgAll/%Rra", pszMsg, cchMsg);
216 }
217 RTTestRestoreAssertions(hTest);
218 }
219
220 RTTestGuardedFree(hTest, pszBuf);
221}
222
223
224#ifdef RT_OS_WINDOWS
225static void tstWinComStatuses(RTTEST hTest)
226{
227 RTTestSub(hTest, "COM/Win status codes");
228
229 char szMsg[640];
230 char szMsgAll[sizeof(szMsg) + 80];
231 size_t const cbBuf = sizeof(szMsg);
232 char * const pszBuf = (char *)RTTestGuardedAllocTail(hTest, cbBuf);
233 RTTESTI_CHECK_RETV(pszBuf);
234
235 static const struct
236 {
237 int32_t rc;
238 const char *pszDefine;
239 } s_aTests[] =
240 {
241 { (int32_t)0x00000000, "ERROR_SUCCESS" },
242 { (int32_t)0x0000000e, "ERROR_OUTOFMEMORY" },
243 { (int32_t)0x8007000e, "E_OUTOFMEMORY" },
244 { (int32_t)0x00000057, "ERROR_INVALID_PARAMETER" },
245 { (int32_t)0x80070057, "E_INVALIDARG" },
246 { (int32_t)0x80004005, "E_FAIL" },
247 { (int32_t)0x00000783, "RPC_S_NOT_ALL_OBJS_EXPORTED" },
248
249 };
250 for (size_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
251 {
252 int32_t const rc = s_aTests[i].rc;
253 const char * const pszDefine = s_aTests[i].pszDefine;
254 size_t const cchDefine = strlen(pszDefine);
255
256 if (RTErrWinIsKnown(rc) != true)
257 RTTestFailed(hTest, "RTErrIsKnown(%s) did not return true", pszDefine);
258
259 RTTestDisableAssertions(hTest);
260 size_t cchMsg = ~(size_t)0;
261 size_t cchMsgAll = ~(size_t)0;
262 size_t cbBuf2 = cbBuf - 1;
263 while (cbBuf2-- > 0)
264 {
265 /* RTErrQueryDefine: */
266 memset(pszBuf, '?', cbBuf);
267 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
268 ssize_t cchRet = RTErrWinQueryDefine(rc, pszBuf2, cbBuf2, false);
269 CHECK_TEST_RESULT("RTErrWinQueryDefine", pszDefine, cchDefine);
270
271 /* Thru the string formatter. */
272 memset(pszBuf, '?', cbBuf);
273 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwc", rc);
274 CHECK_TEST_RESULT2("RTErrWinFormatDefine/%Rwc", pszDefine, cchDefine);
275
276 memset(pszBuf, '?', cbBuf);
277 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhrc", rc);
278 CHECK_TEST_RESULT2("RTErrWinFormatDefine/%Rhrc", pszDefine, cchDefine);
279
280 memset(pszBuf, '?', cbBuf);
281 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwf", rc);
282 if (cchMsg == ~(size_t)0)
283 {
284 cchMsg = (size_t)cchRet;
285 memcpy(szMsg, pszBuf2, cchMsg + 1);
286 }
287 CHECK_TEST_RESULT2("RTErrWinFormatMsg/%Rwf", szMsg, cchMsg);
288
289 memset(pszBuf, '?', cbBuf);
290 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhrf", rc);
291 CHECK_TEST_RESULT2("RTErrWinFormatMsg/%Rhrf", szMsg, cchMsg);
292
293 if (cchMsgAll == ~(size_t)0)
294 cchMsgAll = RTStrPrintf(szMsgAll, sizeof(szMsgAll), "%s (%#x) - %s", pszDefine, rc, szMsg);
295 memset(pszBuf, '?', cbBuf);
296 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwa", rc);
297 CHECK_TEST_RESULT2("RTErrWinFormatMsgAll/%Rwa", szMsgAll, cchMsgAll);
298
299 memset(pszBuf, '?', cbBuf);
300 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhra", rc);
301 CHECK_TEST_RESULT2("RTErrWinFormatMsgAll/%Rhra", szMsgAll, cchMsgAll);
302 }
303 RTTestRestoreAssertions(hTest);
304 }
305
306 /*
307 * Same but for an unknown status code.
308 */
309 static const int32_t s_arcUnknowns[] = { (int32_t)0xff88ff88, 0x0f88ff88, };
310 for (size_t i = 0; i < RT_ELEMENTS(s_arcUnknowns); i++)
311 {
312 int32_t const rc = s_arcUnknowns[i];
313
314 if (RTErrIsKnown(rc) != false)
315 RTTestFailed(hTest, "RTErrIsKnown(%d) did not return false", rc);
316
317 size_t const cchDefine = RTStrPrintf(szMsg, sizeof(szMsg), "%#x", rc);
318 const char * const pszDefine = szMsg;
319 size_t const cchMsg = RTStrPrintf(szMsgAll, sizeof(szMsgAll), "Unknown Status %#x", rc);
320 const char * const pszMsg = szMsgAll;
321
322 RTTestDisableAssertions(hTest);
323 size_t cbBuf2 = cbBuf - 1;
324 while (cbBuf2-- > 0)
325 {
326 /* RTErrWinQueryDefine: */
327 memset(pszBuf, '?', cbBuf);
328 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
329 ssize_t cchRet = RTErrWinQueryDefine(rc, pszBuf2, cbBuf2, false);
330 CHECK_TEST_RESULT("RTErrWinQueryDefine", pszDefine, cchDefine);
331 RTTEST_CHECK(hTest, RTErrWinQueryDefine(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
332
333 /* Thru the string formatter. */
334 memset(pszBuf, '?', cbBuf);
335 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwc", rc);
336 CHECK_TEST_RESULT2("RTErrWinFormatDefine/%Rwc", pszDefine, cchDefine);
337
338 memset(pszBuf, '?', cbBuf);
339 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhrc", rc);
340 CHECK_TEST_RESULT2("RTErrWinFormatDefine/%Rhrc", pszDefine, cchDefine);
341
342 memset(pszBuf, '?', cbBuf);
343 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwf", rc);
344 CHECK_TEST_RESULT2("RTErrWinFormatMsg/%Rwf", pszMsg, cchMsg);
345
346 memset(pszBuf, '?', cbBuf);
347 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhrf", rc);
348 CHECK_TEST_RESULT2("RTErrWinFormatMsg/%Rhrf", pszMsg, cchMsg);
349
350 memset(pszBuf, '?', cbBuf);
351 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rwa", rc);
352 CHECK_TEST_RESULT2("RTErrWinFormatMsgAll/%Rwa", pszMsg, cchMsg);
353
354 memset(pszBuf, '?', cbBuf);
355 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rhra", rc);
356 CHECK_TEST_RESULT2("RTErrWinFormatMsgAll/%Rhra", pszMsg, cchMsg);
357 }
358 RTTestRestoreAssertions(hTest);
359 }
360}
361#endif /* RT_OS_WINDOWS */
362
363
364int main(int argc, char **argv)
365{
366 RT_NOREF2(argc, argv);
367 RTTEST hTest;
368 int rc = RTTestInitAndCreate("tstRTErr-1", &hTest);
369 if (rc)
370 return rc;
371 RTTestBanner(hTest);
372
373 tstIprtStatuses(hTest);
374#ifdef RT_OS_WINDOWS
375 tstWinComStatuses(hTest);
376#else
377#endif
378
379 /*
380 * Summary
381 */
382 return RTTestSummaryAndDestroy(hTest);
383}
384
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