| 10 | #include "vfs.hpp" |
| 11 | |
| 12 | auto ramfs_test() -> bool { |
| 13 | klog::Info("ramfs_test: start"); |
| 14 | |
| 15 | // FileSystemInit() has already been called in main.cpp. |
| 16 | // ramfs is mounted at "/" — use VFS directly. |
| 17 | |
| 18 | // T1: Create file, write, read back |
| 19 | { |
| 20 | auto file_result = vfs::Open( |
| 21 | "/hello.txt", vfs::OpenFlags::kOCreate | vfs::OpenFlags::kOReadWrite); |
| 22 | EXPECT_TRUE(file_result.has_value(), "ramfs_test: open /hello.txt failed"); |
| 23 | vfs::File* file = file_result.value(); |
| 24 | |
| 25 | const char kMsg[] = "Hello, ramfs!"; |
| 26 | auto write_result = vfs::Write(file, kMsg, sizeof(kMsg) - 1); |
| 27 | EXPECT_TRUE(write_result.has_value(), "ramfs_test: write failed"); |
| 28 | EXPECT_EQ(write_result.value(), sizeof(kMsg) - 1, |
| 29 | "ramfs_test: write byte count mismatch"); |
| 30 | klog::Info("ramfs_test: wrote {} bytes", write_result.value()); |
| 31 | |
| 32 | // Seek back to start |
| 33 | auto seek_result = vfs::Seek(file, 0, vfs::SeekWhence::kSet); |
| 34 | EXPECT_TRUE(seek_result.has_value(), "ramfs_test: seek to start failed"); |
| 35 | EXPECT_EQ(seek_result.value(), static_cast<uint64_t>(0), |
| 36 | "ramfs_test: seek position mismatch"); |
| 37 | |
| 38 | // Read back |
| 39 | char buf[64] = {}; |
| 40 | auto read_result = vfs::Read(file, buf, sizeof(buf) - 1); |
| 41 | EXPECT_TRUE(read_result.has_value(), "ramfs_test: read failed"); |
| 42 | EXPECT_EQ(read_result.value(), sizeof(kMsg) - 1, |
| 43 | "ramfs_test: read byte count mismatch"); |
| 44 | EXPECT_EQ(memcmp(buf, kMsg, sizeof(kMsg) - 1), 0, |
| 45 | "ramfs_test: read content mismatch"); |
| 46 | klog::Info("ramfs_test: read back: {}", buf); |
| 47 | |
| 48 | (void)vfs::Close(file); |
| 49 | } |
| 50 | |
| 51 | // T3: Seek to middle, partial read |
| 52 | { |
| 53 | auto file_result = vfs::Open("/hello.txt", vfs::OpenFlags::kOReadOnly); |
| 54 | EXPECT_TRUE(file_result.has_value(), |
| 55 | "ramfs_test: re-open for seek test failed"); |
| 56 | vfs::File* file = file_result.value(); |
| 57 | |
| 58 | // Seek to offset 7 |
| 59 | auto seek_result = vfs::Seek(file, 7, vfs::SeekWhence::kSet); |
| 60 | EXPECT_TRUE(seek_result.has_value(), "ramfs_test: seek to offset 7 failed"); |
| 61 | EXPECT_EQ(seek_result.value(), static_cast<uint64_t>(7), |
| 62 | "ramfs_test: seek offset 7 mismatch"); |
| 63 | |
| 64 | char buf[32] = {}; |
| 65 | auto read_result = vfs::Read(file, buf, 5); |
| 66 | EXPECT_TRUE(read_result.has_value(), "ramfs_test: partial read failed"); |
| 67 | // "Hello, ramfs!" -> offset 7 = "ramfs" |
| 68 | EXPECT_EQ(read_result.value(), static_cast<size_t>(5), |
| 69 | "ramfs_test: partial read count mismatch"); |