#include #include #include #include #include #include #include #include #include #include SCENARIO("Mount table construction", "[filesystem][mount_table]") { GIVEN("an empty mount table") { kernel::filesystem::mount_table table; THEN("removing any mount returns mount_not_found") { REQUIRE(table.remove_mount("/") == kernel::filesystem::mount_table::operation_result::mount_not_found); REQUIRE(table.remove_mount("/any/path") == kernel::filesystem::mount_table::operation_result::mount_not_found); } } } SCENARIO("Adding, finding and removing mounts in the mount table", "[filesystem][mount_table]") { GIVEN("a mount table and some mounts") { kernel::filesystem::mount_table table; auto fs1 = kstd::make_shared(); auto root_dentry1 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount_dentry1 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount1 = kstd::make_shared(mount_dentry1, root_dentry1, fs1, nullptr); auto fs2 = kstd::make_shared(); auto root_dentry2 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount_dentry2 = kstd::make_shared( nullptr, kstd::make_shared(), "/mnt"); auto mount2 = kstd::make_shared(mount_dentry2, root_dentry2, fs2, nullptr); table.add_mount(mount1); table.add_mount(mount2); THEN("dentry flags are set correctly for mounted dentries") { REQUIRE(mount_dentry1->has_flag(kernel::filesystem::dentry::dentry_flags::is_mount_point)); REQUIRE(mount_dentry2->has_flag(kernel::filesystem::dentry::dentry_flags::is_mount_point)); } THEN("finding mounts by exact valid path returns the correct mount") { REQUIRE(table.find_mount("/") == mount1); REQUIRE(table.find_mount("/mnt") == mount2); } THEN("finding mounts by exact invalid path returns null") { REQUIRE(table.find_mount("/nonexistent") == nullptr); REQUIRE(table.find_mount("/mnt/file") == nullptr); } THEN("removing a mount that has no child mounts succeeds") { REQUIRE(table.remove_mount("/mnt") == kernel::filesystem::mount_table::operation_result::removed); REQUIRE_FALSE(root_dentry2->has_flag(kernel::filesystem::dentry::dentry_flags::is_mount_point)); } THEN("removing a mount that does not exist returns mount_not_found") { REQUIRE(table.remove_mount("/nonexistent") == kernel::filesystem::mount_table::operation_result::mount_not_found); } } GIVEN("multiple mounts with the same path") { kernel::filesystem::mount_table table; auto fs1 = kstd::make_shared(); auto root_dentry1 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount_dentry1 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount1 = kstd::make_shared(mount_dentry1, root_dentry1, fs1, nullptr); auto fs2 = kstd::make_shared(); auto root_dentry2 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount_dentry2 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount2 = kstd::make_shared(mount_dentry2, root_dentry2, fs2, nullptr); table.add_mount(mount1); table.add_mount(mount2); THEN("finding mounts by exact valid path returns the correct mount") { REQUIRE(table.find_mount("/") == mount2); } THEN("removing the topmost mount with the same path succeeds") { REQUIRE(table.remove_mount("/") == kernel::filesystem::mount_table::operation_result::removed); REQUIRE_FALSE(root_dentry2->has_flag(kernel::filesystem::dentry::dentry_flags::is_mount_point)); } } GIVEN("a mount with child mounts") { kernel::filesystem::mount_table table; auto fs1 = kstd::make_shared(); auto root_dentry1 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount_dentry1 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount1 = kstd::make_shared(mount_dentry1, root_dentry1, fs1, nullptr); auto fs2 = kstd::make_shared(); auto root_dentry2 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount_dentry2 = kstd::make_shared( mount_dentry1, kstd::make_shared(), "mnt"); auto mount2 = kstd::make_shared(mount_dentry2, root_dentry2, fs2, mount1); auto fs3 = kstd::make_shared(); auto root_dentry3 = kstd::make_shared( nullptr, kstd::make_shared(), "/"); auto mount_dentry3 = kstd::make_shared( mount_dentry2, kstd::make_shared(), "submnt"); auto mount3 = kstd::make_shared(mount_dentry3, root_dentry3, fs3, mount2); table.add_mount(mount1); table.add_mount(mount2); table.add_mount(mount3); THEN("removing a mount with child mounts returns has_child_mounts") { REQUIRE(table.remove_mount("/") == kernel::filesystem::mount_table::operation_result::has_child_mounts); REQUIRE(table.remove_mount("/mnt") == kernel::filesystem::mount_table::operation_result::has_child_mounts); } THEN("removing a leaf mount succeeds") { REQUIRE(table.remove_mount("/mnt/submnt") == kernel::filesystem::mount_table::operation_result::removed); REQUIRE_FALSE(root_dentry3->has_flag(kernel::filesystem::dentry::dentry_flags::is_mount_point)); } } }