aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/filesystem/mount.tests.cpp
blob: e7dd709848943713b9877e5823b17f6e07764abd (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
#include <kernel/filesystem/mount.hpp>

#include <kernel/filesystem/dentry.hpp>
#include <kernel/test_support/cpu.hpp>
#include <kernel/test_support/filesystem/filesystem.hpp>
#include <kernel/test_support/filesystem/inode.hpp>

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

#include <catch2/catch_test_macros.hpp>

SCENARIO("Mount construction", "[filesystem][mount]")
{
  GIVEN("a filesystem and a root dentry")
  {
    auto fs = kstd::make_shared<kernel::tests::filesystem::filesystem>();
    auto root_inode = kstd::make_shared<kernel::tests::filesystem::inode>();
    auto root_dentry = kstd::make_shared<kernel::filesystem::dentry>(nullptr, root_inode, "/");

    WHEN("constructing a mount with the filesystem and root dentry")
    {
      auto mount = kernel::filesystem::mount{root_dentry, root_dentry, fs, nullptr};

      THEN("the mount has the correct filesystem, root dentry, mount dentry, and mount path")
      {
        REQUIRE(mount.get_filesystem() == fs);
        REQUIRE(mount.get_root_dentry() == root_dentry);
        REQUIRE(mount.get_mount_dentry() == root_dentry);
        REQUIRE(mount.get_mount_path() == "/");
        REQUIRE(mount.is_ready_to_unmount());
      }

      THEN("the mount has no parent mount")
      {
        REQUIRE(mount.get_parent_mount() == nullptr);
      }
    }

    WHEN("constructing a mount with a null filesystem")
    {
      THEN("the constructor panics")
      {
        REQUIRE_THROWS_AS((kernel::filesystem::mount{root_dentry, root_dentry, nullptr, nullptr}),
                          kernel::tests::cpu::halt);
      }
    }
  }
}

SCENARIO("Mount reference counting", "[filesystem][mount]")
{
  GIVEN("a filesystem and a root dentry")
  {
    auto fs = kstd::make_shared<kernel::tests::filesystem::filesystem>();
    auto root_inode = kstd::make_shared<kernel::tests::filesystem::inode>();
    auto root_dentry = kstd::make_shared<kernel::filesystem::dentry>(nullptr, root_inode, "/");

    THEN("reference count can be incremented and decremented, the mount is ready to unmount when the reference "
         "count == 0")
    {
      auto mount = kernel::filesystem::mount{root_dentry, root_dentry, fs, nullptr};

      mount.increment_ref_count();
      REQUIRE(mount.get_ref_count() == 1);
      REQUIRE_FALSE(mount.is_ready_to_unmount());

      mount.increment_ref_count();
      REQUIRE(mount.get_ref_count() == 2);
      REQUIRE_FALSE(mount.is_ready_to_unmount());

      REQUIRE(mount.decrement_ref_count());
      REQUIRE(mount.get_ref_count() == 1);
      REQUIRE_FALSE(mount.is_ready_to_unmount());

      REQUIRE(mount.decrement_ref_count());
      REQUIRE(mount.get_ref_count() == 0);
      REQUIRE(mount.is_ready_to_unmount());
    }

    THEN("decrementing reference count when it is already zero does not decrement it below zero")
    {
      auto mount = kernel::filesystem::mount{root_dentry, root_dentry, fs, nullptr};

      REQUIRE_FALSE(mount.decrement_ref_count());
      REQUIRE(mount.get_ref_count() == 0);
      REQUIRE(mount.is_ready_to_unmount());
    }
  }
}