aboutsummaryrefslogtreecommitdiff
path: root/kernel/src/devices/storage/ram_disk/device.tests.cpp
blob: eacdb04bd3d0d39941ac5edc87e12cc088e42207 (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
#include "kernel/devices/storage/ram_disk/device.hpp"

#include "kapi/boot_module/boot_module.hpp"
#include "kapi/memory.hpp"

#include "catch2/matchers/catch_matchers.hpp"
#include "catch2/matchers/catch_matchers_range_equals.hpp"

#include <catch2/catch_test_macros.hpp>

#include <cstddef>
#include <ranges>
#include <stdexcept>
#include <vector>

SCENARIO("RAM Disk Device Construction and Initialization", "[ram_disk_device]")
{
  GIVEN("an empty boot module")
  {
    kapi::boot_modules::boot_module boot_module{};
    boot_module.start_address = kapi::memory::linear_address{nullptr};
    boot_module.size = 0;

    WHEN("constructing the device")
    {
      kernel::devices::storage::ram_disk::device device{boot_module, 0, 0};

      THEN("init return false")
      {
        REQUIRE_FALSE(device.init());
      }
    }
  }

  GIVEN("a boot module with a valid start address and size")
  {
    kapi::boot_modules::boot_module boot_module{};
    boot_module.start_address = kapi::memory::linear_address{reinterpret_cast<std::byte *>(0x1000)};
    boot_module.size = 512;

    WHEN("constructing the device")
    {
      kernel::devices::storage::ram_disk::device device{boot_module, 0, 0};

      THEN("init return true")
      {
        REQUIRE(device.init());
      }
    }
  }
}

SCENARIO("RAM Disk Device Read and Write", "[ram_disk_device]")
{
  GIVEN("a device initialized with a valid boot module")
  {
    auto storage = std::vector{4096, std::byte{0xff}};
    auto module =
        kapi::boot_modules::boot_module{"test_module", kapi::memory::linear_address{storage.data()}, storage.size()};
    auto device = kernel::devices::storage::ram_disk::device{module, 0, 0};
    REQUIRE(device.init());

    WHEN("reading a full block from the device")
    {
      auto buffer = std::vector<std::byte>(device.block_size());
      device.read_block(0, buffer.data());

      THEN("the buffer is filled with the module data")
      {
        REQUIRE_THAT(buffer, Catch::Matchers::RangeEquals(std::views::take(storage, device.block_size())));
      }
    }

    WHEN("reading from a block index beyond the module size")
    {
      auto buffer = std::vector<std::byte>(device.block_size());
      device.read_block(10, buffer.data());

      THEN("the buffer is filled with zeros")
      {
        REQUIRE_THAT(buffer, Catch::Matchers::RangeEquals(std::vector<std::byte>(device.block_size(), std::byte{0})));
      }
    }

    WHEN("reading into a null buffer")
    {
      REQUIRE_THROWS_AS(device.read_block(0, nullptr), std::runtime_error);
    }

    WHEN("writing to a full block")
    {
      auto buffer = std::vector<std::byte>(device.block_size(), std::byte{0x01});
      device.write_block(0, buffer.data());

      THEN("the module data is updated")
      {
        REQUIRE_THAT(std::views::take(storage, device.block_size()), Catch::Matchers::RangeEquals(buffer));
      }
    }

    WHEN("writing to a block index beyond the module size")
    {
      auto buffer = std::vector<std::byte>(device.block_size(), std::byte{0x01});
      device.write_block(10, buffer.data());

      THEN("the module data is not updated")
      {
        REQUIRE_THAT(storage, Catch::Matchers::RangeEquals(std::vector{4096, std::byte{0xff}}));
      }
    }

    WHEN("writing from a null buffer")
    {
      REQUIRE_THROWS_AS(device.write_block(0, nullptr), std::runtime_error);
    }
  }
}