16 {
18
19
21 const size_t blk_count = DeviceManagerSingleton::instance().FindDevicesByType(
23 if (blk_count == 0 || blk_nodes[0]->block_device == nullptr) {
25 "fatfs_system_test: SKIP — no virtio-blk device "
26 "available");
27 return true;
28 }
32 "fatfs_system_test: virtio-blk has zero sectors");
33
34
35
36
38 EXPECT_TRUE(init_result.has_value(),
"fatfs_system_test: vfs init failed");
39
40
41
44
46 auto fat_mount = fat_fs.Mount(blk);
48 "fatfs_system_test: FatFsFileSystem::Mount failed");
49 klog::Info(
"fatfs_system_test: FatFsFileSystem::Mount ok");
50
53 "fatfs_system_test: vfs mount at /mnt/fat failed");
54 klog::Info(
"fatfs_system_test: vfs mount at /mnt/fat ok");
55
56
57 {
58 auto file_result =
62 "fatfs_system_test: open /mnt/fat/test.txt failed");
64
65 const char kMsg[] = "Hello, FatFS!";
66 auto write_result =
vfs::Write(file, kMsg,
sizeof(kMsg) - 1);
68 "fatfs_system_test: write to /mnt/fat/test.txt failed");
69 EXPECT_EQ(write_result.value(),
sizeof(kMsg) - 1,
70 "fatfs_system_test: write byte count mismatch");
71 klog::Info(
"fatfs_system_test: wrote {} bytes to /mnt/fat/test.txt",
72 write_result.value());
73
75 }
76
77
78 {
79 auto file_result =
82 "fatfs_system_test: re-open /mnt/fat/test.txt failed");
84
85 char buf[64] = {};
86 const char kMsg[] = "Hello, FatFS!";
87 auto read_result =
vfs::Read(file, buf,
sizeof(buf) - 1);
89 "fatfs_system_test: read from /mnt/fat/test.txt failed");
90 EXPECT_EQ(read_result.value(),
sizeof(kMsg) - 1,
91 "fatfs_system_test: read byte count mismatch");
93 "fatfs_system_test: read content mismatch");
94 klog::Info(
"fatfs_system_test: verified read: {}", buf);
95
97 }
98
99
100 {
101 auto mkdir_result =
vfs::MkDir(
"/mnt/fat/subdir");
103 "fatfs_system_test: mkdir /mnt/fat/subdir failed");
104 klog::Info(
"fatfs_system_test: mkdir /mnt/fat/subdir ok");
105
106
107 auto inner =
111 "fatfs_system_test: create /mnt/fat/subdir/inner.txt failed");
112 if (inner.has_value()) {
114 }
115
116
120 "fatfs_system_test: open /mnt/fat as dir failed");
121 if (dir_file.has_value()) {
123 auto readdir_result =
vfs::ReadDir(dir_file.value(), entries, 16);
125 "fatfs_system_test: readdir /mnt/fat failed");
126
127 EXPECT_GT(readdir_result.value(),
static_cast<size_t>(1),
128 "fatfs_system_test: readdir /mnt/fat should return > 1 entry");
129 klog::Info(
"fatfs_system_test: readdir /mnt/fat returned {} entries",
130 readdir_result.value());
132 }
133 }
134
135
136 {
137 auto unmount_result = fat_fs.Unmount();
139 "fatfs_system_test: FatFsFileSystem::Unmount failed");
140 klog::Info(
"fatfs_system_test: unmounted ok");
141
142
143 auto remount_result = fat_fs.Mount(blk);
145 "fatfs_system_test: remount failed");
146 klog::Info(
"fatfs_system_test: remounted ok");
147
148
151 "fatfs_system_test: vfs remount failed");
152
153
154 auto file_result =
157 "fatfs_system_test: test.txt not found after remount");
158 if (file_result.has_value()) {
159 char buf[64] = {};
160 const char kMsg[] = "Hello, FatFS!";
161 auto read_result =
vfs::Read(file_result.value(), buf,
sizeof(buf) - 1);
163 "fatfs_system_test: read after remount failed");
165 "fatfs_system_test: data corrupted after remount");
166 klog::Info(
"fatfs_system_test: persistence verified: {}", buf);
168 }
169 }
170
171 klog::Info(
"fatfs_system_test: all tests passed");
172 return true;
173}
virtual auto GetName() const -> const char *=0
获取设备名称(如 "virtio-blk0")
virtual auto GetSectorCount() const -> uint64_t=0
获取设备总扇区数
auto Info(etl::format_string< Args... > fmt, Args &&... args) -> void
以 INFO 级别记录日志
auto Read(File *file, void *buf, size_t count) -> Expected< size_t >
从文件读取数据
auto Write(File *file, const void *buf, size_t count) -> Expected< size_t >
向文件写入数据
auto Open(const char *path, OpenFlags flags) -> Expected< File * >
打开文件
auto ReadDir(File *file, DirEntry *dirent, size_t count) -> Expected< size_t >
读取目录内容
auto MkDir(const char *path) -> Expected< void >
创建目录
auto Close(File *file) -> Expected< void >
关闭文件
auto GetMountTable() -> MountTable &
获取全局挂载表实例
auto Init() -> Expected< void >
VFS 全局初始化
File — 打开的文件实例(每次 open 产生一个)
#define EXPECT_TRUE(cond, msg)
#define EXPECT_GT(val1, val2, msg)
#define EXPECT_EQ(val1, val2, msg)