#include #include #include #include #include #include #include #include #include #include // NOLINTBEGIN (readability-magic-numbers) 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("Two real image files") { 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").value(); auto buffer = std::vector(6); auto bytes_read = kapi::filesystem::read(fd, buffer); auto buffer_as_str = bytes_read.transform( [&](auto length) { return std::string_view{std::bit_cast(buffer.data()), length}; }); REQUIRE(buffer_as_str == "info_1"); REQUIRE(kapi::filesystem::close(fd)); } THEN("files can be opened, written to, read from and closed again") { auto write_fd = kapi::filesystem::open("/information/info_1.txt"); REQUIRE(write_fd); auto write_buffer = std::vector{std::byte{'H'}, std::byte{'e'}}; auto const bytes_written = kapi::filesystem::write(*write_fd, write_buffer); REQUIRE(bytes_written == 2); auto read_fd = kapi::filesystem::open("/information/info_1.txt"); REQUIRE(read_fd); auto read_buffer = std::vector(6); auto const bytes_read = kapi::filesystem::read(*read_fd, read_buffer); auto buffer_as_str = bytes_read.transform( [&](auto length) { return std::string_view{std::bit_cast(read_buffer.data()), length}; }); REQUIRE(buffer_as_str == "Hefo_1"); REQUIRE(kapi::filesystem::close(*write_fd)); REQUIRE(kapi::filesystem::close(*read_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").value(); auto buffer = std::vector(6); auto bytes_read = kapi::filesystem::read(fd, buffer); auto buffer_as_str = bytes_read.transform( [&](auto length) { return std::string_view{std::bit_cast(buffer.data()), length}; }); REQUIRE(buffer_as_str == "info_1"); 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").value(); auto buffer = std::vector(6); auto bytes_read = kapi::filesystem::read(fd, buffer); auto buffer_as_str = bytes_read.transform( [&](auto length) { return std::string_view{std::bit_cast(buffer.data()), length}; }); REQUIRE(buffer_as_str == "info_1"); REQUIRE(kapi::filesystem::close(fd)); } THEN("files can be opened through relative symbolic link over multiple mount points, read and closed again") { CHECK(kapi::filesystem::mount("/archiv/2024.img", "/information")); auto fd = kapi::filesystem::open("/information/symlinks/traverse_back_twice/information/sheep_1.txt").value(); auto buffer = std::vector(7); auto bytes_read = kapi::filesystem::read(fd, buffer); auto buffer_as_str = bytes_read.transform( [&](auto length) { return std::string_view{std::bit_cast(buffer.data()), length}; }); REQUIRE(buffer_as_str == "sheep_1"); 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")); auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt").value(); auto buffer = std::vector(8); auto bytes_read = kapi::filesystem::read(fd, buffer); auto buffer_as_str = bytes_read.transform( [&](auto length) { return std::string_view{std::bit_cast(buffer.data()), length}; }); REQUIRE(buffer_as_str == "monkey_1"); 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")); auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt").value(); REQUIRE(!kapi::filesystem::umount("/information")); 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").value(); auto buffer = std::vector(512); auto bytes_read = kapi::filesystem::read(fd, buffer); REQUIRE(bytes_read); 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").value(); auto buffer = std::vector(512, std::byte{0xAB}); auto bytes_written = kapi::filesystem::write(read_fd, buffer); REQUIRE(bytes_written); auto write_fd = kapi::filesystem::open("/dev/ram16").value(); auto read_buffer = std::vector(512); auto bytes_read = kapi::filesystem::read(write_fd, read_buffer); REQUIRE(bytes_read); REQUIRE(std::equal(buffer.begin(), buffer.end(), read_buffer.begin())); 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")); } THEN("invalid paths cannot be unmounted") { REQUIRE(!kapi::filesystem::umount("invalid_path")); } THEN("non existent files cannot be opened") { auto fd = kapi::filesystem::open("/information/non_existent.txt"); REQUIRE(!fd); } THEN("not opened files cannot closed") { REQUIRE(!kapi::filesystem::close(999)); } THEN("same file cannot be closed twice") { auto fd = kapi::filesystem::open("/information/info_1.txt").value(); REQUIRE(kapi::filesystem::close(fd)); REQUIRE(!kapi::filesystem::close(fd)); } THEN("not opened files cannot be read from") { std::vector buffer(10); auto const invalid_fd = 999uz; auto bytes_read = kapi::filesystem::read(invalid_fd, buffer); REQUIRE(!bytes_read); } THEN("not opened files cannot be written to") { std::vector buffer(10); auto const invalid_fd = 999uz; auto bytes_written = kapi::filesystem::write(invalid_fd, buffer); REQUIRE(!bytes_written); } THEN("a new file can be created, written to, read from, and closed again") { auto new_file_path = "/information/new_information.txt"; REQUIRE(!kapi::filesystem::open(new_file_path)); REQUIRE(kapi::filesystem::create(new_file_path)); auto fd = kapi::filesystem::open(new_file_path); REQUIRE(fd); auto write_buffer = std::vector{std::byte{'T'}, std::byte{'e'}, std::byte{'s'}, std::byte{'t'}}; CHECK(kapi::filesystem::write(*fd, write_buffer)); REQUIRE(kapi::filesystem::close(*fd)); fd = kapi::filesystem::open(new_file_path); REQUIRE(fd); auto read_buffer = std::vector(4); auto bytes_read = kapi::filesystem::read(*fd, read_buffer); std::string_view buffer_as_str{reinterpret_cast(read_buffer.data()), static_cast(*bytes_read)}; REQUIRE(buffer_as_str == "Test"); REQUIRE(kapi::filesystem::close(*fd)); } THEN("a new directory can be created and a file within can be created, opened, and closed again") { auto new_directory_path = "/information/new_directory"; auto new_file_path = "/information/new_directory/new_information.txt"; REQUIRE(!kapi::filesystem::open(new_file_path)); REQUIRE(kapi::filesystem::mkdir(new_directory_path)); REQUIRE(kapi::filesystem::create(new_file_path)); auto fd = kapi::filesystem::open(new_file_path); REQUIRE(fd); REQUIRE(kapi::filesystem::close(*fd)); } THEN("a file can be created in a mounted filesystem, opened, and closed again") { CHECK(kapi::filesystem::mount("/dev/ram16", "/information")); auto new_file_path = "/information/monkey_house/monkey_4.txt"; REQUIRE(!kapi::filesystem::open(new_file_path)); REQUIRE(kapi::filesystem::create(new_file_path)); auto fd = kapi::filesystem::open(new_file_path); REQUIRE(fd); REQUIRE(kapi::filesystem::close(*fd)); REQUIRE(kapi::filesystem::umount("/information")); } } } // NOLINTEND (readability-magic-numbers)