1 | /* $Id: tstGuestCtrlParseBuffer.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Tests for VBoxService toolbox output streams.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 |
|
---|
36 | #include "../include/GuestCtrlImplPrivate.h"
|
---|
37 |
|
---|
38 | using namespace com;
|
---|
39 |
|
---|
40 | #include <iprt/env.h>
|
---|
41 | #include <iprt/file.h>
|
---|
42 | #include <iprt/test.h>
|
---|
43 | #include <iprt/rand.h>
|
---|
44 | #include <iprt/stream.h>
|
---|
45 |
|
---|
46 | #ifndef BYTE
|
---|
47 | # define BYTE uint8_t
|
---|
48 | #endif
|
---|
49 |
|
---|
50 |
|
---|
51 | /*********************************************************************************************************************************
|
---|
52 | * Defined Constants And Macros *
|
---|
53 | *********************************************************************************************************************************/
|
---|
54 | /** Defines a test entry string size (in bytes). */
|
---|
55 | #define TST_STR_BYTES(a_sz) (sizeof(a_sz) - 1)
|
---|
56 | /** Defines a test entry string, followed by its size (in bytes). */
|
---|
57 | #define TST_STR_AND_BYTES(a_sz) a_sz, (sizeof(a_sz) - 1)
|
---|
58 | /** Defines the termination sequence for a single key/value pair. */
|
---|
59 | #define TST_STR_VAL_TRM GUESTTOOLBOX_STRM_TERM_PAIR_STR
|
---|
60 | /** Defines the termination sequence for a single stream block. */
|
---|
61 | #define TST_STR_BLK_TRM GUESTTOOLBOX_STRM_TERM_BLOCK_STR
|
---|
62 | /** Defines the termination sequence for the stream. */
|
---|
63 | #define TST_STR_STM_TRM GUESTTOOLBOX_STRM_TERM_STREAM_STR
|
---|
64 |
|
---|
65 |
|
---|
66 | /*********************************************************************************************************************************
|
---|
67 | * Structures and Typedefs *
|
---|
68 | *********************************************************************************************************************************/
|
---|
69 | typedef struct VBOXGUESTCTRL_BUFFER_VALUE
|
---|
70 | {
|
---|
71 | char *pszValue;
|
---|
72 | } VBOXGUESTCTRL_BUFFER_VALUE, *PVBOXGUESTCTRL_BUFFER_VALUE;
|
---|
73 | typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE > GuestBufferMap;
|
---|
74 | typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE >::iterator GuestBufferMapIter;
|
---|
75 | typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE >::const_iterator GuestBufferMapIterConst;
|
---|
76 |
|
---|
77 |
|
---|
78 | /*********************************************************************************************************************************
|
---|
79 | * Global Variables *
|
---|
80 | *********************************************************************************************************************************/
|
---|
81 | char szUnterm1[] = { 'a', 's', 'd', 'f' };
|
---|
82 | char szUnterm2[] = { 'f', 'o', 'o', '3', '=', 'b', 'a', 'r', '3' };
|
---|
83 |
|
---|
84 | PRTLOGGER g_pLog = NULL;
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Tests single block parsing.
|
---|
88 | */
|
---|
89 | static struct
|
---|
90 | {
|
---|
91 | const char *pbData;
|
---|
92 | size_t cbData;
|
---|
93 | uint32_t offStart;
|
---|
94 | uint32_t offAfter;
|
---|
95 | uint32_t cMapElements;
|
---|
96 | int iResult;
|
---|
97 | } g_aTestBlocks[] =
|
---|
98 | {
|
---|
99 | /* Invalid stuff. */
|
---|
100 | { NULL, 0, 0, 0, 0, VERR_INVALID_POINTER },
|
---|
101 | { NULL, 512, 0, 0, 0, VERR_INVALID_POINTER },
|
---|
102 | { "", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
103 | { "", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
104 | { "foo=bar1", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
105 | { "foo=bar2", 0, 50, 50, 0, VERR_INVALID_PARAMETER },
|
---|
106 | /* Has a empty key (not allowed). */
|
---|
107 | { TST_STR_AND_BYTES("=test2" TST_STR_VAL_TRM), 0, TST_STR_BYTES(""), 0, VERR_INVALID_PARAMETER },
|
---|
108 | /* Empty buffers, i.e. nothing to process. */
|
---|
109 | /* Index 6*/
|
---|
110 | { "", 1, 0, 0, 0, VINF_SUCCESS },
|
---|
111 | { TST_STR_VAL_TRM, 1, 0, 0, 0, VINF_SUCCESS },
|
---|
112 | /* Stream termination sequence. */
|
---|
113 | { TST_STR_AND_BYTES(TST_STR_STM_TRM), 0,
|
---|
114 | TST_STR_BYTES (TST_STR_STM_TRM), 0, VINF_EOF },
|
---|
115 | /* Trash after stream termination sequence (skipped / ignored). */
|
---|
116 | { TST_STR_AND_BYTES(TST_STR_STM_TRM "trash"), 0,
|
---|
117 | TST_STR_BYTES (TST_STR_STM_TRM "trash"), 0, VINF_EOF },
|
---|
118 | { TST_STR_AND_BYTES("a=b" TST_STR_STM_TRM), 0,
|
---|
119 | TST_STR_BYTES ("a=b" TST_STR_STM_TRM), 1, VINF_EOF },
|
---|
120 | { TST_STR_AND_BYTES("a=b" TST_STR_VAL_TRM "c=d" TST_STR_STM_TRM), 0,
|
---|
121 | TST_STR_BYTES ("a=b" TST_STR_VAL_TRM "c=d" TST_STR_STM_TRM), 2, VINF_EOF },
|
---|
122 | /* Unterminated values (missing separator, i.e. no valid pair). */
|
---|
123 | { TST_STR_AND_BYTES("test1"), 0, 0, 0, VINF_SUCCESS },
|
---|
124 | /* Has a NULL value (allowed). */
|
---|
125 | { TST_STR_AND_BYTES("test2=" TST_STR_VAL_TRM), 0,
|
---|
126 | TST_STR_BYTES ("test2="), 1, VINF_SUCCESS },
|
---|
127 | /* One completed pair only. */
|
---|
128 | { TST_STR_AND_BYTES("test3=test3" TST_STR_VAL_TRM), 0,
|
---|
129 | TST_STR_BYTES ("test3=test3"), 1, VINF_SUCCESS },
|
---|
130 | /* One completed pair, plus an unfinished pair (separator + terminator missing). */
|
---|
131 | { TST_STR_AND_BYTES("test4=test4" TST_STR_VAL_TRM "t41"), 0,
|
---|
132 | TST_STR_BYTES ("test4=test4" TST_STR_VAL_TRM), 1, VINF_SUCCESS },
|
---|
133 | /* Two completed pairs. */
|
---|
134 | { TST_STR_AND_BYTES("test5=test5" TST_STR_VAL_TRM "t51=t51" TST_STR_VAL_TRM), 0,
|
---|
135 | TST_STR_BYTES ("test5=test5" TST_STR_VAL_TRM "t51=t51"), 2, VINF_SUCCESS },
|
---|
136 | /* One complete block, next block unterminated. */
|
---|
137 | { TST_STR_AND_BYTES("a51=b51" TST_STR_VAL_TRM "c52=d52" TST_STR_BLK_TRM "e53=f53"), 0,
|
---|
138 | TST_STR_BYTES ("a51=b51" TST_STR_VAL_TRM "c52=d52" TST_STR_BLK_TRM), 2, VINF_SUCCESS },
|
---|
139 | /* Ditto. */
|
---|
140 | { TST_STR_AND_BYTES("test6=test6" TST_STR_BLK_TRM "t61=t61"), 0,
|
---|
141 | TST_STR_BYTES ("test6=test6" TST_STR_BLK_TRM), 1, VINF_SUCCESS },
|
---|
142 | /* Two complete pairs with a complete stream. */
|
---|
143 | { TST_STR_AND_BYTES("test61=" TST_STR_VAL_TRM "test611=test612" TST_STR_STM_TRM), 0,
|
---|
144 | TST_STR_BYTES ("test61=" TST_STR_VAL_TRM "test611=test612" TST_STR_STM_TRM), 2, VINF_EOF },
|
---|
145 | /* One complete block. */
|
---|
146 | { TST_STR_AND_BYTES("test7=test7" TST_STR_BLK_TRM), 0,
|
---|
147 | TST_STR_BYTES ("test7=test7"), 1, VINF_SUCCESS },
|
---|
148 | /* Ditto. */
|
---|
149 | { TST_STR_AND_BYTES("test81=test82" TST_STR_VAL_TRM "t81=t82" TST_STR_BLK_TRM), 0,
|
---|
150 | TST_STR_BYTES ("test81=test82" TST_STR_VAL_TRM "t81=t82"), 2, VINF_SUCCESS },
|
---|
151 | /* Good stuff, but with a second block -- should be *not* taken into account since
|
---|
152 | * we're only interested in parsing/handling the first object. */
|
---|
153 | { TST_STR_AND_BYTES("t91=t92" TST_STR_VAL_TRM "t93=t94" TST_STR_BLK_TRM "t95=t96" TST_STR_BLK_TRM), 0,
|
---|
154 | TST_STR_BYTES ("t91=t92" TST_STR_VAL_TRM "t93=t94" TST_STR_BLK_TRM), 2, VINF_SUCCESS },
|
---|
155 | /* Nasty stuff. */
|
---|
156 | /* iso 8859-1 encoding (?) of 'aou' all with diaeresis '=f' and 'ao' with diaeresis. */
|
---|
157 | { TST_STR_AND_BYTES("1\xe4\xf6\xfc=\x66\xe4\xf6" TST_STR_BLK_TRM), 0,
|
---|
158 | TST_STR_BYTES ("1\xe4\xf6\xfc=\x66\xe4\xf6"), 1, VINF_SUCCESS },
|
---|
159 | /* Like above, but after the first '\0' it adds 'ooo=aaa' all letters with diaeresis. */
|
---|
160 | { TST_STR_AND_BYTES("2\xe4\xf6\xfc=\x66\xe4\xf6" TST_STR_VAL_TRM "\xf6\xf6\xf6=\xe4\xe4\xe4"), 0,
|
---|
161 | TST_STR_BYTES ("2\xe4\xf6\xfc=\x66\xe4\xf6" TST_STR_VAL_TRM), 1, VINF_SUCCESS },
|
---|
162 | /* Some "real world" examples from VBoxService toolbox. */
|
---|
163 | { TST_STR_AND_BYTES("hdr_id=vbt_stat" TST_STR_VAL_TRM "hdr_ver=1" TST_STR_VAL_TRM "name=foo.txt" TST_STR_BLK_TRM), 0,
|
---|
164 | TST_STR_BYTES ("hdr_id=vbt_stat" TST_STR_VAL_TRM "hdr_ver=1" TST_STR_VAL_TRM "name=foo.txt"), 3, VINF_SUCCESS }
|
---|
165 | };
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Tests parsing multiple stream blocks.
|
---|
169 | *
|
---|
170 | * Same parsing behavior as for the tests above apply.
|
---|
171 | */
|
---|
172 | static struct
|
---|
173 | {
|
---|
174 | /** Stream data. */
|
---|
175 | const char *pbData;
|
---|
176 | /** Size of stream data (in bytes). */
|
---|
177 | size_t cbData;
|
---|
178 | /** Number of data blocks retrieved. These are separated by "\0\0". */
|
---|
179 | uint32_t cBlocks;
|
---|
180 | /** Overall result when done parsing. */
|
---|
181 | int iResult;
|
---|
182 | } const g_aTestStream[] =
|
---|
183 | {
|
---|
184 | /* No blocks. */
|
---|
185 | { "", sizeof(""), 0, VINF_SUCCESS },
|
---|
186 | /* Empty block (no key/value pairs), will not be accounted. */
|
---|
187 | { TST_STR_STM_TRM,
|
---|
188 | TST_STR_BYTES(TST_STR_STM_TRM), 0, VINF_EOF },
|
---|
189 | /* Good stuff. */
|
---|
190 | { TST_STR_AND_BYTES(TST_STR_VAL_TRM "b1=b2" TST_STR_STM_TRM), 1, VINF_EOF },
|
---|
191 | { TST_STR_AND_BYTES("b3=b31" TST_STR_STM_TRM), 1, VINF_EOF },
|
---|
192 | { TST_STR_AND_BYTES("b4=b41" TST_STR_BLK_TRM "b51=b61" TST_STR_STM_TRM), 2, VINF_EOF },
|
---|
193 | { TST_STR_AND_BYTES("b5=b51" TST_STR_VAL_TRM "b61=b71" TST_STR_STM_TRM), 1, VINF_EOF }
|
---|
194 | };
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Tests payload data to string vector parsing.
|
---|
198 | */
|
---|
199 | static struct
|
---|
200 | {
|
---|
201 | /** Payload data to test. */
|
---|
202 | const char *pbData;
|
---|
203 | /** Size (in bytes) of \b pbData. */
|
---|
204 | size_t cbData;
|
---|
205 | /** Number of extracted strings. */
|
---|
206 | size_t cStrings;
|
---|
207 | /** Expected result (IPRT-style). */
|
---|
208 | int iResult;
|
---|
209 | } g_aTestPayloadToStringVector[] =
|
---|
210 | {
|
---|
211 | /** Empty payload. */
|
---|
212 | { NULL, 0, 0, VINF_SUCCESS },
|
---|
213 | { RT_STR_TUPLE("\0"), 0, VINF_SUCCESS },
|
---|
214 | { RT_STR_TUPLE(""), 0, VINF_SUCCESS },
|
---|
215 | /** Invalid data. */
|
---|
216 | { RT_STR_TUPLE("two\0\0terminators"), 1, VERR_INVALID_PARAMETER },
|
---|
217 | { RT_STR_TUPLE("no\0ending\0terminator"), 2, VERR_BUFFER_OVERFLOW },
|
---|
218 | { RT_STR_TUPLE("foo"), 0, VERR_BUFFER_OVERFLOW },
|
---|
219 | /** Valid data. */
|
---|
220 | { RT_STR_TUPLE("foo\0"), 1, VINF_SUCCESS },
|
---|
221 | { RT_STR_TUPLE("foo\0bar\0"), 2, VINF_SUCCESS },
|
---|
222 | { RT_STR_TUPLE("twoendterminators\0\0"), 1, VINF_SUCCESS },
|
---|
223 | { RT_STR_TUPLE("이것은 테스트입니다\0bar\0"), 2, VINF_SUCCESS }
|
---|
224 | };
|
---|
225 |
|
---|
226 |
|
---|
227 | /**
|
---|
228 | * Reads and parses the stream from a given file.
|
---|
229 | *
|
---|
230 | * @returns RTEXITCODE
|
---|
231 | * @param pszFile Absolute path to file to parse.
|
---|
232 | */
|
---|
233 | static int tstReadFromFile(const char *pszFile)
|
---|
234 | {
|
---|
235 | RTFILE fh;
|
---|
236 | int rc = RTFileOpen(&fh, pszFile, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
237 | AssertRCReturn(rc, RTEXITCODE_FAILURE);
|
---|
238 |
|
---|
239 | uint64_t cbFileSize;
|
---|
240 | rc = RTFileQuerySize(fh, &cbFileSize);
|
---|
241 | AssertRCReturn(rc, RTEXITCODE_FAILURE);
|
---|
242 |
|
---|
243 | GuestToolboxStream stream;
|
---|
244 | GuestToolboxStreamBlock block;
|
---|
245 |
|
---|
246 | size_t cPairs = 0;
|
---|
247 | size_t cBlocks = 0;
|
---|
248 |
|
---|
249 | unsigned aToRead[] = { 256, 23, 13 }; /* Array of certain read amounts to check for in a sequence. */
|
---|
250 | unsigned idxToRead = 0; /* Index within aToRead. */
|
---|
251 |
|
---|
252 | uint64_t cbToRead = cbFileSize;
|
---|
253 |
|
---|
254 | for (unsigned a = 0; a < 32; a++)
|
---|
255 | {
|
---|
256 | uint8_t buf[_64K];
|
---|
257 | do
|
---|
258 | {
|
---|
259 | size_t cbChunk = cbToRead;
|
---|
260 | size_t const cbToReadFromArray = idxToRead < RT_ELEMENTS(aToRead) ? aToRead[idxToRead++] : 0;
|
---|
261 | if (cbToReadFromArray)
|
---|
262 | cbChunk = RT_MIN(cbChunk, cbToReadFromArray);
|
---|
263 | size_t const cbRand = RTRandU64Ex(8, RT_MIN(sizeof(buf), 64));
|
---|
264 | cbChunk = RT_MIN(cbChunk, cbRand);
|
---|
265 | if (cbChunk)
|
---|
266 | {
|
---|
267 | RTTestIPrintf(RTTESTLVL_DEBUG, "Reading %zu bytes (of %zu left) ...\n", cbChunk, cbToRead);
|
---|
268 |
|
---|
269 | size_t cbRead;
|
---|
270 | rc = RTFileRead(fh, &buf, cbChunk, &cbRead);
|
---|
271 | AssertRCBreak(rc);
|
---|
272 |
|
---|
273 | if (!cbRead)
|
---|
274 | continue;
|
---|
275 |
|
---|
276 | cbToRead -= cbRead;
|
---|
277 |
|
---|
278 | rc = stream.AddData((BYTE *)buf, cbRead);
|
---|
279 | AssertRCBreak(rc);
|
---|
280 | }
|
---|
281 |
|
---|
282 | rc = stream.ParseBlock(block);
|
---|
283 | Assert(rc != VERR_INVALID_PARAMETER);
|
---|
284 | RTTestIPrintf(RTTESTLVL_DEBUG, "Parsing ended with %Rrc\n", rc);
|
---|
285 | if (block.IsComplete())
|
---|
286 | {
|
---|
287 | /* Sanity checks; disable this if you parse anything else but fsinfo output from VBoxService toolbox. */
|
---|
288 | //Assert(block.GetString("name") != NULL);
|
---|
289 |
|
---|
290 | cPairs += block.GetCount();
|
---|
291 | cBlocks = stream.GetBlocks();
|
---|
292 | block.Clear();
|
---|
293 | }
|
---|
294 | } while (VINF_SUCCESS == rc /* Might also be VINF_EOF when finished */);
|
---|
295 |
|
---|
296 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Total %zu blocks + %zu pairs\n", cBlocks, cPairs);
|
---|
297 |
|
---|
298 | /* Reset. */
|
---|
299 | RTFileSeek(fh, 0, RTFILE_SEEK_BEGIN, NULL);
|
---|
300 | cbToRead = cbFileSize;
|
---|
301 | cPairs = 0;
|
---|
302 | cBlocks = 0;
|
---|
303 | block.Clear();
|
---|
304 | stream.Destroy();
|
---|
305 | }
|
---|
306 |
|
---|
307 | int rc2 = RTFileClose(fh);
|
---|
308 | if (RT_SUCCESS(rc))
|
---|
309 | rc = rc2;
|
---|
310 |
|
---|
311 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
312 | }
|
---|
313 |
|
---|
314 | int main(int argc, char **argv)
|
---|
315 | {
|
---|
316 | RTTEST hTest;
|
---|
317 | RTEXITCODE rcExit = RTTestInitAndCreate("tstParseBuffer", &hTest);
|
---|
318 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
319 | return rcExit;
|
---|
320 | RTTestBanner(hTest);
|
---|
321 |
|
---|
322 | #ifdef DEBUG
|
---|
323 | RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
|
---|
324 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
325 | fFlags |= RTLOGFLAGS_USECRLF;
|
---|
326 | #endif
|
---|
327 | static const char * const s_apszLogGroups[] = VBOX_LOGGROUP_NAMES;
|
---|
328 | int rc = RTLogCreate(&g_pLog, fFlags, "guest_control.e.l.l2.l3.f", NULL,
|
---|
329 | RT_ELEMENTS(s_apszLogGroups), s_apszLogGroups, RTLOGDEST_STDOUT, NULL /*"vkat-release.log"*/);
|
---|
330 | AssertRCReturn(rc, rc);
|
---|
331 | RTLogSetDefaultInstance(g_pLog);
|
---|
332 | #endif
|
---|
333 |
|
---|
334 | if (argc > 1)
|
---|
335 | return tstReadFromFile(argv[1]);
|
---|
336 |
|
---|
337 | RTTestIPrintf(RTTESTLVL_DEBUG, "Initializing COM...\n");
|
---|
338 | HRESULT hrc = com::Initialize();
|
---|
339 | if (FAILED(hrc))
|
---|
340 | {
|
---|
341 | RTTestFailed(hTest, "Failed to initialize COM (%Rhrc)!\n", hrc);
|
---|
342 | return RTEXITCODE_FAILURE;
|
---|
343 | }
|
---|
344 |
|
---|
345 | AssertCompile(TST_STR_BYTES("1") == 1);
|
---|
346 | AssertCompile(TST_STR_BYTES("sizecheck") == 9);
|
---|
347 | AssertCompile(TST_STR_BYTES("off=rab") == 7);
|
---|
348 | AssertCompile(TST_STR_BYTES("off=rab\0\0") == 9);
|
---|
349 |
|
---|
350 | RTTestSub(hTest, "Blocks");
|
---|
351 |
|
---|
352 | RTTestDisableAssertions(hTest);
|
---|
353 |
|
---|
354 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(g_aTestBlocks); iTest++)
|
---|
355 | {
|
---|
356 | RTTestIPrintf(RTTESTLVL_DEBUG, "=> Block test #%u:\n'%.*Rhxd\n", iTest, g_aTestBlocks[iTest].cbData, g_aTestBlocks[iTest].pbData);
|
---|
357 |
|
---|
358 | GuestToolboxStream stream;
|
---|
359 | int iResult = stream.AddData((BYTE *)g_aTestBlocks[iTest].pbData, g_aTestBlocks[iTest].cbData);
|
---|
360 | if (RT_SUCCESS(iResult))
|
---|
361 | {
|
---|
362 | GuestToolboxStreamBlock curBlock;
|
---|
363 | iResult = stream.ParseBlock(curBlock);
|
---|
364 | if (iResult != g_aTestBlocks[iTest].iResult)
|
---|
365 | RTTestFailed(hTest, "Block #%u: Returned %Rrc, expected %Rrc\n", iTest, iResult, g_aTestBlocks[iTest].iResult);
|
---|
366 | else if (stream.GetOffset() != g_aTestBlocks[iTest].offAfter)
|
---|
367 | RTTestFailed(hTest, "Block #%u: Offset %zu wrong ('%#x'), expected %u ('%#x')\n",
|
---|
368 | iTest, stream.GetOffset(), g_aTestBlocks[iTest].pbData[stream.GetOffset()],
|
---|
369 | g_aTestBlocks[iTest].offAfter, g_aTestBlocks[iTest].pbData[g_aTestBlocks[iTest].offAfter]);
|
---|
370 | else if (iResult == VERR_MORE_DATA)
|
---|
371 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tMore data (Offset: %zu)\n", stream.GetOffset());
|
---|
372 |
|
---|
373 | if (RT_SUCCESS(iResult) || iResult == VERR_MORE_DATA)
|
---|
374 | if (curBlock.GetCount() != g_aTestBlocks[iTest].cMapElements)
|
---|
375 | RTTestFailed(hTest, "Block #%u: Map has %u elements, expected %u\n",
|
---|
376 | iTest, curBlock.GetCount(), g_aTestBlocks[iTest].cMapElements);
|
---|
377 |
|
---|
378 | /* There is remaining data left in the buffer (which needs to be merged
|
---|
379 | * with a following buffer) -- print it. */
|
---|
380 | size_t off = stream.GetOffset();
|
---|
381 | size_t cbToWrite = g_aTestBlocks[iTest].cbData - off;
|
---|
382 | if (cbToWrite)
|
---|
383 | {
|
---|
384 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tRemaining (%u):\n", cbToWrite);
|
---|
385 |
|
---|
386 | /* How to properly get the current RTTESTLVL (aka IPRT_TEST_MAX_LEVEL) here?
|
---|
387 | * Hack alert: Using RTEnvGet for now. */
|
---|
388 | if (!RTStrICmp(RTEnvGet("IPRT_TEST_MAX_LEVEL"), "debug"))
|
---|
389 | RTStrmWriteEx(g_pStdOut, &g_aTestBlocks[iTest].pbData[off], cbToWrite - 1, NULL);
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (RTTestIErrorCount())
|
---|
393 | break;
|
---|
394 | }
|
---|
395 | }
|
---|
396 |
|
---|
397 | RTTestSub(hTest, "Streams");
|
---|
398 |
|
---|
399 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(g_aTestStream); iTest++)
|
---|
400 | {
|
---|
401 | RTTestIPrintf(RTTESTLVL_DEBUG, "=> Stream test #%u\n%.*Rhxd\n",
|
---|
402 | iTest, g_aTestStream[iTest].cbData, g_aTestStream[iTest].pbData);
|
---|
403 |
|
---|
404 | GuestToolboxStream stream;
|
---|
405 | int iResult = stream.AddData((BYTE*)g_aTestStream[iTest].pbData, g_aTestStream[iTest].cbData);
|
---|
406 | if (RT_SUCCESS(iResult))
|
---|
407 | {
|
---|
408 | uint32_t cBlocksComplete = 0;
|
---|
409 | uint8_t cSafety = 0;
|
---|
410 | do
|
---|
411 | {
|
---|
412 | GuestToolboxStreamBlock curBlock;
|
---|
413 | iResult = stream.ParseBlock(curBlock);
|
---|
414 | RTTestIPrintf(RTTESTLVL_DEBUG, "Stream #%u: Returned with %Rrc\n", iTest, iResult);
|
---|
415 | if (cSafety++ > 8)
|
---|
416 | break;
|
---|
417 | if (curBlock.IsComplete())
|
---|
418 | cBlocksComplete++;
|
---|
419 | } while (iResult != VINF_EOF);
|
---|
420 |
|
---|
421 | if (iResult != g_aTestStream[iTest].iResult)
|
---|
422 | RTTestFailed(hTest, "Stream #%u: Returned %Rrc, expected %Rrc\n", iTest, iResult, g_aTestStream[iTest].iResult);
|
---|
423 | else if (cBlocksComplete != g_aTestStream[iTest].cBlocks)
|
---|
424 | RTTestFailed(hTest, "Stream #%u: Returned %u blocks, expected %u\n", iTest, cBlocksComplete, g_aTestStream[iTest].cBlocks);
|
---|
425 | }
|
---|
426 | else
|
---|
427 | RTTestFailed(hTest, "Stream #%u: Adding data failed with %Rrc\n", iTest, iResult);
|
---|
428 |
|
---|
429 | if (RTTestIErrorCount())
|
---|
430 | break;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /*
|
---|
434 | * Payload to string vector testing.
|
---|
435 | */
|
---|
436 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(g_aTestPayloadToStringVector); iTest++)
|
---|
437 | {
|
---|
438 | GuestWaitEventPayload Payload(0 /* Type */,
|
---|
439 | g_aTestPayloadToStringVector[iTest].pbData, (uint32_t)g_aTestPayloadToStringVector[iTest].cbData);
|
---|
440 | std::vector<Utf8Str> vecStr;
|
---|
441 | RTTEST_CHECK_RC(hTest, Payload.ToStringVector(vecStr), g_aTestPayloadToStringVector[iTest].iResult);
|
---|
442 | RTTEST_CHECK(hTest, vecStr.size() == g_aTestPayloadToStringVector[iTest].cStrings);
|
---|
443 | }
|
---|
444 |
|
---|
445 | RTTestRestoreAssertions(hTest);
|
---|
446 |
|
---|
447 | RTTestIPrintf(RTTESTLVL_DEBUG, "Shutting down COM...\n");
|
---|
448 | com::Shutdown();
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Summary.
|
---|
452 | */
|
---|
453 | return RTTestSummaryAndDestroy(hTest);
|
---|
454 | }
|
---|
455 |
|
---|