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

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

#include <catch2/catch_test_macros.hpp>

SCENARIO("Rootfs inode creation", "[filesystem][rootfs][inode]")
{
  GIVEN("a rootfs inode")
  {
    auto inode = kernel::filesystem::rootfs::inode{};

    THEN("the inode has the correct kind")
    {
      REQUIRE(inode.is_directory());
      REQUIRE_FALSE(inode.is_device());
      REQUIRE_FALSE(inode.is_regular());
    }

    THEN("the inode has no children")
    {
      REQUIRE(inode.lookup_child("child") == nullptr);
    }
  }
}

SCENARIO("Rootfs inode child management", "[filesystem][rootfs][inode]")
{
  GIVEN("a rootfs inode")
  {
    auto inode = kernel::filesystem::rootfs::inode{};

    WHEN("adding a child inode")
    {
      inode.add_child("child");
      inode.add_child("another child");

      THEN("the child can be looked up by name")
      {
        auto child_inode = inode.lookup_child("child");
        REQUIRE(child_inode != nullptr);
        REQUIRE(child_inode->is_directory());
      }

      THEN("looking up a non-existent child returns null")
      {
        REQUIRE(inode.lookup_child("nonexistent") == nullptr);
      }
    }
  }
}

SCENARIO("Rootfs inode read/write", "[filesystem][rootfs][inode]")
{
  GIVEN("a rootfs inode")
  {
    auto inode = kernel::filesystem::rootfs::inode{};

    WHEN("reading from the inode")
    {
      kstd::vector<char> buffer(10);
      auto bytes_read = inode.read(buffer.data(), 0, buffer.size());

      THEN("no bytes are read")
      {
        REQUIRE(bytes_read == 0);
      }
    }

    WHEN("writing to the inode")
    {
      kstd::vector<char> buffer(10, 'x');
      auto bytes_written = inode.write(buffer.data(), 0, buffer.size());

      THEN("no bytes are written")
      {
        REQUIRE(bytes_written == 0);
      }
    }
  }
}