VirtualBox

Changeset 38210 in vbox


Ignore:
Timestamp:
Jul 28, 2011 8:42:59 AM (14 years ago)
Author:
vboxsync
Message:

Main: Testcase for guest control output buffer parsing now is using the same code as GuestCtrlIO.

Location:
trunk/src/VBox/Main
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/GuestCtrlImplPrivate.h

    r38085 r38210  
    1919#define ____H_GUESTIMPLPRIVATE
    2020
    21 #include "VirtualBoxBase.h"
     21#include <VBox/com/com.h>
     22#include <VBox/com/string.h>
     23#include <VBox/com/VirtualBox.h>
    2224
    2325#include <map>
    2426#include <vector>
     27
     28using namespace com;
    2529
    2630#ifdef VBOX_WITH_GUEST_CONTROL
     
    5155public:
    5256
    53     void Destroy();
     57    int AddData(const BYTE *pbData, size_t cbData);
    5458
    5559    void ClearPairs();
    5660
    57     const char* GetString(const char *pszKey);
    58 
    59     int GetUInt32Ex(const char *pszKey, uint32_t *puVal);
    60 
    61     uint32_t GetUInt32(const char *pszKey);
     61    void Destroy();
    6262
    6363    int GetInt64Ex(const char *pszKey, int64_t *piVal);
     
    6767    size_t GetNumPairs();
    6868
    69     int AddData(const BYTE *pbData, size_t cbData);
     69    uint32_t GetOffset();
     70
     71    const char* GetString(const char *pszKey);
     72
     73    int GetUInt32Ex(const char *pszKey, uint32_t *puVal);
     74
     75    uint32_t GetUInt32(const char *pszKey);
    7076
    7177    int Parse();
  • trunk/src/VBox/Main/src-client/GuestCtrlIO.cpp

    r38182 r38210  
    4444}
    4545
    46 /**
    47  * Destroys the stored stream pairs.
    48  */
    49 void GuestProcessStream::Destroy()
    50 {
    51     ClearPairs();
    52 
    53     if (m_pbBuffer)
    54         RTMemFree(m_pbBuffer);
    55 }
    56 
    57 void GuestProcessStream::ClearPairs()
    58 {
    59     for (GuestCtrlStreamPairsIter it = m_mapPairs.begin(); it != m_mapPairs.end(); it++)
    60     {
    61         if (it->second.pszValue)
    62             RTMemFree(it->second.pszValue);
    63     }
    64 
    65     m_mapPairs.clear();
    66 }
    67 
    68 /**
    69  * Returns a 32-bit unsigned integer of a specified key.
    70  *
    71  * @return  uint32_t            Value to return, 0 if not found / on failure.
    72  * @param   pszKey              Name of key to get the value for.
    73  */
    74 const char* GuestProcessStream::GetString(const char *pszKey)
    75 {
    76     AssertPtrReturn(pszKey, NULL);
    77 
    78     try
    79     {
    80         GuestCtrlStreamPairsIterConst itPairs = m_mapPairs.find(Utf8Str(pszKey));
    81         if (itPairs != m_mapPairs.end())
    82             return itPairs->second.pszValue;
    83     }
    84     catch (const std::exception &ex)
    85     {
    86         NOREF(ex);
    87     }
    88     return NULL;
    89 }
    90 
    91 int GuestProcessStream::GetUInt32Ex(const char *pszKey, uint32_t *puVal)
    92 {
    93     AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
    94     AssertPtrReturn(puVal, VERR_INVALID_POINTER);
    95     const char *pszValue = GetString(pszKey);
    96     if (pszValue)
    97     {
    98         *puVal = RTStrToUInt32(pszValue);
    99         return VINF_SUCCESS;
    100     }
    101     return VERR_NOT_FOUND;
    102 }
    103 
    104 /**
    105  * Returns a 32-bit unsigned integer of a specified key.
    106  *
    107  * @return  uint32_t            Value to return, 0 if not found / on failure.
    108  * @param   pszKey              Name of key to get the value for.
    109  */
    110 uint32_t GuestProcessStream::GetUInt32(const char *pszKey)
    111 {
    112     uint32_t uVal;
    113     if (RT_SUCCESS(GetUInt32Ex(pszKey, &uVal)))
    114         return uVal;
    115     return 0;
    116 }
    117 
    118 int GuestProcessStream::GetInt64Ex(const char *pszKey, int64_t *piVal)
    119 {
    120     AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
    121     AssertPtrReturn(piVal, VERR_INVALID_POINTER);
    122     const char *pszValue = GetString(pszKey);
    123     if (pszValue)
    124     {
    125         *piVal = RTStrToInt64(pszValue);
    126         return VINF_SUCCESS;
    127     }
    128     return VERR_NOT_FOUND;
    129 }
    130 
    131 /**
    132  * Returns a 64-bit integer of a specified key.
    133  *
    134  * @return  int64_t             Value to return, 0 if not found / on failure.
    135  * @param   pszKey              Name of key to get the value for.
    136  */
    137 int64_t GuestProcessStream::GetInt64(const char *pszKey)
    138 {
    139     int64_t iVal;
    140     if (RT_SUCCESS(GetInt64Ex(pszKey, &iVal)))
    141         return iVal;
    142     return 0;
    143 }
    144 
    145 /**
    146  * Returns the current number of stream pairs.
    147  *
    148  * @return  uint32_t            Current number of stream pairs.
    149  */
    150 size_t GuestProcessStream::GetNumPairs()
    151 {
    152     return m_mapPairs.size();
    153 }
    15446
    15547int GuestProcessStream::AddData(const BYTE *pbData, size_t cbData)
     
    214106
    215107    return rc;
     108}
     109
     110void GuestProcessStream::ClearPairs()
     111{
     112    for (GuestCtrlStreamPairsIter it = m_mapPairs.begin(); it != m_mapPairs.end(); it++)
     113    {
     114        if (it->second.pszValue)
     115            RTMemFree(it->second.pszValue);
     116    }
     117
     118    m_mapPairs.clear();
     119}
     120
     121/**
     122 * Destroys the stored stream pairs.
     123 */
     124void GuestProcessStream::Destroy()
     125{
     126    ClearPairs();
     127
     128    if (m_pbBuffer)
     129        RTMemFree(m_pbBuffer);
     130}
     131
     132int GuestProcessStream::GetInt64Ex(const char *pszKey, int64_t *piVal)
     133{
     134    AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
     135    AssertPtrReturn(piVal, VERR_INVALID_POINTER);
     136    const char *pszValue = GetString(pszKey);
     137    if (pszValue)
     138    {
     139        *piVal = RTStrToInt64(pszValue);
     140        return VINF_SUCCESS;
     141    }
     142    return VERR_NOT_FOUND;
     143}
     144
     145/**
     146 * Returns a 64-bit integer of a specified key.
     147 *
     148 * @return  int64_t             Value to return, 0 if not found / on failure.
     149 * @param   pszKey              Name of key to get the value for.
     150 */
     151int64_t GuestProcessStream::GetInt64(const char *pszKey)
     152{
     153    int64_t iVal;
     154    if (RT_SUCCESS(GetInt64Ex(pszKey, &iVal)))
     155        return iVal;
     156    return 0;
     157}
     158
     159/**
     160 * Returns the current number of stream pairs.
     161 *
     162 * @return  uint32_t            Current number of stream pairs.
     163 */
     164size_t GuestProcessStream::GetNumPairs()
     165{
     166    return m_mapPairs.size();
     167}
     168
     169uint32_t GuestProcessStream::GetOffset()
     170{
     171    return m_cbOffset;
     172}
     173
     174/**
     175 * Returns a 32-bit unsigned integer of a specified key.
     176 *
     177 * @return  uint32_t            Value to return, 0 if not found / on failure.
     178 * @param   pszKey              Name of key to get the value for.
     179 */
     180const char* GuestProcessStream::GetString(const char *pszKey)
     181{
     182    AssertPtrReturn(pszKey, NULL);
     183
     184    try
     185    {
     186        GuestCtrlStreamPairsIterConst itPairs = m_mapPairs.find(Utf8Str(pszKey));
     187        if (itPairs != m_mapPairs.end())
     188            return itPairs->second.pszValue;
     189    }
     190    catch (const std::exception &ex)
     191    {
     192        NOREF(ex);
     193    }
     194    return NULL;
     195}
     196
     197int GuestProcessStream::GetUInt32Ex(const char *pszKey, uint32_t *puVal)
     198{
     199    AssertPtrReturn(pszKey, VERR_INVALID_POINTER);
     200    AssertPtrReturn(puVal, VERR_INVALID_POINTER);
     201    const char *pszValue = GetString(pszKey);
     202    if (pszValue)
     203    {
     204        *puVal = RTStrToUInt32(pszValue);
     205        return VINF_SUCCESS;
     206    }
     207    return VERR_NOT_FOUND;
     208}
     209
     210/**
     211 * Returns a 32-bit unsigned integer of a specified key.
     212 *
     213 * @return  uint32_t            Value to return, 0 if not found / on failure.
     214 * @param   pszKey              Name of key to get the value for.
     215 */
     216uint32_t GuestProcessStream::GetUInt32(const char *pszKey)
     217{
     218    uint32_t uVal;
     219    if (RT_SUCCESS(GetUInt32Ex(pszKey, &uVal)))
     220        return uVal;
     221    return 0;
    216222}
    217223
  • trunk/src/VBox/Main/testcase/Makefile.kmk

    r37883 r38210  
    2323#
    2424ifndef VBOX_ONLY_SDK
    25  if defined(VBOX_WITH_TESTCASES) || "$(USERNAME)" == "umoeller"
     25 if defined(VBOX_WITH_TESTCASES)
    2626  PROGRAMS       += \
    2727        tstAPI \
     
    6969endif
    7070
     71
    7172#
    7273# tstOVF
     
    9798        ovf-winxp-vbox-sharedfolders/winxp.ovf=>ovf-winxp-vbox-sharedfolders/winxp.ovf
    9899endif
     100
    99101
    100102#
     
    144146tstCollector_LDFLAGS.win     += psapi.lib powrprof.lib
    145147
     148
    146149#
    147150# tstGuestCtrlParseBuffer
    148151#
    149 tstGuestCtrlParseBuffer_TEMPLATE = VBOXR3TSTNPEXE
    150 tstGuestCtrlParseBuffer_SOURCES = tstGuestCtrlParseBuffer.cpp
     152tstGuestCtrlParseBuffer_TEMPLATE = VBOXMAINCLIENTEXE
     153tstGuestCtrlParseBuffer_SOURCES  = \
     154        tstGuestCtrlParseBuffer.cpp \
     155        ../src-client/GuestCtrlIO.cpp
     156tstGuestCtrlParseBuffer_INCS     = ../include
     157ifeq ($(KBUILD_TARGET),win) ## @todo just add this to the template.
     158 tstGuestCtrlParseBuffer_DEPS    = $(VBOX_PATH_SDK)/bindings/mscom/include/VirtualBox.h
     159else
     160 tstGuestCtrlParseBuffer_DEPS    = $(VBOX_PATH_SDK)/bindings/xpcom/include/VirtualBox_XPCOM.h
     161endif
     162
    151163
    152164#
  • trunk/src/VBox/Main/testcase/tstGuestCtrlParseBuffer.cpp

    r38085 r38210  
    1818 */
    1919
    20 #include <map>
    21 
    22 #include <iprt/string.h>
    23 #include <iprt/cpp/ministring.h>
     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>
    2431
    2532#include <iprt/test.h>
     
    2936# define BYTE uint8_t
    3037#endif
    31 
    32 /** @todo Use original source of GuestCtrlImpl.cpp! */
    3338
    3439typedef struct VBOXGUESTCTRL_BUFFER_VALUE
     
    101106};
    102107
    103 int outputBufferParse(const BYTE *pbData, size_t cbData, uint32_t *puOffset, GuestBufferMap& mapBuf)
    104 {
    105     AssertPtrReturn(pbData, VERR_INVALID_POINTER);
    106     AssertReturn(cbData, VERR_INVALID_PARAMETER);
    107     AssertPtrReturn(puOffset, VERR_INVALID_POINTER);
    108     AssertReturn(*puOffset < cbData, VERR_INVALID_PARAMETER);
    109 
    110     int rc = VINF_SUCCESS;
    111 
    112     size_t uCur = *puOffset;
    113     for (;uCur < cbData;)
    114     {
    115         const char *pszStart = (char*)&pbData[uCur];
    116         const char *pszEnd = pszStart;
    117 
    118         /* Search end of current pair (key=value\0). */
    119         while (uCur++ < cbData)
    120         {
    121             if (*pszEnd == '\0')
    122                 break;
    123             pszEnd++;
    124         }
    125 
    126         size_t uPairLen = pszEnd - pszStart;
    127         if (uPairLen)
    128         {
    129             const char *pszSep = pszStart;
    130             while (   *pszSep != '='
    131                    &&  pszSep != pszEnd)
    132             {
    133                 pszSep++;
    134             }
    135 
    136             /* No separator found (or incomplete key=value pair)? */
    137             if (   pszSep == pszStart
    138                 || pszSep == pszEnd)
    139             {
    140                 *puOffset =  uCur - uPairLen - 1;
    141                 rc = VERR_MORE_DATA;
    142             }
    143 
    144             if (RT_FAILURE(rc))
    145                 break;
    146 
    147             size_t uKeyLen = pszSep - pszStart;
    148             size_t uValLen = pszEnd - (pszSep + 1);
    149 
    150             /* Get key (if present). */
    151             if (uKeyLen)
    152             {
    153                 Assert(pszSep > pszStart);
    154                 char *pszKey = (char*)RTMemAllocZ(uKeyLen + 1);
    155                 if (!pszKey)
    156                 {
    157                     rc = VERR_NO_MEMORY;
    158                     break;
    159                 }
    160                 memcpy(pszKey, pszStart, uKeyLen);
    161 
    162                 mapBuf[RTCString(pszKey)].pszValue = NULL;
    163 
    164                 /* Get value (if present). */
    165                 if (uValLen)
    166                 {
    167                     Assert(pszEnd > pszSep);
    168                     char *pszVal = (char*)RTMemAllocZ(uValLen + 1);
    169                     if (!pszVal)
    170                     {
    171                         rc = VERR_NO_MEMORY;
    172                         break;
    173                     }
    174                     memcpy(pszVal, pszSep + 1, uValLen);
    175 
    176                     mapBuf[RTCString(pszKey)].pszValue = pszVal;
    177                 }
    178 
    179                 RTMemFree(pszKey);
    180 
    181                 *puOffset += uCur - *puOffset;
    182             }
    183         }
    184         else /* No pair detected, check for a new block. */
    185         {
    186             do
    187             {
    188                 if (*pszEnd == '\0')
    189                 {
    190                     *puOffset = uCur;
    191                     rc = VERR_MORE_DATA;
    192                     break;
    193                 }
    194                 pszEnd++;
    195             } while (++uCur < cbData);
    196         }
    197 
    198         if (RT_FAILURE(rc))
    199             break;
    200     }
    201 
    202     RT_CLAMP(*puOffset, 0, cbData);
    203 
    204     return rc;
    205 }
    206 
    207 void tstOutputAndDestroyMap(GuestBufferMap &bufMap)
    208 {
    209     for (GuestBufferMapIter it = bufMap.begin(); it != bufMap.end(); it++)
    210     {
    211         RTTestIPrintf(RTTESTLVL_DEBUG, "\t%s -> %s\n",
    212                       it->first.c_str(), it->second.pszValue ? it->second.pszValue : "<undefined>");
    213 
    214         if (it->second.pszValue)
    215             RTMemFree(it->second.pszValue);
    216     }
    217 
    218     bufMap.clear();
    219 }
    220 
    221108int main()
    222109{
     
    226113        return rc;
    227114    RTTestBanner(hTest);
     115
     116    RTPrintf("Initializing COM...\n");
     117    rc = com::Initialize();
     118    if (FAILED(rc))
     119    {
     120        RTPrintf("ERROR: failed to initialize COM!\n");
     121        return rc;
     122    }
    228123
    229124    RTTestIPrintf(RTTESTLVL_INFO, "Doing basic tests ...\n");
     
    241136    for (iTest; iTest < RT_ELEMENTS(aTests); iTest++)
    242137    {
    243         GuestBufferMap bufMap;
    244138        uint32_t uOffset = aTests[iTest].uOffsetStart;
    245139
    246         int iResult = outputBufferParse((BYTE*)aTests[iTest].pbData, aTests[iTest].cbData,
    247                                         &uOffset, bufMap);
    248 
    249140        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_FAILURE(iResult))
     145        {
     146            RTTestFailed(hTest, "\tAdding data returned %Rrc, expected VINF_SUCCESS",
     147                         iResult);
     148            continue;
     149        }
     150
     151        iResult = stream.Parse();
    250152
    251153        if (iResult != aTests[iTest].iResult)
     
    254156                         iResult, aTests[iTest].iResult);
    255157        }
    256         else if (bufMap.size() != aTests[iTest].uMapElements)
     158        else if (stream.GetNumPairs() != aTests[iTest].uMapElements)
    257159        {
    258160            RTTestFailed(hTest, "\tMap has %u elements, expected %u",
    259                          bufMap.size(), aTests[iTest].uMapElements);
    260         }
    261         else if (uOffset != aTests[iTest].uOffsetAfter)
     161                         stream.GetNumPairs(), aTests[iTest].uMapElements);
     162        }
     163        else if (stream.GetOffset() != aTests[iTest].uOffsetAfter)
    262164        {
    263165            RTTestFailed(hTest, "\tOffset %u wrong, expected %u",
     
    279181            }
    280182        }
    281 
    282         tstOutputAndDestroyMap(bufMap);
    283183    }
    284184
     
    289189        RTTestIPrintf(RTTESTLVL_DEBUG, "=> Block test #%u\n", iTest);
    290190
    291         int iResult;
    292 
    293         GuestBufferMap bufMap;
    294         uint32_t uOffset = 0;
    295         uint32_t uNumBlocks = 0;
    296 
    297         while (uOffset < aTests2[iTest].cbData - 1)
    298         {
    299             iResult = outputBufferParse((BYTE*)aTests2[iTest].pbData, aTests2[iTest].cbData,
    300                                         &uOffset, bufMap);
    301             RTTestIPrintf(RTTESTLVL_DEBUG, "\tReturned with %Rrc\n", iResult);
    302             if (   iResult == VINF_SUCCESS
    303                 || iResult == VERR_MORE_DATA)
    304             {
    305                 if (bufMap.size()) /* Only count block which have some valid data. */
     191        GuestProcessStream stream;
     192        int iResult = stream.AddData((BYTE*)aTests2[iTest].pbData, aTests2[iTest].cbData);
     193        if (RT_SUCCESS(iResult))
     194        {
     195            uint32_t uNumBlocks = 0;
     196
     197            do
     198            {
     199                iResult = stream.Parse();
     200                if (iResult == VERR_MORE_DATA)
    306201                    uNumBlocks++;
    307 
    308                 tstOutputAndDestroyMap(bufMap);
    309 
    310                 RTTestIPrintf(RTTESTLVL_DEBUG, "\tNext offset %u (total: %u)\n",
    311                               uOffset, aTests2[iTest].cbData);
     202                if (uNumBlocks > 32)
     203                    break; /* Give up if unreasonable big. */
     204            } while (iResult == VERR_MORE_DATA);
     205
     206            if (iResult != aTests2[iTest].iResult)
     207            {
     208                RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc",
     209                             iResult, aTests2[iTest].iResult);
    312210            }
    313             else
    314                 break;
    315 
    316             if (uNumBlocks > 32)
    317                 break; /* Give up if unreasonable big. */
    318         }
    319 
    320         if (iResult != aTests2[iTest].iResult)
    321         {
    322             RTTestFailed(hTest, "\tReturned %Rrc, expected %Rrc",
    323                          iResult, aTests2[iTest].iResult);
    324         }
    325         else if (uNumBlocks != aTests2[iTest].uNumBlocks)
    326         {
    327             RTTestFailed(hTest, "\tReturned %u blocks, expected %u\n",
    328                          uNumBlocks, aTests2[iTest].uNumBlocks);
    329         }
     211            else if (uNumBlocks != aTests2[iTest].uNumBlocks)
     212            {
     213                RTTestFailed(hTest, "\tReturned %u blocks, expected %u\n",
     214                             uNumBlocks, aTests2[iTest].uNumBlocks);
     215            }
     216        }
     217        else
     218            RTTestFailed(hTest, "\tAdding data failed with %Rrc", iResult);
    330219    }
     220
     221    RTPrintf("Shutting down COM...\n");
     222    com::Shutdown();
    331223
    332224    /*
Note: See TracChangeset for help on using the changeset viewer.

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