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