Changeset d101368 in mainline for uspace/lib/fmgt/test


Ignore:
Timestamp:
2025-11-14T16:41:33Z (5 months ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
master
Children:
3a4c6d9
Parents:
4b2065a6
git-author:
Jiri Svoboda <jiri@…> (2025-11-14 16:32:57)
git-committer:
Jiri Svoboda <jiri@…> (2025-11-14 16:41:33)
Message:

Split new file creation into a separate fmgt module.

Location:
uspace/lib/fmgt/test
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/fmgt/test/fmgt.c

    r4b2065a6 rd101368  
    3737PCUT_TEST_SUITE(fmgt);
    3838
    39 #define TEMP_DIR "/tmp"
    40 
    41 static void test_fmgt_progress(void *, fmgt_progress_t *);
    42 
    43 static fmgt_cb_t test_fmgt_cb = {
    44         .progress = test_fmgt_progress
    45 };
    46 
    47 typedef struct {
    48         unsigned nupdates;
    49 } test_resp_t;
    50 
    5139/** Create and destroy file management object succeeds. */
    5240PCUT_TEST(create_destroy)
     
    6250}
    6351
    64 /** Suggesting new file name succeeds and returns unique name. */
    65 PCUT_TEST(new_file_suggest)
    66 {
    67         FILE *f = NULL;
    68         char *fname1 = NULL;
    69         char *fname2 = NULL;
    70         errno_t rc;
    71         int rv;
    72 
    73         rc = vfs_cwd_set(TEMP_DIR);
    74         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    75 
    76         /* Suggest unique file name. */
    77         rc = fmgt_new_file_suggest(&fname1);
    78         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    79 
    80         /* See if we can actually create the file. */
    81         f = fopen(fname1, "wx");
    82         PCUT_ASSERT_NOT_NULL(f);
    83         (void) fclose(f);
    84 
    85         /* Now suggest another unique file name. */
    86         rc = fmgt_new_file_suggest(&fname2);
    87         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    88 
    89         /* They should be different. */
    90         PCUT_ASSERT_FALSE(str_cmp(fname1, fname2) == 0);
    91 
    92         /* Remove the file. */
    93         rv = remove(fname1);
    94         PCUT_ASSERT_INT_EQUALS(0, rv);
    95 
    96         free(fname1);
    97         free(fname2);
    98 }
    99 
    100 /** New empty file can be created. */
    101 PCUT_TEST(new_file_empty)
    102 {
    103         fmgt_t *fmgt = NULL;
    104         char *fname = NULL;
    105         errno_t rc;
    106         int rv;
    107 
    108         rc = vfs_cwd_set(TEMP_DIR);
    109         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    110 
    111         rc = fmgt_create(&fmgt);
    112         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    113 
    114         /* Suggest unique file name. */
    115         rc = fmgt_new_file_suggest(&fname);
    116         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    117 
    118         rc = fmgt_new_file(fmgt, fname, 0, nf_none);
    119         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    120 
    121         /* Remove the file [this also verifies the file exists]. */
    122         rv = remove(fname);
    123         PCUT_ASSERT_INT_EQUALS(0, rv);
    124 
    125         free(fname);
    126         fmgt_destroy(fmgt);
    127 }
    128 
    129 /** New zero-filled file can be created. */
    130 PCUT_TEST(new_file_zerofill)
    131 {
    132         FILE *f = NULL;
    133         fmgt_t *fmgt = NULL;
    134         char *fname = NULL;
    135         char buf[128];
    136         errno_t rc;
    137         size_t nr;
    138         size_t total_rd;
    139         size_t i;
    140         int rv;
    141 
    142         rc = vfs_cwd_set(TEMP_DIR);
    143         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    144 
    145         rc = fmgt_create(&fmgt);
    146         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    147 
    148         /* Suggest unique file name. */
    149         rc = fmgt_new_file_suggest(&fname);
    150         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    151 
    152         rc = fmgt_new_file(fmgt, fname, 20000, nf_none);
    153         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    154 
    155         /* Verify the file. */
    156         f = fopen(fname, "rb");
    157         PCUT_ASSERT_NOT_NULL(f);
    158         total_rd = 0;
    159         do {
    160                 nr = fread(buf, 1, sizeof(buf), f);
    161                 for (i = 0; i < nr; i++)
    162                         PCUT_ASSERT_INT_EQUALS(0, buf[i]);
    163 
    164                 total_rd += nr;
    165         } while (nr > 0);
    166 
    167         PCUT_ASSERT_INT_EQUALS(20000, total_rd);
    168 
    169         (void)fclose(f);
    170 
    171         /* Remove the file. */
    172         rv = remove(fname);
    173         PCUT_ASSERT_INT_EQUALS(0, rv);
    174 
    175         free(fname);
    176         fmgt_destroy(fmgt);
    177 }
    178 
    179 /** New sparse file can be created. */
    180 PCUT_TEST(new_file_sparse)
    181 {
    182         FILE *f = NULL;
    183         fmgt_t *fmgt = NULL;
    184         char *fname = NULL;
    185         char buf[128];
    186         errno_t rc;
    187         size_t nr;
    188         size_t total_rd;
    189         size_t i;
    190         int rv;
    191 
    192         rc = vfs_cwd_set(TEMP_DIR);
    193         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    194 
    195         rc = fmgt_create(&fmgt);
    196         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    197 
    198         /* Suggest unique file name. */
    199         rc = fmgt_new_file_suggest(&fname);
    200         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    201 
    202         rc = fmgt_new_file(fmgt, fname, 20000, nf_sparse);
    203         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    204 
    205         /* Verify the file. */
    206         f = fopen(fname, "rb");
    207         PCUT_ASSERT_NOT_NULL(f);
    208         total_rd = 0;
    209         do {
    210                 nr = fread(buf, 1, sizeof(buf), f);
    211                 for (i = 0; i < nr; i++)
    212                         PCUT_ASSERT_INT_EQUALS(0, buf[i]);
    213 
    214                 total_rd += nr;
    215         } while (nr > 0);
    216 
    217         PCUT_ASSERT_INT_EQUALS(20000, total_rd);
    218 
    219         (void)fclose(f);
    220 
    221         /* Remove the file. */
    222         rv = remove(fname);
    223         PCUT_ASSERT_INT_EQUALS(0, rv);
    224 
    225         free(fname);
    226         fmgt_destroy(fmgt);
    227 }
    228 
    229 /** Initial update provided when requested while creating new file. */
    230 PCUT_TEST(new_file_init_upd)
    231 {
    232         FILE *f = NULL;
    233         fmgt_t *fmgt = NULL;
    234         char *fname = NULL;
    235         char buf[128];
    236         errno_t rc;
    237         size_t nr;
    238         size_t total_rd;
    239         size_t i;
    240         test_resp_t resp;
    241         int rv;
    242 
    243         rc = vfs_cwd_set(TEMP_DIR);
    244         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    245 
    246         rc = fmgt_create(&fmgt);
    247         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    248 
    249         fmgt_set_cb(fmgt, &test_fmgt_cb, (void *)&resp);
    250         resp.nupdates = 0;
    251 
    252         fmgt_set_init_update(fmgt, true);
    253 
    254         /* Suggest unique file name. */
    255         rc = fmgt_new_file_suggest(&fname);
    256         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    257 
    258         rc = fmgt_new_file(fmgt, fname, 20000, nf_none);
    259         PCUT_ASSERT_ERRNO_VAL(EOK, rc);
    260 
    261         /* Verify the file. */
    262         f = fopen(fname, "rb");
    263         PCUT_ASSERT_NOT_NULL(f);
    264         total_rd = 0;
    265         do {
    266                 nr = fread(buf, 1, sizeof(buf), f);
    267                 for (i = 0; i < nr; i++)
    268                         PCUT_ASSERT_INT_EQUALS(0, buf[i]);
    269 
    270                 total_rd += nr;
    271         } while (nr > 0);
    272 
    273         PCUT_ASSERT_INT_EQUALS(20000, total_rd);
    274 
    275         (void)fclose(f);
    276 
    277         PCUT_ASSERT_TRUE(resp.nupdates > 0);
    278 
    279         /* Remove the file. */
    280         rv = remove(fname);
    281         PCUT_ASSERT_INT_EQUALS(0, rv);
    282 
    283         free(fname);
    284         fmgt_destroy(fmgt);
    285 }
    286 
    287 static void test_fmgt_progress(void *arg, fmgt_progress_t *progress)
    288 {
    289         test_resp_t *resp = (test_resp_t *)arg;
    290 
    291         (void)progress;
    292         ++resp->nupdates;
    293 }
    294 
    29552PCUT_EXPORT(fmgt);
  • uspace/lib/fmgt/test/main.c

    r4b2065a6 rd101368  
    3232
    3333PCUT_IMPORT(fmgt);
     34PCUT_IMPORT(newfile);
    3435
    3536PCUT_MAIN();
Note: See TracChangeset for help on using the changeset viewer.