VirtualBox

Changeset 27968 in vbox for trunk/src/VBox/Runtime/testcase


Ignore:
Timestamp:
Apr 2, 2010 8:36:35 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
59674
Message:

RTGetOptArgvToString: Implemented bourne shell style quoting.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstRTGetOptArgv.cpp

    r27797 r27968  
    4949        const char * const      apszArgs[5];
    5050        const char             *pszCmdLine;
    51     } s_aTests[] =
     51    } s_aMscCrtTests[] =
    5252    {
    5353        {
     
    6969    };
    7070
    71     for (size_t i = 0; i < RT_ELEMENTS(s_aTests); i++)
     71    for (size_t i = 0; i < RT_ELEMENTS(s_aMscCrtTests); i++)
    7272    {
    7373        char *pszCmdLine = NULL;
    74         int rc = RTGetOptArgvToString(&pszCmdLine, s_aTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
    75         RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
    76         if (strcmp(s_aTests[i].pszCmdLine, pszCmdLine))
     74        int rc = RTGetOptArgvToString(&pszCmdLine, s_aMscCrtTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
     75        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     76        if (strcmp(s_aMscCrtTests[i].pszCmdLine, pszCmdLine))
    7777            RTTestIFailed("g_aTest[%i] failed:\n"
    7878                          " got      '%s'\n"
    7979                          " expected '%s'\n",
    80                           i, pszCmdLine, s_aTests[i].pszCmdLine);
     80                          i, pszCmdLine, s_aMscCrtTests[i].pszCmdLine);
     81        RTStrFree(pszCmdLine);
     82    }
     83
     84
     85    RTTestISub("RTGetOptArgvToString / BOURNE_SH");
     86
     87    static const struct
     88    {
     89        const char * const      apszArgs[5];
     90        const char             *pszCmdLine;
     91    } s_aBournShTests[] =
     92    {
     93        {
     94            { "abcd", "a ", " b", " c ", NULL },
     95            "abcd 'a ' ' b' ' c '"
     96        },
     97        {
     98            { "a\n\\b", "de'fg", "h", "'", NULL },
     99            "'a\n\\b' 'de'\"'\"'fg' h ''\"'\"''"
     100        }
     101    };
     102
     103    for (size_t i = 0; i < RT_ELEMENTS(s_aBournShTests); i++)
     104    {
     105        char *pszCmdLine = NULL;
     106        int rc = RTGetOptArgvToString(&pszCmdLine, s_aBournShTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
     107        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     108        if (strcmp(s_aBournShTests[i].pszCmdLine, pszCmdLine))
     109            RTTestIFailed("g_aTest[%i] failed:\n"
     110                          " got      |%s|\n"
     111                          " expected |%s|\n",
     112                          i, pszCmdLine, s_aBournShTests[i].pszCmdLine);
     113        RTStrFree(pszCmdLine);
     114    }
     115
     116
     117    RTTestISub("RTGetOptArgvToString <-> RTGetOptArgvFromString");
     118
     119    for (size_t i = 0; i < RT_ELEMENTS(s_aBournShTests); i++)
     120    {
     121        char *pszCmdLine = NULL;
     122        int rc = RTGetOptArgvToString(&pszCmdLine, s_aBournShTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_BOURNE_SH);
     123        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     124
     125        char  **papszArgs;
     126        int     cArgs;
     127        rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszCmdLine, NULL);
     128        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     129
     130        size_t j = 0;
     131        while (papszArgs[j] && s_aBournShTests[i].apszArgs[j])
     132        {
     133            if (strcmp(papszArgs[j], s_aBournShTests[i].apszArgs[j]))
     134                RTTestIFailed("Test #%u, argument #%u mismatch:\n"
     135                              " FromString: |%s| (got)\n"
     136                              " ToString:   |%s| (expected)\n",
     137                              i, j, papszArgs[j], s_aBournShTests[i].apszArgs[j]);
     138
     139            /* next */
     140            j++;
     141        }
     142        RTTESTI_CHECK(papszArgs[j] == NULL);
     143        RTTESTI_CHECK(s_aBournShTests[i].apszArgs[j] == NULL);
     144
     145        RTGetOptArgvFree(papszArgs);
    81146        RTStrFree(pszCmdLine);
    82147    }
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