VirtualBox

Changeset 9418 in vbox for trunk/src


Ignore:
Timestamp:
Jun 5, 2008 12:58:01 PM (17 years ago)
Author:
vboxsync
Message:

VHD creation/deletion code as well as create/write/close/open/read testcase and some bugfixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Devices/Storage/testcase/tstVD.cpp

    r9264 r9418  
    8383}
    8484
    85 #if 0
    86 static int tstVDOpenCreateWriteMerge(const char *pszBackend,
    87                                      const char *pszBaseFilename,
    88                                      const char *pszDiffFilename)
    89 {
    90     int rc;
    91     PVBOXHDD pVD = NULL;
    92     char *pszFormat;
    93     PDMMEDIAGEOMETRY PCHS = { 0, 0, 0 };
    94     PDMMEDIAGEOMETRY LCHS = { 0, 0, 0 };
    95 
    96 #define CHECK(str) \
    97     do \
    98     { \
    99         RTPrintf("%s rc=%Vrc\n", str, rc); \
    100         if (VBOX_FAILURE(rc)) \
    101         { \
    102             VDCloseAll(pVD); \
    103             return rc; \
    104         } \
    105     } while (0)
    106 
    107     rc = VDCreate(tstVDError, NULL, &pVD);
    108     CHECK("VDCreate()");
    109 
    110     RTFILE File;
    111     rc = RTFileOpen(&File, pszBaseFilename, RTFILE_O_READ);
    112     if (VBOX_SUCCESS(rc))
    113     {
    114         RTFileClose(File);
    115         rc = VDGetFormat(pszBaseFilename, &pszFormat);
    116         RTPrintf("VDGetFormat() pszFormat=%s rc=%Vrc\n", pszFormat, rc);
    117         if (VBOX_SUCCESS(rc) && strcmp(pszFormat, pszBackend))
    118         {
    119             rc = VERR_GENERAL_FAILURE;
    120             RTPrintf("VDGetFormat() returned incorrect backend name\n");
    121         }
    122         RTStrFree(pszFormat);
    123         CHECK("VDGetFormat()");
    124 
    125         rc = VDOpen(pVD, pszBackend, pszBaseFilename, VD_OPEN_FLAGS_NORMAL);
    126         CHECK("VDOpen()");
    127     }
    128     else
    129     {
    130         rc = VDCreateBase(pVD, pszBackend, pszBaseFilename,
    131                           VD_IMAGE_TYPE_NORMAL, 1000 * _1M,
    132                           VD_IMAGE_FLAGS_NONE, "Test image",
    133                           &PCHS, &LCHS, VD_OPEN_FLAGS_NORMAL,
    134                           NULL, NULL);
    135         CHECK("VDCreateBase()");
    136     }
    137 
    138     void *pvBuf = RTMemAlloc(_1M);
    139 
    140     memset(pvBuf, 0x33, _1M);
    141     rc = VDWrite(pVD, 20 * _1M + 594432, pvBuf, _1M);
    142     CHECK("VDWrite()");
    143 
    144     memset(pvBuf, 0x46, _1M);
    145     rc = VDWrite(pVD, 20 * _1M + 594432, pvBuf, _1K);
    146     CHECK("VDWrite()");
    147 
    148     memset(pvBuf, 0x51, _1M);
    149     rc = VDWrite(pVD, 40 * _1M + 594432, pvBuf, _1K);
    150     CHECK("VDWrite()");
    151 
    152     rc = VDCreateDiff(pVD, pszBackend, pszDiffFilename,
    153                       VD_IMAGE_FLAGS_NONE, "Test diff image",
    154                       VD_OPEN_FLAGS_NORMAL, NULL, NULL);
    155     CHECK("VDCreateDiff()");
    156 
    157     memset(pvBuf, '_', _1M);
    158     rc = VDWrite(pVD, 20 * _1M + 594432, pvBuf, 512);
    159     CHECK("VDWrite()");
    160 
    161     rc = VDWrite(pVD, 22 * _1M + 594432, pvBuf, 78336);
    162     CHECK("VDWrite()");
    163     rc = VDWrite(pVD, 13 * _1M + 594432, pvBuf, 783360);
    164     CHECK("VDWrite()");
    165     rc = VDWrite(pVD, 44 * _1M + 594432, pvBuf, 68096);
    166     CHECK("VDWrite()");
    167 
    168     VDDumpImages(pVD);
    169 
    170     RTPrintf("Merging diff into base..\n");
    171     rc = VDMerge(pVD, -1, 0, NULL, NULL);
    172     CHECK("VDMerge()");
    173 
    174     VDDumpImages(pVD);
    175 
    176     VDCloseAll(pVD);
    177 #undef CHECK
    178     return 0;
    179 }
    180 #else
    18185
    18286#undef RTDECL
     
    408312        pSegment[i].u32Length = RTPRandU32Ex(pCtx, 1, RT_MIN(pSegment[i+1].u64Offset - pSegment[i].u64Offset,
    409313                                                             u32MaxSegmentSize) / u32SectorSize) * u32SectorSize;
     314        Assert(pSegment[i].u32Length <= u32MaxSegmentSize);
    410315        pSegment[i].u8Value  = RTPRandU32Ex(pCtx, (uint32_t)u8ValueLow, (uint32_t)u8ValueHigh);
    411316    }
    412317}
    413318
    414 static void mergeSegments(PSEGMENT pBaseSegment, PSEGMENT pDiffSegment, PSEGMENT pMergeSegment)
     319static void mergeSegments(PSEGMENT pBaseSegment, PSEGMENT pDiffSegment, PSEGMENT pMergeSegment, uint32_t u32MaxLength)
    415320{
    416321    while (pBaseSegment->u32Length > 0 || pDiffSegment->u32Length > 0)
     
    424329            {
    425330                pMergeSegment->u32Length = pDiffSegment->u64Offset - pMergeSegment->u64Offset;
     331                Assert(pMergeSegment->u32Length <= u32MaxLength);
    426332                if (pBaseSegment->u64Offset + pBaseSegment->u32Length >
    427333                    pDiffSegment->u64Offset + pDiffSegment->u32Length)
    428334                {
    429                     pBaseSegment->u32Length -= pBaseSegment->u64Offset -pDiffSegment->u64Offset - pDiffSegment->u32Length;
     335                    pBaseSegment->u32Length -= pDiffSegment->u64Offset + pDiffSegment->u32Length - pBaseSegment->u64Offset;
     336                    Assert(pBaseSegment->u32Length <= u32MaxLength);
    430337                    pBaseSegment->u64Offset = pDiffSegment->u64Offset + pDiffSegment->u32Length;
    431338                }
     
    448355                {
    449356                    pBaseSegment->u32Length -= pDiffSegment->u64Offset + pDiffSegment->u32Length - pBaseSegment->u64Offset;
     357                    Assert(pBaseSegment->u32Length <= u32MaxLength);
    450358                    pBaseSegment->u64Offset = pDiffSegment->u64Offset + pDiffSegment->u32Length;
    451359                    pDiffSegment++;
     
    555463    }
    556464
    557     int nSegments = 10;
     465    int nSegments = 100;
    558466    /* Allocate one extra element for a sentinel. */
    559467    PSEGMENT paBaseSegments  = (PSEGMENT)RTMemAllocZ(sizeof(struct Segment) * (nSegments + 1));
     
    590498    CHECK("VDMerge()");
    591499
    592     mergeSegments(paBaseSegments, paDiffSegments, paMergeSegments);
     500    mergeSegments(paBaseSegments, paDiffSegments, paMergeSegments, _1M);
    593501    /*RTPrintf("\nMerged segments:\n");
    594502    for (pSegment = paMergeSegments; pSegment->u32Length; pSegment++)
     
    607515    return 0;
    608516}
    609 #endif
     517
     518static int tstVDCreateWriteOpenRead(const char *pszBackend,
     519                                    const char *pszFilename,
     520                                    uint32_t u32Seed)
     521{
     522    int rc;
     523    PVBOXHDD pVD = NULL;
     524    PDMMEDIAGEOMETRY PCHS = { 0, 0, 0 };
     525    PDMMEDIAGEOMETRY LCHS = { 0, 0, 0 };
     526    uint64_t u64DiskSize  = 1000 * _1M;
     527    uint32_t u32SectorSize = 512;
     528
     529#define CHECK(str) \
     530    do \
     531    { \
     532        RTPrintf("%s rc=%Vrc\n", str, rc); \
     533        if (VBOX_FAILURE(rc)) \
     534        { \
     535            VDCloseAll(pVD); \
     536            return rc; \
     537        } \
     538    } while (0)
     539
     540    rc = VDCreate(tstVDError, NULL, &pVD);
     541    CHECK("VDCreate()");
     542
     543    RTFILE File;
     544    rc = RTFileOpen(&File, pszFilename, RTFILE_O_READ);
     545    if (VBOX_SUCCESS(rc))
     546    {
     547        RTFileClose(File);
     548        RTFileDelete(pszFilename);
     549    }
     550
     551    rc = VDCreateBase(pVD, pszBackend, pszFilename,
     552                      VD_IMAGE_TYPE_NORMAL, u64DiskSize,
     553                      VD_IMAGE_FLAGS_NONE, "Test image",
     554                      &PCHS, &LCHS, VD_OPEN_FLAGS_NORMAL,
     555                      NULL, NULL);
     556    CHECK("VDCreateBase()");
     557
     558    int nSegments = 100;
     559    /* Allocate one extra element for a sentinel. */
     560    PSEGMENT paSegments  = (PSEGMENT)RTMemAllocZ(sizeof(struct Segment) * (nSegments + 1));
     561
     562    void *pvBuf = RTMemAlloc(_1M);
     563
     564    RNDCTX ctx;
     565    initializeRandomGenerator(&ctx, u32Seed);
     566    generateRandomSegments(&ctx, paSegments, nSegments, _1M, u64DiskSize, u32SectorSize, 0u, 127u);
     567
     568    writeSegmentsToDisk(pVD, pvBuf, paSegments);
     569
     570    VDCloseAll(pVD);
     571
     572    rc = VDOpen(pVD, pszBackend, pszFilename, VD_OPEN_FLAGS_NORMAL);
     573    CHECK("VDOpen()");
     574    rc = readAndCompareSegments(pVD, pvBuf, paSegments);
     575    CHECK("readAndCompareSegments()");
     576
     577    RTMemFree(paSegments);
     578
     579    VDCloseAll(pVD);
     580#undef CHECK
     581    return 0;
     582}
    610583
    611584int main(int argc, char *argv[])
     
    631604    RTFileDelete("tmpVDCreate.vdi");
    632605    RTFileDelete("tmpVDCreate.vmdk");
     606    RTFileDelete("tmpVDCreate.vhd");
    633607    RTFileDelete("tmpVDBase.vdi");
    634608    RTFileDelete("tmpVDDiff.vdi");
    635609    RTFileDelete("tmpVDBase.vmdk");
    636610    RTFileDelete("tmpVDDiff.vmdk");
     611    RTFileDelete("tmpVDBase.vhd");
     612    RTFileDelete("tmpVDDiff.vhd");
    637613
    638614    rc = tstVDCreateDelete("VDI", "tmpVDCreate.vdi", 2 * _4G,
     
    684660        g_cErrors++;
    685661    }
     662    rc = tstVDCreateDelete("VHD", "tmpVDCreate.vhd", 2 * _4G,
     663                           VD_IMAGE_TYPE_NORMAL, VD_IMAGE_FLAGS_NONE,
     664                           true);
     665    if (VBOX_FAILURE(rc))
     666    {
     667        RTPrintf("tstVD: dynamic VHD create test failed! rc=%Vrc\n", rc);
     668        g_cErrors++;
     669    }
     670    rc = tstVDCreateDelete("VHD", "tmpVDCreate.vhd", 2 * _4G,
     671                           VD_IMAGE_TYPE_FIXED, VD_IMAGE_FLAGS_NONE,
     672                           true);
     673    if (VBOX_FAILURE(rc))
     674    {
     675        RTPrintf("tstVD: fixed VHD create test failed! rc=%Vrc\n", rc);
     676        g_cErrors++;
     677    }
    686678
    687679    rc = tstVDOpenCreateWriteMerge("VDI", "tmpVDBase.vdi", "tmpVDDiff.vdi", u32Seed);
     
    710702    }
    711703
     704    rc = tstVDCreateWriteOpenRead("VHD", "tmpVDCreate.vhd", u32Seed);
     705    if (VBOX_FAILURE(rc))
     706    {
     707        RTPrintf("tstVD: VHD test failed (creating image)! rc=%Vrc\n", rc);
     708        g_cErrors++;
     709    }
     710#if 0
     711    rc = tstVDOpenCreateWriteMerge("VHD", "tmpVDBase.vhd", "tmpVDDiff.vhd", u32Seed);
     712    if (VBOX_FAILURE(rc))
     713    {
     714        RTPrintf("tstVD: VHD test failed (existing image)! rc=%Vrc\n", rc);
     715        g_cErrors++;
     716    }
     717#endif
    712718    /*
    713719     * Clean up any leftovers.
     
    715721    RTFileDelete("tmpVDCreate.vdi");
    716722    RTFileDelete("tmpVDCreate.vmdk");
     723    RTFileDelete("tmpVDCreate.vhd");
    717724    RTFileDelete("tmpVDBase.vdi");
    718725    RTFileDelete("tmpVDDiff.vdi");
    719726    RTFileDelete("tmpVDBase.vmdk");
    720727    RTFileDelete("tmpVDDiff.vmdk");
    721 #if 0
    722     rc = tstVDOpenCreateWriteMerge("VDI", "tmpVDBase.vdi", "tmpVDDiff.vdi", u32Seed);
    723     if (VBOX_FAILURE(rc))
    724     {
    725         RTPrintf("tstVD: VHD test failed (existing image)! rc=%Vrc\n", rc);
    726         g_cErrors++;
    727     }
    728     RTFileDelete("tmpVDBase.vdi");
    729     RTFileDelete("tmpVDDiff.vdi");
    730     //RTFileDelete("tmpVDDiff.vhd");
    731 #endif
     728    RTFileDelete("tmpVDBase.vhd");
     729    RTFileDelete("tmpVDDiff.vhd");
    732730
    733731    /*
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