VirtualBox

Changeset 29392 in vbox


Ignore:
Timestamp:
May 12, 2010 12:18:31 AM (15 years ago)
Author:
vboxsync
Message:

iprt: Converted tstFileAio and tstFileAppend-1 to RTTest, fixing lock validator assertion at startup.

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

Legend:

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

    r28800 r29392  
    55
    66/*
    7  * Copyright (C) 2006-2007 Oracle Corporation
     7 * Copyright (C) 2006-2010 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3030*******************************************************************************/
    3131#include <iprt/file.h>
    32 #include <iprt/types.h>
     32
    3333#include <iprt/err.h>
     34#include <iprt/mem.h>
     35#include <iprt/param.h>
    3436#include <iprt/string.h>
    35 #include <iprt/stream.h>
    36 #include <iprt/mem.h>
    37 #include <iprt/initterm.h>
    38 
     37#include <iprt/test.h>
     38
     39
     40/*******************************************************************************
     41*   Defined Constants And Macros                                               *
     42*******************************************************************************/
    3943/** @todo make configurable through cmd line. */
    40 #define TSTFILEAIO_MAX_REQS_IN_FLIGHT 64
    41 #define TSTFILEAIO_BUFFER_SIZE 64*_1K
    42 
    43 /* Global error counter. */
    44 int         cErrors = 0;
    45 
    46 void tstFileAioTestReadWriteBasic(RTFILE File, bool fWrite, void *pvTestBuf, size_t cbTestBuf, size_t cbTestFile, uint32_t cMaxReqsInFlight)
     44#define TSTFILEAIO_MAX_REQS_IN_FLIGHT   64
     45#define TSTFILEAIO_BUFFER_SIZE          (64*_1K)
     46
     47
     48/*******************************************************************************
     49*   Global Variables                                                           *
     50*******************************************************************************/
     51static RTTEST g_hTest = NIL_RTTEST;
     52
     53
     54void tstFileAioTestReadWriteBasic(RTFILE File, bool fWrite, void *pvTestBuf,
     55                                  size_t cbTestBuf, size_t cbTestFile, uint32_t cMaxReqsInFlight)
    4756{
    48     int rc = VINF_SUCCESS;
     57    /* Allocate request array. */
     58    RTFILEAIOREQ *paReqs;
     59    paReqs = (PRTFILEAIOREQ)RTTestGuardedAllocHead(g_hTest, cMaxReqsInFlight * sizeof(RTFILEAIOREQ));
     60    RTTESTI_CHECK_RETV(paReqs);
     61    RT_BZERO(paReqs, sizeof(cMaxReqsInFlight * sizeof(RTFILEAIOREQ)));
     62
     63    /* Allocate array holding pointer to data buffers. */
     64    void **papvBuf = (void **)RTTestGuardedAllocHead(g_hTest, cMaxReqsInFlight * sizeof(void *));
     65    RTTESTI_CHECK_RETV(papvBuf);
     66
     67    /* Allocate the buffers*/
     68    for (unsigned i = 0; i < cMaxReqsInFlight; i++)
     69    {
     70        RTTESTI_CHECK_RC_OK_RETV(RTTestGuardedAlloc(g_hTest, cbTestBuf, PAGE_SIZE, true /*fHead*/, &papvBuf[i]));
     71        if (fWrite)
     72            memcpy(papvBuf[i], pvTestBuf, cbTestBuf);
     73        if (fWrite)
     74            memcpy(papvBuf[i], pvTestBuf, cbTestBuf);
     75        else
     76            RT_BZERO(papvBuf[i], cbTestBuf);
     77    }
     78
     79    /* Allocate array holding completed requests. */
     80    RTFILEAIOREQ *paReqsCompleted;
     81    paReqsCompleted = (PRTFILEAIOREQ)RTTestGuardedAllocHead(g_hTest, cMaxReqsInFlight * sizeof(RTFILEAIOREQ));
     82    RTTESTI_CHECK_RETV(paReqsCompleted);
     83    RT_BZERO(paReqsCompleted, cMaxReqsInFlight * sizeof(RTFILEAIOREQ));
     84
     85    /* Create a context and associate the file handle with it. */
    4986    RTFILEAIOCTX hAioContext;
    50     uint64_t NanoTS = RTTimeNanoTS();
    51 
    52     RTPrintf("tstFileAio: Starting simple %s test...\n", fWrite ? "write" : "read");
    53 
    54     rc = RTFileAioCtxCreate(&hAioContext, cMaxReqsInFlight);
     87    RTTESTI_CHECK_RC_RETV(RTFileAioCtxCreate(&hAioContext, cMaxReqsInFlight), VINF_SUCCESS);
     88    RTTESTI_CHECK_RC_RETV(RTFileAioCtxAssociateWithFile(hAioContext, File), VINF_SUCCESS);
     89
     90    /* Initialize requests. */
     91    for (unsigned i = 0; i < cMaxReqsInFlight; i++)
     92        RTFileAioReqCreate(&paReqs[i]);
     93
     94    RTFOFF      off    = 0;
     95    int         cRuns  = 0;
     96    uint64_t    NanoTS = RTTimeNanoTS();
     97    size_t      cbLeft = cbTestFile;
     98    while (cbLeft)
     99    {
     100        int rc;
     101        int cReqs = 0;
     102        for (unsigned i = 0; i < cMaxReqsInFlight; i++)
     103        {
     104            size_t cbTransfer = cbLeft < cbTestBuf ? cbLeft : cbTestBuf;
     105            if (!cbTransfer)
     106                break;
     107
     108            if (fWrite)
     109                rc = RTFileAioReqPrepareWrite(paReqs[i], File, off, papvBuf[i],
     110                                              cbTransfer, papvBuf[i]);
     111            else
     112                rc = RTFileAioReqPrepareRead(paReqs[i], File, off, papvBuf[i],
     113                                             cbTransfer, papvBuf[i]);
     114            RTTESTI_CHECK_RC(rc, VINF_SUCCESS);
     115
     116            cbLeft -= cbTransfer;
     117            off    += cbTransfer;
     118            cReqs++;
     119        }
     120
     121        rc = RTFileAioCtxSubmit(hAioContext, paReqs, cReqs);
     122        RTTESTI_CHECK_MSG(rc == VINF_SUCCESS, ("Failed to submit tasks after %d runs. rc=%Rrc\n", cRuns, rc));
     123        if (rc != VINF_SUCCESS)
     124            break;
     125
     126        /* Wait */
     127        uint32_t cCompleted = 0;
     128        RTTESTI_CHECK_RC(rc = RTFileAioCtxWait(hAioContext, cReqs, RT_INDEFINITE_WAIT,
     129                                               paReqsCompleted, cMaxReqsInFlight, &cCompleted),
     130                         VINF_SUCCESS);
     131        if (rc != VINF_SUCCESS)
     132            break;
     133
     134        if (!fWrite)
     135        {
     136            for (uint32_t i = 0; i < cCompleted; i++)
     137            {
     138                /* Compare that we read the right stuff. */
     139                void *pvBuf = RTFileAioReqGetUser(paReqsCompleted[i]);
     140                RTTESTI_CHECK(pvBuf);
     141
     142                size_t cbTransfered;
     143                RTTESTI_CHECK_RC(rc = RTFileAioReqGetRC(paReqsCompleted[i], &cbTransfered), VINF_SUCCESS);
     144                if (rc != VINF_SUCCESS)
     145                    break;
     146                RTTESTI_CHECK_MSG(cbTransfered == cbTestBuf, ("cbTransfered=%zd\n", cbTransfered));
     147                RTTESTI_CHECK_RC_OK(rc = (memcmp(pvBuf, pvTestBuf, cbTestBuf) == 0 ? VINF_SUCCESS : VERR_BAD_EXE_FORMAT));
     148                if (rc != VINF_SUCCESS)
     149                    break;
     150                memset(pvBuf, 0, cbTestBuf);
     151            }
     152        }
     153        cRuns++;
     154        if (RT_FAILURE(rc))
     155            break;
     156    }
     157
     158    NanoTS = RTTimeNanoTS() - NanoTS;
     159    uint64_t SpeedKBs = (uint64_t)(cbTestFile / (NanoTS / 1000000000.0) / 1024);
     160    RTTestValue(g_hTest, "Throughput", SpeedKBs, RTTESTUNIT_KILOBYTES_PER_SEC);
     161
     162    /* cleanup */
     163    for (unsigned i = 0; i < cMaxReqsInFlight; i++)
     164        RTTestGuardedFree(g_hTest, papvBuf[i]);
     165    RTTestGuardedFree(g_hTest, papvBuf);
     166    for (unsigned i = 0; i < cMaxReqsInFlight; i++)
     167        RTTESTI_CHECK_RC(RTFileAioReqDestroy(paReqs[i]), VINF_SUCCESS);
     168    RTTESTI_CHECK_RC(RTFileAioCtxDestroy(hAioContext), VINF_SUCCESS);
     169    RTTestGuardedFree(g_hTest, paReqs);
     170}
     171
     172int main()
     173{
     174    int rc = RTTestInitAndCreate("tstRTFileAio", &g_hTest);
     175    if (rc)
     176        return rc;
     177
     178    /* Check if the API is available. */
     179    RTTestSub(g_hTest, "RTFileAioGetLimits");
     180    RTFILEAIOLIMITS AioLimits;
     181    RT_ZERO(AioLimits);
     182    RTTESTI_CHECK_RC(rc = RTFileAioGetLimits(&AioLimits), VINF_SUCCESS);
    55183    if (RT_SUCCESS(rc))
    56184    {
    57         RTFILEAIOREQ    *paReqs  = NULL;
    58         void           **papvBuf = NULL;
    59         RTFOFF           Offset = 0;
    60         size_t           cbLeft = cbTestFile;
    61         int cRun = 0;
    62 
    63         /* Allocate request array. */
    64         paReqs = (PRTFILEAIOREQ)RTMemAllocZ(cMaxReqsInFlight * sizeof(RTFILEAIOREQ));
    65         if (paReqs)
     185        RTTestSub(g_hTest, "Write");
     186        RTFILE hFile;
     187        RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFileAio#1.tst",
     188                                         RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_ASYNC_IO),
     189                         VINF_SUCCESS);
     190        if (RT_SUCCESS(rc))
    66191        {
    67             /* Allocate array holding pointer to data buffers. */
    68             papvBuf = (void **)RTMemAllocZ(cMaxReqsInFlight * sizeof(void *));
    69             if (papvBuf)
     192            uint8_t *pbTestBuf = (uint8_t *)RTTestGuardedAllocTail(g_hTest, TSTFILEAIO_BUFFER_SIZE);
     193            for (unsigned i = 0; i < TSTFILEAIO_BUFFER_SIZE; i++)
     194                pbTestBuf[i] = i % 256;
     195
     196            uint32_t cReqsMax = AioLimits.cReqsOutstandingMax < TSTFILEAIO_MAX_REQS_IN_FLIGHT
     197                              ? AioLimits.cReqsOutstandingMax
     198                              : TSTFILEAIO_MAX_REQS_IN_FLIGHT;
     199
     200            /* Basic write test. */
     201            RTTestIPrintf(RTTESTLVL_ALWAYS, "Preparing test file, this can take some time and needs quite a bit of harddisk space...\n");
     202            tstFileAioTestReadWriteBasic(hFile, true /*fWrite*/, pbTestBuf, TSTFILEAIO_BUFFER_SIZE, 100*_1M, cReqsMax);
     203
     204            /* Reopen the file before doing the next test. */
     205            RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
     206            if (RTTestErrorCount(g_hTest) == 0)
    70207            {
    71                 /* Allocate array holding completed requests. */
    72                 RTFILEAIOREQ *paReqsCompleted = NULL;
    73                 paReqsCompleted = (PRTFILEAIOREQ)RTMemAllocZ(cMaxReqsInFlight * sizeof(RTFILEAIOREQ));
    74                 if (paReqsCompleted)
     208                RTTestSub(g_hTest, "Read/Write");
     209                RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFileAio#1.tst",
     210                                                 RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_ASYNC_IO),
     211                                 VINF_SUCCESS);
     212                if (RT_SUCCESS(rc))
    75213                {
    76                     /* Associate file with context.*/
    77                     rc = RTFileAioCtxAssociateWithFile(hAioContext, File);
    78                     if (RT_SUCCESS(rc))
    79                     {
    80                         /* Initialize buffers. */
    81                         for (unsigned i = 0; i < cMaxReqsInFlight; i++)
    82                         {
    83                             papvBuf[i] = RTMemPageAllocZ(cbTestBuf);
    84 
    85                             if (fWrite)
    86                                 memcpy(papvBuf[i], pvTestBuf, cbTestBuf);
    87                         }
    88 
    89                         /* Initialize requests. */
    90                         for (unsigned i = 0; i < cMaxReqsInFlight; i++)
    91                             RTFileAioReqCreate(&paReqs[i]);
    92 
    93                         while (cbLeft)
    94                         {
    95                             int cReqs = 0;
    96 
    97                             for (unsigned i = 0; i < cMaxReqsInFlight; i++)
    98                             {
    99                                 size_t cbTransfer = (cbLeft < cbTestBuf) ? cbLeft : cbTestBuf;
    100 
    101                                 if (!cbTransfer)
    102                                     break;
    103 
    104                                 if (fWrite)
    105                                     rc = RTFileAioReqPrepareWrite(paReqs[i], File, Offset, papvBuf[i],
    106                                                                   cbTransfer, papvBuf[i]);
    107                                 else
    108                                     rc = RTFileAioReqPrepareRead(paReqs[i], File, Offset, papvBuf[i],
    109                                                                  cbTransfer, papvBuf[i]);
    110 
    111                                 cbLeft -= cbTransfer;
    112                                 Offset += cbTransfer;
    113                                 cReqs++;
    114                             }
    115 
    116                             rc = RTFileAioCtxSubmit(hAioContext, paReqs, cReqs);
    117                             if (RT_FAILURE(rc))
    118                             {
    119                                 RTPrintf("tstFileAio: FATAL ERROR - Failed to submit tasks after %d runs. rc=%Rrc\n", cRun, rc);
    120                                 cErrors++;
    121                                 break;
    122                             }
    123 
    124                             /* Wait */
    125                             uint32_t cCompleted = 0;
    126                             rc = RTFileAioCtxWait(hAioContext, cReqs, RT_INDEFINITE_WAIT,
    127                                                   paReqsCompleted, cMaxReqsInFlight,
    128                                                   &cCompleted);
    129                             if (RT_FAILURE(rc))
    130                             {
    131                                 RTPrintf("tstFileAio: FATAL ERROR - Waiting failed. rc=%Rrc\n", rc);
    132                                 cErrors++;
    133                                 break;
    134                             }
    135 
    136                             if (!fWrite)
    137                             {
    138                                 for (uint32_t i = 0; i < cCompleted; i++)
    139                                 {
    140                                     /* Compare that we read the right stuff. */
    141                                     void *pvBuf = RTFileAioReqGetUser(paReqsCompleted[i]);
    142 
    143                                     size_t cbTransfered;
    144                                     int rcReq = RTFileAioReqGetRC(paReqsCompleted[i], &cbTransfered);
    145                                     if (RT_FAILURE(rcReq) || (cbTransfered != cbTestBuf))
    146                                     {
    147                                         RTPrintf("tstFileAio: FATAL ERROR - Request %d failed with rc=%Rrc cbTransfered=%d.\n",
    148                                                  i, rcReq, cbTransfered);
    149                                         cErrors++;
    150                                         rc = rcReq;
    151                                         break;
    152                                     }
    153 
    154                                     if (memcmp(pvBuf, pvTestBuf, cbTestBuf) != 0)
    155                                     {
    156                                         RTPrintf("tstFileAio: FATAL ERROR - Unexpected content in memory.\n");
    157                                         cErrors++;
    158                                         break;
    159                                     }
    160                                     memset(pvBuf, 0, cbTestBuf);
    161                                 }
    162                             }
    163                             cRun++;
    164                             if (RT_FAILURE(rc))
    165                                 break;
    166                         }
    167 
    168                         /* Free buffers. */
    169                         for (unsigned i = 0; i < cMaxReqsInFlight; i++)
    170                             RTMemPageFree(papvBuf[i], cbTestBuf);
    171 
    172                         /* Free requests. */
    173                         for (unsigned i = 0; i < cMaxReqsInFlight; i++)
    174                         {
    175                             rc = RTFileAioReqDestroy(paReqs[i]);
    176                             if (RT_FAILURE(rc))
    177                             {
    178                                 RTPrintf("tstFileAio: ERROR - Failed to destroy request %d. rc=%Rrc\n", i, rc);
    179                                 cErrors++;
    180                             }
    181                         }
    182 
    183                         NanoTS = RTTimeNanoTS() - NanoTS;
    184                         unsigned SpeedKBs = (unsigned)(cbTestFile / (NanoTS / 1000000000.0) / 1024);
    185 
    186                         RTPrintf("tstFileAio: Completed simple %s test: %d.%03d MB/sec\n",
    187                                  fWrite ? "write" : "read",
    188                                  SpeedKBs / 1000,
    189                                  SpeedKBs % 1000);
    190                     }
    191                     else
    192                     {
    193                         RTPrintf("tstFileAio: FATAL ERROR - Failed to asssociate file with async I/O context. rc=%Rrc\n", rc);
    194                         cErrors++;
    195                     }
     214                    tstFileAioTestReadWriteBasic(hFile, false /*fWrite*/, pbTestBuf, TSTFILEAIO_BUFFER_SIZE, 100*_1M, cReqsMax);
     215                    RTFileClose(hFile);
    196216                }
    197                 else
    198                 {
    199                     RTPrintf("tstFileAio: FATAL ERROR - Failed to allocate memory for completed request array.\n");
    200                     cErrors++;
    201                 }
    202                 RTMemFree(papvBuf);
    203217            }
    204             else
    205             {
    206                 RTPrintf("tstFileAio: FATAL ERROR - Failed to allocate memory for buffer array.\n");
    207                 cErrors++;
    208             }
    209             RTMemFree(paReqs);
    210         }
    211         else
    212         {
    213             RTPrintf("tstFileAio: FATAL ERROR - Failed to allocate memory for request array.\n");
    214             cErrors++;
    215         }
    216 
    217         rc = RTFileAioCtxDestroy(hAioContext);
    218         if (RT_FAILURE(rc))
    219         {
    220             RTPrintf("tstFileAio: FATAL ERROR - Failed to destroy async I/O context. rc=%Rrc\n", rc);
    221             cErrors++;
     218
     219            /* Cleanup */
     220            RTFileDelete("tstFileAio#1.tst");
    222221        }
    223222    }
    224     else
    225     {
    226         RTPrintf("tstFileAio: FATAL ERROR - Failed to create async I/O context. rc=%Rrc\n", rc);
    227         cErrors++;
    228     }
    229 }
    230 
    231 int main()
    232 {
    233     RTPrintf("tstFileAio: TESTING\n");
    234     RTR3Init();
    235 
    236     /* Check if the API is available. */
    237     RTFILEAIOLIMITS AioLimits;
    238 
    239     memset(&AioLimits, 0, sizeof(AioLimits));
    240     int rc = RTFileAioGetLimits(&AioLimits);
    241     if (RT_FAILURE(rc))
    242     {
    243         if (rc == VERR_NOT_SUPPORTED)
    244             RTPrintf("tstFileAio: FATAL ERROR - File AIO API not available\n");
    245         else
    246             RTPrintf("tstFileAio: FATAL ERROR - Unexpected error rc=%Rrc\n", rc);
    247         return 1;
    248     }
    249 
    250     RTFILE    File;
    251     void *pvTestBuf = NULL;
    252     rc = RTFileOpen(&File, "tstFileAio#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_ASYNC_IO);
    253     if (RT_FAILURE(rc))
    254     {
    255         RTPrintf("tstFileAio: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
    256         return 1;
    257     }
    258 
    259     pvTestBuf = RTMemAllocZ(64 * _1K);
    260     for (unsigned i = 0; i < 64*_1K; i++)
    261         ((char *)pvTestBuf)[i] = i % 256;
    262 
    263     uint32_t cReqsMax =   AioLimits.cReqsOutstandingMax < TSTFILEAIO_MAX_REQS_IN_FLIGHT
    264                         ? AioLimits.cReqsOutstandingMax
    265                         : TSTFILEAIO_MAX_REQS_IN_FLIGHT;
    266 
    267     /* Basic write test. */
    268     RTPrintf("tstFileAio: Preparing test file, this can take some time and needs quite a bit of harddisk\n");
    269     tstFileAioTestReadWriteBasic(File, true, pvTestBuf, 64*_1K, 100*_1M, cReqsMax);
    270     /* Reopen the file. */
    271     RTFileClose(File);
    272     rc = RTFileOpen(&File, "tstFileAio#1.tst", RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_ASYNC_IO);
    273     if (RT_SUCCESS(rc))
    274     {
    275         tstFileAioTestReadWriteBasic(File, false, pvTestBuf, 64*_1K, 100*_1M, cReqsMax);
    276         RTFileClose(File);
    277     }
    278     else
    279     {
    280         RTPrintf("tstFileAio: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
    281         cErrors++;
    282     }
    283 
    284     /* Cleanup */
    285     RTMemFree(pvTestBuf);
    286     RTFileDelete("tstFileAio#1.tst");
     223
    287224    /*
    288225     * Summary
    289226     */
    290     if (cErrors == 0)
    291         RTPrintf("tstFileAio: SUCCESS\n");
    292     else
    293         RTPrintf("tstFileAio: FAILURE - %d errors\n", cErrors);
    294     return !!cErrors;
     227    return RTTestSummaryAndDestroy(g_hTest);
    295228}
    296229
  • trunk/src/VBox/Runtime/testcase/tstFileAppend-1.cpp

    r28800 r29392  
    55
    66/*
    7  * Copyright (C) 2009 Oracle Corporation
     7 * Copyright (C) 2009-2010 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    3030*******************************************************************************/
    3131#include <iprt/file.h>
     32
    3233#include <iprt/err.h>
    33 #include <iprt/initterm.h>
    34 #include <iprt/stream.h>
    3534#include <iprt/string.h>
    36 
    37 
    38 /*******************************************************************************
    39 *   Global Variables                                                           *
    40 *******************************************************************************/
    41 static int g_cErrors = 0;
    42 
    43 
    44 static int MyFailure(const char *pszFormat, ...)
     35#include <iprt/test.h>
     36
     37
     38void tstFileAppend1(RTTEST hTest)
    4539{
    46     va_list va;
    47 
    48     RTPrintf("tstFileAppend-1: FATAL: ");
    49     va_start(va, pszFormat);
    50     RTPrintfV(pszFormat, va);
    51     va_end(va);
    52     g_cErrors++;
    53     return 1;
    54 }
    55 
    56 
    57 void MyError(const char *pszFormat, ...)
    58 {
    59     va_list va;
    60 
    61     RTPrintf("tstFileAppend-1: ERROR: ");
    62     va_start(va, pszFormat);
    63     RTPrintfV(pszFormat, va);
    64     va_end(va);
    65     g_cErrors++;
    66 }
    67 
    68 int main()
    69 {
    70     int rc;
    71     RTFILE File;
    72     int64_t off;
    73     uint64_t offActual;
    74     size_t cb;
    75     char szBuf[256];
    76 
    77 
    78     RTPrintf("tstFileAppend-1: TESTING...\n");
    79 
    80     RTR3Init();
    81 
    8240    /*
    8341     * Open it write only and do some appending.
    8442     * Checking that read fails and that the file position changes after the write.
    8543     */
     44    RTTestSub(hTest, "Basic 1");
    8645    RTFileDelete("tstFileAppend-1.tst");
    87     rc = RTFileOpen(&File,
    88                     "tstFileAppend-1.tst",
    89                       RTFILE_O_WRITE
    90                     | RTFILE_O_APPEND
    91                     | RTFILE_O_OPEN_CREATE
    92                     | RTFILE_O_DENY_NONE
    93                     | (0644 << RTFILE_O_CREATE_MODE_SHIFT)
    94                    );
    95     if (RT_FAILURE(rc))
    96         return MyFailure("1st RTFileOpen: %Rrc\n", rc);
    97 
    98     off = 0;
    99     rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
    100     if (RT_FAILURE(rc))
    101         MyError("1st RTFileSeek failed: %Rrc\n", rc);
    102     else if (offActual != 0)
    103         MyError("unexpected position on 1st open: %llu - expected 0\n", offActual);
    104 
    105     rc = RTFileWrite(File, "0123456789", 10, &cb);
    106     if (RT_FAILURE(rc))
    107         MyError("1st write fail: %Rrc\n", rc);
    108 
    109     off = 0;
    110     rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
    111     if (RT_FAILURE(rc))
    112         MyError("2nd RTFileSeek failed: %Rrc\n", rc);
    113     else if (offActual != 10)
    114         MyError("unexpected position after 1st write: %llu - expected 10\n", offActual);
    115     else
    116         RTPrintf("tstFileAppend-1: off=%llu after first write\n", offActual);
    117 
    118     rc = RTFileRead(File, szBuf, 1, &cb);
    119     if (RT_SUCCESS(rc))
    120         MyError("read didn't fail! cb=%#lx\n", (long)cb);
    121 
     46    RTFILE hFile = NIL_RTFILE;
     47    int rc = RTFileOpen(&hFile,
     48                        "tstFileAppend-1.tst",
     49                        RTFILE_O_WRITE
     50                        | RTFILE_O_APPEND
     51                        | RTFILE_O_OPEN_CREATE
     52                        | RTFILE_O_DENY_NONE
     53                        | (0644 << RTFILE_O_CREATE_MODE_SHIFT)
     54                        );
     55    RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     56
     57    uint64_t offActual = 42;
     58    uint64_t off       = 0;
     59    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
     60    RTTESTI_CHECK_MSG(offActual == 0 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     61
     62    RTTESTI_CHECK_RC(RTFileWrite(hFile, "0123456789", 10, NULL), VINF_SUCCESS);
     63
     64    offActual = 99;
     65    off = 0;
     66    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
     67    RTTESTI_CHECK_MSG(offActual == 10 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     68    RTTestIPrintf(RTTESTLVL_INFO, "off=%llu after first write\n", offActual);
     69
     70    size_t  cb = 4;
     71    char    szBuf[256];
     72    rc = RTFileRead(hFile, szBuf, 1, &cb);
     73    RTTESTI_CHECK_MSG(rc == VERR_ACCESS_DENIED || rc == VERR_INVALID_HANDLE, ("rc=%Rrc\n", rc));
     74
     75    offActual = 999;
    12276    off = 5;
    123     rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, &offActual);
    124     if (RT_FAILURE(rc))
    125         MyError("3rd RTFileSeek failed: %Rrc\n", rc);
    126     else if (offActual != 5)
    127         MyError("unexpected position after 3rd set file pointer: %llu - expected 5\n", offActual);
    128 
    129     RTFileClose(File);
     77    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, &offActual), VINF_SUCCESS);
     78    RTTESTI_CHECK_MSG(offActual == 5 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     79
     80    RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
    13081
    13182
     
    13485     * Checking the initial position and that it changes after the write.
    13586     */
    136     rc = RTFileOpen(&File,
     87    RTTestSub(hTest, "Basic 2");
     88    rc = RTFileOpen(&hFile,
    13789                    "tstFileAppend-1.tst",
    13890                      RTFILE_O_WRITE
     
    14193                    | RTFILE_O_DENY_NONE
    14294                   );
    143     if (RT_FAILURE(rc))
    144         return MyFailure("2nd RTFileOpen: %Rrc\n", rc);
    145 
    146     off = 0;
    147     rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
    148     if (RT_FAILURE(rc))
    149         MyError("4th RTFileSeek failed: %Rrc\n", rc);
    150     else if (offActual != 0)
    151         MyError("unexpected position on 2nd open: %llu - expected 0\n", offActual);
    152     else
    153         RTPrintf("tstFileAppend-1: off=%llu on 2nd open\n", offActual);
    154 
    155     rc = RTFileWrite(File, "abcdefghij", 10, &cb);
    156     if (RT_FAILURE(rc))
    157         MyError("2nd write fail: %Rrc\n", rc);
    158 
    159     off = 0;
    160     rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
    161     if (RT_FAILURE(rc))
    162         MyError("5th RTFileSeek failed: %Rrc\n", rc);
    163     else if (offActual != 20)
    164         MyError("unexpected position after 2nd write: %llu - expected 20\n", offActual);
    165     else
    166         RTPrintf("tstFileAppend-1: off=%llu after 2nd write\n", offActual);
    167 
    168     RTFileClose(File);
     95    RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     96
     97    offActual = 99;
     98    off = 0;
     99    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
     100    RTTESTI_CHECK_MSG(offActual == 0 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     101    RTTestIPrintf(RTTESTLVL_INFO, "off=%llu on 2nd open\n", offActual);
     102
     103    RTTESTI_CHECK_RC(rc = RTFileWrite(hFile, "abcdefghij", 10, &cb), VINF_SUCCESS);
     104
     105    offActual = 999;
     106    off = 0;
     107    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
     108    RTTESTI_CHECK_MSG(offActual == 20 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     109    RTTestIPrintf(RTTESTLVL_INFO, "off=%llu after 2nd write\n", offActual);
     110
     111    RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
    169112
    170113    /*
     
    174117     * do some seeking and read from a new position.
    175118     */
    176     rc = RTFileOpen(&File,
     119    RTTestSub(hTest, "Basic 3");
     120    rc = RTFileOpen(&hFile,
    177121                    "tstFileAppend-1.tst",
    178122                      RTFILE_O_READWRITE
     
    181125                    | RTFILE_O_DENY_NONE
    182126                   );
    183     if (RT_FAILURE(rc))
    184         return MyFailure("3rd RTFileOpen: %Rrc\n", rc);
    185 
    186     off = 0;
    187     rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
    188     if (RT_FAILURE(rc))
    189         MyError("6th RTFileSeek failed: %Rrc\n", rc);
    190     else if (offActual != 0)
    191         MyError("unexpected position on 3rd open: %llu - expected 0\n", offActual);
    192     else
    193         RTPrintf("tstFileAppend-1: off=%llu on 3rd open\n", offActual);
    194 
    195     rc = RTFileRead(File, szBuf, 10, &cb);
    196     if (RT_FAILURE(rc) || cb != 10)
    197         MyError("1st RTFileRead failed: %Rrc\n", rc);
    198     else if (memcmp(szBuf, "0123456789", 10))
    199         MyError("read the wrong stuff: %.10s - expected 0123456789\n", szBuf);
    200 
    201     off = 0;
    202     rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
    203     if (RT_FAILURE(rc))
    204         MyError("7th RTFileSeek failed: %Rrc\n", rc);
    205     else if (offActual != 10)
    206         MyError("unexpected position after 1st read: %llu - expected 10\n", offActual);
    207     else
    208         RTPrintf("tstFileAppend-1: off=%llu on 1st open\n", offActual);
    209 
    210     rc = RTFileWrite(File, "klmnopqrst", 10, &cb);
    211     if (RT_FAILURE(rc))
    212         MyError("3rd write fail: %Rrc\n", rc);
    213 
    214     off = 0;
    215     rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
    216     if (RT_FAILURE(rc))
    217         MyError("8th RTFileSeek failed: %Rrc\n", rc);
    218     else if (offActual != 30)
    219         MyError("unexpected position after 3rd write: %llu - expected 30\n", offActual);
    220     else
    221         RTPrintf("tstFileAppend-1: off=%llu after 3rd write\n", offActual);
    222 
    223     rc = RTFileRead(File, szBuf, 1, &cb);
    224     if (RT_SUCCESS(rc) && cb != 0)
    225         MyError("read after write didn't fail! cb=%#lx\n", (long)cb);
    226 
     127    RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     128
     129    offActual = 9;
     130    off = 0;
     131    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
     132    RTTESTI_CHECK_MSG(offActual == 0 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     133    RTTestIPrintf(RTTESTLVL_INFO, "off=%llu on 3rd open\n", offActual);
     134
     135    cb = 99;
     136    RTTESTI_CHECK_RC(rc = RTFileRead(hFile, szBuf, 10, &cb), VINF_SUCCESS);
     137    RTTESTI_CHECK(RT_FAILURE(rc) || cb == 10);
     138    RTTESTI_CHECK_MSG(RT_FAILURE(rc) || !memcmp(szBuf, "0123456789", 10), ("read the wrong stuff: %.10s - expected 0123456789\n", szBuf));
     139
     140    offActual = 999;
     141    off = 0;
     142    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
     143    RTTESTI_CHECK_MSG(offActual == 10 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     144    RTTestIPrintf(RTTESTLVL_INFO, "off=%llu on 1st open\n", offActual);
     145
     146    RTTESTI_CHECK_RC(RTFileWrite(hFile, "klmnopqrst", 10, NULL), VINF_SUCCESS);
     147
     148    offActual = 9999;
     149    off = 0;
     150    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
     151    RTTESTI_CHECK_MSG(offActual == 30 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     152    RTTestIPrintf(RTTESTLVL_INFO, "off=%llu after 3rd write\n", offActual);
     153
     154    RTTESTI_CHECK_RC(rc = RTFileRead(hFile, szBuf, 1, NULL), VERR_EOF);
     155    cb = 99;
     156    RTTESTI_CHECK_RC(rc = RTFileRead(hFile, szBuf, 1, &cb), VINF_SUCCESS);
     157    RTTESTI_CHECK(cb == 0);
     158
     159
     160    offActual = 99999;
    227161    off = 15;
    228     rc = RTFileSeek(File, off, RTFILE_SEEK_BEGIN, &offActual);
    229     if (RT_FAILURE(rc))
    230         MyError("9th RTFileSeek failed: %Rrc\n", rc);
    231     else if (offActual != 15)
    232         MyError("unexpected position after 9th seek: %llu - expected 15\n", offActual);
    233     else
     162    RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_BEGIN, &offActual), VINF_SUCCESS);
     163    RTTESTI_CHECK_MSG(offActual == 15 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     164    if (RT_SUCCESS(rc) && offActual == 15)
    234165    {
    235         rc = RTFileRead(File, szBuf, 10, &cb);
    236         if (RT_FAILURE(rc) || cb != 10)
    237             MyError("2nd RTFileRead failed: %Rrc\n", rc);
    238         else if (memcmp(szBuf, "fghijklmno", 10))
    239             MyError("read the wrong stuff: %.10s - expected fghijklmno\n", szBuf);
    240 
     166        RTTESTI_CHECK_RC(rc = RTFileRead(hFile, szBuf, 10, NULL), VINF_SUCCESS);
     167        RTTESTI_CHECK_MSG(RT_FAILURE(rc) || !memcmp(szBuf, "fghijklmno", 10), ("read the wrong stuff: %.10s - expected fghijklmno\n", szBuf));
     168
     169        offActual = 9999999;
    241170        off = 0;
    242         rc = RTFileSeek(File, off, RTFILE_SEEK_CURRENT, &offActual);
    243         if (RT_FAILURE(rc))
    244             MyError("10th RTFileSeek failed: %Rrc\n", rc);
    245         else if (offActual != 25)
    246             MyError("unexpected position after 2nd read: %llu - expected 25\n", offActual);
    247         else
    248             RTPrintf("tstFileAppend-1: off=%llu after 2nd read\n", offActual);
     171        RTTESTI_CHECK_RC(rc = RTFileSeek(hFile, off, RTFILE_SEEK_CURRENT, &offActual), VINF_SUCCESS);
     172        RTTESTI_CHECK_MSG(offActual == 25 || RT_FAILURE(rc), ("offActual=%llu", offActual));
     173        RTTestIPrintf(RTTESTLVL_INFO, "off=%llu after 2nd read\n", offActual);
    249174    }
    250175
    251     RTFileClose(File);
     176    RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
    252177
    253178    /*
    254179     * Open it read only + append and check that we cannot write to it.
    255180     */
    256     rc = RTFileOpen(&File,
     181    RTTestSub(hTest, "Basic 4");
     182    rc = RTFileOpen(&hFile,
    257183                    "tstFileAppend-1.tst",
    258184                      RTFILE_O_READ
    259185                    | RTFILE_O_APPEND
    260186                    | RTFILE_O_OPEN
    261                     | RTFILE_O_DENY_NONE
    262                    );
    263     if (RT_FAILURE(rc))
    264         return MyFailure("4th RTFileOpen: %Rrc\n", rc);
    265 
    266     rc = RTFileWrite(File, "pqrstuvwx", 10, &cb);
    267     if (RT_SUCCESS(rc))
    268         MyError("write didn't fail on read-only+append open: cb=%#lx\n", (long)cb);
    269 
    270     RTFileClose(File);
     187                    | RTFILE_O_DENY_NONE);
     188    RTTESTI_CHECK_RC_RETV(rc, VINF_SUCCESS);
     189
     190    rc = RTFileWrite(hFile, "pqrstuvwx", 10, &cb);
     191    RTTESTI_CHECK_MSG(rc == VERR_ACCESS_DENIED || rc == VERR_INVALID_HANDLE, ("rc=%Rrc\n", rc));
     192
     193    RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
     194    RTTESTI_CHECK_RC(RTFileDelete("tstFileAppend-1.tst"), VINF_SUCCESS);
     195}
     196
     197
     198int main()
     199{
     200    RTTEST hTest;
     201    int rc = RTTestInitAndCreate("tstRTFileAppend-1", &hTest);
     202    if (rc)
     203        return rc;
     204    RTTestBanner(hTest);
     205    tstFileAppend1(hTest);
    271206    RTFileDelete("tstFileAppend-1.tst");
    272 
    273     if (g_cErrors)
    274         RTPrintf("tstFileAppend-1: FAILED\n");
    275     else
    276         RTPrintf("tstFileAppend-1: SUCCESS\n");
    277     return g_cErrors ? 1 : 0;
     207    return RTTestSummaryAndDestroy(hTest);
    278208}
    279209
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