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

#include "kernel/filesystem/open_file_description.hpp"
#include "kernel/test_support/filesystem/inode.hpp"

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

#include <catch2/catch_test_macros.hpp>

SCENARIO("File descriptor table add/get file", "[filesystem][file_descriptor_table]")
{
  GIVEN("a file descriptor table and an open file description")
  {
    auto & table = kernel::filesystem::file_descriptor_table::get();
    auto inode = kstd::make_shared<kernel::tests::filesystem::inode>();
    auto file_description_1 = kstd::make_shared<kernel::filesystem::open_file_description>(inode);
    auto file_description_2 = kstd::make_shared<kernel::filesystem::open_file_description>(inode);

    WHEN("adding the open file description to the file descriptor table")
    {
      auto fd_1 = table.add_file(file_description_1);
      auto fd_2 = table.add_file(file_description_2);
      auto fd_3 = table.add_file(file_description_2);

      THEN("a valid file descriptor is returned")
      {
        REQUIRE(fd_1 == 0);
        REQUIRE(fd_2 == 1);
        REQUIRE(fd_3 == 2);
      }

      THEN("the file description can be retrieved using the returned file descriptor")
      {
        auto retrieved_description = table.get_file(fd_1);
        REQUIRE(retrieved_description == file_description_1);
      }
    }
  }

  GIVEN("a invalid open file description")
  {
    auto & table = kernel::filesystem::file_descriptor_table::get();

    THEN("adding a null file description returns an error code")
    {
      auto fd = table.add_file(nullptr);
      REQUIRE(fd == -1);
    }

    THEN("retrieving a file description with a negative file descriptor returns a null pointer")
    {
      auto retrieved_description = table.get_file(-1);
      REQUIRE(retrieved_description == nullptr);
    }

    THEN("retrieving a file description with an out-of-bounds file descriptor returns a null pointer")
    {
      auto retrieved_description = table.get_file(1000);
      REQUIRE(retrieved_description == nullptr);
    }
  }
}

SCENARIO("File descriptor table remove file", "[filesystem][file_descriptor_table]")
{
  GIVEN("a file descriptor table with an open file description")
  {
    auto & table = kernel::filesystem::file_descriptor_table::get();
    auto inode = kstd::make_shared<kernel::tests::filesystem::inode>();
    auto file_description = kstd::make_shared<kernel::filesystem::open_file_description>(inode);
    auto fd = table.add_file(file_description);

    WHEN("removing the file description using the file descriptor")
    {
      table.remove_file(fd);

      THEN("the file description can no longer be retrieved using the file descriptor")
      {
        auto retrieved_description = table.get_file(fd);
        REQUIRE(retrieved_description == nullptr);
      }
    }

    WHEN("removing a file description the other file descriptor keep the same index")
    {
      auto fd2 = table.add_file(file_description);
      table.remove_file(fd);

      THEN("the second file description can still be retrieved using its file descriptor")
      {
        auto retrieved_description = table.get_file(fd2);
        REQUIRE(retrieved_description == file_description);
      }
    }
  }

  GIVEN("an invalid file descriptor")
  {
    auto & table = kernel::filesystem::file_descriptor_table::get();

    THEN("removing a file with a negative file descriptor does nothing")
    {
      REQUIRE_NOTHROW(table.remove_file(-1));
    }

    THEN("removing a file with an out-of-bounds file descriptor does nothing")
    {
      REQUIRE_NOTHROW(table.remove_file(1000));
    }
  }
}