VirtualBox

Changeset 95000 in vbox


Ignore:
Timestamp:
May 13, 2022 9:39:42 AM (3 years ago)
Author:
vboxsync
Message:

IPRT/testcase: Made tstFile more flexible by querying file system properties (free space, ++) before we actually perform any tests and clamp accordingly, plus making it more random (for beyond 2GB writes). This is needed because we have test VMs which have less disk space configured than 2GB.

File:
1 edited

Legend:

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

    r93115 r95000  
    3131#include <iprt/test.h>
    3232#include <iprt/file.h>
    33 #include <iprt/errcore.h>
     33#include <iprt/err.h>
     34#include <iprt/path.h>
     35#include <iprt/rand.h>
    3436#include <iprt/string.h>
    3537#include <iprt/stream.h>
     
    5557"habitasse platea dictumst.\n";
    5658
     59/**
     60 * Structure holding queried file system properties we're performing our tests on.
     61 */
     62typedef struct FsProps
     63{
     64    RTFOFF   cbTotal;
     65    RTFOFF   cbFree;
     66    uint32_t cbBlock;
     67    uint32_t cbSector;
     68} FsProps;
     69/** Queried file system properties we're performing our tests on. */
     70static FsProps s_FsProps;
     71
    5772
    5873static void tstAppend(RTFILE hFile)
     
    112127    }
    113128
    114     /* grow file beyond 2G */
    115     rc = RTFileSetSize(hFile, _2G + _1M);
     129    uint64_t cbFileSize = _2G + RTRandU32Ex(_1K, _1M); /* Try growing file beyond 2G by default. */
     130    if ((uint64_t)s_FsProps.cbFree <= cbFileSize)
     131    {
     132        RTTestIPrintf(RTTESTLVL_ALWAYS, "Warning: Free disk space less than testcase file size (%RTfoff vs. %RU64), limiting\n",
     133                      s_FsProps.cbFree, cbFileSize);
     134        cbFileSize = s_FsProps.cbFree -  _1M; /* Leave a bit of space on the fs. */
     135    }
     136    /** @todo Also check and clamp for fs max file size limits? */
     137    RTTESTI_CHECK_MSG_RETV(cbFileSize, ("No space left on file system (disk full)"));
     138
     139    rc = RTFileSetSize(hFile, cbFileSize);
    116140    if (RT_FAILURE(rc))
    117         RTTestIFailed("Failed to grow file #1 to 2.001GB. rc=%Rrc", rc);
     141        RTTestIFailed("Failed to grow file #1 to %RU64. rc=%Rrc", cbFileSize, rc);
    118142    else
    119143    {
    120144        uint64_t cb;
    121145        RTTESTI_CHECK_RC(RTFileQuerySize(hFile, &cb), VINF_SUCCESS);
    122         RTTESTI_CHECK_MSG(cb == _2G + _1M, ("RTFileQuerySize return %RX64 bytes, expected %RX64.", cb, _2G + _1M));
     146        RTTESTI_CHECK_MSG(cb == cbFileSize, ("RTFileQuerySize return %RX64 bytes, expected %RX64.", cb, cbFileSize));
    123147
    124148        /*
     
    172196         * Try some writes at the end of the file.
    173197         */
    174         rc = RTFileSeek(hFile, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
    175         if (RT_FAILURE(rc))
    176             RTTestIFailed("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
     198        rc = RTFileSeek(hFile, cbFileSize, RTFILE_SEEK_BEGIN, NULL);
     199        if (RT_FAILURE(rc))
     200            RTTestIFailed("Failed to seek to %RU64 in file #1. rc=%Rrc\n", cbFileSize, rc);
    177201        else
    178202        {
    179203            offFile = RTFileTell(hFile);
    180             if (offFile != _2G + _1M)
    181                 RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
     204            if (offFile != cbFileSize)
     205                RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, cbFileSize);
    182206            else
    183207            {
     
    192216                }
    193217                if (RT_FAILURE(rc))
    194                     RTTestIFailed("Failed to write to file #1 at offset 2G + 1M.  rc=%Rrc\n", rc);
     218                    RTTestIFailed("Failed to write to file #1 at offset %RU64.  rc=%Rrc\n", cbFileSize, rc);
    195219                else
    196220                {
     
    211235                        }
    212236                        if (RT_FAILURE(rc))
    213                             RTTestIFailed("Failed to read from file #1 at offset 2G + 1M.  rc=%Rrc\n", rc);
     237                            RTTestIFailed("Failed to read from file #1 at offset %RU64. rc=%Rrc\n", cbFileSize, rc);
    214238                        else
    215239                        {
     
    217241                                RTPrintf("tstFile: tail write ok\n");
    218242                            else
    219                                 RTTestIFailed("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
     243                                RTTestIFailed("Data read from file #1 at offset %RU64 differs from what we wrote there.\n",
     244                                              cbFileSize);
    220245                        }
    221246                    }
     
    227252         * Some general seeking around.
    228253         */
    229         rc = RTFileSeek(hFile, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
    230         if (RT_FAILURE(rc))
    231             RTTestIFailed("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
     254        RTFOFF offSeek = RTRandS64Ex(0, cbFileSize);
     255        rc = RTFileSeek(hFile, offSeek, RTFILE_SEEK_BEGIN, NULL);
     256        if (RT_FAILURE(rc))
     257            RTTestIFailed("Failed to seek to %RTfoff in file #1. rc=%Rrc\n", offSeek, rc);
    232258        else
    233259        {
    234260            offFile = RTFileTell(hFile);
    235             if (offFile != _2G + 1)
    236                 RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
     261            if (offFile != (uint64_t)offSeek)
     262                RTTestIFailed("RTFileTell -> %#RTfoff, expected %RTfoff (#3)\n", offFile, offSeek);
    237263        }
    238264
     
    244270        {
    245271            offFile = RTFileTell(hFile);
    246             if (offFile != _2G + _1M + sizeof(g_szTestStr)) /* assuming tail write was ok. */
    247                 RTTestIFailed("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(g_szTestStr));
     272            if (offFile != cbFileSize + sizeof(g_szTestStr)) /* assuming tail write was ok. */
     273                RTTestIFailed("RTFileTell -> %RTfoff, expected %#RX64 (#4)\n", offFile, cbFileSize + sizeof(g_szTestStr));
    248274        }
    249275
     
    256282            offFile = RTFileTell(hFile);
    257283            if (offFile != 0)
    258                 RTTestIFailed("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
     284                RTTestIFailed("RTFileTell -> %RTfoff, expected 0 (#5)\n", offFile);
    259285        }
    260286    }
     
    270296
    271297    /*
     298     * Query file system sizes first.
     299     * This is needed beforehand, so that we don't perform tests which cannot succeed because of known limitations
     300     * (too little space, file system maximum file size restrictions, ++).
     301     */
     302    char szCWD[RTPATH_MAX];
     303    int rc = RTPathGetCurrent(szCWD, sizeof(szCWD));
     304    RTTESTI_CHECK_MSG(RT_SUCCESS(rc), ("Unable to query current directory, rc=%Rrc", rc));
     305    rc = RTFsQuerySizes(szCWD, &s_FsProps.cbTotal, &s_FsProps.cbFree, &s_FsProps.cbBlock, &s_FsProps.cbSector);
     306    RTTESTI_CHECK_MSG(RT_SUCCESS(rc), ("Unable to query file system sizes of '%s', rc=%Rrc", szCWD, rc));
     307
     308    /*
    272309     * Some basic tests.
    273310     */
    274311    RTTestSub(hTest, "Basics");
    275     int rc = -1;
    276312    RTFILE hFile = NIL_RTFILE;
    277313    RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
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