1 | /* $Id: tstGuestCtrlParseBuffer.cpp 37903 2011-07-12 13:56:18Z 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 pszUnterm1[] = { 'a', 's', 'd', 'f' };
|
---|
43 | char pszUnterm2[] = { '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 | { pszUnterm1, 5, 0, 0, 0, VERR_MORE_DATA },
|
---|
66 | { "foo1", sizeof("foo1"), 0, 0, 0, VERR_MORE_DATA },
|
---|
67 | { pszUnterm2, 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 | int outputBufferParse(const BYTE *pbData, size_t cbData, uint32_t *puOffset, GuestBufferMap& mapBuf)
|
---|
82 | {
|
---|
83 | AssertPtrReturn(pbData, VERR_INVALID_POINTER);
|
---|
84 | AssertReturn(cbData, VERR_INVALID_PARAMETER);
|
---|
85 | AssertPtrReturn(puOffset, VERR_INVALID_POINTER);
|
---|
86 | AssertReturn(*puOffset < cbData, VERR_INVALID_PARAMETER);
|
---|
87 |
|
---|
88 | int rc = VINF_SUCCESS;
|
---|
89 |
|
---|
90 | size_t uCur = *puOffset;
|
---|
91 | for (;uCur < cbData;)
|
---|
92 | {
|
---|
93 | const char *pszStart = (char*)&pbData[uCur];
|
---|
94 | const char *pszEnd = pszStart;
|
---|
95 |
|
---|
96 | /* Search and of current pair (key=value\0). */
|
---|
97 | while (uCur++ < cbData)
|
---|
98 | {
|
---|
99 | if (*pszEnd == '\0')
|
---|
100 | break;
|
---|
101 | pszEnd++;
|
---|
102 | }
|
---|
103 |
|
---|
104 | size_t uPairLen = pszEnd - pszStart;
|
---|
105 | if ( *pszEnd != '\0'
|
---|
106 | || !uPairLen)
|
---|
107 | {
|
---|
108 | rc = VERR_MORE_DATA;
|
---|
109 | break;
|
---|
110 | }
|
---|
111 |
|
---|
112 | const char *pszSep = pszStart;
|
---|
113 | while ( *pszSep != '='
|
---|
114 | && pszSep != pszEnd)
|
---|
115 | {
|
---|
116 | pszSep++;
|
---|
117 | }
|
---|
118 |
|
---|
119 | if ( pszSep == pszStart
|
---|
120 | || pszSep == pszEnd)
|
---|
121 | {
|
---|
122 | rc = VERR_MORE_DATA;
|
---|
123 | break;
|
---|
124 | }
|
---|
125 |
|
---|
126 | size_t uKeyLen = pszSep - pszStart;
|
---|
127 | size_t uValLen = pszEnd - (pszSep + 1);
|
---|
128 |
|
---|
129 | /* Get key (if present). */
|
---|
130 | if (uKeyLen)
|
---|
131 | {
|
---|
132 | Assert(pszSep > pszStart);
|
---|
133 | char *pszKey = (char*)RTMemAllocZ(uKeyLen + 1);
|
---|
134 | if (!pszKey)
|
---|
135 | {
|
---|
136 | rc = VERR_NO_MEMORY;
|
---|
137 | break;
|
---|
138 | }
|
---|
139 | memcpy(pszKey, pszStart, uKeyLen);
|
---|
140 |
|
---|
141 | mapBuf[RTCString(pszKey)].pszValue = NULL;
|
---|
142 |
|
---|
143 | /* Get value (if present). */
|
---|
144 | if (uValLen)
|
---|
145 | {
|
---|
146 | Assert(pszEnd > pszSep);
|
---|
147 | char *pszVal = (char*)RTMemAllocZ(uValLen + 1);
|
---|
148 | if (!pszVal)
|
---|
149 | {
|
---|
150 | rc = VERR_NO_MEMORY;
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | memcpy(pszVal, pszSep + 1, uValLen);
|
---|
154 |
|
---|
155 | mapBuf[RTCString(pszKey)].pszValue = pszVal;
|
---|
156 | }
|
---|
157 |
|
---|
158 | RTMemFree(pszKey);
|
---|
159 |
|
---|
160 | *puOffset += uCur - *puOffset;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 | int main()
|
---|
168 | {
|
---|
169 | RTTEST hTest;
|
---|
170 | int rc = RTTestInitAndCreate("tstParseBuffer", &hTest);
|
---|
171 | if (rc)
|
---|
172 | return rc;
|
---|
173 | RTTestBanner(hTest);
|
---|
174 |
|
---|
175 | if (sizeof("sizecheck") != 10)
|
---|
176 | RTTestFailed(hTest, "Basic size test failed (%u <-> 10)", sizeof("sizecheck"));
|
---|
177 |
|
---|
178 | for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
|
---|
179 | {
|
---|
180 | GuestBufferMap bufMap;
|
---|
181 |
|
---|
182 | int iResult = outputBufferParse((BYTE*)aTests[iTest].pbData, aTests[iTest].cbData,
|
---|
183 | &aTests[iTest].uOffsetStart, bufMap);
|
---|
184 |
|
---|
185 | RTTestIPrintf(RTTESTLVL_DEBUG, "=> Test #%u\n", iTest);
|
---|
186 |
|
---|
187 | if (iResult != aTests[iTest].iResult)
|
---|
188 | {
|
---|
189 | RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc",
|
---|
190 | iResult, aTests[iTest].iResult);
|
---|
191 | }
|
---|
192 | else if (bufMap.size() != aTests[iTest].uMapElements)
|
---|
193 | {
|
---|
194 | RTTestFailed(hTest, "\tMap has %u elements, expected %u",
|
---|
195 | bufMap.size(), aTests[iTest].uMapElements);
|
---|
196 | }
|
---|
197 | else if (aTests[iTest].uOffsetStart != aTests[iTest].uOffsetAfter)
|
---|
198 | {
|
---|
199 | RTTestFailed(hTest, "\tOffset %u wrong, expected %u",
|
---|
200 | aTests[iTest].uOffsetStart, aTests[iTest].uOffsetAfter);
|
---|
201 | }
|
---|
202 | else if (iResult == VERR_MORE_DATA)
|
---|
203 | {
|
---|
204 | /* There is remaining data left in the buffer (which needs to be merged
|
---|
205 | * with a following buffer) -- print it. */
|
---|
206 | const char *pszRemaining = aTests[iTest].pbData;
|
---|
207 | size_t uOffsetNew = aTests[iTest].uOffsetStart;
|
---|
208 | size_t uToWrite = aTests[iTest].cbData - uOffsetNew;
|
---|
209 | if (pszRemaining && uOffsetNew)
|
---|
210 | {
|
---|
211 | RTTestIPrintf(RTTESTLVL_DEBUG, "\tRemaining (%u):\n", uToWrite);
|
---|
212 | RTStrmWriteEx(g_pStdOut, &aTests[iTest].pbData[uOffsetNew], uToWrite - 1, NULL);
|
---|
213 | RTTestIPrintf(RTTESTLVL_DEBUG, "\n");
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | for (GuestBufferMapIter it = bufMap.begin(); it != bufMap.end(); it++)
|
---|
218 | {
|
---|
219 | RTTestIPrintf(RTTESTLVL_DEBUG, "\t%s -> %s\n",
|
---|
220 | it->first.c_str(), it->second.pszValue ? it->second.pszValue : "<undefined>");
|
---|
221 |
|
---|
222 | if (it->second.pszValue)
|
---|
223 | RTMemFree(it->second.pszValue);
|
---|
224 | }
|
---|
225 | }
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Summary.
|
---|
229 | */
|
---|
230 | return RTTestSummaryAndDestroy(hTest);
|
---|
231 | }
|
---|
232 |
|
---|