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

#include <kernel/test_support/filesystem/storage_boot_module_vfs_fixture.hpp>

#include <catch2/catch_test_macros.hpp>

#include <algorithm>
#include <cstddef>
#include <filesystem>
#include <string_view>
#include <vector>

SCENARIO_METHOD(kernel::tests::filesystem::storage_boot_module_vfs_fixture, "Kapi filesystem with real images",
                "[kapi][filesystem]")
{
  auto const image_path_1 = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "ext2_1KB_fs.img";
  auto const image_path_2 = std::filesystem::path{KERNEL_TEST_ASSETS_DIR} / "ext2_2KB_fs.img";

  GIVEN("a real image file")
  {
    REQUIRE(std::filesystem::exists(image_path_1));
    REQUIRE(std::filesystem::exists(image_path_2));
    REQUIRE_NOTHROW(
        setup_modules_from_img_and_init_vfs({"test_img_module_1", "test_img_module_2"}, {image_path_1, image_path_2}));

    THEN("files can be opened, read and closed again")
    {
      auto fd = kapi::filesystem::open("/information/info_1.txt");
      REQUIRE(fd >= 0);

      auto buffer = std::vector<std::byte>(6);
      auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
      REQUIRE(bytes_read >= 0);

      std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)};
      REQUIRE(buffer_as_str == "info_1");

      REQUIRE(kapi::filesystem::close(fd) == 0);
    }

    THEN("a filesystem can be mounted, files can be opened, read and closed again and unmounted")
    {
      REQUIRE(kapi::filesystem::mount("/dev/ram16", "/information") == 0);

      auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt");
      REQUIRE(fd >= 0);

      auto buffer = std::vector<std::byte>(8);
      auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
      REQUIRE(bytes_read >= 0);

      std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(bytes_read)};
      REQUIRE(buffer_as_str == "monkey_1");

      REQUIRE(kapi::filesystem::close(fd) == 0);
      REQUIRE(kapi::filesystem::umount("/information") == 0);
    }

    THEN("device can be opened as file and read from")
    {
      auto fd = kapi::filesystem::open("/dev/ram0");
      REQUIRE(fd >= 0);

      auto buffer = std::vector<std::byte>(512);
      auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
      REQUIRE(bytes_read >= 0);

      REQUIRE(kapi::filesystem::close(fd) == 0);
    }

    THEN("device can be opened as file and written to and read from again")
    {
      auto read_fd = kapi::filesystem::open("/dev/ram16");
      REQUIRE(read_fd >= 0);

      auto buffer = std::vector<std::byte>(512, std::byte{0xAB});
      auto bytes_written = kapi::filesystem::write(read_fd, buffer.data(), buffer.size());
      REQUIRE(bytes_written >= 0);

      auto write_fd = kapi::filesystem::open("/dev/ram16");
      REQUIRE(write_fd >= 0);

      auto read_buffer = std::vector<std::byte>(512);
      auto bytes_read = kapi::filesystem::read(write_fd, read_buffer.data(), read_buffer.size());
      REQUIRE(bytes_read >= 0);

      REQUIRE(std::equal(buffer.begin(), buffer.end(), read_buffer.begin()));

      REQUIRE(kapi::filesystem::close(write_fd) == 0);
      REQUIRE(kapi::filesystem::close(read_fd) == 0);
    }

    THEN("invalid paths cannot be mounted or unmounted")
    {
      REQUIRE(kapi::filesystem::mount("/dev/ram16", "invalid_path") < 0);
    }

    THEN("invalid paths cannot be unmounted")
    {
      REQUIRE(kapi::filesystem::umount("invalid_path") < 0);
    }

    THEN("non existent files cannot be opened")
    {
      auto fd = kapi::filesystem::open("/information/non_existent.txt");
      REQUIRE(fd < 0);
    }

    THEN("not opened files cannot closed")
    {
      REQUIRE(kapi::filesystem::close(999) < 0);
    }

    THEN("not opened files cannot be read from")
    {
      std::vector<std::byte> buffer(10);
      auto bytes_read = kapi::filesystem::read(999, buffer.data(), buffer.size());
      REQUIRE(bytes_read < 0);
    }

    THEN("not opened files cannot be written to")
    {
      std::vector<std::byte> buffer(10);
      auto bytes_written = kapi::filesystem::write(999, buffer.data(), buffer.size());
      REQUIRE(bytes_written < 0);
    }
  }
}