VirtualBox

Ignore:
Timestamp:
Mar 13, 2019 7:47:51 PM (6 years ago)
Author:
vboxsync
Message:

FsPerf: Borrow open action tests from tstRTFileOpenEx-1. ticketref:9276

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/utils/fs/FsPerf.cpp

    r77660 r77690  
    709709
    710710
     711/** @note tstRTFileOpenEx-1.cpp has a copy of this code.   */
     712static void tstOpenExTest(unsigned uLine, int cbExist, int cbNext, const char *pszFilename, uint64_t fAction,
     713                          int rcExpect, RTFILEACTION enmActionExpected)
     714{
     715    uint64_t const  fCreateMode = (0644 << RTFILE_O_CREATE_MODE_SHIFT);
     716    RTFILE          hFile;
     717    int             rc;
     718
     719    /*
     720     * File existence and size.
     721     */
     722    bool fOkay = false;
     723    RTFSOBJINFO ObjInfo;
     724    rc = RTPathQueryInfoEx(pszFilename, &ObjInfo, RTFSOBJATTRADD_NOTHING, RTPATH_F_ON_LINK);
     725    if (RT_SUCCESS(rc))
     726        fOkay = cbExist == (int64_t)ObjInfo.cbObject;
     727    else
     728        fOkay = rc == VERR_FILE_NOT_FOUND && cbExist < 0;
     729    if (!fOkay)
     730    {
     731        if (cbExist >= 0)
     732        {
     733            rc = RTFileOpen(&hFile, pszFilename, RTFILE_O_WRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | fCreateMode);
     734            if (RT_SUCCESS(rc))
     735            {
     736                while (cbExist > 0)
     737                {
     738                    size_t cbToWrite = strlen(pszFilename);
     739                    if ((ssize_t)cbToWrite > cbExist)
     740                        cbToWrite = cbExist;
     741                    rc = RTFileWrite(hFile, pszFilename, cbToWrite, NULL);
     742                    if (RT_FAILURE(rc))
     743                    {
     744                        RTTestIFailed("%u: RTFileWrite(%s,%#x) -> %Rrc\n", uLine, pszFilename, cbToWrite, rc);
     745                        break;
     746                    }
     747                    cbExist -= cbToWrite;
     748                }
     749
     750                RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
     751            }
     752            else
     753                RTTestIFailed("%u: RTFileDelete(%s) -> %Rrc\n", uLine, pszFilename, rc);
     754
     755        }
     756        else
     757        {
     758            rc = RTFileDelete(pszFilename);
     759            if (rc != VINF_SUCCESS && rc != VERR_FILE_NOT_FOUND)
     760                RTTestIFailed("%u: RTFileDelete(%s) -> %Rrc\n", uLine, pszFilename, rc);
     761        }
     762    }
     763
     764    /*
     765     * The actual test.
     766     */
     767    RTFILEACTION enmActuallyTaken = RTFILEACTION_END;
     768    hFile = NIL_RTFILE;
     769    rc = RTFileOpenEx(pszFilename, fAction | RTFILE_O_READWRITE | RTFILE_O_DENY_NONE | fCreateMode, &hFile, &enmActuallyTaken);
     770    if (   rc != rcExpect
     771        || enmActuallyTaken != enmActionExpected
     772        || (RT_SUCCESS(rc) ? hFile == NIL_RTFILE : hFile != NIL_RTFILE))
     773        RTTestIFailed("%u: RTFileOpenEx(%s, %#llx) -> %Rrc + %d  (hFile=%p), expected %Rrc + %d\n",
     774                      uLine, pszFilename, fAction, rc, enmActuallyTaken, hFile, rcExpect, enmActionExpected);
     775    if (RT_SUCCESS(rc))
     776    {
     777        if (   enmActionExpected == RTFILEACTION_REPLACED
     778            || enmActionExpected == RTFILEACTION_TRUNCATED)
     779        {
     780            uint8_t abBuf[16];
     781            rc = RTFileRead(hFile, abBuf, 1, NULL);
     782            if (rc != VERR_EOF)
     783                RTTestIFailed("%u: RTFileRead(%s,,1,) -> %Rrc, expected VERR_EOF\n", uLine, pszFilename, rc);
     784        }
     785
     786        while (cbNext > 0)
     787        {
     788            size_t cbToWrite = strlen(pszFilename);
     789            if ((ssize_t)cbToWrite > cbNext)
     790                cbToWrite = cbNext;
     791            rc = RTFileWrite(hFile, pszFilename, cbToWrite, NULL);
     792            if (RT_FAILURE(rc))
     793            {
     794                RTTestIFailed("%u: RTFileWrite(%s,%#x) -> %Rrc\n", uLine, pszFilename, cbToWrite, rc);
     795                break;
     796            }
     797            cbNext -= cbToWrite;
     798        }
     799
     800        rc = RTFileClose(hFile);
     801        if (RT_FAILURE(rc))
     802            RTTestIFailed("%u: RTFileClose(%p) -> %Rrc\n", uLine, hFile, rc);
     803    }
     804}
     805
     806
    711807void fsPerfOpen(void)
    712808{
     
    721817    RTTESTI_CHECK_RC(RTFileOpen(&hFile, InDir(RT_STR_TUPLE("known-file" RTPATH_SLASH_STR "no-such-file")),
    722818                                RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ), VERR_PATH_NOT_FOUND);
     819
     820    /*
     821     * The following is copied from tstRTFileOpenEx-1.cpp:
     822     */
     823    InDir(RT_STR_TUPLE("file1"));
     824    tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN,                        VERR_FILE_NOT_FOUND, RTFILEACTION_INVALID);
     825    tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN_CREATE,                        VINF_SUCCESS, RTFILEACTION_CREATED);
     826    tstOpenExTest(__LINE__,  0,  0, g_szDir, RTFILE_O_OPEN_CREATE,                        VINF_SUCCESS, RTFILEACTION_OPENED);
     827    tstOpenExTest(__LINE__,  0,  0, g_szDir, RTFILE_O_OPEN,                               VINF_SUCCESS, RTFILEACTION_OPENED);
     828
     829    tstOpenExTest(__LINE__,  0,  0, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE,           VINF_SUCCESS, RTFILEACTION_TRUNCATED);
     830    tstOpenExTest(__LINE__,  0, 10, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE,    VINF_SUCCESS, RTFILEACTION_TRUNCATED);
     831    tstOpenExTest(__LINE__, 10, 10, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE,    VINF_SUCCESS, RTFILEACTION_TRUNCATED);
     832    tstOpenExTest(__LINE__, 10, -1, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE,           VINF_SUCCESS, RTFILEACTION_TRUNCATED);
     833    tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_OPEN | RTFILE_O_TRUNCATE,    VERR_FILE_NOT_FOUND, RTFILEACTION_INVALID);
     834    tstOpenExTest(__LINE__, -1,  0, g_szDir, RTFILE_O_OPEN_CREATE | RTFILE_O_TRUNCATE,    VINF_SUCCESS, RTFILEACTION_CREATED);
     835
     836    tstOpenExTest(__LINE__,  0, -1, g_szDir, RTFILE_O_CREATE_REPLACE,                     VINF_SUCCESS, RTFILEACTION_REPLACED);
     837    tstOpenExTest(__LINE__, -1,  0, g_szDir, RTFILE_O_CREATE_REPLACE,                     VINF_SUCCESS, RTFILEACTION_CREATED);
     838    tstOpenExTest(__LINE__,  0, -1, g_szDir, RTFILE_O_CREATE,                      VERR_ALREADY_EXISTS, RTFILEACTION_ALREADY_EXISTS);
     839    tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_CREATE,                             VINF_SUCCESS, RTFILEACTION_CREATED);
     840
     841    tstOpenExTest(__LINE__, -1, 10, g_szDir, RTFILE_O_CREATE | RTFILE_O_TRUNCATE,         VINF_SUCCESS, RTFILEACTION_CREATED);
     842    tstOpenExTest(__LINE__, 10, 10, g_szDir, RTFILE_O_CREATE | RTFILE_O_TRUNCATE,  VERR_ALREADY_EXISTS, RTFILEACTION_ALREADY_EXISTS);
     843    tstOpenExTest(__LINE__, 10, -1, g_szDir, RTFILE_O_CREATE_REPLACE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_REPLACED);
     844    tstOpenExTest(__LINE__, -1, -1, g_szDir, RTFILE_O_CREATE_REPLACE | RTFILE_O_TRUNCATE, VINF_SUCCESS, RTFILEACTION_CREATED);
     845
     846    RTTESTI_CHECK_RC(RTFileDelete(g_szDir), VINF_SUCCESS);
    723847
    724848    /*
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