1 | /* $Id: tstGuestCtrlParseBuffer.cpp 37974 2011-07-15 09:46:23Z vboxsync $ */
|
---|
2 |
|
---|
3 | /** @file
|
---|
4 | *
|
---|
5 | * Output stream parsing test cases.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2011 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <map>
|
---|
21 |
|
---|
22 | #include <iprt/string.h>
|
---|
23 | #include <iprt/cpp/ministring.h>
|
---|
24 |
|
---|
25 | #include <iprt/test.h>
|
---|
26 | #include <iprt/stream.h>
|
---|
27 |
|
---|
28 | #ifndef BYTE
|
---|
29 | # define BYTE uint8_t
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | /** @todo Use original source of GuestCtrlImpl.cpp! */
|
---|
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 | } aTests[] =
|
---|
54 | {
|
---|
55 | /* Invalid stuff. */
|
---|
56 | { NULL, 0, 0, 0, 0, VERR_INVALID_POINTER },
|
---|
57 | { NULL, 512, 0, 0, 0, VERR_INVALID_POINTER },
|
---|
58 | { "", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
59 | { "", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
60 | { "foo=bar1", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
|
---|
61 | { "foo=bar2", 0, 50, 50, 0, VERR_INVALID_PARAMETER },
|
---|
62 | /* Incomplete buffer (missing \0 termination). */
|
---|
63 | { "", 1, 0, 0, 0, VERR_MORE_DATA },
|
---|
64 | { "\0", 1, 0, 0, 0, VERR_MORE_DATA },
|
---|
65 | { szUnterm1, 5, 0, 0, 0, VERR_MORE_DATA },
|
---|
66 | { "foo1", sizeof("foo1"), 0, 0, 0, VERR_MORE_DATA },
|
---|
67 | { szUnterm2, 8, 0, 0, 0, VERR_MORE_DATA },
|
---|
68 | /* Incomplete buffer (missing components). */
|
---|
69 | { "=bar\0", sizeof("=bar"), 0, 0, 0, VERR_MORE_DATA },
|
---|
70 | /* Last sequence is incomplete -- new offset should point to it. */
|
---|
71 | { "hug=sub\0incomplete", sizeof("hug=sub\0incomplete"), 0, sizeof("hug=sub"), 1, VERR_MORE_DATA },
|
---|
72 | { "boo=hoo\0baz=boo\0qwer", sizeof("boo=hoo\0baz=boo\0qwer"), 0, sizeof("boo=hoo\0baz=boo"), 2, VERR_MORE_DATA },
|
---|
73 | /* Parsing good stuff. */
|
---|
74 | { "foo2=", sizeof("foo2="), 0, sizeof("foo2="), 1, VINF_SUCCESS },
|
---|
75 | { "har=hor", sizeof("har=hor"), 0, sizeof("har=hor"), 1, VINF_SUCCESS },
|
---|
76 | { "foo=bar\0baz=boo", sizeof("foo=bar\0baz=boo"), 0, sizeof("foo=bar\0baz=boo"), 2, VINF_SUCCESS },
|
---|
77 | /* Parsing until a different block (two terminations, returning offset to next block). */
|
---|
78 | { "off=rab\0\0zab=oob", sizeof("off=rab\0\0zab=oob"), 0, sizeof("zab=oob"), 1, VERR_MORE_DATA }
|
---|
79 | };
|
---|
80 |
|
---|
81 | static struct
|
---|
82 | {
|
---|
83 | const char *pbData;
|
---|
84 | size_t cbData;
|
---|
85 | /** Number of data blocks retrieved. These are separated by "\0\0". */
|
---|
86 | uint32_t uNumBlocks;
|
---|
87 | /** Overall result when done parsing. */
|
---|
88 | int iResult;
|
---|
89 | } aTests2[] =
|
---|
90 | {
|
---|
91 | { "off=rab\0\0zab=oob", sizeof("off=rab\0\0zab=oob"), 2, VINF_SUCCESS },
|
---|
92 | { "\0\0\0soo=foo\0goo=loo\0\0zab=oob", sizeof("\0\0\0soo=foo\0goo=loo\0\0zab=oob"), 2, VINF_SUCCESS },
|
---|
93 | { "qoo=uoo\0\0\0\0asdf=\0\0", sizeof("qoo=uoo\0\0\0\0asdf=\0\0"), 2, VINF_SUCCESS },
|
---|
94 | { "foo=bar\0\0\0\0\0\0", sizeof("foo=bar\0\0\0\0\0\0"), 1, VINF_SUCCESS }
|
---|
95 | };
|
---|
96 |
|
---|
97 | int outputBufferParse(const BYTE *pbData, size_t cbData, uint32_t *puOffset, GuestBufferMap& mapBuf)
|
---|
98 | {
|
---|
99 | AssertPtrReturn(pbData, VERR_INVALID_POINTER);
|
---|
100 | AssertReturn(cbData, VERR_INVALID_PARAMETER);
|
---|
101 | AssertPtrReturn(puOffset, VERR_INVALID_POINTER);
|
---|
102 | AssertReturn(*puOffset < cbData, VERR_INVALID_PARAMETER);
|
---|
103 |
|
---|
104 | int rc = VINF_SUCCESS;
|
---|
105 |
|
---|
106 | size_t uCur = *puOffset;
|
---|
107 | for (;uCur < cbData;)
|
---|
108 | {
|
---|
109 | const char *pszStart = (char*)&pbData[uCur];
|
---|
110 | const char *pszEnd = pszStart;
|
---|
111 |
|
---|
112 | /* Search and of current pair (key=value\0). */
|
---|
113 | while (uCur++ < cbData)
|
---|
114 | {
|
---|
115 | if (*pszEnd == '\0')
|
---|
116 | break;
|
---|
117 | pszEnd++;
|
---|
118 | }
|
---|
119 |
|
---|
120 | size_t uPairLen = pszEnd - pszStart;
|
---|
121 | if ( *pszEnd != '\0'
|
---|
122 | || !uPairLen)
|
---|
123 | {
|
---|
124 | rc = VERR_MORE_DATA;
|
---|
125 | break;
|
---|
126 | }
|
---|
127 |
|
---|
128 | const char *pszSep = pszStart;
|
---|
129 | while ( *pszSep != '='
|
---|
130 | && pszSep != pszEnd)
|
---|
131 | {
|
---|
132 | pszSep++;
|
---|
133 | }
|
---|
134 |
|
---|
135 | if ( pszSep == pszStart
|
---|
136 | || pszSep == pszEnd)
|
---|
137 | {
|
---|
138 | rc = VERR_MORE_DATA;
|
---|
139 | break;
|
---|
140 | }
|
---|
141 |
|
---|
142 | size_t uKeyLen = pszSep - pszStart;
|
---|
143 | size_t uValLen = pszEnd - (pszSep + 1);
|
---|
144 |
|
---|
145 | /* Get key (if present). */
|
---|
146 | if (uKeyLen)
|
---|
147 | {
|
---|
148 | Assert(pszSep > pszStart);
|
---|
149 | char *pszKey = (char*)RTMemAllocZ(uKeyLen + 1);
|
---|
150 | if (!pszKey)
|
---|
151 | {
|
---|
152 | rc = VERR_NO_MEMORY;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | memcpy(pszKey, pszStart, uKeyLen);
|
---|
156 |
|
---|
157 | mapBuf[RTCString(pszKey)].pszValue = NULL;
|
---|
158 |
|
---|
159 | /* Get value (if present). */
|
---|
160 | if (uValLen)
|
---|
161 | {
|
---|
162 | Assert(pszEnd > pszSep);
|
---|
163 | char *pszVal = (char*)RTMemAllocZ(uValLen + 1);
|
---|
164 | if (!pszVal)
|
---|
165 | {
|
---|
166 | rc = VERR_NO_MEMORY;
|
---|
167 | break;
|
---|
168 | }
|
---|
169 | memcpy(pszVal, pszSep + 1, uValLen);
|
---|
170 |
|
---|
171 | mapBuf[RTCString(pszKey)].pszValue = pszVal;
|
---|
172 | }
|
---|
173 |
|
---|
174 | RTMemFree(pszKey);
|
---|
175 |
|
---|
176 | *puOffset += uCur - *puOffset;
|
---|
177 | }
|
---|
178 | }
|
---|
179 |
|
---|
180 | return rc;
|
---|
181 | }
|
---|
182 |
|
---|
183 | void tstOutputAndDestroyMap(GuestBufferMap &bufMap)
|
---|
184 | {
|
---|
185 | for (GuestBufferMapIter it = bufMap.begin(); it != bufMap.end(); it++)
|
---|
186 | {
|
---|
187 | RTTestIPrintf(RTTESTLVL_DEBUG, "\t%s -> %s\n",
|
---|
188 | it->first.c_str(), it->second.pszValue ? it->second.pszValue : "<undefined>");
|
---|
189 |
|
---|
190 | if (it->second.pszValue)
|
---|
191 | RTMemFree(it->second.pszValue);
|
---|
192 | }
|
---|
193 |
|
---|
194 | bufMap.clear();
|
---|
195 | }
|
---|
196 |
|
---|
197 | int main()
|
---|
198 | {
|
---|
199 | RTTEST hTest;
|
---|
200 | int rc = RTTestInitAndCreate("tstParseBuffer", &hTest);
|
---|
201 | if (rc)
|
---|
202 | return rc;
|
---|
203 | RTTestBanner(hTest);
|
---|
204 |
|
---|
205 | RTTestIPrintf(RTTESTLVL_INFO, "Doing basic tests ...\n");
|
---|
206 |
|
---|
207 | if (sizeof("sizecheck") != 10)
|
---|
208 | RTTestFailed(hTest, "Basic size test failed (%u <-> 10)", sizeof("sizecheck"));
|
---|
209 |
|
---|
210 | RTTestIPrintf(RTTESTLVL_INFO, "Doing line tests ...\n");
|
---|
211 |
|
---|
212 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
|
---|
213 | {
|
---|
214 | GuestBufferMap bufMap;
|
---|
215 |
|
---|
216 | int iResult = outputBufferParse((BYTE*)aTests[iTest].pbData, aTests[iTest].cbData,
|
---|
217 | &aTests[iTest].uOffsetStart, bufMap);
|
---|
218 |
|
---|
219 | RTTestIPrintf(RTTESTLVL_DEBUG, "=> Test #%u\n", iTest);
|
---|
220 |
|
---|
221 | if (iResult != aTests[iTest].iResult)
|
---|
222 | {
|
---|
223 | RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc",
|
---|
224 | iResult, aTests[iTest].iResult);
|
---|
225 | }
|
---|
226 | else if (bufMap.size() != aTests[iTest].uMapElements)
|
---|
227 | {
|
---|
228 | RTTestFailed(hTest, "\tMap has %u elements, expected %u",
|
---|
229 | bufMap.size(), aTests[iTest].uMapElements);
|
---|
230 | }
|
---|
231 | else if (aTests[iTest].uOffsetStart != aTests[iTest].uOffsetAfter)
|
---|
232 | {
|
---|
233 | RTTestFailed(hTest, "\tOffset %u wrong, expected %u",
|
---|
234 | aTests[iTest].uOffsetStart, aTests[iTest].uOffsetAfter);
|
---|
235 | }
|
---|
236 | else if (iResult == VERR_MORE_DATA)
|
---|
237 | {
|
---|
238 | /* There is remaining data left in the buffer (which needs to be merged
|
---|
239 | * with a following buffer) -- print it. */
|
---|
240 | const char *pszRemaining = aTests[iTest].pbData;
|
---|
241 | size_t uOffsetNew = aTests[iTest].uOffsetStart;
|
---|
242 | size_t uToWrite = aTests[iTest].cbData - uOffsetNew;
|
---|
243 | if (pszRemaining && uOffsetNew)
|
---|
244 | {
|
---|
245 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tRemaining (%u):\n", uToWrite);
|
---|
246 | RTStrmWriteEx(g_pStdOut, &aTests[iTest].pbData[uOffsetNew], uToWrite - 1, NULL);
|
---|
247 | RTTestIPrintf(RTTESTLVL_DEBUG, "\n");
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | tstOutputAndDestroyMap(bufMap);
|
---|
252 | }
|
---|
253 |
|
---|
254 | RTTestIPrintf(RTTESTLVL_INFO, "Doing block tests ...\n");
|
---|
255 |
|
---|
256 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests2); iTest++)
|
---|
257 | {
|
---|
258 | RTTestIPrintf(RTTESTLVL_DEBUG, "=> Block test #%u\n", iTest);
|
---|
259 |
|
---|
260 | int iResult;
|
---|
261 |
|
---|
262 | GuestBufferMap bufMap;
|
---|
263 | uint32_t uOffset = 0;
|
---|
264 | uint32_t uNumBlocks = 0;
|
---|
265 |
|
---|
266 | while (uOffset < aTests2[iTest].cbData)
|
---|
267 | {
|
---|
268 | iResult = outputBufferParse((BYTE*)aTests2[iTest].pbData, aTests2[iTest].cbData,
|
---|
269 | &uOffset, bufMap);
|
---|
270 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tReturned with %Rrc\n", iResult);
|
---|
271 | if ( iResult == VINF_SUCCESS
|
---|
272 | || iResult == VERR_MORE_DATA)
|
---|
273 | {
|
---|
274 | if (bufMap.size()) /* Only count block which have some valid data. */
|
---|
275 | uNumBlocks++;
|
---|
276 |
|
---|
277 | tstOutputAndDestroyMap(bufMap);
|
---|
278 |
|
---|
279 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tNext offset %u (total: %u)\n",
|
---|
280 | uOffset, aTests2[iTest].cbData);
|
---|
281 | uOffset++;
|
---|
282 | }
|
---|
283 | else
|
---|
284 | break;
|
---|
285 |
|
---|
286 | if (uNumBlocks > 32)
|
---|
287 | break; /* Give up if unreasonable big. */
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (uNumBlocks != aTests2[iTest].uNumBlocks)
|
---|
291 | {
|
---|
292 | RTTestFailed(hTest, "\tReturned %u blocks, expected %u\n",
|
---|
293 | uNumBlocks, aTests2[iTest].uNumBlocks);
|
---|
294 | }
|
---|
295 | }
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Summary.
|
---|
299 | */
|
---|
300 | return RTTestSummaryAndDestroy(hTest);
|
---|
301 | }
|
---|
302 |
|
---|