VirtualBox

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

Last change on this file since 38269 was 38269, checked in by vboxsync, 14 years ago

GuestCtrl: Update.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.8 KB
Line 
1/* $Id: tstGuestCtrlParseBuffer.cpp 38269 2011-08-01 17:22:10Z 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.215389.xyz. 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
25using 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
39typedef struct VBOXGUESTCTRL_BUFFER_VALUE
40{
41 char *pszValue;
42} VBOXGUESTCTRL_BUFFER_VALUE, *PVBOXGUESTCTRL_BUFFER_VALUE;
43typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE > GuestBufferMap;
44typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE >::iterator GuestBufferMapIter;
45typedef std::map< RTCString, VBOXGUESTCTRL_BUFFER_VALUE >::const_iterator GuestBufferMapIterConst;
46
47char szUnterm1[] = { 'a', 's', 'd', 'f' };
48char szUnterm2[] = { 'f', 'o', 'o', '3', '=', 'b', 'a', 'r', '3' };
49
50static 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 /* Invalid stuff. */
61 { NULL, 0, 0, 0, 0, VERR_INVALID_POINTER },
62 { NULL, 512, 0, 0, 0, VERR_INVALID_POINTER },
63 { "", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
64 { "", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
65 { "foo=bar1", 0, 0, 0, 0, VERR_INVALID_PARAMETER },
66 { "foo=bar2", 0, 50, 50, 0, VERR_INVALID_PARAMETER },
67 /* Empty buffers. */
68 { "", 1, 0, 1, 0, VERR_MORE_DATA },
69 { "\0", 1, 0, 1, 0, VERR_MORE_DATA },
70 /* Incomplete buffer (missing components). */
71 { szUnterm1, 5, 0, 0, 0, VERR_MORE_DATA },
72 { "foo1", sizeof("foo1"), 0, 0, 0, VERR_MORE_DATA },
73 { "=bar\0", sizeof("=bar"), 0, 0, 0, VERR_MORE_DATA },
74 /* Last sequence is incomplete -- new offset should point to it. */
75 { "hug=sub\0incomplete", sizeof("hug=sub\0incomplete"), 0, sizeof("hug=sub"), 1, VERR_MORE_DATA },
76 { "boo=hoo\0baz=boo\0qwer", sizeof("boo=hoo\0baz=boo\0qwer"), 0, sizeof("boo=hoo\0baz=boo"), 2, VERR_MORE_DATA },
77 /* Parsing good stuff. */
78 { "novalue=", sizeof("novalue="), 0, sizeof("novalue="), 1, VINF_SUCCESS },
79 { szUnterm2, 8, 0, sizeof(szUnterm2), 1, VINF_SUCCESS },
80 { "foo2=", sizeof("foo2="), 0, sizeof("foo2="), 1, VINF_SUCCESS },
81 { "har=hor", sizeof("har=hor"), 0, sizeof("har=hor"), 1, VINF_SUCCESS },
82 { "foo=bar\0baz=boo", sizeof("foo=bar\0baz=boo"), 0, sizeof("foo=bar\0baz=boo"), 2, VINF_SUCCESS },
83 /* Parsing until a different block (two terminations, returning offset to next block). */
84 { "off=rab\0a=b\0\0\0\0", sizeof("off=rab\0a=b\0\0\0"), 0, 13, 2, VERR_MORE_DATA },
85 { "off=rab\0\0zab=oob", sizeof("off=rab\0\0zab=oob"), 0, 9, 1, VERR_MORE_DATA },
86 { "\0\0\0\0off=rab\0zab=oob\0\0", sizeof("\0\0\0\0off=rab\0zab=oob\0\0"), 0, 1, 0, VERR_MORE_DATA },
87 { "o2=r2\0z3=o3\0\0f3=g3", sizeof("o2=r2\0z3=o3\0\0f3=g3"), 0, 13, 2, VERR_MORE_DATA }
88};
89
90static struct
91{
92 const char *pbData;
93 size_t cbData;
94 /** Number of data blocks retrieved. These are separated by "\0\0". */
95 uint32_t uNumBlocks;
96 /** Overall result when done parsing. */
97 int iResult;
98} aTests2[] =
99{
100 { "\0\0\0\0", sizeof("\0\0\0\0"), 0, VINF_SUCCESS },
101 { "off=rab\0\0zab=oob", sizeof("off=rab\0\0zab=oob"), 2, VINF_SUCCESS },
102 { "\0\0\0soo=foo\0goo=loo\0\0zab=oob", sizeof("\0\0\0soo=foo\0goo=loo\0\0zab=oob"), 2, VINF_SUCCESS },
103 { "qoo=uoo\0\0\0\0asdf=\0\0", sizeof("qoo=uoo\0\0\0\0asdf=\0\0"), 2, VINF_SUCCESS },
104 { "foo=bar\0\0\0\0\0\0", sizeof("foo=bar\0\0\0\0\0\0"), 1, VINF_SUCCESS },
105 { "qwer=cvbnr\0\0\0gui=uig\0\0\0", sizeof("qwer=cvbnr\0\0\0gui=uig\0\0\0"), 2, VINF_SUCCESS }
106};
107
108int main()
109{
110 RTTEST hTest;
111 int rc = RTTestInitAndCreate("tstParseBuffer", &hTest);
112 if (rc)
113 return rc;
114 RTTestBanner(hTest);
115
116 RTTestIPrintf(RTTESTLVL_DEBUG, "Initializing COM...\n");
117 rc = com::Initialize();
118 if (FAILED(rc))
119 {
120 RTPrintf("ERROR: failed to initialize COM!\n");
121 return rc;
122 }
123
124 RTTestIPrintf(RTTESTLVL_INFO, "Doing basic tests ...\n");
125
126 if (sizeof("sizecheck") != 10)
127 RTTestFailed(hTest, "Basic size test #1 failed (%u <-> 10)", sizeof("sizecheck"));
128 if (sizeof("off=rab") != 8)
129 RTTestFailed(hTest, "Basic size test #2 failed (%u <-> 7)", sizeof("off=rab"));
130 if (sizeof("off=rab\0\0") != 10)
131 RTTestFailed(hTest, "Basic size test #3 failed (%u <-> 10)", sizeof("off=rab\0\0"));
132
133 RTTestIPrintf(RTTESTLVL_INFO, "Doing line tests ...\n");
134
135 unsigned iTest = 0;
136 for (iTest; iTest < RT_ELEMENTS(aTests); iTest++)
137 {
138 uint32_t uOffset = aTests[iTest].uOffsetStart;
139
140 RTTestIPrintf(RTTESTLVL_DEBUG, "=> Test #%u\n", iTest);
141
142 GuestProcessStream stream;
143 int iResult = stream.AddData((BYTE*)aTests[iTest].pbData, aTests[iTest].cbData);
144 if (RT_SUCCESS(iResult))
145 {
146 GuestProcessStreamBlock block;
147 iResult = stream.ParseBlock(block);
148 if (iResult != aTests[iTest].iResult)
149 {
150 RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc",
151 iResult, aTests[iTest].iResult);
152 }
153 else if (block.GetCount() != aTests[iTest].uMapElements)
154 {
155 RTTestFailed(hTest, "\tMap has %u elements, expected %u",
156 block.GetCount(), aTests[iTest].uMapElements);
157 }
158 else if (stream.GetOffset() != aTests[iTest].uOffsetAfter)
159 {
160 RTTestFailed(hTest, "\tOffset %u wrong, expected %u",
161 stream.GetOffset(), aTests[iTest].uOffsetAfter);
162 }
163 else if (iResult == VERR_MORE_DATA)
164 {
165 RTTestIPrintf(RTTESTLVL_DEBUG, "\tMore data (Offset: %u)\n", uOffset);
166
167 /* There is remaining data left in the buffer (which needs to be merged
168 * with a following buffer) -- print it. */
169 size_t uToWrite = aTests[iTest].cbData - uOffset;
170 if (uToWrite)
171 {
172 const char *pszRemaining = aTests[iTest].pbData;
173 RTTestIPrintf(RTTESTLVL_DEBUG, "\tRemaining (%u):\n", uToWrite);
174 RTStrmWriteEx(g_pStdOut, &aTests[iTest].pbData[uOffset], uToWrite - 1, NULL);
175 RTTestIPrintf(RTTESTLVL_DEBUG, "\n");
176 }
177 }
178 }
179 }
180
181 RTTestIPrintf(RTTESTLVL_INFO, "Doing block tests ...\n");
182
183 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests2); iTest++)
184 {
185 RTTestIPrintf(RTTESTLVL_DEBUG, "=> Block test #%u\n", iTest);
186
187 GuestProcessStream stream;
188 int iResult = stream.AddData((BYTE*)aTests2[iTest].pbData, aTests2[iTest].cbData);
189 if (RT_SUCCESS(iResult))
190 {
191 uint32_t uNumBlocks = 0;
192
193 do
194 {
195 GuestProcessStreamBlock block;
196 iResult = stream.ParseBlock(block);
197 RTTestIPrintf(RTTESTLVL_DEBUG, "\tReturned with %Rrc\n", iResult);
198 if ( iResult == VINF_SUCCESS
199 || iResult == VERR_MORE_DATA)
200 {
201 /* Only count block which have at least one pair. */
202 if (block.GetCount())
203 uNumBlocks++;
204 }
205 if (uNumBlocks > 32)
206 break; /* Give up if unreasonable big. */
207 block.Clear();
208 } while (iResult == VERR_MORE_DATA);
209
210 if (iResult != aTests2[iTest].iResult)
211 {
212 RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc",
213 iResult, aTests2[iTest].iResult);
214 }
215 else if (uNumBlocks != aTests2[iTest].uNumBlocks)
216 {
217 RTTestFailed(hTest, "\tReturned %u blocks, expected %u\n",
218 uNumBlocks, aTests2[iTest].uNumBlocks);
219 }
220 }
221 else
222 RTTestFailed(hTest, "\tAdding data failed with %Rrc", iResult);
223 }
224
225 RTTestIPrintf(RTTESTLVL_DEBUG, "Shutting down COM...\n");
226 com::Shutdown();
227
228 /*
229 * Summary.
230 */
231 return RTTestSummaryAndDestroy(hTest);
232}
233
Note: See TracBrowser for help on using the repository browser.

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