VirtualBox

Ignore:
Timestamp:
Apr 28, 2015 11:54:11 PM (10 years ago)
Author:
vboxsync
Message:

tstRTGetOptArgv.cpp: Use CommandLineToArgvW to verify the RTGetOptArgvToString/MS_CRT output when we're on windows.

File:
1 edited

Legend:

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

    r55494 r55499  
    3333#include <iprt/param.h>
    3434#include <iprt/getopt.h>
     35#include <iprt/ldr.h>
    3536#include <iprt/string.h>
    3637#include <iprt/test.h>
     
    173174        "arg1 \"arg2=\\\"zyx\\\"\" arg3=\\\\\\"
    174175    },
     176    {
     177        " a\\\\\\\\b  d\"e f\"g h ij\t",
     178        NULL,
     179        4,
     180        {
     181            "a\\\\b",
     182            "de fg",
     183            "h",
     184            "ij",
     185            NULL, NULL, NULL, NULL,
     186            NULL, NULL, NULL, NULL,  NULL, NULL, NULL, NULL,
     187        },
     188        "'a\\\\b' 'de fg' h ij",
     189        "a\\\\b \"de fg\" h ij",
     190    }
    175191};
     192
     193
     194
     195static void tstCheckNativeMsCrtToArgv(const char *pszCmdLine, int cExpectedArgs, const char * const *papszExpectedArgs)
     196{
     197#ifdef RT_OS_WINDOWS
     198    /*
     199     * Resolve APIs.
     200     */
     201    static void     *(__stdcall * s_pfnLocalFree)(void *pvFree);
     202    static PRTUTF16 *(__stdcall * s_pfnCommandLineToArgvW)(PCRTUTF16 pwszCmdLine, int *pcArgs);
     203    if (!s_pfnCommandLineToArgvW)
     204    {
     205        *(void **)&s_pfnLocalFree = RTLdrGetSystemSymbol("kernel32.dll", "LocalFree");
     206        RTTESTI_CHECK_RETV(s_pfnLocalFree != NULL);
     207        *(void **)&s_pfnCommandLineToArgvW = RTLdrGetSystemSymbol("shell32.dll", "CommandLineToArgvW");
     208        RTTESTI_CHECK_RETV(s_pfnCommandLineToArgvW != NULL);
     209    }
     210
     211    /*
     212     * Calc expected arguments if needed.
     213     */
     214    if (cExpectedArgs == -1)
     215        for (cExpectedArgs = 0; papszExpectedArgs[cExpectedArgs]; cExpectedArgs++)
     216        { /* nothing */ }
     217
     218    /*
     219     * Convert input command line to UTF-16 and call native API.
     220     */
     221    RTUTF16 wszCmdLine[1024];
     222    PRTUTF16 pwszCmdLine = &wszCmdLine[1];
     223    RTTESTI_CHECK_RC_RETV(RTStrToUtf16Ex(pszCmdLine, RTSTR_MAX, &pwszCmdLine, 1023, NULL), VINF_SUCCESS);
     224    wszCmdLine[0] = ' ';
     225
     226    int cArgs = -2;
     227    PRTUTF16 *papwszArgs = s_pfnCommandLineToArgvW(wszCmdLine, &cArgs);
     228
     229    /*
     230     * Check the result.
     231     */
     232    if (cArgs - 1 != cExpectedArgs)
     233        RTTestIFailed("Native returns cArgs=%d, expected %d (cmdline=|%s|)", cArgs - 1, cExpectedArgs, pszCmdLine);
     234    int cArgsCheck = RT_MIN(cArgs - 1, cExpectedArgs);
     235    for (int i = 0; i < cArgsCheck; i++)
     236    {
     237        char *pszArg = NULL;
     238        RTTESTI_CHECK_RC_RETV(RTUtf16ToUtf8(papwszArgs[i + 1], &pszArg), VINF_SUCCESS);
     239        if (strcmp(pszArg, papszExpectedArgs[i]))
     240            RTTestIFailed("Native returns argv[%i]='%s', expected '%s' (cmdline=|%s|)",
     241                          i, pszArg, papszExpectedArgs[i], pszCmdLine);
     242        RTStrFree(pszArg);
     243    }
     244
     245    if (papwszArgs)
     246        s_pfnLocalFree(papwszArgs);
     247#else
     248    NOREF(pszCmdLine);
     249    NOREF(cExpectedArgs);
     250    NOREF(papszExpectedArgs);
     251#endif
     252}
     253
    176254
    177255
     
    194272            for (int iArg = 0; iArg < cArgs1; iArg++)
    195273                if (strcmp(papszArgs1[iArg], g_aBourneTests[i].apszArgs[iArg]) != 0)
    196                     RTTestIFailed("g_aBourneTests[%i]/#1: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s', '%s'))",
     274                    RTTestIFailed("g_aBourneTests[%i]/1: argv[%i] differs: got '%s', expected '%s' (RTGetOptArgvFromString(,,'%s', '%s'))",
    197275                                  i, iArg, papszArgs1[iArg], g_aBourneTests[i].apszArgs[iArg],
    198276                                  g_aBourneTests[i].pszInput, g_aBourneTests[i].pszSeparators);
     
    287365        int rc = RTGetOptArgvToString(&pszCmdLine, s_aMscCrtTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
    288366        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
    289         if (strcmp(s_aMscCrtTests[i].pszCmdLine, pszCmdLine))
     367        if (!strcmp(s_aMscCrtTests[i].pszCmdLine, pszCmdLine))
     368            tstCheckNativeMsCrtToArgv(pszCmdLine, -1, s_aMscCrtTests[i].apszArgs);
     369        else
    290370            RTTestIFailed("g_aTest[%i] failed:\n"
    291371                          " got      '%s'\n"
     
    300380        int rc = RTGetOptArgvToString(&pszCmdLine, g_aBourneTests[i].apszArgs, RTGETOPTARGV_CNV_QUOTE_MS_CRT);
    301381        RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
    302         if (strcmp(g_aBourneTests[i].pszOutMsCrt, pszCmdLine))
     382        if (!strcmp(g_aBourneTests[i].pszOutMsCrt, pszCmdLine))
     383            tstCheckNativeMsCrtToArgv(pszCmdLine, g_aBourneTests[i].cArgs, g_aBourneTests[i].apszArgs);
     384        else
    303385            RTTestIFailed("g_aBourneTests[%i] failed:\n"
    304386                          " got      |%s|\n"
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