Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | // SPDX-License-Identifier: GPL-2.0+ /* * Tests for the filesystems layer * * Copyright 2025 Simon Glass <sjg@chromium.org> */ #include <dir.h> #include <dm.h> #include <file.h> #include <fs.h> #include <dm/test.h> #include <test/ut.h> #define READ_SIZE 0x20 /* Test basic filesystem access */ static int dm_test_fs_base(struct unit_test_state *uts) { struct udevice *dev; ut_assertok(uclass_first_device_err(UCLASS_FS, &dev)); ut_assertok(fs_mount(dev)); ut_asserteq(-EISCONN, fs_mount(dev)); ut_assertok(fs_unmount(dev)); ut_asserteq(-ENOTCONN, fs_unmount(dev)); return 0; } DM_TEST(dm_test_fs_base, UTF_SCAN_FDT); /* Test accessing a directory */ static int dm_test_fs_dir(struct unit_test_state *uts) { struct udevice *fsdev, *dir; struct fs_dir_stream *strm; struct fs_dirent dent; int found; ut_assertok(uclass_first_device_err(UCLASS_FS, &fsdev)); ut_assertok(fs_mount(fsdev)); ut_asserteq(-ENOENT, fs_lookup_dir(fsdev, "does-not-exit", &dir)); ut_assertok(fs_lookup_dir(fsdev, "", &dir)); ut_assertnonnull(dir); ut_asserteq_str("fs.dir", dir->name); ut_assertok(dir_open(dir, &strm)); found = 0; do { ut_assertok(dir_read(dir, strm, &dent)); if (!strcmp("README", dent.name)) { ut_asserteq(FS_DT_REG, dent.type); found += 1; } else if (!strcmp("common", dent.name)) { ut_asserteq(FS_DT_DIR, dent.type); found += 1; } } while (found < 2); ut_assertok(dir_close(dir, strm)); ut_assertok(fs_unmount(fsdev)); return 0; } DM_TEST(dm_test_fs_dir, UTF_SCAN_FDT); /* Test reading a file */ static int dm_test_fs_file(struct unit_test_state *uts) { struct udevice *fsdev, *dir, *fil; struct file_uc_priv *uc_priv; char buf[READ_SIZE + 1]; ut_assertok(uclass_first_device_err(UCLASS_FS, &fsdev)); ut_assertok(fs_mount(fsdev)); ut_assertok(fs_lookup_dir(fsdev, "", &dir)); ut_assertnonnull(dir); ut_asserteq_str("fs.dir", dir->name); /* check the start and end of the README, which perhaps won't change */ ut_assertok(dir_open_file(dir, "README", DIR_O_RDONLY, &fil)); ut_assertnonnull(fil); ut_asserteq_str("fs.dir.file.1", fil->name); uc_priv = dev_get_uclass_priv(fil); ut_asserteq_str("README", uc_priv->leaf); ut_asserteq(0, uc_priv->pos); ut_assert(uc_priv->size > 0x10000); buf[READ_SIZE] = '\0'; ut_asserteq(READ_SIZE, file_read(fil, buf, READ_SIZE)); ut_asserteq_str("# SPDX-License-Identifier: GPL-2", buf); ut_asserteq(0x20, uc_priv->pos); ut_asserteq(READ_SIZE, file_read_at(fil, buf, uc_priv->size - 0x20, 0)); ut_asserteq_str("d the patch submission process.\n", buf); ut_asserteq(uc_priv->size, uc_priv->pos); return 0; } DM_TEST(dm_test_fs_file, UTF_SCAN_FDT); |