aboutsummaryrefslogtreecommitdiff
path: root/kernel/kapi
diff options
context:
space:
mode:
authorLukas Oesch <lukasoesch20@gmail.com>2026-04-14 09:43:45 +0200
committerLukas Oesch <lukasoesch20@gmail.com>2026-04-14 09:43:45 +0200
commite70ea2357a80386b0a12138201b353d942910296 (patch)
tree8cded750c8abf16c1c64c29e7dcc168d8a03a33d /kernel/kapi
parent5354654a486296be674ecc7ba5e92ca01cc29107 (diff)
downloadteachos-e70ea2357a80386b0a12138201b353d942910296.tar.xz
teachos-e70ea2357a80386b0a12138201b353d942910296.zip
add kapi filesystem tests
Diffstat (limited to 'kernel/kapi')
-rw-r--r--kernel/kapi/filesystem.tests.cpp128
1 files changed, 128 insertions, 0 deletions
diff --git a/kernel/kapi/filesystem.tests.cpp b/kernel/kapi/filesystem.tests.cpp
new file mode 100644
index 0000000..aa24aed
--- /dev/null
+++ b/kernel/kapi/filesystem.tests.cpp
@@ -0,0 +1,128 @@
+#include "kapi/filesystem.hpp"
+
+#include "kernel/test_support/filesystem/storage_boot_module_vfs_fixture.hpp"
+
+#include <catch2/catch_test_macros.hpp>
+
+#include <algorithm>
+#include <cstddef>
+#include <filesystem>
+#include <string_view>
+#include <vector>
+
+SCENARIO_METHOD(kernel::tests::filesystem::storage_boot_module_vfs_fixture, "Kapi filesystem with real images",
+ "[kapi][filesystem]")
+{
+ auto const image_path_1 = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "ext2_1KB_fs.img";
+ auto const image_path_2 = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "ext2_2KB_fs.img";
+
+ GIVEN("a real image file")
+ {
+ REQUIRE(std::filesystem::exists(image_path_1));
+ REQUIRE(std::filesystem::exists(image_path_2));
+ REQUIRE_NOTHROW(
+ setup_modules_from_img_and_init_vfs({"test_img_module_1", "test_img_module_2"}, {image_path_1, image_path_2}));
+
+ THEN("files can be opened, read and closed again")
+ {
+ auto fd = kapi::filesystem::open("/information/info_1.txt");
+ REQUIRE(fd >= 0);
+
+ auto buffer = std::vector<std::byte>(6);
+ auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
+ REQUIRE(bytes_read >= 0);
+
+ std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)};
+ REQUIRE(buffer_as_str == "info_1");
+
+ REQUIRE(kapi::filesystem::close(fd) == 0);
+ }
+
+ THEN("a filesystem can be mounted, files can be opened, read and closed again and unmounted")
+ {
+ REQUIRE(kapi::filesystem::mount("/dev/ram16", "/information") == 0);
+
+ auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt");
+ REQUIRE(fd >= 0);
+
+ auto buffer = std::vector<std::byte>(8);
+ auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
+ REQUIRE(bytes_read >= 0);
+
+ std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)};
+ REQUIRE(buffer_as_str == "monkey_1");
+
+ REQUIRE(kapi::filesystem::close(fd) == 0);
+ REQUIRE(kapi::filesystem::umount("/information") == 0);
+ }
+
+ THEN("device can be opened as file and read from")
+ {
+ auto fd = kapi::filesystem::open("/dev/ram0");
+ REQUIRE(fd >= 0);
+
+ auto buffer = std::vector<std::byte>(512);
+ auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
+ REQUIRE(bytes_read >= 0);
+
+ REQUIRE(kapi::filesystem::close(fd) == 0);
+ }
+
+ THEN("device can be opened as file and written to and read from again")
+ {
+ auto read_fd = kapi::filesystem::open("/dev/ram16");
+ REQUIRE(read_fd >= 0);
+
+ auto buffer = std::vector<std::byte>(512, std::byte{0xAB});
+ auto bytes_written = kapi::filesystem::write(read_fd, buffer.data(), buffer.size());
+ REQUIRE(bytes_written >= 0);
+
+ auto write_fd = kapi::filesystem::open("/dev/ram16");
+ REQUIRE(write_fd >= 0);
+
+ auto read_buffer = std::vector<std::byte>(512);
+ auto bytes_read = kapi::filesystem::read(write_fd, read_buffer.data(), read_buffer.size());
+ REQUIRE(bytes_read >= 0);
+
+ REQUIRE(std::equal(buffer.begin(), buffer.end(), read_buffer.begin()));
+
+ REQUIRE(kapi::filesystem::close(write_fd) == 0);
+ REQUIRE(kapi::filesystem::close(read_fd) == 0);
+ }
+
+ THEN("invalid paths cannot be mounted or unmounted")
+ {
+ REQUIRE(kapi::filesystem::mount("/dev/ram16", "invalid_path") < 0);
+ }
+
+ THEN("invalid paths cannot be unmounted")
+ {
+ REQUIRE(kapi::filesystem::umount("invalid_path") < 0);
+ }
+
+ THEN("non existent files cannot be opened")
+ {
+ auto fd = kapi::filesystem::open("/information/non_existent.txt");
+ REQUIRE(fd < 0);
+ }
+
+ THEN("not opened files cannot closed")
+ {
+ REQUIRE(kapi::filesystem::close(999) < 0);
+ }
+
+ THEN("not opened files cannot be read from")
+ {
+ std::vector<std::byte> buffer(10);
+ auto bytes_read = kapi::filesystem::read(999, buffer.data(), buffer.size());
+ REQUIRE(bytes_read < 0);
+ }
+
+ THEN("not opened files cannot be written to")
+ {
+ std::vector<std::byte> buffer(10);
+ auto bytes_written = kapi::filesystem::write(999, buffer.data(), buffer.size());
+ REQUIRE(bytes_written < 0);
+ }
+ }
+}