VirtualBox

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

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

IPRT,++: Apply bldprog-strtab.h and friends to the IPRT status message database (errmsg.cpp) to reduce size. The interface (RTErrMsg*) has been reworked as we no longer have C-strings in the database, but 'compressed' string w/o zero terminators. [build fixes] bugref:9726

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/* $Id: tstRTErr-1.cpp 84057 2020-04-28 16:33:50Z 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 *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) \
128 RTTestFailed(hTest, "%s(%s, , %#x) -> %zd, expected %zd", a_szFunction, pszDefine, cbBuf2, \
129 cchRet, cchLocalExpect); \
130 else if (cbBuf2 > 0 && memcmp(pszBuf2, (a_pszExpect), RT_MIN(cbBuf2 - 1, (a_cchExpect) + 1)) != 0) \
131 RTTestFailed(hTest, "%s(%s, , %#x) -> '%.*s', expected '%s'", a_szFunction, pszDefine, cbBuf2, \
132 cbBuf2, pszBuf2, (a_pszExpect)); \
133 else if (cbBuf2 > 0 && cbBuf2 <= (a_cchExpect) && pszBuf2[cbBuf2 - 1] != '\0') \
134 RTTestFailed(hTest, "%s(%s, , %#x) -> result not terminated", a_szFunction, pszDefine, cbBuf2); \
135 } while (0)
136 memset(pszBuf, '?', cbBuf);
137 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrc", rc);
138 CHECK_TEST_RESULT2("RTErrFormateDefine/%Rrc", pszDefine, cchDefine);
139
140 memset(pszBuf, '?', cbBuf);
141 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrs", rc);
142 CHECK_TEST_RESULT2("RTErrFormateDefine/%Rrs", szMsgShort, cchMsgShort);
143
144 memset(pszBuf, '?', cbBuf);
145 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrf", rc);
146 CHECK_TEST_RESULT2("RTErrFormateDefine/%Rrf", szMsgFull, cchMsgFull);
147
148 if (cchMsgAll == ~(size_t)0)
149 cchMsgAll = RTStrPrintf(szMsgAll, sizeof(szMsgAll), "%s (%d) - %s", pszDefine, rc, szMsgFull);
150 memset(pszBuf, '?', cbBuf);
151 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rra", rc);
152 CHECK_TEST_RESULT2("RTErrFormateDefine/%Rra", szMsgAll, cchMsgAll);
153 }
154 RTTestRestoreAssertions(hTest);
155 }
156
157 /*
158 * Same but for an unknown status code.
159 */
160 static const int s_arcUnknowns[] = { -270, 270, -88888888, 88888888, };
161 for (size_t i = 0; i < RT_ELEMENTS(s_arcUnknowns); i++)
162 {
163 int const rc = s_arcUnknowns[i];
164
165 if (RTErrIsKnown(rc) != false)
166 RTTestFailed(hTest, "RTErrIsKnown(%d) did not return false", rc);
167
168 size_t const cchDefine = RTStrPrintf(szMsgFull, sizeof(szMsgFull), "%d", rc);
169 const char * const pszDefine = szMsgFull;
170 size_t const cchMsg = RTStrPrintf(szMsgShort, sizeof(szMsgShort), "Unknown Status %d (%#x)", rc, rc);
171 const char * const pszMsg = szMsgShort;
172
173 RTTestDisableAssertions(hTest);
174 size_t cbBuf2 = cbBuf - 1;
175 while (cbBuf2-- > 0)
176 {
177 /* RTErrQueryDefine: */
178 memset(pszBuf, '?', cbBuf);
179 char *pszBuf2 = &pszBuf[cbBuf - cbBuf2];
180 ssize_t cchRet = RTErrQueryDefine(rc, pszBuf2, cbBuf2, false);
181 CHECK_TEST_RESULT("RTErrQueryDefine", pszDefine, cchDefine);
182 RTTEST_CHECK(hTest, RTErrQueryDefine(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
183
184
185 /* RTErrQueryMsgShort: */
186 memset(pszBuf, '?', cbBuf);
187 cchRet = RTErrQueryMsgShort(rc, pszBuf2, cbBuf2, false);
188 CHECK_TEST_RESULT("RTErrQueryMsgShort", pszMsg, cchMsg);
189 RTTEST_CHECK(hTest, RTErrQueryMsgShort(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
190
191 /* RTErrQueryMsgFull: */
192 memset(pszBuf, '?', cbBuf);
193 cchRet = RTErrQueryMsgFull(rc, pszBuf2, cbBuf2, false);
194 CHECK_TEST_RESULT("RTErrQueryMsgFull", pszMsg, cchMsg);
195 RTTEST_CHECK(hTest, RTErrQueryMsgFull(rc, pszBuf2, cbBuf2, true) == VERR_NOT_FOUND);
196
197 /* Same thru the string formatter. */
198 memset(pszBuf, '?', cbBuf);
199 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrc", rc);
200 CHECK_TEST_RESULT2("RTErrFormateDefine/%Rrc", pszDefine, cchDefine);
201
202 memset(pszBuf, '?', cbBuf);
203 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrs", rc);
204 CHECK_TEST_RESULT2("RTErrFormateDefine/%Rrs", pszMsg, cchMsg);
205
206 memset(pszBuf, '?', cbBuf);
207 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rrf", rc);
208 CHECK_TEST_RESULT2("RTErrFormateDefine/%Rrf", pszMsg, cchMsg);
209
210 memset(pszBuf, '?', cbBuf);
211 cchRet = RTStrPrintf2(pszBuf2, cbBuf2, "%Rra", rc);
212 CHECK_TEST_RESULT2("RTErrFormateDefine/%Rra", pszMsg, cchMsg);
213 }
214 RTTestRestoreAssertions(hTest);
215 }
216}
217
218
219int main(int argc, char **argv)
220{
221 RT_NOREF2(argc, argv);
222 RTTEST hTest;
223 int rc = RTTestInitAndCreate("tstRTErr-1", &hTest);
224 if (rc)
225 return rc;
226 RTTestBanner(hTest);
227
228 tstIprtStatuses(hTest);
229 //tstComStatuses(hTest);
230
231 /*
232 * Summary
233 */
234 return RTTestSummaryAndDestroy(hTest);
235}
236
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