|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Tenstorrent AI ULC |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <errno.h> |
| 8 | +#include <stdio.h> |
| 9 | +#include <string.h> |
| 10 | + |
| 11 | +#include <ff.h> |
| 12 | +#include <zephyr/fs/fs.h> |
| 13 | +#include <zephyr/sys/util.h> |
| 14 | +#include <zephyr/ztest.h> |
| 15 | + |
| 16 | +static FATFS _fs; |
| 17 | + |
| 18 | +static struct fs_mount_t _mnt = { |
| 19 | + .type = FS_FATFS, |
| 20 | + .mnt_point = "/", |
| 21 | + .fs_data = &_fs, |
| 22 | +}; |
| 23 | + |
| 24 | +struct _entry { |
| 25 | + const char *const name; |
| 26 | + const char *const data; |
| 27 | + FILE *fp; |
| 28 | +}; |
| 29 | + |
| 30 | +static struct _entry _data[] = { |
| 31 | + {.name = "/tmp/foo.txt", .data = ""}, |
| 32 | + {.name = "/tmp/bar.txt", .data = ""}, |
| 33 | +}; |
| 34 | + |
| 35 | +void setup_callback(void *arg); |
| 36 | +void before_callback(void *arg); |
| 37 | +void after_callback(void *arg); |
| 38 | + |
| 39 | +void *setup(void) |
| 40 | +{ |
| 41 | + int ret; |
| 42 | + |
| 43 | + memset(&_fs, 0, sizeof(_fs)); |
| 44 | + |
| 45 | + ret = fs_mount(&_mnt); |
| 46 | + zassert_ok(ret, "mount failed: %d", ret); |
| 47 | + |
| 48 | + ret = fs_mkdir("/tmp"); |
| 49 | + zassert_ok(ret, "mkdir failed: %d", ret); |
| 50 | + |
| 51 | + setup_callback(NULL); |
| 52 | + |
| 53 | + return NULL; |
| 54 | +} |
| 55 | + |
| 56 | +void before(void *arg) |
| 57 | +{ |
| 58 | + ARG_UNUSED(arg); |
| 59 | + |
| 60 | + ARRAY_FOR_EACH_PTR(_data, entry) { |
| 61 | + int len; |
| 62 | + int ret; |
| 63 | + |
| 64 | + entry->fp = fopen(entry->name, "w+"); |
| 65 | + zassert_not_null(entry->fp, "fopen() failed: %d", errno); |
| 66 | + |
| 67 | + len = strlen(entry->data); |
| 68 | + if (len > 0) { |
| 69 | + ret = (int)fwrite(entry->data, len, 1, entry->fp); |
| 70 | + zassert_equal(ret, len, "%s returned %d instead of %d: %d", "fwrite", ret, |
| 71 | + len, errno); |
| 72 | + } |
| 73 | + |
| 74 | + before_callback(entry->fp); |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +void after(void *arg) |
| 79 | +{ |
| 80 | + ARG_UNUSED(arg); |
| 81 | + |
| 82 | + ARRAY_FOR_EACH_PTR(_data, entry) { |
| 83 | + (void)fclose(entry->fp); |
| 84 | + }; |
| 85 | + |
| 86 | + after_callback(NULL); |
| 87 | +} |
| 88 | + |
| 89 | +void teardown(void *arg) |
| 90 | +{ |
| 91 | + ARG_UNUSED(arg); |
| 92 | + (void)fs_unmount(&_mnt); |
| 93 | +} |
0 commit comments