diff options
Diffstat (limited to 'kernel/kapi/filesystem.tests.cpp')
| -rw-r--r-- | kernel/kapi/filesystem.tests.cpp | 104 |
1 files changed, 48 insertions, 56 deletions
diff --git a/kernel/kapi/filesystem.tests.cpp b/kernel/kapi/filesystem.tests.cpp index d241afa8..d1e01db6 100644 --- a/kernel/kapi/filesystem.tests.cpp +++ b/kernel/kapi/filesystem.tests.cpp @@ -2,6 +2,8 @@ #include <kernel/test_support/filesystem/storage_boot_module_vfs_fixture.hpp> +#include <kstd/system_error.hpp> + #include <catch2/catch_test_macros.hpp> #include <algorithm> @@ -25,159 +27,149 @@ SCENARIO_METHOD(kernel::tests::filesystem::storage_boot_module_vfs_fixture, "Kap THEN("files can be opened, read and closed again") { - auto fd = kapi::filesystem::open("/information/info_1.txt"); - REQUIRE(fd >= 0); + auto fd = kapi::filesystem::open("/information/info_1.txt").value(); auto buffer = std::vector<std::byte>(6); auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size()); - REQUIRE(bytes_read >= 0); + REQUIRE(bytes_read); - std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)}; + 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); + REQUIRE(kapi::filesystem::close(fd)); } THEN("files can be opened through absolute symbolic link, read and closed again") { - auto fd = kapi::filesystem::open("/symlinks/information_directory_absolute/info_1.txt"); - REQUIRE(fd >= 0); + auto fd = kapi::filesystem::open("/symlinks/information_directory_absolute/info_1.txt").value(); auto buffer = std::vector<std::byte>(6); auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size()); - REQUIRE(bytes_read >= 0); + REQUIRE(bytes_read); - std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)}; + 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); + REQUIRE(kapi::filesystem::close(fd)); } THEN("files can be opened through relative symbolic link, read and closed again") { - auto fd = kapi::filesystem::open("/symlinks/information_directory_relative/info_1.txt"); - REQUIRE(fd >= 0); + auto fd = kapi::filesystem::open("/symlinks/information_directory_relative/info_1.txt").value(); auto buffer = std::vector<std::byte>(6); auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size()); - REQUIRE(bytes_read >= 0); + REQUIRE(bytes_read); - std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)}; + 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); + REQUIRE(kapi::filesystem::close(fd)); } THEN("files can be opened through relative symbolic link over multiple mount points, read and closed again") { - kapi::filesystem::mount("/archiv/2024.img", "/information"); + CHECK(kapi::filesystem::mount("/archiv/2024.img", "/information")); - auto fd = kapi::filesystem::open("/information/symlinks/traverse_back_twice/information/sheep_1.txt"); - REQUIRE(fd >= 0); + auto fd = kapi::filesystem::open("/information/symlinks/traverse_back_twice/information/sheep_1.txt").value(); auto buffer = std::vector<std::byte>(7); auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size()); - REQUIRE(bytes_read >= 0); + REQUIRE(bytes_read); - std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)}; + std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(*bytes_read)}; REQUIRE(buffer_as_str == "sheep_1"); - REQUIRE(kapi::filesystem::close(fd) == 0); + REQUIRE(kapi::filesystem::close(fd)); } THEN("a filesystem can be mounted, files can be opened, read and closed again and unmounted") { - REQUIRE(kapi::filesystem::mount("/dev/ram16", "/information") == 0); + REQUIRE(kapi::filesystem::mount("/dev/ram16", "/information")); - auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt"); - REQUIRE(fd >= 0); + auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt").value(); auto buffer = std::vector<std::byte>(8); auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size()); - REQUIRE(bytes_read >= 0); + REQUIRE(bytes_read); - std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)}; + 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); + REQUIRE(kapi::filesystem::close(fd)); + REQUIRE(kapi::filesystem::umount("/information")); } THEN("a filesystem cannot be unmounted if files are still open and can be unmounted after files are closed") { - REQUIRE(kapi::filesystem::mount("/dev/ram16", "/information") == 0); + REQUIRE(kapi::filesystem::mount("/dev/ram16", "/information")); - auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt"); - REQUIRE(fd >= 0); + auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt").value(); - REQUIRE(kapi::filesystem::umount("/information") < 0); + REQUIRE(!kapi::filesystem::umount("/information")); - REQUIRE(kapi::filesystem::close(fd) == 0); - REQUIRE(kapi::filesystem::umount("/information") == 0); + REQUIRE(kapi::filesystem::close(fd)); + REQUIRE(kapi::filesystem::umount("/information")); } THEN("device can be opened as file and read from") { - auto fd = kapi::filesystem::open("/dev/ram0"); - REQUIRE(fd >= 0); + auto fd = kapi::filesystem::open("/dev/ram0").value(); auto buffer = std::vector<std::byte>(512); auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size()); - REQUIRE(bytes_read >= 0); + REQUIRE(bytes_read); - REQUIRE(kapi::filesystem::close(fd) == 0); + REQUIRE(kapi::filesystem::close(fd)); } 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 read_fd = kapi::filesystem::open("/dev/ram16").value(); 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); + REQUIRE(bytes_written); - auto write_fd = kapi::filesystem::open("/dev/ram16"); - REQUIRE(write_fd >= 0); + auto write_fd = kapi::filesystem::open("/dev/ram16").value(); 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(bytes_read); REQUIRE(std::equal(buffer.begin(), buffer.end(), read_buffer.begin())); - REQUIRE(kapi::filesystem::close(write_fd) == 0); - REQUIRE(kapi::filesystem::close(read_fd) == 0); + REQUIRE(kapi::filesystem::close(write_fd)); + REQUIRE(kapi::filesystem::close(read_fd)); } THEN("invalid paths cannot be mounted or unmounted") { - REQUIRE(kapi::filesystem::mount("/dev/ram16", "invalid_path") < 0); + REQUIRE(!kapi::filesystem::mount("/dev/ram16", "invalid_path")); } THEN("invalid paths cannot be unmounted") { - REQUIRE(kapi::filesystem::umount("invalid_path") < 0); + REQUIRE(!kapi::filesystem::umount("invalid_path")); } THEN("non existent files cannot be opened") { auto fd = kapi::filesystem::open("/information/non_existent.txt"); - REQUIRE(fd < 0); + REQUIRE(!fd); } THEN("not opened files cannot closed") { - REQUIRE(kapi::filesystem::close(999) < 0); + REQUIRE(!kapi::filesystem::close(999)); } THEN("same file cannot be closed twice") { - auto fd = kapi::filesystem::open("/information/info_1.txt"); - REQUIRE(fd >= 0); + auto fd = kapi::filesystem::open("/information/info_1.txt").value(); - REQUIRE(kapi::filesystem::close(fd) == 0); - REQUIRE(kapi::filesystem::close(fd) < 0); + REQUIRE(kapi::filesystem::close(fd)); + REQUIRE(!kapi::filesystem::close(fd)); } THEN("not opened files cannot be read from") @@ -185,7 +177,7 @@ SCENARIO_METHOD(kernel::tests::filesystem::storage_boot_module_vfs_fixture, "Kap std::vector<std::byte> buffer(10); auto const invalid_fd = 999uz; auto bytes_read = kapi::filesystem::read(invalid_fd, buffer.data(), buffer.size()); - REQUIRE(bytes_read < 0); + REQUIRE(!bytes_read); } THEN("not opened files cannot be written to") @@ -193,7 +185,7 @@ SCENARIO_METHOD(kernel::tests::filesystem::storage_boot_module_vfs_fixture, "Kap std::vector<std::byte> buffer(10); auto const invalid_fd = 999uz; auto bytes_written = kapi::filesystem::write(invalid_fd, buffer.data(), buffer.size()); - REQUIRE(bytes_written < 0); + REQUIRE(!bytes_written); } } } |
