aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/dentry.tests.cpp
blob: 94aa48db2b6a292e9014dc2c78cc93921e8f4935 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "kernel/filesystem/dentry.hpp"

#include "kernel/filesystem/devfs/inode.hpp"
#include "kernel/test_support/cpu.hpp"

#include <kstd/memory>
#include <kstd/print>

#include <catch2/catch_test_macros.hpp>

SCENARIO("Dentry construction", "[filesystem][dentry]")
{
  GIVEN("A parent dentry and inode")
  {
    auto inode = kstd::make_shared<kernel::filesystem::devfs::inode>();
    auto parent_dentry = kstd::make_shared<kernel::filesystem::dentry>(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<kernel::filesystem::devfs::inode>();
    auto parent_dentry = kstd::make_shared<kernel::filesystem::dentry>(nullptr, inode);

    WHEN("adding child dentries")
    {
      auto child1 = kstd::make_shared<kernel::filesystem::dentry>(parent_dentry, inode, "child1");
      auto child2 = kstd::make_shared<kernel::filesystem::dentry>(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<kernel::filesystem::devfs::inode>();
    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));
      }
    }
  }
}