VirtualBox

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


Ignore:
Timestamp:
Feb 10, 2012 9:35:27 PM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
76205
Message:

Runtime/strings: add Utf-8 and Utf-16 sanitising to a white list of characters.

File:
1 edited

Legend:

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

    r40065 r40071  
    836836
    837837    RTTestSubDone(hTest);
     838}
     839
     840
     841/**
     842 * Check string sanitising.
     843 */
     844void TstRTStrPurgeComplementSet(RTTEST hTest)
     845{
     846    RTTestSub(hTest, "RTStrPurgeComplementSet");
     847    RTUNICP aCpSet[] = { '1', '2', '3', '4', '5', 'w', 'r', 'f', 't', 'e',
     848                         '\0' };
     849    struct
     850    {
     851        const char *pcszIn;
     852        const char *pcszOut;
     853        PCRTUNICP   pcCpSet;
     854        char        chReplacement;
     855        ssize_t     cExpected;
     856    }
     857    aTests[] =
     858    {
     859        { "1234werttrew4321", "1234werttrew4321", aCpSet, '_', 0 },
     860        { "123654wert\xc2\xa2trew\xe2\x82\xac""4321",
     861          "123_54wert__trew___4321", aCpSet, '_', 3 },
     862        { "hjhj8766", "????????", aCpSet, '?', 8 },
     863        { "123\xf0\xa4\xad\xa2""4", "123____4", aCpSet, '_', 1 },
     864        { "\xff", "\xff", aCpSet, '_', -1 }
     865    };
     866    enum { MAX_IN_STRING = 256 };
     867
     868    for (unsigned i = 0; i < RT_ELEMENTS(aTests); ++i)
     869    {
     870        char szCopy[MAX_IN_STRING];
     871        ssize_t cReplacements;
     872        AssertRC(RTStrCopy(szCopy, RT_ELEMENTS(szCopy), aTests[i].pcszIn));
     873        cReplacements = RTStrPurgeComplementSet(szCopy, aTests[i].pcCpSet,
     874                                                aTests[i].chReplacement);
     875        if (cReplacements != aTests[i].cExpected)
     876            RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
     877                         (long long) aTests[i].cExpected,
     878                         (long long) cReplacements);
     879        if (strcmp(aTests[i].pcszOut, szCopy))
     880            RTTestFailed(hTest, "#%u: expected %s, actual %s\n", i,
     881                         aTests[i].pcszOut, szCopy);
     882    }
     883}
     884
     885
     886/**
     887 * Check string sanitising.
     888 */
     889void TstRTUtf16PurgeComplementSet(RTTEST hTest)
     890{
     891    RTTestSub(hTest, "RTUtf16PurgeComplementSet");
     892    RTUNICP aCpSet[] = { '1', '2', '3', '4', '5', 'w', 'r', 'f', 't', 'e',
     893                         '\0' };
     894    struct
     895    {
     896        const char *pcszIn;
     897        const char *pcszOut;
     898        PCRTUNICP   pcCpSet;
     899        char        chReplacement;
     900        ssize_t     cExpected;
     901    }
     902    aTests[] =
     903    {
     904        { "1234werttrew4321", "1234werttrew4321", aCpSet, '_', 0 },
     905        { "123654wert\xc2\xa2trew\xe2\x82\xac""4321",
     906          "123_54wert_trew_4321", aCpSet, '_', 3 },
     907        { "hjhj8766", "????????", aCpSet, '?', 8 },
     908        { "123\xf0\xa4\xad\xa2""4", "123__4", aCpSet, '_', 1 }
     909        /* No test for invalid encoding. */
     910    };
     911    enum { MAX_IN_STRING = 256 };
     912
     913    for (unsigned i = 0; i < RT_ELEMENTS(aTests); ++i)
     914    {
     915        RTUTF16 wszInCopy[MAX_IN_STRING],  *pwszInCopy  = wszInCopy;
     916        RTUTF16 wszOutCopy[MAX_IN_STRING], *pwszOutCopy = wszOutCopy;
     917        ssize_t cReplacements;
     918        AssertRC(RTStrToUtf16Ex(aTests[i].pcszIn, RTSTR_MAX, &pwszInCopy,
     919                                RT_ELEMENTS(wszInCopy), NULL));
     920        AssertRC(RTStrToUtf16Ex(aTests[i].pcszOut, RTSTR_MAX, &pwszOutCopy,
     921                                RT_ELEMENTS(wszOutCopy), NULL));
     922        cReplacements = RTUtf16PurgeComplementSet(wszInCopy, aTests[i].pcCpSet,
     923                                                  aTests[i].chReplacement);
     924        if (cReplacements != aTests[i].cExpected)
     925            RTTestFailed(hTest, "#%u: expected %lld, actual %lld\n", i,
     926                         (long long) aTests[i].cExpected,
     927                         (long long) cReplacements);
     928        if (RTUtf16Cmp(wszInCopy, wszOutCopy))
     929            RTTestFailed(hTest, "#%u: expected %ls, actual %ls\n", i,
     930                         wszOutCopy, wszInCopy);
     931    }
    838932}
    839933
     
    14031497    TstRTStrXCmp(hTest);
    14041498    TstRTStrPurgeEncoding(hTest);
     1499    TstRTStrPurgeComplementSet(hTest);
     1500    TstRTUtf16PurgeComplementSet(hTest);
    14051501    testStrEnd(hTest);
    14061502    testStrStr(hTest);
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