diff options
| author | manuel <manuel.bott@eps.ch> | 2026-11-22 01:06:22 +0000 |
|---|---|---|
| committer | manuel <manuel.bott@eps.ch> | 2026-11-22 01:06:22 +0000 |
| commit | d260c7339080e8c72224b68b93781139c297861f (patch) | |
| tree | 1c40512c6b4257c0fdc7c1ec6a7168f0c2808758 | |
| parent | ea79ee8e74f28f05803f458294f17fc873df2153 (diff) | |
| download | kernel-test/fat32-real-image.tar.xz kernel-test/fat32-real-image.zip | |
kernel/filesystem: add bigger fat32 image to test and adjust teststest/fat32-real-imagefa26/develop
| -rw-r--r-- | arch/x86_64/support/modules/README.md | 24 | ||||
| -rw-r--r-- | kernel/kapi/filesystem.tests.cpp | 55 | ||||
| -rw-r--r-- | kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp | 109 | ||||
| -rw-r--r-- | kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp | 158 | ||||
| -rw-r--r-- | kernel/kernel/filesystems/fat32/filesystem.tests.cpp | 168 | ||||
| -rw-r--r-- | kernel/kernel/filesystems/fat32/inode.tests.cpp | 296 | ||||
| -rw-r--r-- | kernel/kernel/test_support/filesystems/fat32_image.cpp | 169 | ||||
| -rw-r--r-- | kernel/kernel/test_support/filesystems/fat32_image.hpp | 135 | ||||
| -rw-r--r--[l---------] | kernel/kernel/test_support/vfs/test_assets/fat32.img | 4 | ||||
| -rwxr-xr-x | scripts/create_fat32_test_image.sh | 65 |
10 files changed, 979 insertions, 204 deletions
diff --git a/arch/x86_64/support/modules/README.md b/arch/x86_64/support/modules/README.md index 6d235a7a..f20a4ca3 100644 --- a/arch/x86_64/support/modules/README.md +++ b/arch/x86_64/support/modules/README.md @@ -128,3 +128,27 @@ The ext2_4KB_fs image is intentionally fragmented, as some files were created an ./symlinks ./symlinks/symlink_chain_1 -> ../enclosures/lion_house/symlink_chain_2 ./symlinks/very_long_symlink -> ../enclosures/aquarium/tank_2/this_is_a_very_very_long_fish_filename_that_keeps_going_and_going_until_it_almost_breaks_linux_filesystem_limits_for_testing_purposes_and_we_add_more_characters_to_make_it_even_longer_30.txt + +## fat32.img (boot module) +Mounted by the fat32 demo in `kernel/kernel/main.cpp`. + +. +./HELLO.TXT + +## fat32.img (test asset) +`kernel/kernel/test_support/vfs/test_assets/fat32.img` is a separate, larger image used by the fat32 driver tests. +It is created by `scripts/create_fat32_test_image.sh`; its exact layout (cluster numbers, sizes and offsets) is +documented in `kernel/kernel/test_support/filesystems/fat32_image.hpp`, which has to be updated when the image is +regenerated. Long file names are listed with their 8.3 alias in parentheses. + +. +./docs +./docs/sub +./docs/sub/file1.txt +./docs/sub/file2.txt +./docs/tmp1.bin ... ./docs/tmp35.bin (odd numbers only, the even ones are deleted) +./docs/fragment.txt +./docs/file.txt +./longDirectoryName (LONGDI~1) +./longDirectoryName/longFileName.jpeg (LONGFI~1.JPE) +./hello.txt diff --git a/kernel/kapi/filesystem.tests.cpp b/kernel/kapi/filesystem.tests.cpp index f71eead0..baa587e5 100644 --- a/kernel/kapi/filesystem.tests.cpp +++ b/kernel/kapi/filesystem.tests.cpp @@ -1,5 +1,6 @@ #include <kapi/filesystem.hpp> +#include <kernel/test_support/filesystems/fat32_image.hpp> #include <kernel/test_support/vfs/storage_boot_module_vfs_fixture.hpp> #include <kstd/system_error.hpp> @@ -347,6 +348,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_vfs_fixture, "Kapi files { auto const ext2_image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "ext2_1KB_fs.img"; auto const fat32_image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "fat32.img"; + namespace image = kernel::tests::filesystems::fat32::image; GIVEN("an ext2 root filesystem with a fat32 image mounted on /information") { @@ -375,7 +377,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_vfs_fixture, "Kapi files { auto const fd = kapi::filesystem::open("/information/hello.txt"); REQUIRE(fd); - REQUIRE(read_text(*fd, 64) == "Hello World!"); + REQUIRE(read_text(*fd, 64) == image::hello_content); REQUIRE(kapi::filesystem::close(*fd)); } @@ -383,7 +385,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_vfs_fixture, "Kapi files { auto const fd = kapi::filesystem::open("/information/hello.txt"); REQUIRE(fd); - REQUIRE(read_text(*fd, 5) == "Hello World!"); + REQUIRE(read_text(*fd, 5) == image::hello_content); REQUIRE(kapi::filesystem::close(*fd)); } @@ -392,7 +394,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_vfs_fixture, "Kapi files auto const fd = kapi::filesystem::open("/information/hello.txt"); REQUIRE(fd); REQUIRE(kapi::filesystem::seek(*fd, 6_B_off, kapi::filesystem::seek_origin::beginning) == 6_B); - REQUIRE(read_text(*fd, 16) == "World!"); + REQUIRE(read_text(*fd, 16) == "World!\n"); REQUIRE(kapi::filesystem::close(*fd)); } @@ -401,7 +403,7 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_vfs_fixture, "Kapi files auto const status = kapi::filesystem::status("/information/hello.txt"); REQUIRE(status); REQUIRE(status->mode == std::to_underlying(kapi::filesystem::file_type::regular)); - REQUIRE(status->size == 12_B); + REQUIRE(status->size == image::hello_size); } THEN("the mounted root directory can be listed") @@ -410,11 +412,52 @@ SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_vfs_fixture, "Kapi files REQUIRE(directory); auto buffer = std::vector<kapi::filesystem::directory_entry>{10}; - REQUIRE(kapi::filesystem::read_directory(*directory, buffer) == 1); - REQUIRE(buffer[0].name == "HELLO.TXT"); + REQUIRE(kapi::filesystem::read_directory(*directory, buffer) == image::root_names().size()); + for (auto index = 0uz; index < image::root_names().size(); ++index) + { + REQUIRE(buffer[index].name == image::root_names()[index]); + } REQUIRE(kapi::filesystem::read_directory(*directory, buffer) == 0); } + THEN("a directory spanning two clusters can be listed in small pages") + { + auto const directory = kapi::filesystem::open("/information/docs"); + REQUIRE(directory); + + auto names = std::vector<std::string>{}; + auto buffer = std::vector<kapi::filesystem::directory_entry>{4}; + while (true) + { + auto const count = kapi::filesystem::read_directory(*directory, buffer); + REQUIRE(count); + if (*count == 0) + { + break; + } + for (auto index = 0uz; index < *count; ++index) + { + names.emplace_back(buffer[index].name.data(), buffer[index].name.size()); + } + } + REQUIRE(names == image::docs_names()); + } + + THEN("a multi cluster file in a nested directory can be read in chunks") + { + auto const fd = kapi::filesystem::open("/information/docs/sub/file2.txt"); + REQUIRE(fd); + REQUIRE(read_text(*fd, 100) == image::text_content(image::multi_cluster_file_size)); + REQUIRE(kapi::filesystem::close(*fd)); + } + + THEN("a path through \"..\" leads back to the mounted root directory") + { + auto const status = kapi::filesystem::status("/information/docs/../hello.txt"); + REQUIRE(status); + REQUIRE(status->size == image::hello_size); + } + THEN("a file can be overwritten and extended through its file descriptor") { auto const fd = kapi::filesystem::open("/information/hello.txt"); diff --git a/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp b/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp index a6711a90..a842e012 100644 --- a/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp +++ b/kernel/kernel/filesystems/fat32/directory_iterator.tests.cpp @@ -1,6 +1,5 @@ #include <kernel/filesystems/fat32/directory_iterator.hpp> -#include <kernel/devices/storage.hpp> #include <kernel/filesystems/fat32/boot_sector.hpp> #include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/filesystems/fat32/driver_state.hpp> @@ -9,7 +8,7 @@ #include <kernel/filesystems/fat32/filesystem.hpp> #include <kernel/test_support/devices/block_device.hpp> #include <kernel/test_support/filesystems/fat32.hpp> -#include <kernel/test_support/vfs/storage_boot_module_fixture.hpp> +#include <kernel/test_support/filesystems/fat32_image.hpp> #include <kernel/vfs/device_inode.hpp> #include <kstd/memory.hpp> @@ -20,7 +19,6 @@ #include <algorithm> #include <array> #include <cstdint> -#include <filesystem> #include <span> #include <string> #include <string_view> @@ -49,7 +47,7 @@ namespace auto names = std::vector<std::string>{}; for (; it != directory_iterator{}; ++it) { - REQUIRE(names.size() < 64); // fails loudly on a non-terminating loop rather than hanging the suite + REQUIRE(names.size() < 128); // fails loudly on a non-terminating loop rather than hanging the suite auto const name = it->entry.short_name(); names.emplace_back(name.data(), name.size()); } @@ -107,48 +105,101 @@ SCENARIO("Fat32 short names are formatted for display", "[filesystem][fat32][rea } } -SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 directory_iterator walks a real directory", - "[filesystem][fat32][readdir][img]") +SCENARIO_METHOD(kernel::tests::filesystems::fat32::fat32_image_fixture, + "Fat32 directory_iterator walks a real directory", "[filesystem][fat32][readdir][img]") { - auto const image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "fat32.img"; + namespace image = kernel::tests::filesystems::fat32::image; + using kernel::filesystems::fat32::cluster_offset; GIVEN("a mounted fat32 filesystem from a real image") { - REQUIRE(std::filesystem::exists(image_path)); - REQUIRE_NOTHROW(setup_modules_from_img({"test_img_module"}, {image_path})); + REQUIRE_NOTHROW(mount_image()); - auto boot_device = kernel::devices::storage::determine_boot_device(); - REQUIRE(boot_device); - - auto dev_inode = kstd::make_shared<kernel::vfs::device_inode>(boot_device); - auto fs = kstd::make_shared<kernel::filesystems::fat32::filesystem>(); - auto mount = fs->mount(dev_inode); - REQUIRE(mount); - auto driver_state = static_pointer_cast<kernel::filesystems::fat32::driver_state>(mount->second); - REQUIRE(driver_state); - - auto const root_cluster = driver_state->boot_sector.root_directory_cluster_index(); - - THEN("the volume label is skipped and only HELLO.TXT is found") + THEN("the root directory skips the volume label and the long name entries") { - REQUIRE(collect_names(directory_iterator{root_cluster, *driver_state}) == std::vector<std::string>{"HELLO.TXT"}); + REQUIRE(collect_names(directory_iterator{image::root_cluster, *state}) == image::root_names()); } - THEN("the record describes HELLO.TXT and where it is stored") + THEN("the records of the root directory describe the entries and where they are stored") { - auto it = directory_iterator{root_cluster, *driver_state}; + auto it = directory_iterator{image::root_cluster, *state}; REQUIRE(it != directory_iterator{}); - REQUIRE(it->entry.first_cluster() == 3); - REQUIRE(it->entry.file_size() == 12_B); + REQUIRE(it->entry.short_name() == "docs"); + REQUIRE(it->entry.first_cluster() == image::docs_chain[0]); REQUIRE(it->offset == 32_B); - REQUIRE(it->location == kernel::filesystems::fat32::cluster_offset(root_cluster, *driver_state) + 32_B); + REQUIRE(it->location == cluster_offset(image::root_cluster, *state) + 32_B); REQUIRE(it->inode_number() == it->location.value); - REQUIRE(it.offset() == 64_B); + + ++it; + REQUIRE(it->entry.short_name() == "LONGDI~1"); + REQUIRE(it->offset == 128_B); // behind the two long name entries + + ++it; + REQUIRE(it->entry.short_name() == "hello.txt"); + REQUIRE(it->entry.first_cluster() == image::hello_cluster); + REQUIRE(it->entry.file_size() == image::hello_size); + REQUIRE(it->offset == 160_B); + REQUIRE(it.offset() == 192_B); ++it; REQUIRE(it == directory_iterator{}); REQUIRE_FALSE(it.error()); } + + THEN("a directory spanning two non contiguous clusters is walked completely, skipping deleted entries") + { + REQUIRE(collect_names(directory_iterator{image::docs_chain[0], *state}) == image::docs_names()); + } + + THEN("the records behind the cluster boundary are located in the second cluster of the chain") + { + auto it = directory_iterator{image::docs_chain[0], *state}; + while (it != directory_iterator{} && it->entry.short_name() != "tmp31.bin") + { + ++it; + } + + REQUIRE(it != directory_iterator{}); + REQUIRE(it->offset == image::docs_second_cluster_first_live_entry_offset); + REQUIRE(it->location == cluster_offset(image::docs_chain[1], *state) + + (image::docs_second_cluster_first_live_entry_offset - image::cluster_size)); + } + + THEN("an iterator created at an offset in the second cluster starts there") + { + auto it = directory_iterator{image::docs_chain[0], image::cluster_size, *state}; + REQUIRE(it != directory_iterator{}); + REQUIRE(it->entry.short_name() == "tmp31.bin"); + REQUIRE(collect_names(it) == std::vector<std::string>{"tmp31.bin", "tmp33.bin", "tmp35.bin"}); + } + + THEN("an iterator created behind the last entry is the end iterator") + { + auto const it = directory_iterator{image::docs_chain[0], 2 * image::cluster_size, *state}; + REQUIRE(it == directory_iterator{}); + REQUIRE_FALSE(it.error()); + } + + THEN("a nested directory reports its dot entries first") + { + REQUIRE(collect_names(directory_iterator{image::sub_cluster, *state}) == image::sub_names()); + } + + THEN("the \"..\" entry of a top level directory points to the root cluster") + { + auto it = directory_iterator{image::docs_chain[0], *state}; + ++it; + REQUIRE(it->entry.short_name() == ".."); + REQUIRE(it->entry.first_cluster() == image::root_cluster); + } + + THEN("the \"..\" entry of a nested directory points to its parent") + { + auto it = directory_iterator{image::sub_cluster, *state}; + ++it; + REQUIRE(it->entry.short_name() == ".."); + REQUIRE(it->entry.first_cluster() == image::docs_chain[0]); + } } } diff --git a/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp b/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp index 41a9169c..cf549e40 100644 --- a/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp +++ b/kernel/kernel/filesystems/fat32/file_allocation_table.tests.cpp @@ -1,6 +1,5 @@ #include <kernel/filesystems/fat32/file_allocation_table.hpp> -#include <kernel/devices/storage.hpp> #include <kernel/filesystems/fat32/boot_sector.hpp> #include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/filesystems/fat32/driver_state.hpp> @@ -8,7 +7,7 @@ #include <kernel/filesystems/fat32/filesystem.hpp> #include <kernel/test_support/devices/block_device.hpp> #include <kernel/test_support/filesystems/fat32.hpp> -#include <kernel/test_support/vfs/storage_boot_module_fixture.hpp> +#include <kernel/test_support/filesystems/fat32_image.hpp> #include <kernel/vfs/device_inode.hpp> #include <kstd/memory.hpp> @@ -22,7 +21,6 @@ #include <array> #include <cstddef> #include <cstdint> -#include <filesystem> #include <optional> #include <span> #include <string_view> @@ -32,88 +30,150 @@ using namespace kstd::units_literals; // NOLINTBEGIN(readability-magic-numbers) -SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 cluster chains with real image", +SCENARIO_METHOD(kernel::tests::filesystems::fat32::fat32_image_fixture, "Fat32 cluster chains with real image", "[filesystem][fat32][fat][img]") { - auto const image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "fat32.img"; + namespace image = kernel::tests::filesystems::fat32::image; + using namespace kernel::filesystems::fat32; + + auto const as_vector = [](auto const & chain) { + return kstd::vector<std::uint32_t>{chain.begin(), chain.end()}; + }; GIVEN("a mounted fat32 filesystem from a real image") { - REQUIRE(std::filesystem::exists(image_path)); - REQUIRE_NOTHROW(setup_modules_from_img({"test_img_module"}, {image_path})); - - auto boot_device = kernel::devices::storage::determine_boot_device(); - REQUIRE(boot_device); - - auto dev_inode = kstd::make_shared<kernel::vfs::device_inode>(boot_device); - auto fs = kstd::make_shared<kernel::filesystems::fat32::filesystem>(); - auto mount = fs->mount(dev_inode); - REQUIRE(mount); - auto driver_state = static_pointer_cast<kernel::filesystems::fat32::driver_state>(mount->second); - REQUIRE(driver_state); + REQUIRE_NOTHROW(mount_image()); THEN("the root directory occupies a single cluster") { - auto const chain = kernel::filesystems::fat32::cluster_chain( - driver_state->boot_sector.root_directory_cluster_index(), *driver_state); + auto const chain = cluster_chain(image::root_cluster, *state); REQUIRE(chain); - REQUIRE(*chain == kstd::vector<std::uint32_t>{2}); + REQUIRE(*chain == kstd::vector<std::uint32_t>{image::root_cluster}); } - THEN("the root directory cluster is located right after the file allocation tables") + THEN("a directory that outgrew its first cluster has a non contiguous chain") { - REQUIRE(kernel::filesystems::fat32::cluster_offset(2, *driver_state) == (32 + 2 * 536) * 512_B); + auto const chain = cluster_chain(image::docs_chain[0], *state); + REQUIRE(chain); + REQUIRE(*chain == as_vector(image::docs_chain)); } - THEN("the entries of the root directory, HELLO.TXT and an unused cluster are classified") + THEN("files spanning multiple clusters have a chain covering their size") { - using kernel::filesystems::fat32::fat_entry_type; - using kernel::filesystems::fat32::read_fat_entry; + for (auto const & expected : {image::fragment_chain, image::file2_chain, image::long_file_chain}) + { + auto const chain = cluster_chain(expected[0], *state); + REQUIRE(chain); + REQUIRE(*chain == as_vector(expected)); + REQUIRE(chain->size() * image::cluster_size >= image::multi_cluster_file_size); + } + } + + THEN("the first data cluster is located right after both file allocation tables") + { + REQUIRE(cluster_offset(image::root_cluster, *state) == image::data_region_offset); + REQUIRE(cluster_offset(image::docs_chain[1], *state) == + image::data_region_offset + (image::docs_chain[1] - 2) * image::cluster_size); + } - REQUIRE(read_fat_entry(2, *driver_state)->type == fat_entry_type::end_of_chain); - REQUIRE(read_fat_entry(3, *driver_state)->type == fat_entry_type::end_of_chain); - REQUIRE(read_fat_entry(4, *driver_state)->type == fat_entry_type::free); + THEN("used, chained and free clusters are classified") + { + REQUIRE(read_fat_entry(image::root_cluster, *state)->type == fat_entry_type::end_of_chain); + REQUIRE(read_fat_entry(image::docs_chain[0], *state)->type == fat_entry_type::allocated); + REQUIRE(read_fat_entry(image::docs_chain[0], *state)->value == image::docs_chain[1]); + REQUIRE(read_fat_entry(image::docs_chain[1], *state)->type == fat_entry_type::end_of_chain); + REQUIRE(read_fat_entry(image::deleted_tmp2_cluster, *state)->type == fat_entry_type::free); + REQUIRE(read_fat_entry(image::first_allocated_cluster, *state)->type == fat_entry_type::free); + } + + THEN("both file allocation tables hold the same entries") + { + for (auto cluster = std::uint32_t{2}; cluster < image::first_allocated_cluster + 8; ++cluster) + { + auto entries = std::array<std::uint32_t, image::fats_count>{}; + for (auto fat_index = 0uz; fat_index < image::fats_count; ++fat_index) + { + auto const offset = image::fat1_offset + fat_index * image::fat_size + cluster * constants::fat_entry_size; + REQUIRE(dev_inode->read(kstd::raw_bytes(entries[fat_index]), offset)); + } + REQUIRE(entries[0] == entries[1]); + } } THEN("following a free cluster fails") { - REQUIRE(kernel::filesystems::fat32::next_cluster(100, *driver_state).error() == - kernel::filesystems::fat32::errc::corrupted_cluster_chain); + REQUIRE(next_cluster(image::deleted_tmp2_cluster, *state).error() == errc::corrupted_cluster_chain); } THEN("the root directory entries can be read from its cluster chain") { - using namespace kernel::filesystems::fat32; - - auto entries = std::array<directory_entry_data, 3>{}; + auto entries = std::array<directory_entry_data, 7>{}; auto const buffer = std::as_writable_bytes(std::span{entries}); - auto const read_result = - read_cluster_chain(driver_state->boot_sector.root_directory_cluster_index(), buffer, 0_B, *driver_state); + auto const read_result = read_cluster_chain(image::root_cluster, buffer, 0_B, *state); REQUIRE(read_result); REQUIRE(*read_result == kstd::bytes{buffer.size()}); - auto const & volume_label = entries[0]; - REQUIRE(std::string_view{volume_label.name.data(), volume_label.name.size()} == "TESTFAT "); - REQUIRE(volume_label.attributes == constants::attribute_volume_id); + auto const name_of = [](directory_entry_data const & entry) { + return std::string_view{entry.name.data(), entry.name.size()}; + }; - auto const & hello = entries[1]; - REQUIRE(std::string_view{hello.name.data(), hello.name.size()} == "HELLO TXT"); + REQUIRE(name_of(entries[0]) == "TESTFAT "); + REQUIRE(entries[0].attributes == constants::attribute_volume_id); + REQUIRE(name_of(entries[1]) == "DOCS "); + REQUIRE(entries[1].attributes == constants::attribute_directory); + REQUIRE((entries[2].attributes & constants::long_name_mask) == constants::attribute_long_name); + REQUIRE((entries[3].attributes & constants::long_name_mask) == constants::attribute_long_name); + REQUIRE(name_of(entries[4]) == "LONGDI~1 "); + + auto const & hello = entries[5]; + REQUIRE(name_of(hello) == "HELLO TXT"); REQUIRE(hello.attributes == constants::attribute_archive); REQUIRE(hello.first_cluster_high == 0); - REQUIRE(hello.first_cluster_low == 3); - REQUIRE(hello.file_size == 12); + REQUIRE(hello.first_cluster_low == image::hello_cluster); + REQUIRE(hello.file_size == image::hello_size.value); + + REQUIRE(static_cast<std::uint8_t>(entries[6].name[0]) == constants::end_of_directory_marker); + } + + THEN("directory entries are read across the boundary of a non contiguous chain") + { + // entries 31 (tmp29.bin, last of the first cluster) to 33 (tmp31.bin, second of the second cluster) + auto entries = std::array<directory_entry_data, 3>{}; + auto const read_result = read_cluster_chain(image::docs_chain[0], std::as_writable_bytes(std::span{entries}), + 31 * constants::directory_entry_size, *state); + REQUIRE(read_result); + REQUIRE(*read_result == kstd::size_of(entries)); - REQUIRE(static_cast<std::uint8_t>(entries[2].name[0]) == constants::end_of_directory_marker); + REQUIRE(std::string_view{entries[0].name.data(), entries[0].name.size()} == "TMP29 BIN"); + REQUIRE(static_cast<std::uint8_t>(entries[1].name[0]) == constants::deleted_entry_marker); + REQUIRE(std::string_view{entries[2].name.data(), entries[2].name.size()} == "TMP31 BIN"); } - THEN("the contents of HELLO.TXT can be read from its cluster chain") + THEN("the contents of a single cluster file can be read from its cluster chain") { - auto contents = std::array<char, 12>{}; - auto const read_result = kernel::filesystems::fat32::read_cluster_chain( - 3, std::as_writable_bytes(std::span{contents}), 0_B, *driver_state); + auto contents = std::array<char, image::hello_content.size()>{}; + auto const read_result = + read_cluster_chain(image::hello_cluster, std::as_writable_bytes(std::span{contents}), 0_B, *state); REQUIRE(read_result); - REQUIRE(*read_result == 12_B); - REQUIRE(std::string_view{contents.data(), contents.size()} == "Hello World!"); + REQUIRE(*read_result == image::hello_size); + REQUIRE(std::string_view{contents.data(), contents.size()} == image::hello_content); + } + + THEN("a multi cluster file can be read from an offset in its middle cluster up to the end of its chain") + { + auto const offset = image::cluster_size + 100_B; + auto const chain_size = image::file2_chain.size() * image::cluster_size; + auto contents = std::vector<char>((chain_size - offset).value); + + auto const read_result = + read_cluster_chain(image::file2_chain[0], std::as_writable_bytes(std::span{contents}), offset, *state); + REQUIRE(read_result); + REQUIRE(*read_result == chain_size - offset); + + // the chain is padded behind the end of the file, so only the bytes up to the file size are compared + auto const file_part = (image::multi_cluster_file_size - offset).value; + REQUIRE(std::string_view{contents.data(), file_part} == + image::text_content(image::multi_cluster_file_size).substr(offset.value)); } } } diff --git a/kernel/kernel/filesystems/fat32/filesystem.tests.cpp b/kernel/kernel/filesystems/fat32/filesystem.tests.cpp index ae11de39..b96f776a 100644 --- a/kernel/kernel/filesystems/fat32/filesystem.tests.cpp +++ b/kernel/kernel/filesystems/fat32/filesystem.tests.cpp @@ -9,6 +9,7 @@ #include <kernel/filesystems/fat32/inode.hpp> #include <kernel/test_support/devices/block_device.hpp> #include <kernel/test_support/filesystems/fat32.hpp> +#include <kernel/test_support/filesystems/fat32_image.hpp> #include <kernel/test_support/vfs/storage_boot_module_fixture.hpp> #include <kernel/vfs/device_inode.hpp> #include <kernel/vfs/error.hpp> @@ -26,31 +27,46 @@ using namespace kstd::units_literals; // NOLINTBEGIN(readability-magic-numbers) -SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 filesystem mount with real image", +SCENARIO_METHOD(kernel::tests::filesystems::fat32::fat32_image_fixture, "Fat32 filesystem mount with real image", "[filesystem][fat32][filesystem][img]") { - auto const image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "fat32.img"; + namespace image = kernel::tests::filesystems::fat32::image; GIVEN("a mounted fat32 filesystem from a real image") { - REQUIRE(std::filesystem::exists(image_path)); - REQUIRE_NOTHROW(setup_modules_from_img({"test_img_module"}, {image_path})); - - auto boot_device = kernel::devices::storage::determine_boot_device(); - REQUIRE(boot_device); - - auto dev_inode = kstd::make_shared<kernel::vfs::device_inode>(boot_device); - auto fs = kstd::make_shared<kernel::filesystems::fat32::filesystem>(); - auto mount = fs->mount(dev_inode); - REQUIRE(mount); - auto [root, driver_data] = *mount; - auto driver_state = static_pointer_cast<kernel::filesystems::fat32::driver_state>(driver_data); + REQUIRE_NOTHROW(mount_image()); THEN("the root inode is a directory starting at the root cluster") { REQUIRE(root); REQUIRE(root->is_directory()); - REQUIRE(static_pointer_cast<kernel::filesystems::fat32::inode>(root)->first_cluster() == 2); + REQUIRE(static_pointer_cast<kernel::filesystems::fat32::inode>(root)->first_cluster() == image::root_cluster); + } + + THEN("the root inode reports the root inode number") + { + auto const status = root->status(); + REQUIRE(status); + REQUIRE(status->inode_number == kernel::filesystems::fat32::constants::root_inode_number); + } + + THEN("the volume geometry is derived from the boot sector") + { + auto const & boot_sector = state->boot_sector; + REQUIRE(boot_sector.root_directory_cluster_index() == image::root_cluster); + REQUIRE(boot_sector.cluster_size() == image::cluster_size); + REQUIRE(boot_sector.fats_count() == image::fats_count); + REQUIRE(boot_sector.fat_size() == image::fat_size); + REQUIRE(boot_sector.fat1_offset() == image::fat1_offset); + REQUIRE(boot_sector.data_region_offset() == image::data_region_offset); + REQUIRE(boot_sector.cluster_count() == image::cluster_count); + REQUIRE(boot_sector.fs_info_offset() == image::fs_info_offset); + } + + THEN("the free cluster hints are read from the filesystem information sector") + { + REQUIRE(state->fs_info.free_cluster_count() == image::free_clusters_count); + REQUIRE(state->fs_info.next_free_cluster() == image::next_free_cluster); } } } @@ -318,37 +334,123 @@ SCENARIO("Fat32 filesystem lookup on a mock layout", "[filesystem][fat32][filesy } } -SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 filesystem lookup with real image", +SCENARIO_METHOD(kernel::tests::filesystems::fat32::fat32_image_fixture, "Fat32 filesystem lookup with real image", "[filesystem][fat32][filesystem][lookup][img]") { - auto const image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "fat32.img"; + namespace image = kernel::tests::filesystems::fat32::image; + using kernel::filesystems::fat32::constants::root_inode_number; GIVEN("a mounted fat32 filesystem from a real image") { - REQUIRE(std::filesystem::exists(image_path)); - REQUIRE_NOTHROW(setup_modules_from_img({"test_img_module"}, {image_path})); - - auto boot_device = kernel::devices::storage::determine_boot_device(); - REQUIRE(boot_device); + REQUIRE_NOTHROW(mount_image()); - auto dev_inode = kstd::make_shared<kernel::vfs::device_inode>(boot_device); - auto fs = kstd::make_shared<kernel::filesystems::fat32::filesystem>(); - auto mount = fs->mount(dev_inode); - REQUIRE(mount); - auto [root, driver_data] = *mount; - - THEN("HELLO.TXT is found by its lowercase name with its size") + THEN("a file in the root directory is found by its displayed name with its first cluster and size") { - auto const found = fs->lookup(root, "hello.txt", driver_data); + auto const found = lookup(root, "hello.txt"); REQUIRE(found); REQUIRE((*found)->is_regular()); - REQUIRE(static_pointer_cast<kernel::filesystems::fat32::inode>(*found)->first_cluster() == 3); - REQUIRE(static_pointer_cast<kernel::filesystems::fat32::inode>(*found)->size() == 12_B); + REQUIRE((*found)->first_cluster() == image::hello_cluster); + REQUIRE((*found)->size() == image::hello_size); + } + + THEN("names are matched ignoring their case") + { + REQUIRE(lookup(root, "HELLO.TXT")); + REQUIRE(lookup(root, "Docs")); + REQUIRE(lookup(root, "longdi~1")); + } + + THEN("a directory is found with its first cluster") + { + auto const docs = lookup(root, "docs"); + REQUIRE(docs); + REQUIRE((*docs)->is_directory()); + REQUIRE((*docs)->first_cluster() == image::docs_chain[0]); + } + + THEN("nested paths are resolved component by component") + { + auto const sub = open("/docs/sub"); + REQUIRE(sub->is_directory()); + REQUIRE(sub->first_cluster() == image::sub_cluster); + + auto const file2 = lookup(sub, "file2.txt"); + REQUIRE(file2); + REQUIRE((*file2)->first_cluster() == image::file2_chain[0]); + REQUIRE((*file2)->size() == image::multi_cluster_file_size); + } + + THEN("an entry stored in the second, non contiguous cluster of a directory is found") + { + auto const tmp35 = lookup(open("/docs"), "tmp35.bin"); + REQUIRE(tmp35); + REQUIRE((*tmp35)->first_cluster() == 42); + REQUIRE((*tmp35)->size() == image::tmp_size); + } + + THEN("\".\" refers to the directory itself") + { + auto const dot = lookup(open("/docs"), "."); + REQUIRE(dot); + REQUIRE((*dot)->is_directory()); + REQUIRE((*dot)->first_cluster() == image::docs_chain[0]); + } + + THEN("\"..\" of a top level directory, stored as cluster 0, refers to the root directory") + { + auto const parent = lookup(open("/docs"), ".."); + REQUIRE(parent); + REQUIRE((*parent)->is_directory()); + REQUIRE((*parent)->first_cluster() == image::root_cluster); + + auto const status = (*parent)->status(); + REQUIRE(status); + REQUIRE(status->inode_number == root_inode_number); + + REQUIRE(lookup(*parent, "hello.txt")); + } + + THEN("\"..\" of a nested directory refers to its parent directory") + { + auto const parent = lookup(open("/docs/sub"), ".."); + REQUIRE(parent); + REQUIRE((*parent)->first_cluster() == image::docs_chain[0]); + + auto const status = (*parent)->status(); + REQUIRE(status); + REQUIRE(status->inode_number != root_inode_number); + } + + THEN("a deleted file is not found") + { + REQUIRE(lookup(open("/docs"), "tmp2.bin").error() == kernel::vfs::errc::no_such_file_or_directory); + REQUIRE(lookup(open("/docs"), "tmp36.bin").error() == kernel::vfs::errc::no_such_file_or_directory); + } + + THEN("long file names are not supported, only their short aliases are found") + { + REQUIRE(lookup(root, "longDirectoryName").error() == kernel::vfs::errc::no_such_file_or_directory); + + auto const long_file = lookup(open("/longdi~1"), "longfi~1.jpe"); + REQUIRE(long_file); + REQUIRE((*long_file)->first_cluster() == image::long_file_chain[0]); + REQUIRE((*long_file)->size() == image::multi_cluster_file_size); + } + + THEN("the volume label is not found as a file") + { + REQUIRE(lookup(root, "TESTFAT").error() == kernel::vfs::errc::no_such_file_or_directory); } THEN("a missing file is not found") { - REQUIRE(fs->lookup(root, "missing.txt", driver_data).error() == kernel::vfs::errc::no_such_file_or_directory); + REQUIRE(lookup(root, "missing.txt").error() == kernel::vfs::errc::no_such_file_or_directory); + REQUIRE(lookup(open("/docs/sub"), "hello.txt").error() == kernel::vfs::errc::no_such_file_or_directory); + } + + THEN("looking up a name in a file fails") + { + REQUIRE(lookup(open("/hello.txt"), "x").error() == kernel::vfs::errc::not_a_directory); } } } diff --git a/kernel/kernel/filesystems/fat32/inode.tests.cpp b/kernel/kernel/filesystems/fat32/inode.tests.cpp index e3578b42..699cc614 100644 --- a/kernel/kernel/filesystems/fat32/inode.tests.cpp +++ b/kernel/kernel/filesystems/fat32/inode.tests.cpp @@ -1,6 +1,5 @@ #include <kernel/filesystems/fat32/inode.hpp> -#include <kernel/devices/storage.hpp> #include <kernel/filesystems/fat32/boot_sector.hpp> #include <kernel/filesystems/fat32/directory_entry.hpp> #include <kernel/filesystems/fat32/directory_iterator.hpp> @@ -11,7 +10,7 @@ #include <kernel/filesystems/fat32/fs_info.hpp> #include <kernel/test_support/devices/block_device.hpp> #include <kernel/test_support/filesystems/fat32.hpp> -#include <kernel/test_support/vfs/storage_boot_module_fixture.hpp> +#include <kernel/test_support/filesystems/fat32_image.hpp> #include <kernel/vfs/device_inode.hpp> #include <kernel/vfs/error.hpp> #include <kernel/vfs/filesystem.hpp> @@ -30,7 +29,6 @@ #include <array> #include <cstddef> #include <cstdint> -#include <filesystem> #include <optional> #include <span> #include <string> @@ -509,66 +507,109 @@ SCENARIO("Fat32 inode lists directories on a mock layout", "[filesystem][fat32][ } } -SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 inode reads files from a real image", +SCENARIO_METHOD(kernel::tests::filesystems::fat32::fat32_image_fixture, "Fat32 inode reads files from a real image", "[filesystem][fat32][inode][img]") { - auto const image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "fat32.img"; + namespace image = kernel::tests::filesystems::fat32::image; + + auto const read_at = [](auto const & file, kstd::bytes offset, std::size_t size) { + auto buffer = std::vector<std::byte>(size); + auto const result = file->read(buffer, offset); + REQUIRE(result); + return std::string{reinterpret_cast<char const *>(buffer.data()), result->value}; + }; GIVEN("a mounted fat32 filesystem from a real image") { - REQUIRE(std::filesystem::exists(image_path)); - REQUIRE_NOTHROW(setup_modules_from_img({"test_img_module"}, {image_path})); + REQUIRE_NOTHROW(mount_image()); - auto boot_device = kernel::devices::storage::determine_boot_device(); - REQUIRE(boot_device); + THEN("a single cluster file in the root directory contains its text") + { + REQUIRE(read_all(open("/hello.txt")) == image::hello_content); + } - auto dev_inode = kstd::make_shared<kernel::vfs::device_inode>(boot_device); - auto fs = kstd::make_shared<kernel::filesystems::fat32::filesystem>(); - auto mount = fs->mount(dev_inode); - REQUIRE(mount); - auto [root, driver_data] = *mount; - root->set_driver_state(driver_data); + THEN("a file can be read from an offset") + { + REQUIRE(read_at(open("/hello.txt"), 6_B, 64) == "World!\n"); + } - auto const open = [&](kernel::vfs::filesystem::inode_ptr const & parent, std::string_view name) { - auto const found = fs->lookup(parent, name, driver_data); - REQUIRE(found); - (*found)->set_driver_state(driver_data); - return *found; - }; + THEN("files in nested directories contain their text") + { + REQUIRE(read_all(open("/docs/file.txt")) == image::text_content(image::file_size)); + REQUIRE(read_all(open("/docs/tmp1.bin")) == image::tmp_content); + REQUIRE(read_all(open("/docs/tmp35.bin")) == image::tmp_content); + REQUIRE(read_all(open("/docs/sub/file1.txt")) == image::text_content(image::file1_size)); + } - auto const read_all = [](kernel::vfs::filesystem::inode_ptr const & file) { - auto buffer = std::vector<std::byte>(4096); - auto const result = file->read(buffer, 0_B); - REQUIRE(result); - return std::string{reinterpret_cast<char const *>(buffer.data()), result->value}; - }; + THEN("files spanning multiple clusters are read completely") + { + auto const expected = image::text_content(image::multi_cluster_file_size); + REQUIRE(read_all(open("/docs/fragment.txt")) == expected); + REQUIRE(read_all(open("/docs/sub/file2.txt")) == expected); + REQUIRE(read_all(open("/longdi~1/longfi~1.jpe")) == expected); + } - THEN("HELLO.TXT contains its text") + THEN("a read crossing cluster boundaries returns the bytes of both clusters") { - REQUIRE(read_all(open(root, "hello.txt")) == "Hello World!"); + auto const expected = image::text_content(image::multi_cluster_file_size); + auto const offset = image::cluster_size - 10_B; + REQUIRE(read_at(open("/docs/sub/file2.txt"), offset, 20) == expected.substr(offset.value, 20)); + + auto const spanning_all = read_at(open("/docs/sub/file2.txt"), 1_B, 2 * image::cluster_size.value + 100); + REQUIRE(spanning_all == expected.substr(1, 2 * image::cluster_size.value + 100)); } - THEN("HELLO.TXT can be read from an offset") + THEN("a read starting in the last cluster is limited by the file size") { - auto buffer = std::vector<std::byte>(64); - auto const result = open(root, "hello.txt")->read(buffer, 6_B); - REQUIRE(result); - REQUIRE(std::string_view{reinterpret_cast<char const *>(buffer.data()), result->value} == "World!"); + auto const expected = image::text_content(image::multi_cluster_file_size); + auto const offset = 2 * image::cluster_size + 200_B; + REQUIRE(read_at(open("/docs/sub/file2.txt"), offset, 4096) == expected.substr(offset.value)); + } + + THEN("reading at or behind the end of a file returns no data") + { + REQUIRE(read_at(open("/docs/sub/file2.txt"), image::multi_cluster_file_size, 16).empty()); + REQUIRE(read_at(open("/hello.txt"), 4 * image::cluster_size, 16).empty()); + } + + THEN("the status reports the size and a distinct inode number per file") + { + auto const file1 = open("/docs/sub/file1.txt")->status(); + auto const file2 = open("/docs/sub/file2.txt")->status(); + REQUIRE(file1); + REQUIRE(file2); + REQUIRE(file1->size == image::file1_size); + REQUIRE(file2->size == image::multi_cluster_file_size); + REQUIRE(file1->inode_number != file2->inode_number); } - THEN("the root directory lists HELLO.TXT only") + THEN("the root directory lists its entries with their types") { auto entries = std::vector<kapi::filesystem::directory_entry>(10); auto const result = root->read_directory(kapi::filesystem::directory_cursor{0}, entries); REQUIRE(result); + REQUIRE(result->first == 3); - auto names = std::vector<std::string>{}; - for (auto index = 0uz; index < result->first; ++index) - { - names.emplace_back(entries[index].name.data(), entries[index].name.size()); - } - REQUIRE(names == std::vector<std::string>{"HELLO.TXT"}); - REQUIRE(entries[0].type == kapi::filesystem::file_type::regular); + REQUIRE(list(root) == image::root_names()); + REQUIRE(entries[0].type == kapi::filesystem::file_type::directory); + REQUIRE(entries[1].type == kapi::filesystem::file_type::directory); + REQUIRE(entries[2].type == kapi::filesystem::file_type::regular); + } + + THEN("a directory spanning two clusters is listed completely in one call") + { + REQUIRE(list(open("/docs")) == image::docs_names()); + } + + THEN("a directory spanning two clusters is listed completely in small pages") + { + REQUIRE(list(open("/docs"), 5) == image::docs_names()); + REQUIRE(list(open("/docs"), 1) == image::docs_names()); + } + + THEN("a nested directory is listed with its dot entries") + { + REQUIRE(list(open("/docs/sub")) == image::sub_names()); } } } @@ -813,71 +854,154 @@ SCENARIO("Fat32 inode writes file data on a mock layout", "[filesystem][fat32][i } } -SCENARIO_METHOD(kernel::tests::vfs::storage_boot_module_fixture, "Fat32 inode writes to a real image", +SCENARIO_METHOD(kernel::tests::filesystems::fat32::fat32_image_fixture, "Fat32 inode writes to a real image", "[filesystem][fat32][inode][write][img]") { - auto const image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "fat32.img"; + namespace image = kernel::tests::filesystems::fat32::image; + using namespace kernel::filesystems::fat32; + + auto const as_bytes = [](std::string_view text) { + return std::vector<std::byte>{reinterpret_cast<std::byte const *>(text.data()), + reinterpret_cast<std::byte const *>(text.data()) + text.size()}; + }; + + auto const raw_fat_entry = [&](std::size_t fat_index, std::uint32_t cluster) { + auto raw_entry = std::uint32_t{}; + auto const offset = image::fat1_offset + fat_index * image::fat_size + cluster * constants::fat_entry_size; + REQUIRE(dev_inode->read(kstd::raw_bytes(raw_entry), offset)); + return raw_entry & constants::cluster_mask; + }; + + auto const on_disk_fs_info = [&] { + auto fs_info = fs_info_data{}; + REQUIRE(dev_inode->read(kstd::raw_bytes(fs_info), image::fs_info_offset)); + return fs_info; + }; GIVEN("a mounted fat32 filesystem from a real image") { - using namespace kernel::filesystems::fat32; + REQUIRE_NOTHROW(mount_image()); - REQUIRE(std::filesystem::exists(image_path)); - REQUIRE_NOTHROW(setup_modules_from_img({"test_img_module"}, {image_path})); + WHEN("a multi cluster file is overwritten across a cluster boundary") + { + auto const file = open("/docs/sub/file2.txt"); + auto const offset = image::cluster_size - 3_B; + auto const data = as_bytes("OVERWRITTEN"); + REQUIRE(file->write(data, offset) == kstd::bytes{data.size()}); - auto boot_device = kernel::devices::storage::determine_boot_device(); - REQUIRE(boot_device); + THEN("the new data replaces the old one and the rest of the file is unchanged") + { + auto expected = image::text_content(image::multi_cluster_file_size); + expected.replace(offset.value, data.size(), "OVERWRITTEN"); + REQUIRE(read_all(file) == expected); + REQUIRE(read_all(open("/docs/sub/file2.txt")) == expected); + } - auto dev_inode = kstd::make_shared<kernel::vfs::device_inode>(boot_device); - auto fs = kstd::make_shared<kernel::filesystems::fat32::filesystem>(); - auto mount = fs->mount(dev_inode); - REQUIRE(mount); - auto [root, driver_data] = *mount; - auto state = static_pointer_cast<driver_state>(driver_data); + THEN("the size and the cluster chain stay the same and no cluster is allocated") + { + REQUIRE(file->size() == image::multi_cluster_file_size); + REQUIRE(*cluster_chain(image::file2_chain[0], *state) == + kstd::vector<std::uint32_t>{image::file2_chain.begin(), image::file2_chain.end()}); + REQUIRE(on_disk_fs_info().free_clusters_count == image::free_clusters_count); + } + } - auto const looked_up = fs->lookup(root, "hello.txt", driver_data); - REQUIRE(looked_up); - auto file = static_pointer_cast<kernel::filesystems::fat32::inode>(*looked_up); - file->set_driver_state(driver_data); + WHEN("a single cluster file grows beyond its first cluster") + { + auto const file = open("/hello.txt"); + auto const appended = std::string(image::cluster_size.value, 'x'); + REQUIRE(file->write(as_bytes(appended), image::hello_size) == image::cluster_size); - REQUIRE(state->boot_sector.fs_info_offset()); - auto const free_clusters_before = state->fs_info.free_cluster_count(); + THEN("the old and the appended data can be read back") + { + auto const expected = std::string{image::hello_content} + appended; + REQUIRE(read_all(file) == expected); + REQUIRE(read_all(open("/hello.txt")) == expected); + } + + THEN("the next free cluster behind the hint is linked in both file allocation tables") + { + for (auto fat_index = 0uz; fat_index < image::fats_count; ++fat_index) + { + REQUIRE(raw_fat_entry(fat_index, image::hello_cluster) == image::first_allocated_cluster); + REQUIRE(raw_fat_entry(fat_index, image::first_allocated_cluster) >= constants::end_of_chain); + } + } + + THEN("the filesystem information sector on disk reflects the allocation") + { + auto const fs_info = on_disk_fs_info(); + REQUIRE(fs_info.free_clusters_count == image::free_clusters_count - 1); + REQUIRE(fs_info.next_free_cluster == image::first_allocated_cluster); + REQUIRE(fs_info.lead_signature == constants::fs_info_lead_signature); + REQUIRE(fs_info.trail_signature == constants::fs_info_trail_signature); + } + + THEN("the new size is stored in the directory entry") + { + REQUIRE(open("/hello.txt")->size() == image::hello_size + image::cluster_size); + } - WHEN("the file grows beyond its first cluster") + THEN("the other entries of the directory are unchanged") + { + REQUIRE(list(root) == image::root_names()); + REQUIRE(read_all(open("/docs/sub/file1.txt")) == image::text_content(image::file1_size)); + } + } + + WHEN("a multi cluster file is written behind the end of its chain") { - auto const data = expected_bytes(0, cluster_size + 100); - REQUIRE(file->write(data, 0_B) == kstd::bytes{data.size()}); + auto const file = open("/docs/sub/file2.txt"); + auto const offset = image::file2_chain.size() * image::cluster_size + 10_B; + REQUIRE(file->write(as_bytes("tail"), offset) == 4_B); - THEN("the data can be read back") + THEN("the chain is extended by one cluster") { - auto buffer = std::vector<std::byte>(data.size()); - REQUIRE(file->read(buffer, 0_B) == kstd::bytes{data.size()}); - REQUIRE(buffer == data); + auto const chain = cluster_chain(image::file2_chain[0], *state); + REQUIRE(chain); + REQUIRE(*chain == kstd::vector<std::uint32_t>{image::file2_chain[0], image::file2_chain[1], + image::file2_chain[2], image::first_allocated_cluster}); } - THEN("the new cluster is linked in both file allocation tables") + THEN("the gap between the old end of the file and the written data reads as zeros") { - auto const second = next_cluster(file->first_cluster(), *state); - REQUIRE(second); - REQUIRE(*second); + auto const contents = read_all(open("/docs/sub/file2.txt")); + REQUIRE(contents.size() == offset.value + 4); + REQUIRE(contents.substr(0, image::multi_cluster_file_size.value) == + image::text_content(image::multi_cluster_file_size)); - auto const fat_size = kstd::bytes{state->boot_sector.fat_size()}; // todo bytes - for (auto fat_index = 0uz; fat_index < state->boot_sector.fats_count(); ++fat_index) - { - auto raw_entry = std::uint32_t{}; - auto const entry_offset = state->boot_sector.fat1_offset() + fat_index * fat_size + - file->first_cluster() * constants::fat_entry_size; - REQUIRE(dev_inode->read(kstd::raw_bytes(raw_entry), entry_offset)); - REQUIRE((raw_entry & constants::cluster_mask) == **second); - } + auto const gap = + contents.substr(image::multi_cluster_file_size.value, offset.value - image::multi_cluster_file_size.value); + REQUIRE(std::ranges::all_of(gap, [](char character) { return character == '\0'; })); + REQUIRE(contents.substr(offset.value) == "tail"); + } + } + + WHEN("a file whose entry is stored in the second cluster of its directory grows") + { + auto const file = open("/docs/tmp35.bin"); + REQUIRE(file->write(as_bytes("more"), image::tmp_size) == 4_B); + + THEN("the entry in the second cluster is updated") + { + auto const reopened = open("/docs/tmp35.bin"); + REQUIRE(reopened->size() == image::tmp_size + 4_B); + REQUIRE(read_all(reopened) == std::string{image::tmp_content} + "more"); } - THEN("the filesystem information sector reflects the allocation") + THEN("the directory still lists all its entries") { - auto fs_info = kernel::filesystems::fat32::fs_info_data{}; - REQUIRE(dev_inode->read(kstd::raw_bytes(fs_info), state->boot_sector.fs_info_offset())); - REQUIRE(fs_info.free_clusters_count == free_clusters_before - 1); - REQUIRE(fs_info.next_free_cluster == *next_cluster(file->first_cluster(), *state).value()); + REQUIRE(list(open("/docs")) == image::docs_names()); + } + } + + WHEN("writing to a directory") + { + auto const result = open("/docs")->write(as_bytes("x"), 0_B); + + THEN("the write fails because it is a directory") + { + REQUIRE(result.error() == kernel::vfs::errc::is_a_directory); } } } diff --git a/kernel/kernel/test_support/filesystems/fat32_image.cpp b/kernel/kernel/test_support/filesystems/fat32_image.cpp new file mode 100644 index 00000000..4475415f --- /dev/null +++ b/kernel/kernel/test_support/filesystems/fat32_image.cpp @@ -0,0 +1,169 @@ +#include <kernel/test_support/filesystems/fat32_image.hpp> + +#include <kernel/devices/storage.hpp> +#include <kernel/filesystems/fat32/driver_state.hpp> +#include <kernel/filesystems/fat32/filesystem.hpp> +#include <kernel/filesystems/fat32/inode.hpp> +#include <kernel/vfs/device_inode.hpp> + +#include <kapi/filesystem.hpp> + +#include <kstd/memory.hpp> +#include <kstd/result.hpp> +#include <kstd/units.hpp> + +#include <cstddef> +#include <filesystem> +#include <format> +#include <stdexcept> +#include <string> +#include <string_view> +#include <vector> + +namespace kernel::tests::filesystems::fat32::image +{ + auto text_content(kstd::bytes size) -> std::string + { + constexpr auto line = std::string_view{"Very important text document stored on fat32 drive.\n"}; + + auto text = std::string{}; + while (text.size() < size.value) + { + text.append(line); + } + text.resize(size.value); + return text; + } + + auto root_names() -> std::vector<std::string> + { + return {"docs", "LONGDI~1", "hello.txt"}; + } + + auto docs_names() -> std::vector<std::string> + { + auto names = std::vector<std::string>{".", "..", "sub", "tmp1.bin", "fragment.txt", "tmp3.bin", "file.txt"}; + for (auto index = 5; index <= 35; index += 2) + { + names.push_back(std::format("tmp{}.bin", index)); + } + return names; + } + + auto sub_names() -> std::vector<std::string> + { + return {".", "..", "file1.txt", "file2.txt"}; + } +} // namespace kernel::tests::filesystems::fat32::image + +namespace kernel::tests::filesystems::fat32 +{ + auto fat32_image_fixture::mount_image() -> void + { + auto const image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / image::file_name; + if (!std::filesystem::exists(image_path)) + { + throw std::runtime_error{"fat32 test image is missing (is git lfs installed?): " + image_path.string()}; + } + + setup_modules_from_img({"test_img_module"}, {image_path}); + + auto boot_device = kernel::devices::storage::determine_boot_device(); + if (!boot_device) + { + throw std::runtime_error{"no boot device for the fat32 test image"}; + } + + dev_inode = kstd::make_shared<kernel::vfs::device_inode>(boot_device); + fs = kstd::make_shared<kernel::filesystems::fat32::filesystem>(); + + auto mount = fs->mount(dev_inode); + if (!mount) + { + throw std::runtime_error{"failed to mount the fat32 test image"}; + } + + root = mount->first; + driver_data = mount->second; + root->set_driver_state(driver_data); + state = static_pointer_cast<kernel::filesystems::fat32::driver_state>(driver_data); + } + + auto fat32_image_fixture::lookup(inode_ptr const & parent, std::string_view name) const + -> kstd::result<kstd::shared_ptr<kernel::filesystems::fat32::inode>> + { + auto const found = fs->lookup(parent, name, driver_data); + if (!found) + { + return kstd::failure(found.error()); + } + + (*found)->set_driver_state(driver_data); + return static_pointer_cast<kernel::filesystems::fat32::inode>(*found); + } + + auto fat32_image_fixture::open(std::string_view path) const -> kstd::shared_ptr<kernel::filesystems::fat32::inode> + { + auto current = static_pointer_cast<kernel::filesystems::fat32::inode>(root); + + while (!path.empty()) + { + auto const separator = path.find('/'); + auto const component = path.substr(0, separator); + path = separator == std::string_view::npos ? std::string_view{} : path.substr(separator + 1); + + if (component.empty()) + { + continue; + } + + auto const next = lookup(current, component); + if (!next) + { + throw std::runtime_error{std::format("failed to look up '{}' in the fat32 test image", component)}; + } + current = *next; + } + + return current; + } + + auto fat32_image_fixture::read_all(kstd::shared_ptr<kernel::filesystems::fat32::inode> const & file) -> std::string + { + auto buffer = std::vector<std::byte>(file->size().value + 1); + auto const result = file->read(buffer, kstd::bytes{0}); + if (!result) + { + throw std::runtime_error{"failed to read a file of the fat32 test image"}; + } + + return std::string{reinterpret_cast<char const *>(buffer.data()), result->value}; + } + + auto fat32_image_fixture::list(inode_ptr const & directory, std::size_t page_size) -> std::vector<std::string> + { + auto names = std::vector<std::string>{}; + auto entries = std::vector<kapi::filesystem::directory_entry>(page_size); + auto cursor = kapi::filesystem::directory_cursor{0}; + + while (true) + { + auto const result = directory->read_directory(cursor, entries); + if (!result) + { + throw std::runtime_error{"failed to list a directory of the fat32 test image"}; + } + + for (auto index = 0uz; index < result->first; ++index) + { + names.emplace_back(entries[index].name.data(), entries[index].name.size()); + } + + if (result->first == 0 || names.size() > 256) + { + return names; + } + cursor = result->second; + } + } +} // namespace kernel::tests::filesystems::fat32 diff --git a/kernel/kernel/test_support/filesystems/fat32_image.hpp b/kernel/kernel/test_support/filesystems/fat32_image.hpp new file mode 100644 index 00000000..c3316477 --- /dev/null +++ b/kernel/kernel/test_support/filesystems/fat32_image.hpp @@ -0,0 +1,135 @@ +#ifndef TEACHOS_KERNEL_TEST_SUPPORT_FILESYSTEMS_FAT32_IMAGE_HPP +#define TEACHOS_KERNEL_TEST_SUPPORT_FILESYSTEMS_FAT32_IMAGE_HPP + +#include <kernel/filesystems/fat32/driver_state.hpp> +#include <kernel/filesystems/fat32/filesystem.hpp> +#include <kernel/filesystems/fat32/inode.hpp> +#include <kernel/test_support/vfs/storage_boot_module_fixture.hpp> +#include <kernel/vfs/device_inode.hpp> +#include <kernel/vfs/filesystem.hpp> + +#include <kstd/memory.hpp> +#include <kstd/units.hpp> + +#include <array> +#include <cstdint> +#include <string> +#include <string_view> +#include <vector> + +//! Known layout of the fat32 test image (kernel/test_support/vfs/test_assets/fat32.img). +//! +//! The image is created by scripts/create_fat32_test_image.sh. All values were read from the image with mtools and +//! must be updated when the image is regenerated. +//! +//! @code +//! / cluster 2 (volume label "TESTFAT", long name entries for longDirectoryName) +//! ├── docs/ clusters 3, 37 (not contiguous, 39 entries spanning both clusters) +//! │ ├── sub/ cluster 4 +//! │ │ ├── file1.txt cluster 49 789 bytes +//! │ │ └── file2.txt clusters 50, 51, 52 2345 bytes +//! │ ├── tmp1.bin cluster 7 4 bytes (tmp1 ... tmp35, odd numbers only, even ones deleted) +//! │ ├── fragment.txt clusters 46, 47, 48 2345 bytes +//! │ └── file.txt cluster 45 11 bytes +//! ├── LONGDI~1/ cluster 5 (longDirectoryName) +//! │ └── LONGFI~1.JPE clusters 53, 54, 55 2345 bytes (longFileName.jpeg) +//! └── hello.txt cluster 6 13 bytes +//! @endcode +namespace kernel::tests::filesystems::fat32::image +{ + using namespace kstd::units_literals; + + constexpr auto file_name = std::string_view{"fat32.img"}; + + // Volume geometry + constexpr auto sector_size = 512_B; + constexpr auto cluster_size = 1024_B; + constexpr auto fats_count = 2u; + constexpr auto fat_size = 516 * sector_size; + constexpr auto fat1_offset = 32 * sector_size; + constexpr auto data_region_offset = fat1_offset + fats_count * fat_size; + constexpr auto cluster_count = 66028u; + constexpr auto fs_info_offset = 1 * sector_size; + + // Filesystem information sector hints as stored in the image + constexpr auto free_clusters_count = std::uint32_t{65993}; + constexpr auto next_free_cluster = std::uint32_t{55}; + //! The first cluster an allocation hands out, since the hint points to the (used) last cluster of LONGFI~1.JPE. + constexpr auto first_allocated_cluster = std::uint32_t{56}; + + // Cluster chains + constexpr auto root_cluster = std::uint32_t{2}; + constexpr auto docs_chain = std::array<std::uint32_t, 2>{3, 37}; + constexpr auto sub_cluster = std::uint32_t{4}; + constexpr auto long_directory_cluster = std::uint32_t{5}; + constexpr auto hello_cluster = std::uint32_t{6}; + constexpr auto tmp1_cluster = std::uint32_t{7}; + //! Cluster of the deleted /docs/tmp2.bin, released when the file was deleted. + constexpr auto deleted_tmp2_cluster = std::uint32_t{8}; + constexpr auto file_cluster = std::uint32_t{45}; + constexpr auto fragment_chain = std::array<std::uint32_t, 3>{46, 47, 48}; + constexpr auto file1_cluster = std::uint32_t{49}; + constexpr auto file2_chain = std::array<std::uint32_t, 3>{50, 51, 52}; + constexpr auto long_file_chain = std::array<std::uint32_t, 3>{53, 54, 55}; + + // File sizes + constexpr auto hello_size = 13_B; + constexpr auto tmp_size = 4_B; + constexpr auto file_size = 11_B; + constexpr auto file1_size = 789_B; + constexpr auto multi_cluster_file_size = 2345_B; //!< fragment.txt, file2.txt and LONGFI~1.JPE + + constexpr auto hello_content = std::string_view{"Hello World!\n"}; + constexpr auto tmp_content = std::string_view{"tmp\n"}; + + //! Offset of the first entry of /docs that is stored in its second cluster (tmp31.bin, behind the deleted tmp30.bin). + constexpr auto docs_second_cluster_first_live_entry_offset = 1056_B; + + //! The contents of the text files of the image: a line repeated and cut off at the size of the file. + [[nodiscard]] auto text_content(kstd::bytes size) -> std::string; + + //! The names of the root directory as reported by the driver, in on-disk order. + [[nodiscard]] auto root_names() -> std::vector<std::string>; + + //! The names of /docs as reported by the driver, in on-disk order. + [[nodiscard]] auto docs_names() -> std::vector<std::string>; + + //! The names of /docs/sub as reported by the driver, in on-disk order. + [[nodiscard]] auto sub_names() -> std::vector<std::string>; +} // namespace kernel::tests::filesystems::fat32::image + +namespace kernel::tests::filesystems::fat32 +{ + //! A fixture that mounts the fat32 test image with the fat32 driver. + //! + //! The image is mapped privately, so writes performed by a test are visible to that test only and never reach the + //! image file. + struct fat32_image_fixture : kernel::tests::vfs::storage_boot_module_fixture + { + using inode_ptr = kernel::vfs::filesystem::inode_ptr; + + //! Load the image as boot module and mount it. Throws if the image cannot be loaded or mounted. + auto mount_image() -> void; + + //! Look up a name in a directory, attaching the driver state to the found inode. + [[nodiscard]] auto lookup(inode_ptr const & parent, std::string_view name) const + -> kstd::result<kstd::shared_ptr<kernel::filesystems::fat32::inode>>; + + //! Resolve an absolute, '/' separated path from the root. Throws if a component cannot be found. + [[nodiscard]] auto open(std::string_view path) const -> kstd::shared_ptr<kernel::filesystems::fat32::inode>; + + //! Read a whole file into a string. Throws if reading fails. + [[nodiscard]] auto static read_all(kstd::shared_ptr<kernel::filesystems::fat32::inode> const & file) -> std::string; + + //! List all names of a directory through the inode's read_directory, using pages of @p page_size entries. + [[nodiscard]] auto static list(inode_ptr const & directory, std::size_t page_size = 64) -> std::vector<std::string>; + + kstd::shared_ptr<kernel::vfs::device_inode> dev_inode{}; + kstd::shared_ptr<kernel::filesystems::fat32::filesystem> fs{}; + inode_ptr root{}; + kernel::vfs::filesystem::driver_data_ptr driver_data{}; + kstd::shared_ptr<kernel::filesystems::fat32::driver_state> state{}; + }; +} // namespace kernel::tests::filesystems::fat32 + +#endif diff --git a/kernel/kernel/test_support/vfs/test_assets/fat32.img b/kernel/kernel/test_support/vfs/test_assets/fat32.img index 31031c9f..b8a2606e 120000..100644 --- a/kernel/kernel/test_support/vfs/test_assets/fat32.img +++ b/kernel/kernel/test_support/vfs/test_assets/fat32.img @@ -1 +1,3 @@ -../../../../../arch/x86_64/support/modules/fat32.img
\ No newline at end of file +version https://git-lfs.github.com/spec/v1 +oid sha256:fa9f26bd5c8d7916772cbfcd0d123d6672a9fa4216825144702fcb7c5786a8c3 +size 68157440 diff --git a/scripts/create_fat32_test_image.sh b/scripts/create_fat32_test_image.sh new file mode 100755 index 00000000..a2d9b45d --- /dev/null +++ b/scripts/create_fat32_test_image.sh @@ -0,0 +1,65 @@ +#!/bin/bash +# +# Create the fat32 image used by the fat32 driver tests (kernel/kernel/test_support/vfs/test_assets/fat32.img). +# +# The image is crafted to exercise the driver beyond a single file in the root directory: +# - nested directories (/docs/sub) and ".." entries pointing to the root (cluster 0) and to a subdirectory +# - a directory (/docs) with more entries than fit into one cluster, whose cluster chain is not contiguous +# - deleted entries interleaved with live ones (every even tmpN.bin is deleted) +# - files spanning multiple clusters (/docs/fragment.txt, /docs/sub/file2.txt, /longDirectoryName/longFileName.jpeg) +# - long file name entries next to their 8.3 aliases (LONGDI~1, LONGFI~1.JPE) +# - short names with the lowercase flags set (hello.txt, docs) +# +# The tests assert concrete cluster numbers and offsets of this image, so regenerating it requires updating +# kernel/kernel/test_support/filesystems/fat32_image.hpp accordingly. +# +# Requires dosfstools (mkfs.fat) and mtools (mmd, mcopy, mdel). + +set -euo pipefail + +IMG="$(realpath -m "${1:-fat32.img}")" +WORK_DIR="$(mktemp -d)" +trap 'rm -rf "${WORK_DIR}"' EXIT +cd "${WORK_DIR}" + +text() { + yes "Very important text document stored on fat32 drive." | head -c "$1" +} + +dd if=/dev/zero of="${IMG}" bs=1M count=65 status=none +mkfs.fat -F 32 -s 2 -n "TESTFAT" -i B50957CD "${IMG}" >/dev/null + +mmd -i "${IMG}" ::/docs +mmd -i "${IMG}" ::/docs/sub +mmd -i "${IMG}" ::/longDirectoryName + +echo "Hello World!" >hello.txt +mcopy -i "${IMG}" hello.txt ::/ + +# more than 32 entries, so that the directory has to grow into a second cluster +for i in {1..36}; do + echo "tmp" >"tmp${i}.bin" + mcopy -i "${IMG}" "tmp${i}.bin" ::/docs +done + +for i in {2..36..2}; do + mdel -i "${IMG}" "::/docs/tmp${i}.bin" +done + +text 234 >fragment.txt +mcopy -i "${IMG}" fragment.txt ::/docs + +text 11 >file.txt +mcopy -i "${IMG}" file.txt ::/docs + +text 2345 >fragment.txt +mcopy -o -i "${IMG}" fragment.txt ::/docs + +text 789 >file1.txt +mcopy -i "${IMG}" file1.txt ::/docs/sub + +text 2345 >file2.txt +mcopy -i "${IMG}" file2.txt ::/docs/sub + +text 2345 >longFileName.jpeg +mcopy -i "${IMG}" longFileName.jpeg ::/longDirectoryName |
