Changeset 37903 in vbox
- Timestamp:
- Jul 12, 2011 1:56:18 PM (14 years ago)
- svn:sync-xref-src-repo-rev:
- 72805
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/testcase/tstGuestCtrlParseBuffer.cpp
r37886 r37903 26 26 #include <iprt/stream.h> 27 27 28 #ifndef BYTE 29 # define BYTE uint8_t 30 #endif 31 28 32 /** @todo Use original source of GuestCtrlImpl.cpp! */ 29 33 … … 37 41 38 42 char pszUnterm1[] = { 'a', 's', 'd', 'f' }; 39 40 #ifndef BYTE 41 # define BYTE uint8_t 42 #endif 43 char pszUnterm2[] = { 'f', 'o', 'o', '3', '=', 'b', 'a', 'r', '3' }; 43 44 44 45 static struct … … 46 47 const char *pbData; 47 48 size_t cbData; 48 uint32_t uOffset; 49 uint32_t uOffsetStart; 50 uint32_t uOffsetAfter; 49 51 uint32_t uMapElements; 50 52 int iResult; … … 52 54 { 53 55 /* Invalid stuff. */ 54 { NULL, 0, 0, 0, VERR_INVALID_POINTER }, 55 { NULL, 512, 0, 0, VERR_INVALID_POINTER }, 56 { "", 0, 0, 0, VERR_INVALID_PARAMETER }, 57 { "", 0, 5, 0, VERR_INVALID_PARAMETER }, 58 { "", 1, 0, 0, VINF_SUCCESS }, 59 { pszUnterm1, 5, 0, 0, VINF_SUCCESS }, 60 { "foo=bar", 0, 0, 0, VERR_INVALID_PARAMETER }, 61 62 /* Parsing stuff. */ 63 { "foo", sizeof("foo"), 0, 0, VINF_SUCCESS }, 64 { "foo=", sizeof("foo="), 0, 1, VINF_SUCCESS }, 65 { "=bar", sizeof("=bar"), 0, 0, VINF_SUCCESS }, 66 { "foo=bar", sizeof("foo=bar"), 0, 1, VINF_SUCCESS }, 67 { "foo=bar\0baz=boo", 16, 0, 2, VINF_SUCCESS } 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 } 68 79 }; 69 80 … … 78 89 79 90 size_t uCur = *puOffset; 80 for ( uCur = 0; uCur < cbData; uCur++)91 for (;uCur < cbData;) 81 92 { 82 93 const char *pszStart = (char*)&pbData[uCur]; … … 84 95 85 96 /* Search and of current pair (key=value\0). */ 86 while ( *pszEnd != '\0' 87 && ++uCur < cbData) 88 { 97 while (uCur++ < cbData) 98 { 99 if (*pszEnd == '\0') 100 break; 89 101 pszEnd++; 90 102 } 91 103 92 size_t uLen = pszEnd - pszStart; 93 if (!uLen) 94 continue; 104 size_t uPairLen = pszEnd - pszStart; 105 if ( *pszEnd != '\0' 106 || !uPairLen) 107 { 108 rc = VERR_MORE_DATA; 109 break; 110 } 95 111 96 112 const char *pszSep = pszStart; … … 104 120 || pszSep == pszEnd) 105 121 { 106 continue; 122 rc = VERR_MORE_DATA; 123 break; 107 124 } 108 125 … … 110 127 size_t uValLen = pszEnd - (pszSep + 1); 111 128 129 /* Get key (if present). */ 112 130 if (uKeyLen) 113 131 { … … 123 141 mapBuf[RTCString(pszKey)].pszValue = NULL; 124 142 143 /* Get value (if present). */ 125 144 if (uValLen) 126 145 { … … 138 157 139 158 RTMemFree(pszKey); 159 160 *puOffset += uCur - *puOffset; 140 161 } 141 162 } … … 152 173 RTTestBanner(hTest); 153 174 175 if (sizeof("sizecheck") != 10) 176 RTTestFailed(hTest, "Basic size test failed (%u <-> 10)", sizeof("sizecheck")); 177 154 178 for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++) 155 179 { 156 GuestBufferMap map;180 GuestBufferMap bufMap; 157 181 158 182 int iResult = outputBufferParse((BYTE*)aTests[iTest].pbData, aTests[iTest].cbData, 159 &aTests[iTest].uOffset, map); 183 &aTests[iTest].uOffsetStart, bufMap); 184 185 RTTestIPrintf(RTTESTLVL_DEBUG, "=> Test #%u\n", iTest); 186 160 187 if (iResult != aTests[iTest].iResult) 161 188 { 162 RTTestFailed(hTest, "#%u: Returned %Rrc, expected %Rrc", 163 iTest, iResult, aTests[iTest].iResult); 164 } 165 else if (map.size() != aTests[iTest].uMapElements) 166 { 167 RTTestFailed(hTest, "#%u: Map has %u elements, expected %u", 168 iTest, map.size(), aTests[iTest].uMapElements); 169 } 170 171 if (map.size()) 172 RTTestIPrintf(RTTESTLVL_DEBUG, "Output for test #%u\n", iTest); 173 for (GuestBufferMapIter it = map.begin(); it != map.end(); it++) 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++) 174 218 { 175 219 RTTestIPrintf(RTTESTLVL_DEBUG, "\t%s -> %s\n",
Note:
See TracChangeset
for help on using the changeset viewer.