#include #include #include #include #include #include SCENARIO("Dentry construction", "[filesystem][dentry]") { GIVEN("A parent dentry and inode") { auto inode = kstd::make_shared(); auto parent_dentry = kstd::make_shared(nullptr, inode, "parent"); WHEN("constructing a dentry") { auto child_dentry = kernel::filesystem::dentry{parent_dentry, inode, "child"}; THEN("the dentry has the correct parent, inode, and name") { REQUIRE(child_dentry.get_parent() == parent_dentry); REQUIRE(child_dentry.get_inode() == inode); REQUIRE(child_dentry.get_name() == "child"); } THEN("no flag is set") { REQUIRE_FALSE(child_dentry.has_flag(kernel::filesystem::dentry::dentry_flags::mounted)); } } WHEN("constructing a dentry with an empty name") { THEN("the dentry has the correct parent and inode, and an empty name") { REQUIRE_THROWS_AS((kernel::filesystem::dentry{parent_dentry, inode, ""}), kernel::tests::cpu::halt); } } WHEN("constructing a dentry with a null parent") { auto child_dentry = kernel::filesystem::dentry{nullptr, inode, "child"}; THEN("the dentry has a null parent, the correct inode, and the correct name") { REQUIRE(child_dentry.get_parent() == nullptr); REQUIRE(child_dentry.get_inode() == inode); REQUIRE(child_dentry.get_name() == "child"); } THEN("no flag is set") { REQUIRE_FALSE(child_dentry.has_flag(kernel::filesystem::dentry::dentry_flags::mounted)); } } WHEN("constructing a dentry with a null inode") { THEN("the system panics") { REQUIRE_THROWS_AS((kernel::filesystem::dentry{parent_dentry, nullptr, "child"}), kernel::tests::cpu::halt); } } } } SCENARIO("Dentry child logic", "[filesystem][dentry]") { GIVEN("A parent dentry and inode") { auto inode = kstd::make_shared(); auto parent_dentry = kstd::make_shared(nullptr, inode, "parent"); WHEN("adding child dentries") { auto child1 = kstd::make_shared(parent_dentry, inode, "child1"); auto child2 = kstd::make_shared(parent_dentry, inode, "child2"); parent_dentry->add_child(child1); parent_dentry->add_child(child2); THEN("the children can be found by name") { REQUIRE(parent_dentry->find_child("child1") == child1); REQUIRE(parent_dentry->find_child("child2") == child2); } THEN("finding a non-existent child returns null") { REQUIRE(parent_dentry->find_child("nonexistent") == nullptr); } } } } SCENARIO("Dentry Flag logic", "[filesystem][dentry]") { GIVEN("A dentry") { auto inode = kstd::make_shared(); auto dentry = kernel::filesystem::dentry{nullptr, inode, "test"}; WHEN("setting a flag") { dentry.set_flag(kernel::filesystem::dentry::dentry_flags::mounted); THEN("the flag is set") { REQUIRE(dentry.has_flag(kernel::filesystem::dentry::dentry_flags::mounted)); } } WHEN("unsetting a flag") { dentry.set_flag(kernel::filesystem::dentry::dentry_flags::mounted); dentry.unset_flag(kernel::filesystem::dentry::dentry_flags::mounted); THEN("the flag is unset") { REQUIRE_FALSE(dentry.has_flag(kernel::filesystem::dentry::dentry_flags::mounted)); } } } } SCENARIO("Dentry path resolution", "[filesystem][dentry]") { GIVEN("A dentry with a parent hierarchy") { auto root_inode = kstd::make_shared(); auto root_dentry = kstd::make_shared(nullptr, root_inode, "/"); auto home_inode = kstd::make_shared(); auto home_dentry = kstd::make_shared(root_dentry, home_inode, "home"); root_dentry->add_child(home_dentry); auto user_inode = kstd::make_shared(); auto user_dentry = kstd::make_shared(home_dentry, user_inode, "user"); home_dentry->add_child(user_dentry); THEN("the full path is constructed correctly") { REQUIRE(root_dentry->get_full_path() == "/"); REQUIRE(home_dentry->get_full_path() == "/home"); REQUIRE(user_dentry->get_full_path() == "/home/user"); } } }