VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTExprEval.cpp@ 106579

Last change on this file since 106579 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/* $Id: tstRTExprEval.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTExprEval
4 */
5
6/*
7 * Copyright (C) 2022-2024 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/expreval.h>
42
43#include <iprt/errcore.h>
44#include <iprt/string.h>
45#include <iprt/test.h>
46
47
48/*********************************************************************************************************************************
49* Global Variables *
50*********************************************************************************************************************************/
51static RTTEST g_hTest;
52static bool g_fQueryVariableExpected = false;
53
54
55
56/** @callbackmethodimpl{PFNRTEXPREVALQUERYVARIABLE} */
57static DECLCALLBACK(int) tstBasicQueryVariable(const char *pchName, size_t cchName, void *pvUser, char **ppszValue)
58{
59 RTTESTI_CHECK(pvUser == (void *)g_hTest);
60 RTTESTI_CHECK_RET(g_fQueryVariableExpected, VERR_WRONG_ORDER);
61
62#define MATCH_VAR(a_szVariable) \
63 (cchName == sizeof(a_szVariable) - 1 && memcmp(pchName, a_szVariable, sizeof(a_szVariable) - 1) == 0)
64 const char *pszValue;
65 if (MATCH_VAR("MYVAR1"))
66 pszValue = "42";
67 else if (MATCH_VAR("MYVAR2"))
68 pszValue = "string";
69 else if (MATCH_VAR("MYNESTED1"))
70 pszValue = "MYVAR1";
71 else if (MATCH_VAR("FOX_AND_DOG"))
72 pszValue = "The quick brown fox jumps over the lazy dog";
73 else
74 return VERR_NOT_FOUND;
75
76 if (ppszValue)
77 return RTStrDupEx(ppszValue, pszValue);
78 return VINF_SUCCESS;
79}
80
81
82static void tstBasic(void)
83{
84 RTTestISub("Basics");
85
86 RTEXPREVAL hExprEval;
87 RTTESTI_CHECK_RC_RETV(RTExprEvalCreate(&hExprEval, 0, "basics", NULL, NULL), VINF_SUCCESS);
88 RTTESTI_CHECK_RETV(hExprEval != NIL_RTEXPREVAL);
89 RTTESTI_CHECK_RETV(RTExprEvalRelease(hExprEval) == 0);
90
91 RTTESTI_CHECK_RC_RETV(RTExprEvalCreate(&hExprEval, 0, "basics", g_hTest, tstBasicQueryVariable), VINF_SUCCESS);
92 RTTESTI_CHECK_RETV(hExprEval != NIL_RTEXPREVAL);
93
94 bool fResult;
95#define CHECK_fResult(a_fExpect) do { \
96 if (fResult != (a_fExpect)) RTTestIFailed("line %u: fResult=%RTbool, expected %RTbool", __LINE__, fResult, (a_fExpect)); \
97 } while (0)
98 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("1"), &fResult, NULL), VINF_SUCCESS);
99 CHECK_fResult(true);
100 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("0"), &fResult, NULL), VINF_SUCCESS);
101 CHECK_fResult(false);
102
103 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("true"), &fResult, NULL), VINF_SUCCESS);
104 CHECK_fResult(true);
105 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("false"), &fResult, NULL), VINF_SUCCESS);
106 CHECK_fResult(false);
107
108 g_fQueryVariableExpected = true;
109 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("defined(MYVAR1)"), &fResult, NULL), VINF_SUCCESS);
110 CHECK_fResult(true);
111 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("defined(NO_SUCH_VARIABLE)"), &fResult, NULL), VINF_SUCCESS);
112 CHECK_fResult(false);
113 g_fQueryVariableExpected = false;
114
115 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("1.0.1 vle 2.0"), &fResult, NULL), VINF_SUCCESS);
116 CHECK_fResult(true);
117 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("1.0.1 vle 1.0"), &fResult, NULL), VINF_SUCCESS);
118 CHECK_fResult(false);
119 RTTESTI_CHECK_RC(RTExprEvalToBool(hExprEval, RT_STR_TUPLE("1.0.1 vle 1.0.1"), &fResult, NULL), VINF_SUCCESS);
120 CHECK_fResult(true);
121
122 int64_t iResult;
123#define CHECK_iResult(a_iExpect) do { \
124 if (iResult != (a_iExpect)) RTTestIFailed("line %u: iResult=%#RX64, expected %#RX64", __LINE__, iResult, (a_iExpect)); \
125 } while (0)
126 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("1"), &iResult, NULL), VINF_SUCCESS);
127 CHECK_iResult(1);
128 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("0"), &iResult, NULL), VINF_SUCCESS);
129 CHECK_iResult(0);
130 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("123459876"), &iResult, NULL), VINF_SUCCESS);
131 CHECK_iResult(123459876);
132 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("123459876"), &iResult, NULL), VINF_SUCCESS);
133 CHECK_iResult(123459876);
134 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("-123459876"), &iResult, NULL), VINF_SUCCESS);
135 CHECK_iResult(-123459876);
136 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("0x7fffffffffffffff"), &iResult, NULL), VINF_SUCCESS);
137 CHECK_iResult(INT64_MAX);
138 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("-9223372036854775808"), &iResult, NULL), VINF_SUCCESS);
139 CHECK_iResult(INT64_MIN);
140
141 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("true"), &iResult, NULL), VINF_SUCCESS);
142 CHECK_iResult(1);
143 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false"), &iResult, NULL), VINF_SUCCESS);
144 CHECK_iResult(0);
145 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false + 2"), &iResult, NULL), VINF_SUCCESS);
146 CHECK_iResult(2);
147 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false - true"), &iResult, NULL), VINF_SUCCESS);
148 CHECK_iResult(-1);
149 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false - ((true))"), &iResult, NULL), VINF_SUCCESS);
150 CHECK_iResult(-1);
151 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("false true - "), &iResult, NULL), VERR_PARSE_ERROR);
152 CHECK_iResult(INT64_MAX);
153 g_fQueryVariableExpected = true;
154 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("${MYVAR1} + 0"), &iResult, NULL), VINF_SUCCESS);
155 CHECK_iResult(42);
156 RTTESTI_CHECK_RC(RTExprEvalToInteger(hExprEval, RT_STR_TUPLE("${${MYNESTED1}} + 2"), &iResult, NULL), VINF_SUCCESS);
157 CHECK_iResult(44);
158 g_fQueryVariableExpected = false;
159
160 char *pszResult;
161#define CHECK_FREE_pszResult(a_pszExpect) do { \
162 const char *pszMacroExpect = (a_pszExpect); \
163 if ( (pszResult != NULL || pszMacroExpect != NULL) \
164 && RTStrCmp(pszResult, pszMacroExpect) != 0 ) \
165 RTTestIFailed("line %u: pszResult=%s, expected %s", __LINE__, pszResult, pszMacroExpect); \
166 RTStrFree(pszResult); \
167 } while (0)
168 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("true"), &pszResult, NULL), VINF_SUCCESS);
169 CHECK_FREE_pszResult("true");
170 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("false"), &pszResult, NULL), VINF_SUCCESS);
171 CHECK_FREE_pszResult("false");
172 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("1+2"), &pszResult, NULL), VINF_SUCCESS);
173 CHECK_FREE_pszResult("3");
174
175 /* hash functions: */
176 g_fQueryVariableExpected = true;
177 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("sha1 \"${FOX_AND_DOG}\""), &pszResult, NULL), VINF_SUCCESS);
178 g_fQueryVariableExpected = false;
179 CHECK_FREE_pszResult("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
180
181 g_fQueryVariableExpected = true;
182 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("sha1(\"${FOX_AND_DOG}\")"), &pszResult, NULL), VINF_SUCCESS);
183 g_fQueryVariableExpected = false;
184 CHECK_FREE_pszResult("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
185
186 g_fQueryVariableExpected = true;
187 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("sha1(${FOX_AND_DOG})"), &pszResult, NULL), VINF_SUCCESS);
188 g_fQueryVariableExpected = false;
189 CHECK_FREE_pszResult("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12");
190
191#if 0 /** @todo not happy with 'strcat' as an operator. Dot doesn't work, so, figure something else out... */
192 g_fQueryVariableExpected = true;
193 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("sha1(${FOX_AND_DOG}) strcat sha1('')"), &pszResult, NULL), VINF_SUCCESS);
194 g_fQueryVariableExpected = false;
195 CHECK_FREE_pszResult("2fd4e1c67a2d28fced849ee1bb76e7391b93eb12da39a3ee5e6b4b0d3255bfef95601890afd80709");
196#endif
197
198 g_fQueryVariableExpected = true;
199 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("sha256('')"), &pszResult, NULL), VINF_SUCCESS);
200 g_fQueryVariableExpected = false;
201 CHECK_FREE_pszResult("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
202
203 g_fQueryVariableExpected = true;
204 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("sha512('')"), &pszResult, NULL), VINF_SUCCESS);
205 g_fQueryVariableExpected = false;
206 CHECK_FREE_pszResult("cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e");
207
208 g_fQueryVariableExpected = true;
209 RTTESTI_CHECK_RC(RTExprEvalToString(hExprEval, RT_STR_TUPLE("md5(${FOX_AND_DOG})"), &pszResult, NULL), VINF_SUCCESS);
210 g_fQueryVariableExpected = false;
211 CHECK_FREE_pszResult("9e107d9d372bb6826bd81d3542a419d6");
212
213 RTTESTI_CHECK_RETV(RTExprEvalRelease(hExprEval) == 0);
214}
215
216
217
218int main()
219{
220 RTEXITCODE rcExit = RTTestInitAndCreate("tstRTExprEval", &g_hTest);
221 if (rcExit != RTEXITCODE_SUCCESS)
222 return rcExit;
223
224 tstBasic();
225
226 /*
227 * Summary.
228 */
229 return RTTestSummaryAndDestroy(g_hTest);
230}
231
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