VirtualBox

Changeset 82832 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Jan 22, 2020 4:21:56 PM (5 years ago)
Author:
vboxsync
Message:

tstFile: Converted to new style and added append+truncate testcase (disabled on windows). ticketref:19003

File:
1 edited

Legend:

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

    r80585 r82832  
    2929*   Header Files                                                                                                                 *
    3030*********************************************************************************************************************************/
     31#include <iprt/test.h>
    3132#include <iprt/file.h>
    3233#include <iprt/errcore.h>
    3334#include <iprt/string.h>
    3435#include <iprt/stream.h>
    35 #include <iprt/initterm.h>
    36 
    37 
    38 int main()
     36
     37
     38/*********************************************************************************************************************************
     39*   Global Variables                                                                                                             *
     40*********************************************************************************************************************************/
     41static const char g_szTestStr[] = "Sausages and bacon for breakfast again!\n";
     42static char       g_szTestStr2[] =
     43"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut "
     44"enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor "
     45"in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
     46"proident, sunt in culpa qui officia deserunt mollit anim id est laborum.\n"
     47"\n"
     48"Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, turpis et commodo pharetra, est eros bibendum "
     49"elit, nec luctus magna felis sollicitudin mauris. Integer in mauris eu nibh euismod gravida. Duis ac tellus et risus "
     50"vulputate vehicula. Donec lobortis risus a elit. Etiam tempor. Ut ullamcorper, ligula eu tempor congue, eros est euismod "
     51"turpis, id tincidunt sapien risus a quam. Maecenas fermentum consequat mi. Donec fermentum. Pellentesque malesuada nulla a mi. "
     52"Duis sapien sem, aliquet nec, commodo eget, consequat quis, neque. Aliquam faucibus, elit ut dictum aliquet, felis nisl "
     53"adipiscing sapien, sed malesuada diam lacus eget erat. Cras mollis scelerisque nunc. Nullam arcu. Aliquam consequat. Curabitur "
     54"augue lorem, dapibus quis, laoreet et, pretium ac, nisi. Aenean magna nisl, mollis quis, molestie eu, feugiat in, orci. In hac "
     55"habitasse platea dictumst.\n";
     56
     57
     58static void tstAppend(RTFILE hFile)
    3959{
    40     int         cErrors = 0;
    41     RTPrintf("tstFile: TESTING\n");
    42     RTR3InitExeNoArguments(0);
    43 
    44     RTFILE    File;
    45     int rc = RTFileOpen(&File, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE);
     60    char achBuf[sizeof(g_szTestStr2) * 4];
     61
     62    /*
     63     * Write some stuff and read it back.
     64     */
     65    size_t const cbWrite1 = sizeof(g_szTestStr2)  / 4;
     66    RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, g_szTestStr2, sizeof(g_szTestStr2) - 1, NULL), VINF_SUCCESS);
     67
     68    size_t const offWrite2 = cbWrite1;
     69    size_t const cbWrite2  = sizeof(g_szTestStr2) / 2;
     70    RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
     71    RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite2], cbWrite2, NULL), VINF_SUCCESS);
     72
     73    RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
     74    RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite1 + cbWrite2, NULL), VINF_SUCCESS);
     75    if (memcmp(achBuf, g_szTestStr2, cbWrite1 + cbWrite2) != 0)
     76        RTTestIFailed("Read back #1 failed (%#zx + %#zx)", cbWrite1, cbWrite2);
     77
     78#ifndef RT_OS_WINDOWS
     79    /*
     80     * Truncate the file and write some more. This is problematic on windows.
     81     */
     82    RTTESTI_CHECK_RC_RETV(RTFileSetSize(hFile, 0), VINF_SUCCESS);
     83
     84    size_t const offWrite3 = cbWrite1 + cbWrite2;
     85    size_t const cbWrite3  = sizeof(g_szTestStr2) - 1 - offWrite3;
     86    RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
     87    RTTESTI_CHECK_RC_RETV(RTFileWrite(hFile, &g_szTestStr2[offWrite3], cbWrite3, NULL), VINF_SUCCESS);
     88
     89    RTTESTI_CHECK_RC_RETV(RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL), VINF_SUCCESS);
     90    RTTESTI_CHECK_RC_RETV(RTFileRead(hFile, achBuf, cbWrite3, NULL), VINF_SUCCESS);
     91    if (memcmp(achBuf, &g_szTestStr2[offWrite3], cbWrite3) != 0)
     92        RTTestIFailed("Read back #2 failed (%#zx)", cbWrite3);
     93#endif
     94}
     95
     96
     97static void tstBasics(RTFILE hFile)
     98{
     99    RTFOFF cbMax = -2;
     100    int rc = RTFileQueryMaxSizeEx(hFile, &cbMax);
     101    if (rc != VERR_NOT_IMPLEMENTED)
     102    {
     103        if (rc != VINF_SUCCESS)
     104            RTTestIFailed("RTFileQueryMaxSizeEx failed: %Rrc", rc);
     105        else
     106        {
     107            RTTESTI_CHECK_MSG(cbMax > 0, ("cbMax=%RTfoff", cbMax));
     108            RTTESTI_CHECK_MSG(cbMax == RTFileGetMaxSize(hFile),
     109                              ("cbMax=%RTfoff, RTFileGetMaxSize->%RTfoff", cbMax, RTFileGetMaxSize(hFile)));
     110        }
     111    }
     112
     113    /* grow file beyond 2G */
     114    rc = RTFileSetSize(hFile, _2G + _1M);
    46115    if (RT_FAILURE(rc))
    47     {
    48         RTPrintf("tstFile: FATAL ERROR - Failed to open file #1. rc=%Rrc\n", rc);
    49         return 1;
    50     }
    51 
    52     RTFOFF cbMax = -2;
    53     rc = RTFileQueryMaxSizeEx(File, &cbMax);
    54     if (RT_FAILURE(rc))
    55     {
    56         RTPrintf("tstFile: RTFileQueryMaxSizeEx failed: %Rrc\n", rc);
    57         cErrors++;
    58     }
    59     else if (cbMax <= 0)
    60     {
    61         RTPrintf("tstFile: RTFileQueryMaxSizeEx failed: cbMax=%RTfoff\n", cbMax);
    62         cErrors++;
    63     }
    64     else if (RTFileGetMaxSize(File) != cbMax)
    65     {
    66         RTPrintf("tstFile: RTFileGetMaxSize failed; returns %RTfoff instead of %RTfoff\n", RTFileGetMaxSize(File), cbMax);
    67         cErrors++;
    68     }
    69     else
    70         RTPrintf("Maximum file size is %RTfoff bytes.\n", cbMax);
    71 
    72     /* grow file beyond 2G */
    73     rc = RTFileSetSize(File, _2G + _1M);
    74     if (RT_FAILURE(rc))
    75     {
    76         RTPrintf("Failed to grow file #1 to 2.001GB. rc=%Rrc\n", rc);
    77         cErrors++;
    78     }
     116        RTTestIFailed("Failed to grow file #1 to 2.001GB. rc=%Rrc", rc);
    79117    else
    80118    {
    81119        uint64_t cb;
    82         rc = RTFileQuerySize(File, &cb);
    83         if (RT_FAILURE(rc))
    84         {
    85             RTPrintf("Failed to get file size of #1. rc=%Rrc\n", rc);
    86             cErrors++;
    87         }
    88         else if (cb != _2G + _1M)
    89         {
    90             RTPrintf("RTFileQuerySize return %RX64 bytes, expected %RX64.\n", cb, _2G + _1M);
    91             cErrors++;
    92         }
    93         else
    94             RTPrintf("tstFile: cb=%RX64\n", cb);
     120        RTTESTI_CHECK_RC(RTFileQuerySize(hFile, &cb), VINF_SUCCESS);
     121        RTTESTI_CHECK_MSG(cb == _2G + _1M, ("RTFileQuerySize return %RX64 bytes, expected %RX64.", cb, _2G + _1M));
    95122
    96123        /*
    97124         * Try some writes at the beginning of the file.
    98125         */
    99         uint64_t offFile = RTFileTell(File);
    100         if (offFile != 0)
    101         {
    102             RTPrintf("RTFileTell -> %#RX64, expected 0 (#1)\n", offFile);
    103             cErrors++;
    104         }
    105         static const char szTestBuf[] = "Sausages and bacon for breakfast again!";
     126        uint64_t offFile = RTFileTell(hFile);
     127        RTTESTI_CHECK_MSG(offFile == 0, ("RTFileTell -> %#RX64, expected 0 (#1)", offFile));
     128
    106129        size_t cbWritten = 0;
    107         while (cbWritten < sizeof(szTestBuf))
     130        while (cbWritten < sizeof(g_szTestStr))
    108131        {
    109132            size_t cbWrittenPart;
    110             rc = RTFileWrite(File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);
     133            rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
    111134            if (RT_FAILURE(rc))
    112135                break;
     
    114137        }
    115138        if (RT_FAILURE(rc))
    116         {
    117             RTPrintf("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
    118             cErrors++;
    119         }
     139            RTTestIFailed("Failed to write to file #1 at offset 0. rc=%Rrc\n", rc);
    120140        else
    121141        {
    122142            /* check that it was written correctly. */
    123             rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL);
     143            rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
    124144            if (RT_FAILURE(rc))
    125             {
    126                 RTPrintf("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
    127                 cErrors++;
    128             }
     145                RTTestIFailed("Failed to seek offset 0 in file #1. rc=%Rrc\n", rc);
    129146            else
    130147            {
    131                 char        szReadBuf[sizeof(szTestBuf)];
     148                char        szReadBuf[sizeof(g_szTestStr)];
    132149                size_t      cbRead = 0;
    133                 while (cbRead < sizeof(szTestBuf))
     150                while (cbRead < sizeof(g_szTestStr))
    134151                {
    135152                    size_t cbReadPart;
    136                     rc = RTFileRead(File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);
     153                    rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
    137154                    if (RT_FAILURE(rc))
    138155                        break;
     
    140157                }
    141158                if (RT_FAILURE(rc))
    142                 {
    143                     RTPrintf("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
    144                     cErrors++;
    145                 }
     159                    RTTestIFailed("Failed to read from file #1 at offset 0. rc=%Rrc\n", rc);
    146160                else
    147161                {
    148                     if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))
     162                    if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
    149163                        RTPrintf("tstFile: head write ok\n");
    150164                    else
    151                     {
    152                         RTPrintf("Data read from file #1 at offset 0 differs from what we wrote there.\n");
    153                         cErrors++;
    154                     }
     165                        RTTestIFailed("Data read from file #1 at offset 0 differs from what we wrote there.\n");
    155166                }
    156167            }
     
    160171         * Try some writes at the end of the file.
    161172         */
    162         rc = RTFileSeek(File, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
    163         if (RT_FAILURE(rc))
    164         {
    165             RTPrintf("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
    166             cErrors++;
    167         }
    168         else
    169         {
    170             offFile = RTFileTell(File);
     173        rc = RTFileSeek(hFile, _2G + _1M, RTFILE_SEEK_BEGIN, NULL);
     174        if (RT_FAILURE(rc))
     175            RTTestIFailed("Failed to seek to _2G + _1M in file #1. rc=%Rrc\n", rc);
     176        else
     177        {
     178            offFile = RTFileTell(hFile);
    171179            if (offFile != _2G + _1M)
    172             {
    173                 RTPrintf("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
    174                 cErrors++;
    175             }
     180                RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#2)\n", offFile, _2G + _1M);
    176181            else
    177182            {
    178183                cbWritten = 0;
    179                 while (cbWritten < sizeof(szTestBuf))
     184                while (cbWritten < sizeof(g_szTestStr))
    180185                {
    181186                    size_t cbWrittenPart;
    182                     rc = RTFileWrite(File, &szTestBuf[cbWritten], sizeof(szTestBuf) - cbWritten, &cbWrittenPart);
     187                    rc = RTFileWrite(hFile, &g_szTestStr[cbWritten], sizeof(g_szTestStr) - cbWritten, &cbWrittenPart);
    183188                    if (RT_FAILURE(rc))
    184189                        break;
     
    186191                }
    187192                if (RT_FAILURE(rc))
    188                 {
    189                     RTPrintf("Failed to write to file #1 at offset 2G + 1M.  rc=%Rrc\n", rc);
    190                     cErrors++;
    191                 }
     193                    RTTestIFailed("Failed to write to file #1 at offset 2G + 1M.  rc=%Rrc\n", rc);
    192194                else
    193195                {
    194                     rc = RTFileSeek(File, offFile, RTFILE_SEEK_BEGIN, NULL);
     196                    rc = RTFileSeek(hFile, offFile, RTFILE_SEEK_BEGIN, NULL);
    195197                    if (RT_FAILURE(rc))
    196                     {
    197                         RTPrintf("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
    198                         cErrors++;
    199                     }
     198                        RTTestIFailed("Failed to seek offset %RX64 in file #1. rc=%Rrc\n", offFile, rc);
    200199                    else
    201200                    {
    202                         char        szReadBuf[sizeof(szTestBuf)];
     201                        char        szReadBuf[sizeof(g_szTestStr)];
    203202                        size_t      cbRead = 0;
    204                         while (cbRead < sizeof(szTestBuf))
     203                        while (cbRead < sizeof(g_szTestStr))
    205204                        {
    206205                            size_t cbReadPart;
    207                             rc = RTFileRead(File, &szReadBuf[cbRead], sizeof(szTestBuf) - cbRead, &cbReadPart);
     206                            rc = RTFileRead(hFile, &szReadBuf[cbRead], sizeof(g_szTestStr) - cbRead, &cbReadPart);
    208207                            if (RT_FAILURE(rc))
    209208                                break;
     
    211210                        }
    212211                        if (RT_FAILURE(rc))
    213                         {
    214                             RTPrintf("Failed to read from file #1 at offset 2G + 1M.  rc=%Rrc\n", rc);
    215                             cErrors++;
    216                         }
     212                            RTTestIFailed("Failed to read from file #1 at offset 2G + 1M.  rc=%Rrc\n", rc);
    217213                        else
    218214                        {
    219                             if (!memcmp(szReadBuf, szTestBuf, sizeof(szTestBuf)))
     215                            if (!memcmp(szReadBuf, g_szTestStr, sizeof(g_szTestStr)))
    220216                                RTPrintf("tstFile: tail write ok\n");
    221217                            else
    222                             {
    223                                 RTPrintf("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
    224                                 cErrors++;
    225                             }
     218                                RTTestIFailed("Data read from file #1 at offset 2G + 1M differs from what we wrote there.\n");
    226219                        }
    227220                    }
     
    233226         * Some general seeking around.
    234227         */
    235         rc = RTFileSeek(File, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
    236         if (RT_FAILURE(rc))
    237         {
    238             RTPrintf("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
    239             cErrors++;
    240         }
    241         else
    242         {
    243             offFile = RTFileTell(File);
     228        rc = RTFileSeek(hFile, _2G + 1, RTFILE_SEEK_BEGIN, NULL);
     229        if (RT_FAILURE(rc))
     230            RTTestIFailed("Failed to seek to _2G + 1 in file #1. rc=%Rrc\n", rc);
     231        else
     232        {
     233            offFile = RTFileTell(hFile);
    244234            if (offFile != _2G + 1)
    245             {
    246                 RTPrintf("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
    247                 cErrors++;
    248             }
     235                RTTestIFailed("RTFileTell -> %#llx, expected %#llx (#3)\n", offFile, _2G + 1);
    249236        }
    250237
    251238        /* seek end */
    252         rc = RTFileSeek(File, 0, RTFILE_SEEK_END, NULL);
    253         if (RT_FAILURE(rc))
    254         {
    255             RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc);
    256             cErrors++;
    257         }
    258         else
    259         {
    260             offFile = RTFileTell(File);
    261             if (offFile != _2G + _1M + sizeof(szTestBuf)) /* assuming tail write was ok. */
    262             {
    263                 RTPrintf("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(szTestBuf));
    264                 cErrors++;
    265             }
     239        rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, NULL);
     240        if (RT_FAILURE(rc))
     241            RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
     242        else
     243        {
     244            offFile = RTFileTell(hFile);
     245            if (offFile != _2G + _1M + sizeof(g_szTestStr)) /* assuming tail write was ok. */
     246                RTTestIFailed("RTFileTell -> %#RX64, expected %#RX64 (#4)\n", offFile, _2G + _1M + sizeof(g_szTestStr));
    266247        }
    267248
    268249        /* seek start */
    269         rc = RTFileSeek(File, 0, RTFILE_SEEK_BEGIN, NULL);
    270         if (RT_FAILURE(rc))
    271         {
    272             RTPrintf("Failed to seek to end of file #1. rc=%Rrc\n", rc);
    273             cErrors++;
    274         }
    275         else
    276         {
    277             offFile = RTFileTell(File);
     250        rc = RTFileSeek(hFile, 0, RTFILE_SEEK_BEGIN, NULL);
     251        if (RT_FAILURE(rc))
     252            RTTestIFailed("Failed to seek to end of file #1. rc=%Rrc\n", rc);
     253        else
     254        {
     255            offFile = RTFileTell(hFile);
    278256            if (offFile != 0)
    279             {
    280                 RTPrintf("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
    281                 cErrors++;
    282             }
     257                RTTestIFailed("RTFileTell -> %#llx, expected 0 (#5)\n", offFile);
    283258        }
    284259    }
    285 
    286 
    287     /*
    288      * Cleanup.
    289      */
    290     rc = RTFileClose(File);
    291     if (RT_FAILURE(rc))
     260}
     261
     262int main()
     263{
     264    RTTEST      hTest;
     265    RTEXITCODE rcExit = RTTestInitAndCreate("tstRTFile", &hTest);
     266    if (rcExit != RTEXITCODE_SUCCESS)
     267        return rcExit;
     268    RTTestBanner(hTest);
     269
     270    /*
     271     * Some basic tests.
     272     */
     273    RTTestSub(hTest, "Basics");
     274    int rc = -1;
     275    RTFILE hFile = NIL_RTFILE;
     276    RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#1.tst", RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE),
     277                     VINF_SUCCESS);
     278    if (RT_SUCCESS(rc))
    292279    {
    293         RTPrintf("Failed to close file #1. rc=%Rrc\n", rc);
    294         cErrors++;
     280        tstBasics(hFile);
     281        RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
     282        RTTESTI_CHECK_RC(RTFileDelete("tstFile#1.tst"), VINF_SUCCESS);
    295283    }
    296     rc = RTFileDelete("tstFile#1.tst");
    297     if (RT_FAILURE(rc))
     284
     285    /*
     286     * Test appending & truncation.
     287     */
     288    RTTestSub(hTest, "Basics");
     289    hFile = NIL_RTFILE;
     290    RTTESTI_CHECK_RC(rc = RTFileOpen(&hFile, "tstFile#2.tst",
     291                                     RTFILE_O_READWRITE | RTFILE_O_CREATE_REPLACE | RTFILE_O_DENY_NONE | RTFILE_O_APPEND),
     292                     VINF_SUCCESS);
     293    if (RT_SUCCESS(rc))
    298294    {
    299         RTPrintf("Failed to delete file #1. rc=%Rrc\n", rc);
    300         cErrors++;
     295        tstAppend(hFile);
     296        RTTESTI_CHECK_RC(RTFileClose(hFile), VINF_SUCCESS);
     297        RTTESTI_CHECK_RC(RTFileDelete("tstFile#2.tst"), VINF_SUCCESS);
    301298    }
    302299
    303300    /*
    304      * Summary
    305      */
    306     if (cErrors == 0)
    307         RTPrintf("tstFile: SUCCESS\n");
    308     else
    309         RTPrintf("tstFile: FAILURE - %d errors\n", cErrors);
    310     return !!cErrors;
     301     * Done.
     302     */
     303    return RTTestSummaryAndDestroy(hTest);
    311304}
    312305
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