From 5354654a486296be674ecc7ba5e92ca01cc29107 Mon Sep 17 00:00:00 2001 From: Lukas Oesch Date: Tue, 14 Apr 2026 08:44:41 +0200 Subject: add tests to mount filesystems backed by a file --- arch/x86_64/support/modules/README.md | 26 +++++++++ arch/x86_64/support/modules/ext2_1KB_fs.img | 2 +- kernel/src/filesystem/vfs.tests.cpp | 67 +++++++++++++++++++++- .../filesystem/test_assets/ext2_1KB_fs.img | 2 +- 4 files changed, 93 insertions(+), 4 deletions(-) diff --git a/arch/x86_64/support/modules/README.md b/arch/x86_64/support/modules/README.md index f3955fa..fb64767 100644 --- a/arch/x86_64/support/modules/README.md +++ b/arch/x86_64/support/modules/README.md @@ -5,11 +5,37 @@ The ext2_4KB_fs image is intentionally fragmented, as some files were created an ## ext2_1KB_fs . ./lost+found +./archiv +./archiv/2024.img +./archiv/2025.img ./information ./information/info_1.txt ./information/info_2.txt ./closed.txt +### 2024.img +(2KB Block size) +. +./lost+found +./sheep_1.txt +./sheep_2.txt +./stable/pig_1.txt +./stable/pig_2.txt +./stable/pig_3.txt + +### 2025.img +(4KB Block size) +. +./lost+found +./snake_1.txt +./snake_2.txt +./petting_zoo/goat_1.txt +./petting_zoo/goat_2.txt +./petting_zoo/chicken_coop +./petting_zoo/chicken_coop/chicken_1.txt +./petting_zoo/chicken_coop/chicken_2.txt +./petting_zoo/chicken_coop/chicken_3.txt + ## ext2_2KB_fs . ./lost+found diff --git a/arch/x86_64/support/modules/ext2_1KB_fs.img b/arch/x86_64/support/modules/ext2_1KB_fs.img index 9f1ee4a..5bbb76d 100644 --- a/arch/x86_64/support/modules/ext2_1KB_fs.img +++ b/arch/x86_64/support/modules/ext2_1KB_fs.img @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:94d3988bc309eb9e81f06042c72bf4c4fb5991cd7fdd597eb00c518a96c792d8 +oid sha256:c2ef9536a439825520d9e230eedaa9ae327f9763350eddbc0f24bf5b9b5d2bf2 size 10485760 diff --git a/kernel/src/filesystem/vfs.tests.cpp b/kernel/src/filesystem/vfs.tests.cpp index 8c963c6..12dce84 100644 --- a/kernel/src/filesystem/vfs.tests.cpp +++ b/kernel/src/filesystem/vfs.tests.cpp @@ -1,12 +1,14 @@ #include "kernel/filesystem/vfs.hpp" -#include "kernel/devices/storage/management.hpp" -#include "kernel/filesystem/filesystem.hpp" #include "kernel/test_support/filesystem/storage_boot_module_vfs_fixture.hpp" +#include + #include +#include #include +#include SCENARIO_METHOD(kernel::tests::filesystem::storage_boot_module_vfs_fixture, "VFS with dummy modules", "[filesystem][vfs]") @@ -165,4 +167,65 @@ SCENARIO_METHOD(kernel::tests::filesystem::storage_boot_module_vfs_fixture, "VFS kernel::filesystem::vfs::operation_result::mount_point_not_found); } } + + GIVEN("A real image file containing as filesystem formatted files") + { + REQUIRE(std::filesystem::exists(image_path_1)); + REQUIRE_NOTHROW(setup_modules_from_img_and_init_vfs({"test_img_module_1"}, {image_path_1})); + + THEN("the file-filesystem in the image can be mounted, files can be read and unmounted again") + { + auto & vfs = kernel::filesystem::vfs::get(); + REQUIRE(vfs.do_mount("/archiv/2024.img", "/information") == kernel::filesystem::vfs::operation_result::success); + + auto sheep_1 = vfs.open("/information/sheep_1.txt"); + REQUIRE(sheep_1 != nullptr); + + kstd::vector buffer(7); + auto bytes_read = sheep_1->read(buffer.data(), buffer.size()); + std::string_view buffer_as_str{reinterpret_cast(buffer.data()), bytes_read}; + REQUIRE(buffer_as_str == "sheep_1"); + + REQUIRE(vfs.unmount("/information") == kernel::filesystem::vfs::operation_result::success); + auto unmounted_sheep_1 = vfs.open("/information/sheep_1.txt"); + REQUIRE(unmounted_sheep_1 == nullptr); + } + + THEN("the file-filesystem in the image can be mounted and in this filesystem can another file-filesystem be " + "mounted, files can be read and unmounted again") + { + auto & vfs = kernel::filesystem::vfs::get(); + REQUIRE(vfs.do_mount("/archiv/2024.img", "/information") == kernel::filesystem::vfs::operation_result::success); + REQUIRE(vfs.do_mount("/archiv/2025.img", "/information/stable") == + kernel::filesystem::vfs::operation_result::success); + + auto sheep_1 = vfs.open("/information/sheep_1.txt"); + auto goat_1 = vfs.open("/information/stable/petting_zoo/goat_1.txt"); + REQUIRE(sheep_1 != nullptr); + REQUIRE(goat_1 != nullptr); + + kstd::vector sheep_buffer(7); + auto bytes_read = sheep_1->read(sheep_buffer.data(), sheep_buffer.size()); + std::string_view buffer_as_str{reinterpret_cast(sheep_buffer.data()), bytes_read}; + REQUIRE(buffer_as_str == "sheep_1"); + + kstd::vector goat_buffer(6); + bytes_read = goat_1->read(goat_buffer.data(), goat_buffer.size()); + buffer_as_str = std::string_view{reinterpret_cast(goat_buffer.data()), bytes_read}; + REQUIRE(buffer_as_str == "goat_1"); + + REQUIRE(vfs.unmount("/information") == kernel::filesystem::vfs::operation_result::unmount_failed); + + REQUIRE(vfs.unmount("/information/stable") == kernel::filesystem::vfs::operation_result::success); + auto unmounted_goat_1 = vfs.open("/information/stable/petting_zoo/goat_1.txt"); + REQUIRE(unmounted_goat_1 == nullptr); + + auto still_mounted_sheep_1 = vfs.open("/information/sheep_1.txt"); + REQUIRE(still_mounted_sheep_1 != nullptr); + + REQUIRE(vfs.unmount("/information") == kernel::filesystem::vfs::operation_result::success); + auto unmounted_sheep_1 = vfs.open("/information/sheep_1.txt"); + REQUIRE(unmounted_sheep_1 == nullptr); + } + } } diff --git a/kernel/src/test_support/filesystem/test_assets/ext2_1KB_fs.img b/kernel/src/test_support/filesystem/test_assets/ext2_1KB_fs.img index 9f1ee4a..5bbb76d 100644 --- a/kernel/src/test_support/filesystem/test_assets/ext2_1KB_fs.img +++ b/kernel/src/test_support/filesystem/test_assets/ext2_1KB_fs.img @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:94d3988bc309eb9e81f06042c72bf4c4fb5991cd7fdd597eb00c518a96c792d8 +oid sha256:c2ef9536a439825520d9e230eedaa9ae327f9763350eddbc0f24bf5b9b5d2bf2 size 10485760 -- cgit v1.2.3