#include "kernel/filesystem/dentry.hpp" #include "kernel/test_support/cpu.hpp" #include "kernel/test_support/filesystem/inode.hpp" #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); 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::dcache_mounted)); } } WHEN("constructing a dentry with an empty name") { auto child_dentry = kernel::filesystem::dentry{parent_dentry, inode}; THEN("the dentry has the correct parent and inode, and an empty name") { REQUIRE(child_dentry.get_parent() == parent_dentry); REQUIRE(child_dentry.get_inode() == inode); REQUIRE(child_dentry.get_name().empty()); } THEN("no flag is set") { REQUIRE_FALSE(child_dentry.has_flag(kernel::filesystem::dentry::dentry_flags::dcache_mounted)); } } 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::dcache_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); 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::dcache_mounted); THEN("the flag is set") { REQUIRE(dentry.has_flag(kernel::filesystem::dentry::dentry_flags::dcache_mounted)); } } WHEN("unsetting a flag") { dentry.set_flag(kernel::filesystem::dentry::dentry_flags::dcache_mounted); dentry.unset_flag(kernel::filesystem::dentry::dentry_flags::dcache_mounted); THEN("the flag is unset") { REQUIRE_FALSE(dentry.has_flag(kernel::filesystem::dentry::dentry_flags::dcache_mounted)); } } } }