1 | /* $Id: tstGuestCtrlParseBuffer.cpp 63161 2016-08-08 12:51:16Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Output stream parsing test cases.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2016 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 |
|
---|
18 | #define LOG_ENABLED
|
---|
19 | #define LOG_GROUP LOG_GROUP_MAIN
|
---|
20 | #include <VBox/log.h>
|
---|
21 |
|
---|
22 | #include "../include/GuestCtrlImplPrivate.h"
|
---|
23 |
|
---|
24 | using namespace com;
|
---|
25 |
|
---|
26 | #include <iprt/env.h>
|
---|
27 | #include <iprt/test.h>
|
---|
28 | #include <iprt/stream.h>
|
---|
29 |
|
---|
30 | #ifndef BYTE
|
---|
31 | # define BYTE uint8_t
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | typedef struct VBOXGUESTCTRL_BUFFER_VALUE
|
---|
35 | {
|
---|
36 | char *pszValue;
|
---|
37 | } VBOXGUESTCTRL_BUFFER_VALUE, *PVBOXGUESTCTRL_BUFFER_VALUE;
|
---|
38 | typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE > GuestBufferMap;
|
---|
39 | typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE >::iterator GuestBufferMapIter;
|
---|
40 | typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE >::const_iterator GuestBufferMapIterConst;
|
---|
41 |
|
---|
42 | char szUnterm1[] = { 'a', 's', 'd', 'f' };
|
---|
43 | char szUnterm2[] = { 'f', 'o', 'o', '3', '=', 'b', 'a', 'r', '3' };
|
---|
44 |
|
---|
45 | static struct
|
---|
46 | {
|
---|
47 | const char *pbData;
|
---|
48 | size_t cbData;
|
---|
49 | uint32_t uOffsetStart;
|
---|
50 | uint32_t uOffsetAfter;
|
---|
51 | uint32_t uMapElements;
|
---|
52 | int iResult;
|
---|
53 | } aTestBlock[] =
|
---|
54 | {
|
---|
55 | /*
|
---|
56 | * Single object parsing.
|
---|
57 | * An object is represented by one or multiple key=value pairs which are
|
---|
58 | * separated by a single "\0". If this termination is missing it will be assumed
|
---|
59 | * that we need to collect more data to do a successful parsing.
|
---|
60 | */
|
---|
61 | /* Invalid stuff. */
|
---|
62 | { NULL, 0, 0, 0, 0, VERR_INVALID_POINTER },
|
---|
63 | { NULL, 512, 0, 0, 0, VERR_INVALID_POINTER },
|
---|
64 | { "", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
65 | { "", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
66 | { "foo=bar1", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
67 | { "foo=bar2", 0, 50, 50, 0, VERR_INVALID_PARAMETER },
|
---|
68 | /* Empty buffers. */
|
---|
69 | { "", 1, 0, 1, 0, VINF_SUCCESS },
|
---|
70 | { "\0", 1, 0, 1, 0, VINF_SUCCESS },
|
---|
71 | /* Unterminated values (missing "\0"). */
|
---|
72 | { "test1", sizeof("test1"), 0, 0, 0, VERR_MORE_DATA },
|
---|
73 | { "test2=", sizeof("test2="), 0, 0, 0, VERR_MORE_DATA },
|
---|
74 | { "test3=test3", sizeof("test3=test3"), 0, 0, 0, VERR_MORE_DATA },
|
---|
75 | { "test4=test4\0t41", sizeof("test4=test4\0t41"), 0, sizeof("test4=test4\0") - 1, 1, VERR_MORE_DATA },
|
---|
76 | { "test5=test5\0t51=t51", sizeof("test5=test5\0t51=t51"), 0, sizeof("test5=test5\0") - 1, 1, VERR_MORE_DATA },
|
---|
77 | /* Next block unterminated. */
|
---|
78 | { "t51=t51\0t52=t52\0\0t53=t53", sizeof("t51=t51\0t52=t52\0\0t53=t53"), 0, sizeof("t51=t51\0t52=t52\0") - 1, 2, VINF_SUCCESS },
|
---|
79 | { "test6=test6\0\0t61=t61", sizeof("test6=test6\0\0t61=t61"), 0, sizeof("test6=test6\0") - 1, 1, VINF_SUCCESS },
|
---|
80 | /* Good stuff. */
|
---|
81 | { "test61=\0test611=test611\0", sizeof("test61=\0test611=test611\0"), 0, sizeof("test61=\0test611=test611\0") - 1, 2, VINF_SUCCESS },
|
---|
82 | { "test7=test7\0\0", sizeof("test7=test7\0\0"), 0, sizeof("test7=test7\0") - 1, 1, VINF_SUCCESS },
|
---|
83 | { "test8=test8\0t81=t81\0\0", sizeof("test8=test8\0t81=t81\0\0"), 0, sizeof("test8=test8\0t81=t81\0") - 1, 2, VINF_SUCCESS },
|
---|
84 | /* Good stuff, but with a second block -- should be *not* taken into account since
|
---|
85 | * we're only interested in parsing/handling the first object. */
|
---|
86 | { "t9=t9\0t91=t91\0\0t92=t92\0\0", sizeof("t9=t9\0t91=t91\0\0t92=t92\0\0"), 0, sizeof("t9=t9\0t91=t91\0") - 1, 2, VINF_SUCCESS },
|
---|
87 | /* Nasty stuff. */
|
---|
88 | { "äöü=fäö\0\0", sizeof("äöü=fäö\0\0"), 0, sizeof("äöü=fäö\0") - 1, 1, VINF_SUCCESS },
|
---|
89 | { "äöü=fäö\0ööö=äää", sizeof("äöü=fäö\0ööö=äää"), 0, sizeof("äöü=fäö\0") - 1, 1, VERR_MORE_DATA },
|
---|
90 | /* Some "real world" examples. */
|
---|
91 | { "hdr_id=vbt_stat\0hdr_ver=1\0name=foo.txt\0\0",
|
---|
92 | sizeof("hdr_id=vbt_stat\0hdr_ver=1\0name=foo.txt\0\0"),
|
---|
93 | 0, sizeof("hdr_id=vbt_stat\0hdr_ver=1\0name=foo.txt\0") - 1,
|
---|
94 | 3, VINF_SUCCESS }
|
---|
95 | };
|
---|
96 |
|
---|
97 | static struct
|
---|
98 | {
|
---|
99 | const char *pbData;
|
---|
100 | size_t cbData;
|
---|
101 | /** Number of data blocks retrieved. These are separated by "\0\0". */
|
---|
102 | uint32_t uNumBlocks;
|
---|
103 | /** Overall result when done parsing. */
|
---|
104 | int iResult;
|
---|
105 | } aTestStream[] =
|
---|
106 | {
|
---|
107 | /* No blocks. */
|
---|
108 | { "\0\0\0\0", sizeof("\0\0\0\0"), 0, VERR_NO_DATA },
|
---|
109 | /* Good stuff. */
|
---|
110 | { "\0b1=b1\0\0", sizeof("\0b1=b1\0\0"), 1, VERR_NO_DATA },
|
---|
111 | { "b1=b1\0\0", sizeof("b1=b1\0\0"), 1, VERR_NO_DATA },
|
---|
112 | { "b1=b1\0b2=b2\0\0", sizeof("b1=b1\0b2=b2\0\0"), 1, VERR_NO_DATA },
|
---|
113 | { "b1=b1\0b2=b2\0\0\0", sizeof("b1=b1\0b2=b2\0\0\0"), 1, VERR_NO_DATA }
|
---|
114 | };
|
---|
115 |
|
---|
116 | int manualTest(void)
|
---|
117 | {
|
---|
118 | int rc = VINF_SUCCESS;
|
---|
119 | static struct
|
---|
120 | {
|
---|
121 | const char *pbData;
|
---|
122 | size_t cbData;
|
---|
123 | uint32_t uOffsetStart;
|
---|
124 | uint32_t uOffsetAfter;
|
---|
125 | uint32_t uMapElements;
|
---|
126 | int iResult;
|
---|
127 | } aTest[] =
|
---|
128 | {
|
---|
129 | { "test5=test5\0t51=t51", sizeof("test5=test5\0t51=t51"), 0, sizeof("test5=test5\0") - 1, 1, VERR_MORE_DATA },
|
---|
130 | { "\0\0test5=test5\0t51=t51", sizeof("\0\0test5=test5\0t51=t51"), 0, sizeof("\0\0test5=test5\0") - 1, 1, VERR_MORE_DATA },
|
---|
131 | };
|
---|
132 |
|
---|
133 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTest); iTest++)
|
---|
134 | {
|
---|
135 | RTTestIPrintf(RTTESTLVL_DEBUG, "Manual test #%d\n", iTest);
|
---|
136 |
|
---|
137 | GuestProcessStream stream;
|
---|
138 | rc = stream.AddData((BYTE*)aTest[iTest].pbData, aTest[iTest].cbData);
|
---|
139 |
|
---|
140 | for (;;)
|
---|
141 | {
|
---|
142 | GuestProcessStreamBlock block;
|
---|
143 | rc = stream.ParseBlock(block);
|
---|
144 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tReturned with rc=%Rrc, numItems=%ld\n",
|
---|
145 | rc, block.GetCount());
|
---|
146 |
|
---|
147 | if (block.GetCount())
|
---|
148 | break;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | return rc;
|
---|
153 | }
|
---|
154 |
|
---|
155 | int main()
|
---|
156 | {
|
---|
157 | RTTEST hTest;
|
---|
158 | int rc = RTTestInitAndCreate("tstParseBuffer", &hTest);
|
---|
159 | if (rc)
|
---|
160 | return rc;
|
---|
161 | RTTestBanner(hTest);
|
---|
162 |
|
---|
163 | RTTestIPrintf(RTTESTLVL_DEBUG, "Initializing COM...\n");
|
---|
164 | HRESULT hrc = com::Initialize();
|
---|
165 | if (FAILED(hrc))
|
---|
166 | {
|
---|
167 | RTTestFailed(hTest, "Failed to initialize COM (%Rhrc)!\n", hrc);
|
---|
168 | return RTEXITCODE_FAILURE;
|
---|
169 | }
|
---|
170 |
|
---|
171 | #ifdef DEBUG_andy
|
---|
172 | rc = manualTest();
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | RTTestIPrintf(RTTESTLVL_INFO, "Doing basic tests ...\n");
|
---|
176 |
|
---|
177 | if (sizeof("sizecheck") != 10)
|
---|
178 | RTTestFailed(hTest, "Basic size test #1 failed (%u <-> 10)", sizeof("sizecheck"));
|
---|
179 | if (sizeof("off=rab") != 8)
|
---|
180 | RTTestFailed(hTest, "Basic size test #2 failed (%u <-> 7)", sizeof("off=rab"));
|
---|
181 | if (sizeof("off=rab\0\0") != 10)
|
---|
182 | RTTestFailed(hTest, "Basic size test #3 failed (%u <-> 10)", sizeof("off=rab\0\0"));
|
---|
183 |
|
---|
184 | RTTestIPrintf(RTTESTLVL_INFO, "Doing line tests ...\n");
|
---|
185 |
|
---|
186 | /* Don't let the assertions trigger here
|
---|
187 | * -- we rely on the return values in the test(s) below. */
|
---|
188 | RTAssertSetQuiet(true);
|
---|
189 |
|
---|
190 | unsigned iTest;
|
---|
191 | for (iTest = 0; iTest < RT_ELEMENTS(aTestBlock); iTest++)
|
---|
192 | {
|
---|
193 | RTTestIPrintf(RTTESTLVL_DEBUG, "=> Test #%u\n", iTest);
|
---|
194 |
|
---|
195 | GuestProcessStream stream;
|
---|
196 | int iResult = stream.AddData((BYTE*)aTestBlock[iTest].pbData, aTestBlock[iTest].cbData);
|
---|
197 | if (RT_SUCCESS(iResult))
|
---|
198 | {
|
---|
199 | GuestProcessStreamBlock curBlock;
|
---|
200 | iResult = stream.ParseBlock(curBlock);
|
---|
201 | if (iResult != aTestBlock[iTest].iResult)
|
---|
202 | {
|
---|
203 | RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc\n",
|
---|
204 | iResult, aTestBlock[iTest].iResult);
|
---|
205 | }
|
---|
206 | else if (stream.GetOffset() != aTestBlock[iTest].uOffsetAfter)
|
---|
207 | {
|
---|
208 | RTTestFailed(hTest, "\tOffset %zu wrong, expected %u\n",
|
---|
209 | stream.GetOffset(), aTestBlock[iTest].uOffsetAfter);
|
---|
210 | }
|
---|
211 | else if (iResult == VERR_MORE_DATA)
|
---|
212 | {
|
---|
213 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tMore data (Offset: %zu)\n", stream.GetOffset());
|
---|
214 | }
|
---|
215 |
|
---|
216 | if ( ( RT_SUCCESS(iResult)
|
---|
217 | || iResult == VERR_MORE_DATA))
|
---|
218 | {
|
---|
219 | if (curBlock.GetCount() != aTestBlock[iTest].uMapElements)
|
---|
220 | {
|
---|
221 | RTTestFailed(hTest, "\tMap has %u elements, expected %u\n",
|
---|
222 | curBlock.GetCount(), aTestBlock[iTest].uMapElements);
|
---|
223 | }
|
---|
224 | }
|
---|
225 |
|
---|
226 | /* There is remaining data left in the buffer (which needs to be merged
|
---|
227 | * with a following buffer) -- print it. */
|
---|
228 | size_t uOffset = stream.GetOffset();
|
---|
229 | size_t uToWrite = aTestBlock[iTest].cbData - uOffset;
|
---|
230 | if (uToWrite)
|
---|
231 | {
|
---|
232 | const char *pszRemaining = aTestBlock[iTest].pbData;
|
---|
233 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tRemaining (%u):\n", uToWrite);
|
---|
234 |
|
---|
235 | /* How to properly get the current RTTESTLVL (aka IPRT_TEST_MAX_LEVEL) here?
|
---|
236 | * Hack alert: Using RTEnvGet for now. */
|
---|
237 | if (!RTStrICmp(RTEnvGet("IPRT_TEST_MAX_LEVEL"), "debug"))
|
---|
238 | RTStrmWriteEx(g_pStdOut, &aTestBlock[iTest].pbData[uOffset], uToWrite - 1, NULL);
|
---|
239 | }
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | RTTestIPrintf(RTTESTLVL_INFO, "Doing block tests ...\n");
|
---|
244 |
|
---|
245 | for (iTest = 0; iTest < RT_ELEMENTS(aTestStream); iTest++)
|
---|
246 | {
|
---|
247 | RTTestIPrintf(RTTESTLVL_DEBUG, "=> Block test #%u\n", iTest);
|
---|
248 |
|
---|
249 | GuestProcessStream stream;
|
---|
250 | int iResult = stream.AddData((BYTE*)aTestStream[iTest].pbData, aTestStream[iTest].cbData);
|
---|
251 | if (RT_SUCCESS(iResult))
|
---|
252 | {
|
---|
253 | uint32_t uNumBlocks = 0;
|
---|
254 | uint8_t uSafeCouunter = 0;
|
---|
255 | do
|
---|
256 | {
|
---|
257 | GuestProcessStreamBlock curBlock;
|
---|
258 | iResult = stream.ParseBlock(curBlock);
|
---|
259 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tReturned with %Rrc\n", iResult);
|
---|
260 | if (RT_SUCCESS(iResult))
|
---|
261 | {
|
---|
262 | /* Only count block which have at least one pair. */
|
---|
263 | if (curBlock.GetCount())
|
---|
264 | uNumBlocks++;
|
---|
265 | }
|
---|
266 | if (uSafeCouunter++ > 32)
|
---|
267 | break;
|
---|
268 | } while (RT_SUCCESS(iResult));
|
---|
269 |
|
---|
270 | if (iResult != aTestStream[iTest].iResult)
|
---|
271 | {
|
---|
272 | RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc\n",
|
---|
273 | iResult, aTestStream[iTest].iResult);
|
---|
274 | }
|
---|
275 | else if (uNumBlocks != aTestStream[iTest].uNumBlocks)
|
---|
276 | {
|
---|
277 | RTTestFailed(hTest, "\tReturned %u blocks, expected %u\n",
|
---|
278 | uNumBlocks, aTestStream[iTest].uNumBlocks);
|
---|
279 | }
|
---|
280 | }
|
---|
281 | else
|
---|
282 | RTTestFailed(hTest, "\tAdding data failed with %Rrc", iResult);
|
---|
283 | }
|
---|
284 |
|
---|
285 | RTTestIPrintf(RTTESTLVL_DEBUG, "Shutting down COM...\n");
|
---|
286 | com::Shutdown();
|
---|
287 |
|
---|
288 | /*
|
---|
289 | * Summary.
|
---|
290 | */
|
---|
291 | return RTTestSummaryAndDestroy(hTest);
|
---|
292 | }
|
---|
293 |
|
---|