VirtualBox

Changeset 85500 in vbox


Ignore:
Timestamp:
Jul 29, 2020 8:54:12 AM (4 years ago)
Author:
vboxsync
Message:

tstLdr-4: Converted to iprt/test.h style and added some seg/off/rva/linkaddr conversion tests. bugref:9801

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstLdr-4.cpp

    r85461 r85500  
    3636#include <iprt/param.h>
    3737#include <iprt/path.h>
    38 #include <iprt/initterm.h>
    3938#include <iprt/err.h>
    4039#include <iprt/string.h>
     40#include <iprt/test.h>
    4141
    4242#include <VBox/sup.h>
     
    4646*   Global Variables                                                                                                             *
    4747*********************************************************************************************************************************/
    48 static SUPGLOBALINFOPAGE g_MyGip = { SUPGLOBALINFOPAGE_MAGIC, SUPGLOBALINFOPAGE_VERSION, SUPGIPMODE_INVARIANT_TSC, 42 };
    49 static PSUPGLOBALINFOPAGE g_pMyGip = &g_MyGip;
     48static RTTEST               g_hTest;
     49static SUPGLOBALINFOPAGE    g_MyGip = { SUPGLOBALINFOPAGE_MAGIC, SUPGLOBALINFOPAGE_VERSION, SUPGIPMODE_INVARIANT_TSC, 42 };
     50static PSUPGLOBALINFOPAGE   g_pMyGip = &g_MyGip;
    5051
    5152extern "C" DECLEXPORT(int) DisasmTest1(void);
     
    5960             , *piSeg, pSeg->RVA, pSeg->cbMapped, pSeg->pszName,
    6061             pSeg->LinkAddress, pSeg->cb, pSeg->Alignment, pSeg->fProt, pSeg->offFile);
     62
     63    /*
     64     * Do some address conversion tests:
     65     */
     66    if (pSeg->cbMapped != NIL_RTLDRADDR)
     67    {
     68        /* RTLdrRvaToSegOffset: */
     69        uint32_t    iSegConv   = ~(uint32_t)42;
     70        RTLDRADDR   offSegConv = ~(RTLDRADDR)22;
     71        int rc = RTLdrRvaToSegOffset(hLdrMod, pSeg->RVA, &iSegConv, &offSegConv);
     72        if (RT_FAILURE(rc))
     73            RTTestIFailed("RTLdrRvaToSegOffset failed on Seg #%u / RVA %#RTptr: %Rrc", *piSeg, pSeg->RVA, rc);
     74        else if (iSegConv != *piSeg || offSegConv != 0)
     75                RTTestIFailed("RTLdrRvaToSegOffset on Seg #%u / RVA %#RTptr returned: iSegConv=%#x offSegConv=%RTptr, expected %#x and 0",
     76                              *piSeg, pSeg->RVA, iSegConv, offSegConv, *piSeg);
     77
     78        /* RTLdrSegOffsetToRva: */
     79        RTLDRADDR uRvaConv = ~(RTLDRADDR)22;
     80        rc = RTLdrSegOffsetToRva(hLdrMod, *piSeg, 0, &uRvaConv);
     81        if (RT_FAILURE(rc))
     82            RTTestIFailed("RTLdrSegOffsetToRva failed on Seg #%u / off 0: %Rrc", *piSeg, rc);
     83        else if (uRvaConv != pSeg->RVA)
     84            RTTestIFailed("RTLdrSegOffsetToRva on Seg #%u / off 0 returned: %RTptr, expected %RTptr", *piSeg, uRvaConv, pSeg->RVA);
     85
     86        /* RTLdrLinkAddressToRva: */
     87        uRvaConv = ~(RTLDRADDR)22;
     88        rc = RTLdrLinkAddressToRva(hLdrMod, pSeg->LinkAddress, &uRvaConv);
     89        if (RT_FAILURE(rc))
     90            RTTestIFailed("RTLdrLinkAddressToRva failed on Seg #%u / %RTptr: %Rrc", *piSeg, pSeg->LinkAddress, rc);
     91        else if (uRvaConv != pSeg->RVA)
     92            RTTestIFailed("RTLdrLinkAddressToRva on Seg #%u / %RTptr returned: %RTptr, expected %RTptr",
     93                          *piSeg, pSeg->LinkAddress, uRvaConv, pSeg->RVA);
     94
     95        /* RTLdrLinkAddressToSegOffset: */
     96        iSegConv   = ~(uint32_t)42;
     97        offSegConv = ~(RTLDRADDR)22;
     98        rc = RTLdrLinkAddressToSegOffset(hLdrMod, pSeg->LinkAddress, &iSegConv, &offSegConv);
     99        if (RT_FAILURE(rc))
     100            RTTestIFailed("RTLdrLinkAddressToSegOffset failed on Seg #%u / %#RTptr: %Rrc", *piSeg, pSeg->LinkAddress, rc);
     101        else if (iSegConv != *piSeg || offSegConv != 0)
     102                RTTestIFailed("RTLdrLinkAddressToSegOffset on Seg #%u / %#RTptr returned: iSegConv=%#x offSegConv=%RTptr, expected %#x and 0",
     103                              *piSeg, pSeg->LinkAddress, iSegConv, offSegConv, *piSeg);
     104    }
     105
    61106    *piSeg += 1;
    62107    RT_NOREF(hLdrMod);
     
    126171 * and then relocated between the two and other locations a few times.
    127172 *
    128  * @returns number of errors.
    129173 * @param   pszFilename     The file to load the mess with.
    130174 */
    131 static int testLdrOne(const char *pszFilename)
     175static void testLdrOne(const char *pszFilename)
    132176{
    133     int             cErrors = 0;
     177    RTTestSub(g_hTest, RTPathFilename(pszFilename));
     178
    134179    size_t          cbImage = 0;
    135180    struct Load
     
    156201        if (RT_FAILURE(rc))
    157202        {
    158             RTPrintf("tstLdr-4: Failed to open '%s'/%d, rc=%Rrc. aborting test.\n", pszFilename, i, rc);
     203            RTTestIFailed("tstLdr-4: Failed to open '%s'/%d, rc=%Rrc. aborting test.", pszFilename, i, rc);
    159204            Assert(aLoads[i].hLdrMod == NIL_RTLDRMOD);
    160             cErrors++;
    161205            break;
    162206        }
     
    166210        if (cbImage && cb != cbImage)
    167211        {
    168             RTPrintf("tstLdr-4: Size mismatch '%s'/%d. aborting test.\n", pszFilename, i);
    169             cErrors++;
     212            RTTestIFailed("tstLdr-4: Size mismatch '%s'/%d. aborting test.", pszFilename, i);
    170213            break;
    171214        }
     
    176219        if (!aLoads[i].pvBits)
    177220        {
    178             RTPrintf("tstLdr-4: Out of memory '%s'/%d cbImage=%d. aborting test.\n", pszFilename, i, cbImage);
    179             cErrors++;
     221            RTTestIFailed("Out of memory '%s'/%d cbImage=%d. aborting test.", pszFilename, i, cbImage);
    180222            break;
    181223        }
     
    185227        if (RT_FAILURE(rc))
    186228        {
    187             RTPrintf("tstLdr-4: Failed to get bits for '%s'/%d, rc=%Rrc. aborting test\n", pszFilename, i, rc);
    188             cErrors++;
     229            RTTestIFailed("Failed to get bits for '%s'/%d, rc=%Rrc. aborting test", pszFilename, i, rc);
    189230            break;
    190231        }
     
    194235     * Execute the code.
    195236     */
    196     if (!cErrors)
     237    if (!RTTestSubErrorCount(g_hTest))
    197238    {
    198239        for (i = 0; i < RT_ELEMENTS(aLoads); i += 1)
     
    210251            if (RT_FAILURE(rc))
    211252            {
    212                 RTPrintf("tstLdr-4: Failed to get symbol \"DisasmTest1\" from load #%d: %Rrc\n", i, rc);
    213                 cErrors++;
     253                RTTestIFailed("Failed to get symbol \"DisasmTest1\" from load #%d: %Rrc", i, rc);
    214254                break;
    215255            }
     
    223263            rc = pfnDisasmTest1();
    224264            if (rc)
    225             {
    226                 RTPrintf("tstLdr-4: load #%d Test1 -> %#x\n", i, rc);
    227                 cErrors++;
    228             }
     265                RTTestIFailed("load #%d Test1 -> %#x", i, rc);
    229266
    230267            /* While we're here, check a couple of RTLdrQueryProp calls too */
     
    257294            rc = RTLdrClose(aLoads[i].hLdrMod);
    258295            if (RT_FAILURE(rc))
    259             {
    260                 RTPrintf("tstLdr-4: Failed to close '%s' i=%d, rc=%Rrc.\n", pszFilename, i, rc);
    261                 cErrors++;
    262             }
    263         }
    264     }
    265 
    266     return cErrors;
     296                RTTestIFailed("Failed to close '%s' i=%d, rc=%Rrc.", pszFilename, i, rc);
     297        }
     298    }
     299
    267300}
    268301
    269302
    270303
    271 int main(int argc, char **argv)
     304int main()
    272305{
    273     int cErrors = 0;
    274     RTR3InitExe(argc, &argv, 0);
     306    RTEXITCODE rcExit = RTTestInitAndCreate("tstLdr-4", &g_hTest);
     307    if (rcExit != RTEXITCODE_SUCCESS)
     308        return rcExit;
    275309
    276310    /*
     
    278312     */
    279313    int rc = DisasmTest1();
    280     if (rc)
    281     {
    282         RTPrintf("tstLdr-4: FATAL ERROR - DisasmTest1 is buggy: rc=%#x\n", rc);
    283         return 1;
    284     }
    285 
    286     /*
    287      * Execute the test.
    288      */
    289     char szPath[RTPATH_MAX];
    290     rc = RTPathExecDir(szPath, sizeof(szPath) - sizeof("/tstLdrObjR0.r0"));
    291     if (RT_SUCCESS(rc))
    292     {
    293         strcat(szPath, "/tstLdrObjR0.r0");
    294         RTPrintf("tstLdr-4: TESTING '%s'...\n", szPath);
    295         cErrors += testLdrOne(szPath);
     314    if (rc == 0)
     315    {
     316        /*
     317         * Execute the test.
     318         */
     319        char szPath[RTPATH_MAX];
     320        rc = RTPathExecDir(szPath, sizeof(szPath) - sizeof("/tstLdrObjR0.r0"));
     321        if (RT_SUCCESS(rc))
     322        {
     323            strcat(szPath, "/tstLdrObjR0.r0");
     324
     325            testLdrOne(szPath);
     326        }
     327        else
     328            RTTestIFailed("RTPathExecDir -> %Rrc", rc);
    296329    }
    297330    else
    298     {
    299         RTPrintf("tstLdr-4: RTPathExecDir -> %Rrc\n", rc);
    300         cErrors++;
    301     }
    302 
    303     /*
    304      * Test result summary.
    305      */
    306     if (!cErrors)
    307         RTPrintf("tstLdr-4: SUCCESS\n");
    308     else
    309         RTPrintf("tstLdr-4: FAILURE - %d errors\n", cErrors);
    310     return !!cErrors;
     331        RTTestIFailed("FATAL ERROR - DisasmTest1 is buggy: rc=%#x", rc);
     332
     333    return RTTestSummaryAndDestroy(g_hTest);
    311334}
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