aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Oesch <lukasoesch20@gmail.com>2026-04-09 10:56:25 +0200
committerLukas Oesch <lukasoesch20@gmail.com>2026-04-11 08:05:53 +0200
commita03d4d9acc5115ec3f651b06f94ced9a7c0e192f (patch)
tree3bb5e3680d29a1eb37ef749fed0868c69bb256f4
parent787671aac288590e40c5cabfc9f82a31f21629fe (diff)
downloadteachos-a03d4d9acc5115ec3f651b06f94ced9a7c0e192f.tar.xz
teachos-a03d4d9acc5115ec3f651b06f94ced9a7c0e192f.zip
add open_file_description tests with real image
-rw-r--r--kernel/src/filesystem/open_file_description.tests.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/kernel/src/filesystem/open_file_description.tests.cpp b/kernel/src/filesystem/open_file_description.tests.cpp
index f045094..db8eb49 100644
--- a/kernel/src/filesystem/open_file_description.tests.cpp
+++ b/kernel/src/filesystem/open_file_description.tests.cpp
@@ -1,6 +1,8 @@
#include "kernel/filesystem/open_file_description.hpp"
+#include "kernel/filesystem/vfs.hpp"
#include "kernel/test_support/filesystem/inode.hpp"
+#include "kernel/test_support/filesystem/storage_boot_module_vfs_fixture.hpp"
#include <kstd/memory>
#include <kstd/print>
@@ -8,6 +10,10 @@
#include <catch2/catch_test_macros.hpp>
+#include <cstddef>
+#include <filesystem>
+#include <string_view>
+
SCENARIO("Open file description construction", "[filesystem][open_file_description]")
{
GIVEN("an inode and an open file description for that inode")
@@ -58,3 +64,51 @@ SCENARIO("Open file description read/write offset management", "[filesystem][ope
}
}
}
+
+SCENARIO_METHOD(kernel::tests::filesystem::storage_boot_module_vfs_fixture,
+ "Open file description read with real image", "[filesystem][open_file_description][img]")
+{
+ auto const image_path = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "ext2_1KB_fs.img";
+
+ GIVEN("an open file description for a file in a real image")
+ {
+ REQUIRE(std::filesystem::exists(image_path));
+ REQUIRE_NOTHROW(setup_modules_from_img_and_init_vfs({"test_img_module"}, {image_path}));
+
+ auto & vfs = kernel::filesystem::vfs::get();
+ auto ofd = vfs.open("/information/info_1.txt");
+ REQUIRE(ofd != nullptr);
+
+ THEN("the file can be read and the offset is updated")
+ {
+ kstd::vector<std::byte> buffer(32);
+ auto bytes_read = ofd->read(buffer.data(), buffer.size());
+ REQUIRE(bytes_read == 32);
+ REQUIRE(ofd->offset() == 32);
+
+ std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)};
+ auto const content_end = buffer_as_str.find('\0');
+ REQUIRE(buffer_as_str.substr(0, content_end) == "info_1\n");
+
+ for (auto i = content_end; i < buffer_as_str.size(); ++i)
+ {
+ REQUIRE(buffer_as_str[i] == '\0');
+ }
+ }
+
+ THEN("the file can be read multiple times")
+ {
+ kstd::vector<std::byte> buffer(4);
+ auto bytes_read_1 = ofd->read(buffer.data(), buffer.size() / 2);
+ REQUIRE(bytes_read_1 == buffer.size() / 2);
+ REQUIRE(ofd->offset() == buffer.size() / 2);
+
+ auto bytes_read_2 = ofd->read(buffer.data() + buffer.size() / 2, buffer.size() / 2);
+ REQUIRE(bytes_read_2 == buffer.size() / 2);
+ REQUIRE(ofd->offset() == buffer.size());
+
+ std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), bytes_read_1 + bytes_read_2};
+ REQUIRE(buffer_as_str == "info");
+ }
+ }
+}