VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstGuestCtrlParseBuffer.cpp@ 53517

Last change on this file since 53517 was 44529, checked in by vboxsync, 12 years ago

header (C) fixes

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette