VirtualBox

Changeset 16765 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Feb 14, 2009 8:49:30 AM (16 years ago)
Author:
vboxsync
Message:

RTBase64Encode* testcase and adjustments.

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/common/string/base64.cpp

    r16762 r16765  
    447447 *          may contain an invalid Base64 string.
    448448 *
     449 * @param   pvData      The data to encode.
     450 * @param   cbData      The number of bytes to encode.
    449451 * @param   pszBuf      Where to put the Base64 string.
    450452 * @param   cbBuf       The size of the output buffer, including the terminator.
    451  * @param   pvData      The data to encode.
    452  * @param   cbData      The number of bytes to encode.
    453  */
    454 RTDECL(int) RTBase64Encode(char *pszBuf, size_t cbBuf, const void *pvData, size_t cbData)
     453 * @param   pcchActual  The actual number of characters returned.
     454 */
     455RTDECL(int) RTBase64Encode(const void *pvData, size_t cbData, char *pszBuf, size_t cbBuf, size_t *pcchActual)
    455456{
    456457    /** @todo implement RTBase64Encode. */
  • trunk/src/VBox/Runtime/testcase/tstBase64.cpp

    r16764 r16765  
    4141int main()
    4242{
    43     int cErrors =0;
     43    int     cErrors = 0;
     44    char    szOut[0x10000];
     45    size_t  cchOut = 0;
    4446    RTR3Init();
     47
    4548
    4649    /*
    4750     * Test 0.
    4851     */
    49     static const char s_szText0[] = "Hey";
    50     static const char s_szEnc0[]  = "SGV5";
    51     size_t cchOut0 = 0;
    52     char szOut0[sizeof(s_szText0)];
    53     int rc = RTBase64Decode(&s_szEnc0[0], szOut0, sizeof(szOut0), &cchOut0, NULL);
    54     if (RT_FAILURE(rc))
     52    static const struct
    5553    {
    56         RTPrintf("tstBase64: FAILURE - RTBase64Decode s_szEnc0 -> %Rrc\n", rc);
    57         cErrors++;
    58     }
    59     else if (cchOut0 != sizeof(s_szText0) - 1)
     54        const char *pszText;
     55        size_t      cchText;
     56        const char *pszEnc;
     57        size_t      cchEnc;
     58    } g_aTests[] =
    6059    {
    61         RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n",
    62                  cchOut0, sizeof(s_szText0) - 1);
    63         cErrors++;
    64     }
    65     else if (memcmp(szOut0, s_szText0, cchOut0))
     60#define TEST_ENTRY(szText, szEnc) { szText, sizeof(szText) - 1, szEnc, sizeof(szEnc) - 1 }
     61        TEST_ENTRY("Hey", "SGV5"),
     62        TEST_ENTRY("Base64", "QmFzZTY0"),
     63        TEST_ENTRY("Call me Ishmael.", "Q2FsbCBtZSBJc2htYWVsLg==")
     64#undef TEST_ENTRY
     65    };
     66
     67    for (unsigned i = 0; i < RT_ELEMENTS(g_aTests); i++)
    6668    {
    67         RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
    68                  (int)cchOut0, szOut0, s_szText0);
    69         cErrors++;
    70     }
     69        int rc = RTBase64Decode(g_aTests[i].pszEnc, szOut, g_aTests[i].cchText, &cchOut, NULL);
     70        if (RT_FAILURE(rc))
     71        {
     72            RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode -> %Rrc\n", i, rc);
     73            cErrors++;
     74        }
     75        else if (cchOut != g_aTests[i].cchText)
     76        {
     77            RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode returned %zu bytes, expected %zu.\n",
     78                     i, cchOut, g_aTests[i].cchText);
     79            cErrors++;
     80        }
     81        else if (memcmp(szOut, g_aTests[i].pszText, cchOut))
     82        {
     83            RTPrintf("tstBase64: FAILURE - #%u: RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
     84                     i, (int)cchOut, szOut, g_aTests[i].pszText);
     85            cErrors++;
     86        }
    7187
    72     cchOut0 = RTBase64DecodedSize(s_szEnc0, NULL);
    73     if (cchOut0 != sizeof(s_szText0) - 1)
    74     {
    75         RTPrintf("tstBase64: FAILURE - RTBase64DecodedSize returned %zu bytes, expected %zu.\n",
    76                  cchOut0, sizeof(s_szText0) - 1);
    77         cErrors++;
     88        cchOut = RTBase64DecodedSize(g_aTests[i].pszEnc, NULL);
     89        if (cchOut != g_aTests[i].cchText)
     90        {
     91            RTPrintf("tstBase64: FAILURE - #%u: RTBase64DecodedSize returned %zu bytes, expected %zu.\n",
     92                     i, cchOut, g_aTests[i].cchText);
     93            cErrors++;
     94        }
     95
     96        rc = RTBase64Encode(g_aTests[i].pszText, g_aTests[i].cchText, szOut, g_aTests[i].cchEnc + 1, &cchOut);
     97        if (RT_FAILURE(rc))
     98        {
     99            RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode -> %Rrc\n", i, rc);
     100            cErrors++;
     101        }
     102        else if (cchOut != g_aTests[i].cchEnc)
     103        {
     104            RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode returned %zu bytes, expected %zu.\n",
     105                     i, cchOut, g_aTests[i].cchEnc);
     106            cErrors++;
     107        }
     108        else if (memcmp(szOut, g_aTests[i].pszEnc, cchOut + 1))
     109        {
     110            RTPrintf("tstBase64: FAILURE - #%u: RTBase64Encode returned:\n%*s\nexpected:\n%s\n",
     111                     i, szOut, g_aTests[i].pszText);
     112            cErrors++;
     113        }
     114
     115        cchOut = RTBase64EncodedLength(g_aTests[i].cchText);
     116        if (cchOut != g_aTests[i].cchEnc)
     117        {
     118            RTPrintf("tstBase64: FAILURE - #%u: RTBase64EncodedLength returned %zu bytes, expected %zu.\n",
     119                     i, cchOut, g_aTests[i].cchEnc);
     120            cErrors++;
     121        }
     122
    78123    }
    79124
    80125    /*
    81      * Test 1.
    82      */
    83     static const char s_szText1[] = "Call me Ishmael.";
    84     static const char s_szEnc1[]  = "Q2FsbCBtZSBJc2htYWVsLg==";
    85     size_t cchOut1 = 0;
    86     char szOut1[sizeof(s_szText1)];
    87     rc = RTBase64Decode(&s_szEnc1[0], szOut1, sizeof(szOut1), &cchOut1, NULL);
    88     if (RT_FAILURE(rc))
    89     {
    90         RTPrintf("tstBase64: FAILURE - RTBase64Decode s_szEnc1 -> %Rrc\n", rc);
    91         cErrors++;
    92     }
    93     else if (cchOut1 != sizeof(s_szText1) - 1)
    94     {
    95         RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n",
    96                  cchOut1, sizeof(s_szText1) - 1);
    97         cErrors++;
    98     }
    99     else if (memcmp(szOut1, s_szText1, cchOut1))
    100     {
    101         RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
    102                  (int)cchOut1, szOut1, s_szText1);
    103         cErrors++;
    104     }
    105 
    106     cchOut1 = RTBase64DecodedSize(s_szEnc1, NULL);
    107     if (cchOut1 != sizeof(s_szText1) - 1)
    108     {
    109         RTPrintf("tstBase64: FAILURE - RTBase64DecodedSize returned %zu bytes, expected %zu.\n",
    110                  cchOut1, sizeof(s_szText1) - 1);
    111         cErrors++;
    112     }
    113 
    114     /*
    115      * Test 2.
     126     * Bigger Test.
    116127     */
    117128    static const char s_szText2[] =
     
    128139        " ZSBzaG9ydCB2ZWhlbWVuY2Ugb2YgYW55IGNhcm5hbCBwbGVhc3VyZS4=\n \n";
    129140
    130     size_t cchOut2 = 0;
    131     char szOut2[sizeof(s_szText2)];
    132     rc = RTBase64Decode(&s_szEnc2[0], szOut2, sizeof(szOut2), &cchOut2, NULL);
     141    int rc = RTBase64Decode(&s_szEnc2[0], szOut, sizeof(s_szText2), &cchOut, NULL);
    133142    if (RT_FAILURE(rc))
    134143    {
     
    136145        cErrors++;
    137146    }
    138     else if (cchOut2 != sizeof(s_szText2) - 1)
     147    else if (cchOut != sizeof(s_szText2) - 1)
    139148    {
    140149        RTPrintf("tstBase64: FAILURE - RTBase64Decode returned %zu bytes, expected %zu.\n",
    141                  cchOut2, sizeof(s_szText2) - 1);
     150                 cchOut, sizeof(s_szText2) - 1);
    142151        cErrors++;
    143152    }
    144     else if (memcmp(szOut2, s_szText2, cchOut2))
     153    else if (memcmp(szOut, s_szText2, cchOut))
    145154    {
    146155        RTPrintf("tstBase64: FAILURE - RTBase64Decode returned:\n%.*s\nexpected:\n%s\n",
    147                  (int)cchOut2, szOut2, s_szText2);
     156                 (int)cchOut, szOut, s_szText2);
    148157        cErrors++;
    149158    }
    150159
    151     cchOut2 = RTBase64DecodedSize(s_szEnc2, NULL);
    152     if (cchOut2 != sizeof(s_szText2) - 1)
     160    cchOut = RTBase64DecodedSize(s_szEnc2, NULL);
     161    if (cchOut != sizeof(s_szText2) - 1)
    153162    {
    154163        RTPrintf("tstBase64: FAILURE - RTBase64DecodedSize returned %zu bytes, expected %zu.\n",
    155                  cchOut2, sizeof(s_szText2) - 1);
     164                 cchOut, sizeof(s_szText2) - 1);
    156165        cErrors++;
    157166    }
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