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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
#include <kapi/filesystem.hpp>
#include <kernel/test_support/filesystem/storage_boot_module_vfs_fixture.hpp>
#include <kstd/system_error.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("Two real image files")
{
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").value();
auto buffer = std::vector<std::byte>(6);
auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
REQUIRE(bytes_read);
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));
}
THEN("files can be opened, written to, read from and closed again")
{
auto write_fd = kapi::filesystem::open("/information/info_1.txt");
REQUIRE(write_fd);
auto write_buffer = std::vector<std::byte>{std::byte{'H'}, std::byte{'e'}};
auto const bytes_written = kapi::filesystem::write(*write_fd, write_buffer.data(), write_buffer.size());
REQUIRE(bytes_written == 2);
auto read_fd = kapi::filesystem::open("/information/info_1.txt");
REQUIRE(read_fd);
auto read_buffer = std::vector<std::byte>(6);
auto const bytes_read = kapi::filesystem::read(*read_fd, read_buffer.data(), read_buffer.size());
REQUIRE(bytes_read);
std::string_view buffer_as_str{reinterpret_cast<char *>(read_buffer.data()), static_cast<size_t>(*bytes_read)};
REQUIRE(buffer_as_str == "Hefo_1");
REQUIRE(kapi::filesystem::close(*write_fd));
REQUIRE(kapi::filesystem::close(*read_fd));
}
THEN("files can be opened through absolute symbolic link, read and closed again")
{
auto fd = kapi::filesystem::open("/symlinks/information_directory_absolute/info_1.txt").value();
auto buffer = std::vector<std::byte>(6);
auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
REQUIRE(bytes_read);
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));
}
THEN("files can be opened through relative symbolic link, read and closed again")
{
auto fd = kapi::filesystem::open("/symlinks/information_directory_relative/info_1.txt").value();
auto buffer = std::vector<std::byte>(6);
auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
REQUIRE(bytes_read);
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));
}
THEN("files can be opened through relative symbolic link over multiple mount points, read and closed again")
{
CHECK(kapi::filesystem::mount("/archiv/2024.img", "/information"));
auto fd = kapi::filesystem::open("/information/symlinks/traverse_back_twice/information/sheep_1.txt").value();
auto buffer = std::vector<std::byte>(7);
auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
REQUIRE(bytes_read);
std::string_view buffer_as_str{reinterpret_cast<char *>(buffer.data()), static_cast<size_t>(*bytes_read)};
REQUIRE(buffer_as_str == "sheep_1");
REQUIRE(kapi::filesystem::close(fd));
}
THEN("a filesystem can be mounted, files can be opened, read and closed again and unmounted")
{
REQUIRE(kapi::filesystem::mount("/dev/ram16", "/information"));
auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt").value();
auto buffer = std::vector<std::byte>(8);
auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
REQUIRE(bytes_read);
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));
REQUIRE(kapi::filesystem::umount("/information"));
}
THEN("a filesystem cannot be unmounted if files are still open and can be unmounted after files are closed")
{
REQUIRE(kapi::filesystem::mount("/dev/ram16", "/information"));
auto fd = kapi::filesystem::open("/information/monkey_house/monkey_1.txt").value();
REQUIRE(!kapi::filesystem::umount("/information"));
REQUIRE(kapi::filesystem::close(fd));
REQUIRE(kapi::filesystem::umount("/information"));
}
THEN("device can be opened as file and read from")
{
auto fd = kapi::filesystem::open("/dev/ram0").value();
auto buffer = std::vector<std::byte>(512);
auto bytes_read = kapi::filesystem::read(fd, buffer.data(), buffer.size());
REQUIRE(bytes_read);
REQUIRE(kapi::filesystem::close(fd));
}
THEN("device can be opened as file and written to and read from again")
{
auto read_fd = kapi::filesystem::open("/dev/ram16").value();
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);
auto write_fd = kapi::filesystem::open("/dev/ram16").value();
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);
REQUIRE(std::equal(buffer.begin(), buffer.end(), read_buffer.begin()));
REQUIRE(kapi::filesystem::close(write_fd));
REQUIRE(kapi::filesystem::close(read_fd));
}
THEN("invalid paths cannot be mounted or unmounted")
{
REQUIRE(!kapi::filesystem::mount("/dev/ram16", "invalid_path"));
}
THEN("invalid paths cannot be unmounted")
{
REQUIRE(!kapi::filesystem::umount("invalid_path"));
}
THEN("non existent files cannot be opened")
{
auto fd = kapi::filesystem::open("/information/non_existent.txt");
REQUIRE(!fd);
}
THEN("not opened files cannot closed")
{
REQUIRE(!kapi::filesystem::close(999));
}
THEN("same file cannot be closed twice")
{
auto fd = kapi::filesystem::open("/information/info_1.txt").value();
REQUIRE(kapi::filesystem::close(fd));
REQUIRE(!kapi::filesystem::close(fd));
}
THEN("not opened files cannot be read from")
{
std::vector<std::byte> buffer(10);
auto const invalid_fd = 999uz;
auto bytes_read = kapi::filesystem::read(invalid_fd, buffer.data(), buffer.size());
REQUIRE(!bytes_read);
}
THEN("not opened files cannot be written to")
{
std::vector<std::byte> buffer(10);
auto const invalid_fd = 999uz;
auto bytes_written = kapi::filesystem::write(invalid_fd, buffer.data(), buffer.size());
REQUIRE(!bytes_written);
}
THEN("a new file can be created, written to, read from, and closed again")
{
auto new_file_path = "/information/new_information.txt";
REQUIRE(!kapi::filesystem::open(new_file_path));
REQUIRE(kapi::filesystem::create(new_file_path));
auto fd = kapi::filesystem::open(new_file_path);
REQUIRE(fd);
auto write_buffer = std::vector<std::byte>{std::byte{'T'}, std::byte{'e'}, std::byte{'s'}, std::byte{'t'}};
CHECK(kapi::filesystem::write(*fd, write_buffer.data(), write_buffer.size()));
REQUIRE(kapi::filesystem::close(*fd));
fd = kapi::filesystem::open(new_file_path);
REQUIRE(fd);
auto read_buffer = std::vector<std::byte>(4);
auto bytes_read = kapi::filesystem::read(*fd, read_buffer.data(), read_buffer.size());
std::string_view buffer_as_str{reinterpret_cast<char *>(read_buffer.data()), static_cast<size_t>(*bytes_read)};
REQUIRE(buffer_as_str == "Test");
REQUIRE(kapi::filesystem::close(*fd));
}
THEN("a new directory can be created and a file within can be created, opened, and closed again")
{
auto new_directory_path = "/information/new_directory";
auto new_file_path = "/information/new_directory/new_information.txt";
REQUIRE(!kapi::filesystem::open(new_file_path));
REQUIRE(kapi::filesystem::mkdir(new_directory_path));
REQUIRE(kapi::filesystem::create(new_file_path));
auto fd = kapi::filesystem::open(new_file_path);
REQUIRE(fd);
REQUIRE(kapi::filesystem::close(*fd));
}
THEN("a file can be created in a mounted filesystem, opened, and closed again")
{
CHECK(kapi::filesystem::mount("/dev/ram16", "/information"));
auto new_file_path = "/information/monkey_house/monkey_4.txt";
REQUIRE(!kapi::filesystem::open(new_file_path));
REQUIRE(kapi::filesystem::create(new_file_path));
auto fd = kapi::filesystem::open(new_file_path);
REQUIRE(fd);
REQUIRE(kapi::filesystem::close(*fd));
REQUIRE(kapi::filesystem::umount("/information"));
}
}
}
|