VirtualBox

Ignore:
Timestamp:
Mar 23, 2011 5:00:53 PM (14 years ago)
Author:
vboxsync
Message:

tstGuestPropSvc.cpp: Used RTTest so we can allocate a guarded buffer and crash when MAX_FLAGS_LEN is wrong. Added some new tests to max out the buffer and such.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostServices/GuestProperties/testcase/tstGuestPropSvc.cpp

    r36259 r36375  
    2323#include <iprt/initterm.h>
    2424#include <iprt/stream.h>
     25#include <iprt/test.h>
     26
     27/*******************************************************************************
     28*   Global Variables                                                           *
     29*******************************************************************************/
     30static RTTEST g_hTest = NIL_RTTEST;
    2531
    2632using namespace guestProp;
     
    6672    const char *pcszOut;
    6773}
    68 validFlagStrings[] =
    69 {
    70     { "  ", "" },
    71     { "transient, ", "TRANSIENT" },
    72     { "  rdOnLyHOST, transIENT  ,     READONLY    ", "TRANSIENT, READONLY" },
    73     { " rdonlyguest", "RDONLYGUEST" },
    74     { "rdonlyhost     ", "RDONLYHOST" },
    75     { "transient, transreset, rdonlyhost",
    76        "TRANSIENT, RDONLYHOST, TRANSRESET" }
     74g_validFlagStrings[] =
     75{
     76    /* pcszIn,                                          pcszOut */
     77    { "  ",                                             "" },
     78    { "transient, ",                                    "TRANSIENT" },
     79    { "  rdOnLyHOST, transIENT  ,     READONLY    ",    "TRANSIENT, READONLY" },
     80    { " rdonlyguest",                                   "RDONLYGUEST" },
     81    { "rdonlyhost     ",                                "RDONLYHOST" },
     82    { "transient, transreset, rdonlyhost",              "TRANSIENT, RDONLYHOST, TRANSRESET" },
     83    { "transient, transreset, rdonlyguest",             "TRANSIENT, RDONLYGUEST, TRANSRESET" },
     84    { "rdonlyguest, rdonlyhost",                        "READONLY" }
    7785};
    7886
     
    8189 * functions should reject these.
    8290 */
    83 const char *invalidFlagStrings[] =
     91const char *g_invalidFlagStrings[] =
    8492{
    8593    "RDONLYHOST,,",
     
    95103{
    96104    int rc = VINF_SUCCESS;
     105    char *pszFlagBuffer = (char *)RTTestGuardedAllocTail(g_hTest, MAX_FLAGS_LEN);
     106
    97107    RTPrintf("tstGuestPropSvc: Testing conversion of valid flags strings.\n");
    98     for (unsigned i = 0; i < RT_ELEMENTS(validFlagStrings) && RT_SUCCESS(rc); ++i)
    99     {
    100         char szFlagBuffer[MAX_FLAGS_LEN * 2];
     108    for (unsigned i = 0; i < RT_ELEMENTS(g_validFlagStrings) && RT_SUCCESS(rc); ++i)
     109    {
    101110        uint32_t fFlags;
    102         rc = validateFlags(validFlagStrings[i].pcszIn, &fFlags);
     111        rc = validateFlags(g_validFlagStrings[i].pcszIn, &fFlags);
    103112        if (RT_FAILURE(rc))
    104             RTPrintf("tstGuestPropSvc: FAILURE - Failed to validate flag string '%s'.\n", validFlagStrings[i].pcszIn);
     113            RTPrintf("tstGuestPropSvc: FAILURE - Failed to validate flag string '%s'.\n", g_validFlagStrings[i].pcszIn);
    105114        if (RT_SUCCESS(rc))
    106115        {
    107             rc = writeFlags(fFlags, szFlagBuffer);
     116            rc = writeFlags(fFlags, pszFlagBuffer);
    108117            if (RT_FAILURE(rc))
    109118                RTPrintf("tstGuestPropSvc: FAILURE - Failed to convert flag string '%s' back to a string.\n",
    110                             validFlagStrings[i].pcszIn);
    111         }
    112         if (RT_SUCCESS(rc) && (strlen(szFlagBuffer) > MAX_FLAGS_LEN - 1))
     119                         g_validFlagStrings[i].pcszIn);
     120        }
     121        if (RT_SUCCESS(rc) && (strlen(pszFlagBuffer) > MAX_FLAGS_LEN - 1))
    113122        {
    114123            RTPrintf("tstGuestPropSvc: FAILURE - String '%s' converts back to a flag string which is too long.\n",
    115                         validFlagStrings[i].pcszIn);
     124                     g_validFlagStrings[i].pcszIn);
    116125            rc = VERR_TOO_MUCH_DATA;
    117126        }
    118         if (RT_SUCCESS(rc) && (strcmp(szFlagBuffer, validFlagStrings[i].pcszOut) != 0))
     127        if (RT_SUCCESS(rc) && (strcmp(pszFlagBuffer, g_validFlagStrings[i].pcszOut) != 0))
    119128        {
    120129            RTPrintf("tstGuestPropSvc: FAILURE - String '%s' converts back to '%s' instead of to '%s'\n",
    121                         validFlagStrings[i].pcszIn, szFlagBuffer,
    122                         validFlagStrings[i].pcszOut);
     130                     g_validFlagStrings[i].pcszIn, pszFlagBuffer,
     131                     g_validFlagStrings[i].pcszOut);
    123132            rc = VERR_PARSE_ERROR;
    124133        }
     
    127136    {
    128137        RTPrintf("Testing rejection of invalid flags strings.\n");
    129         for (unsigned i = 0; i < RT_ELEMENTS(invalidFlagStrings) && RT_SUCCESS(rc); ++i)
     138        for (unsigned i = 0; i < RT_ELEMENTS(g_invalidFlagStrings) && RT_SUCCESS(rc); ++i)
    130139        {
    131140            uint32_t fFlags;
    132141            /* This is required to fail. */
    133             if (RT_SUCCESS(validateFlags(invalidFlagStrings[i], &fFlags)))
     142            if (RT_SUCCESS(validateFlags(g_invalidFlagStrings[i], &fFlags)))
    134143            {
    135144                RTPrintf("String '%s' was incorrectly accepted as a valid flag string.\n",
    136                          invalidFlagStrings[i]);
     145                         g_invalidFlagStrings[i]);
    137146                rc = VERR_PARSE_ERROR;
    138147            }
     
    141150    if (RT_SUCCESS(rc))
    142151    {
    143         char szFlagBuffer[MAX_FLAGS_LEN * 2];
    144152        uint32_t u32BadFlags = ALLFLAGS << 1;
    145153        RTPrintf("Testing rejection of an invalid flags field.\n");
    146154        /* This is required to fail. */
    147         if (RT_SUCCESS(writeFlags(u32BadFlags, szFlagBuffer)))
     155        if (RT_SUCCESS(writeFlags(u32BadFlags, pszFlagBuffer)))
    148156        {
    149157            RTPrintf("Flags 0x%x were incorrectly written out as '%.*s'\n",
    150                      u32BadFlags, MAX_FLAGS_LEN, szFlagBuffer);
     158                     u32BadFlags, MAX_FLAGS_LEN, pszFlagBuffer);
    151159            rc = VERR_PARSE_ERROR;
    152160        }
    153161    }
     162
     163    RTTestGuardedFree(g_hTest, pszFlagBuffer);
    154164    return rc;
    155165}
     
    10401050int main(int argc, char **argv)
    10411051{
    1042     VBOXHGCMSVCFNTABLE svcTable;
    1043     VBOXHGCMSVCHELPERS svcHelpers;
    1044 
     1052    VBOXHGCMSVCFNTABLE  svcTable;
     1053    VBOXHGCMSVCHELPERS  svcHelpers;
     1054    RTEXITCODE          rcExit;
     1055
     1056    rcExit  = RTTestInitAndCreate("tstGuestPropSvc", &g_hTest);
     1057    if (rcExit != RTEXITCODE_SUCCESS)
     1058        return rcExit;
     1059    RTTestBanner(g_hTest);
     1060
     1061/** @todo convert the rest of this testcase. */
    10451062    initTable(&svcTable, &svcHelpers);
    1046     RTR3Init();
     1063
    10471064    if (RT_FAILURE(testConvertFlags()))
    10481065        return 1;
     
    10811098    if (RT_FAILURE(testDelPropROGuest(&svcTable)))
    10821099        return 1;
    1083     RTPrintf("tstGuestPropSvc: SUCCEEDED.\n");
    1084     return 0;
    1085 }
     1100
     1101    return RTTestSummaryAndDestroy(g_hTest);
     1102}
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