VirtualBox

Changeset 37903 in vbox


Ignore:
Timestamp:
Jul 12, 2011 1:56:18 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
72805
Message:

Main: Extended testcase for guest control output buffer parsing.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/testcase/tstGuestCtrlParseBuffer.cpp

    r37886 r37903  
    2626#include <iprt/stream.h>
    2727
     28#ifndef BYTE
     29# define BYTE uint8_t
     30#endif
     31
    2832/** @todo Use original source of GuestCtrlImpl.cpp! */
    2933
     
    3741
    3842char pszUnterm1[] = { 'a', 's', 'd', 'f' };
    39 
    40 #ifndef BYTE
    41 # define BYTE uint8_t
    42 #endif
     43char pszUnterm2[] = { 'f', 'o', 'o', '3', '=', 'b', 'a', 'r', '3' };
    4344
    4445static struct
     
    4647    const char *pbData;
    4748    size_t      cbData;
    48     uint32_t    uOffset;
     49    uint32_t    uOffsetStart;
     50    uint32_t    uOffsetAfter;
    4951    uint32_t    uMapElements;
    5052    int         iResult;
     
    5254{
    5355    /* 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 }
    6879};
    6980
     
    7889
    7990    size_t uCur = *puOffset;
    80     for (uCur = 0; uCur < cbData; uCur++)
     91    for (;uCur < cbData;)
    8192    {
    8293        const char *pszStart = (char*)&pbData[uCur];
     
    8495
    8596        /* 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;
    89101            pszEnd++;
    90102        }
    91103
    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        }
    95111
    96112        const char *pszSep = pszStart;
     
    104120            || pszSep == pszEnd)
    105121        {
    106             continue;
     122            rc = VERR_MORE_DATA;
     123            break;
    107124        }
    108125
     
    110127        size_t uValLen = pszEnd - (pszSep + 1);
    111128
     129        /* Get key (if present). */
    112130        if (uKeyLen)
    113131        {
     
    123141            mapBuf[RTCString(pszKey)].pszValue = NULL;
    124142
     143            /* Get value (if present). */
    125144            if (uValLen)
    126145            {
     
    138157
    139158            RTMemFree(pszKey);
     159
     160            *puOffset += uCur - *puOffset;
    140161        }
    141162    }
     
    152173    RTTestBanner(hTest);
    153174
     175    if (sizeof("sizecheck") != 10)
     176        RTTestFailed(hTest, "Basic size test failed (%u <-> 10)", sizeof("sizecheck"));
     177
    154178    for (unsigned iTest = 0; iTest < RT_ELEMENTS(aTests); iTest++)
    155179    {
    156         GuestBufferMap map;
     180        GuestBufferMap bufMap;
    157181
    158182        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
    160187        if (iResult != aTests[iTest].iResult)
    161188        {
    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++)
    174218        {
    175219            RTTestIPrintf(RTTESTLVL_DEBUG, "\t%s -> %s\n",
Note: See TracChangeset for help on using the changeset viewer.

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