SimpleKernel 1.17.0
Loading...
Searching...
No Matches
vfs_test.cpp File Reference
#include "vfs.hpp"
#include <gtest/gtest.h>
#include "file_descriptor.hpp"
#include "filesystem.hpp"
#include "mount.hpp"
#include "test_environment_state.hpp"
Include dependency graph for vfs_test.cpp:

Go to the source code of this file.

Classes

class  MockFileOps
 
class  MockFs
 
class  VfsTest
 
class  BaseEnvTest
 
class  MountTableTest
 
class  FdTableTest
 
class  VfsInitTest
 

Functions

 TEST_F (MountTableTest, MountAndUnmount)
 
 TEST_F (MountTableTest, LookupMountPoint)
 
 TEST_F (FdTableTest, AllocAndFree)
 
 TEST_F (FdTableTest, InvalidFd)
 
 TEST_F (FdTableTest, DupFd)
 
 TEST_F (FdTableTest, SetupStandardFiles)
 
 TEST_F (VfsTest, LookupRoot)
 
 TEST_F (VfsTest, LookupInvalidPaths)
 
 TEST_F (VfsInitTest, DoubleInit)
 
 TEST (FileStructTest, FileOperations)
 
 TEST (InodeStructTest, InodeDefaults)
 
 TEST (DentryStructTest, DentryDefaults)
 
 TEST (OpenFlagsTest, FlagValues)
 
 TEST (SeekWhenceTest, EnumValues)
 

Function Documentation

◆ TEST() [1/5]

TEST ( DentryStructTest  ,
DentryDefaults   
)

Definition at line 301 of file vfs_test.cpp.

301 {
302 Dentry dentry;
303 EXPECT_EQ(dentry.name[0], '\0');
304 EXPECT_EQ(dentry.inode, nullptr);
305 EXPECT_EQ(dentry.parent, nullptr);
306 EXPECT_EQ(dentry.children, nullptr);
307 EXPECT_EQ(dentry.next_sibling, nullptr);
308 EXPECT_EQ(dentry.fs_private, nullptr);
309}
Dentry — 目录项缓存(路径名 ↔ Inode 的映射)
Definition vfs.hpp:41
void * fs_private
文件系统私有数据
Definition vfs.hpp:53
Dentry * children
子目录项链表头
Definition vfs.hpp:49
char name[256]
文件/目录名
Definition vfs.hpp:43
Dentry * parent
父目录项
Definition vfs.hpp:47
Inode * inode
关联的 inode
Definition vfs.hpp:45
Dentry * next_sibling
兄弟目录项(同一父目录下)
Definition vfs.hpp:51
#define EXPECT_EQ(val1, val2, msg)

◆ TEST() [2/5]

TEST ( FileStructTest  ,
FileOperations   
)

Definition at line 278 of file vfs_test.cpp.

278 {
279 File file;
280 EXPECT_EQ(file.offset, 0);
281 EXPECT_EQ(file.flags, 0);
282 EXPECT_EQ(file.ops, nullptr);
283 EXPECT_EQ(file.inode, nullptr);
284 EXPECT_EQ(file.dentry, nullptr);
285}
File — 打开的文件实例(每次 open 产生一个)
Definition vfs.hpp:65
OpenFlags flags
打开标志 (OpenFlags)
Definition vfs.hpp:73
Dentry * dentry
关联的 dentry
Definition vfs.hpp:69
Inode * inode
关联的 inode
Definition vfs.hpp:67
uint64_t offset
当前读写偏移量
Definition vfs.hpp:71
FileOps * ops
文件操作接口
Definition vfs.hpp:76

◆ TEST() [3/5]

TEST ( InodeStructTest  ,
InodeDefaults   
)

Definition at line 288 of file vfs_test.cpp.

288 {
289 Inode inode;
290 EXPECT_EQ(inode.ino, 0);
291 EXPECT_EQ(inode.type, FileType::kUnknown);
292 EXPECT_EQ(inode.size, 0);
293 EXPECT_EQ(inode.permissions, 0644);
294 EXPECT_EQ(inode.link_count, 1);
295 EXPECT_EQ(inode.fs_private, nullptr);
296 EXPECT_EQ(inode.fs, nullptr);
297 EXPECT_EQ(inode.ops, nullptr);
298}
Inode — 文件元数据(独立于路径名)
Definition vfs.hpp:16
FileSystem * fs
所属文件系统
Definition vfs.hpp:30
uint32_t link_count
硬链接计数
Definition vfs.hpp:26
uint32_t permissions
权限位(简化版)
Definition vfs.hpp:24
uint64_t size
文件大小(字节)
Definition vfs.hpp:22
FileType type
文件类型
Definition vfs.hpp:20
InodeOps * ops
文件操作接口
Definition vfs.hpp:33
uint64_t ino
inode 编号(文件系统内唯一)
Definition vfs.hpp:18
void * fs_private
文件系统私有数据指针
Definition vfs.hpp:28

◆ TEST() [4/5]

TEST ( OpenFlagsTest  ,
FlagValues   
)

Definition at line 312 of file vfs_test.cpp.

312 {
313 EXPECT_EQ(static_cast<uint32_t>(OpenFlags::kOReadOnly), 0x0000u);
314 EXPECT_EQ(static_cast<uint32_t>(OpenFlags::kOWriteOnly), 0x0001u);
315 EXPECT_EQ(static_cast<uint32_t>(OpenFlags::kOReadWrite), 0x0002u);
316 EXPECT_EQ(static_cast<uint32_t>(OpenFlags::kOCreate), 0x0040u);
317 EXPECT_EQ(static_cast<uint32_t>(OpenFlags::kOTruncate), 0x0200u);
318 EXPECT_EQ(static_cast<uint32_t>(OpenFlags::kOAppend), 0x0400u);
319 EXPECT_EQ(static_cast<uint32_t>(OpenFlags::kODirectory), 0x010000u);
320}

◆ TEST() [5/5]

TEST ( SeekWhenceTest  ,
EnumValues   
)

Definition at line 323 of file vfs_test.cpp.

323 {
324 EXPECT_EQ(static_cast<int>(SeekWhence::kSet), 0);
325 EXPECT_EQ(static_cast<int>(SeekWhence::kCur), 1);
326 EXPECT_EQ(static_cast<int>(SeekWhence::kEnd), 2);
327}

◆ TEST_F() [1/9]

TEST_F ( FdTableTest  ,
AllocAndFree   
)

Definition at line 168 of file vfs_test.cpp.

168 {
169 // 创建模拟文件
170 File mock_file;
171
172 // 分配 fd
173 auto alloc_result = fd_table_->Alloc(&mock_file);
174 EXPECT_TRUE(alloc_result.has_value());
175 int fd = alloc_result.value();
176 EXPECT_GE(fd, 3); // 0/1/2 预留给标准流
177
178 // 获取文件
179 File* file = fd_table_->Get(fd);
180 EXPECT_EQ(file, &mock_file);
181
182 // 释放 fd
183 auto free_result = fd_table_->Free(fd);
184 EXPECT_TRUE(free_result.has_value());
185
186 // 再次获取应该返回 nullptr
187 file = fd_table_->Get(fd);
188 EXPECT_EQ(file, nullptr);
189}
#define EXPECT_TRUE(cond, msg)
#define EXPECT_GE(val1, val2, msg)

◆ TEST_F() [2/9]

TEST_F ( FdTableTest  ,
DupFd   
)

Definition at line 204 of file vfs_test.cpp.

204 {
205 File mock_file;
206
207 // 分配 fd
208 auto alloc_result = fd_table_->Alloc(&mock_file);
209 EXPECT_TRUE(alloc_result.has_value());
210 int fd1 = alloc_result.value();
211
212 // 复制 fd
213 auto dup_result = fd_table_->Dup(fd1);
214 EXPECT_TRUE(dup_result.has_value());
215 int fd2 = dup_result.value();
216
217 // fd1 和 fd2 应该指向同一个文件
218 EXPECT_EQ(fd_table_->Get(fd1), fd_table_->Get(fd2));
219
220 // 清理
221 (void)fd_table_->Free(fd1);
222 (void)fd_table_->Free(fd2);
223}

◆ TEST_F() [3/9]

TEST_F ( FdTableTest  ,
InvalidFd   
)

Definition at line 191 of file vfs_test.cpp.

191 {
192 // 无效 fd
193 File* file = fd_table_->Get(-1);
194 EXPECT_EQ(file, nullptr);
195
196 file = fd_table_->Get(999);
197 EXPECT_EQ(file, nullptr);
198
199 // 释放无效 fd
200 auto free_result = fd_table_->Free(-1);
201 EXPECT_FALSE(free_result.has_value());
202}
#define EXPECT_FALSE(cond, msg)

◆ TEST_F() [4/9]

TEST_F ( FdTableTest  ,
SetupStandardFiles   
)

Definition at line 225 of file vfs_test.cpp.

225 {
226 File stdin_file;
227 File stdout_file;
228 File stderr_file;
229
230 auto setup_result =
231 fd_table_->SetupStandardFiles(&stdin_file, &stdout_file, &stderr_file);
232 EXPECT_TRUE(setup_result.has_value());
233
234 // 检查标准文件描述符
235 EXPECT_EQ(fd_table_->Get(0), &stdin_file);
236 EXPECT_EQ(fd_table_->Get(1), &stdout_file);
237 EXPECT_EQ(fd_table_->Get(2), &stderr_file);
238}

◆ TEST_F() [5/9]

TEST_F ( MountTableTest  ,
LookupMountPoint   
)

Definition at line 135 of file vfs_test.cpp.

135 {
136 MountTable mount_table;
137 MockFs mock_fs;
138
139 auto mount_result = mount_table.Mount("/", &mock_fs, nullptr);
140 EXPECT_TRUE(mount_result.has_value());
141
142 // 查找根挂载点
143 auto* mp = mount_table.Lookup("/file.txt");
144 EXPECT_NE(mp, nullptr);
145
146 // 查找不存在挂载的路径
147 mp = mount_table.Lookup("/mnt/nonexistent/file");
148 EXPECT_NE(mp, nullptr); // 应该返回根挂载点
149 (void)mount_table.Unmount("/");
150}
挂载表管理器
Definition mount.hpp:34
auto Unmount(const char *path) -> Expected< void >
卸载指定路径的文件系统
Definition mount.cpp:108
auto Mount(const char *path, FileSystem *fs, BlockDevice *device) -> Expected< void >
挂载文件系统到指定路径
Definition mount.cpp:16
auto Lookup(const char *path) -> MountPoint *
根据路径查找对应的挂载点
Definition mount.cpp:158
#define EXPECT_NE(val1, val2, msg)
Here is the call graph for this function:

◆ TEST_F() [6/9]

TEST_F ( MountTableTest  ,
MountAndUnmount   
)

Definition at line 111 of file vfs_test.cpp.

111 {
112 MountTable mount_table;
113 MockFs mock_fs;
114
115 // 测试挂载
116 auto mount_result = mount_table.Mount("/", &mock_fs, nullptr);
117 EXPECT_TRUE(mount_result.has_value());
118 EXPECT_TRUE(mock_fs.mount_called);
119
120 // 测试重复挂载
121 mount_result = mount_table.Mount("/", &mock_fs, nullptr);
122 EXPECT_FALSE(mount_result.has_value());
123
124 // 测试卸载
125 auto unmount_result = mount_table.Unmount("/");
126 (void)unmount_result;
127 EXPECT_TRUE(unmount_result.has_value());
129
130 // 测试卸载未挂载的路径
131 unmount_result = mount_table.Unmount("/mnt");
132 EXPECT_FALSE(unmount_result.has_value());
133}
bool mount_called
Definition vfs_test.cpp:39
bool unmount_called
Definition vfs_test.cpp:40
Here is the call graph for this function:

◆ TEST_F() [7/9]

TEST_F ( VfsInitTest  ,
DoubleInit   
)

Definition at line 267 of file vfs_test.cpp.

267 {
268 // 第一次初始化
269 auto result = vfs::Init();
270 EXPECT_TRUE(result.has_value());
271
272 // 重复初始化应该成功(幂等)
273 result = vfs::Init();
274 EXPECT_TRUE(result.has_value());
275}
auto Init() -> Expected< void >
VFS 全局初始化
Definition vfs.cpp:80
Here is the call graph for this function:

◆ TEST_F() [8/9]

TEST_F ( VfsTest  ,
LookupInvalidPaths   
)

Definition at line 255 of file vfs_test.cpp.

255 {
256 // 空路径
257 auto result = vfs::Lookup(nullptr);
258 EXPECT_FALSE(result.has_value());
259
260 // 相对路径
261 result = vfs::Lookup("relative/path");
262 EXPECT_FALSE(result.has_value());
263}
auto Lookup(const char *path) -> Expected< Dentry * >
路径解析,查找 dentry
Definition lookup.cpp:14
Here is the call graph for this function:

◆ TEST_F() [9/9]

TEST_F ( VfsTest  ,
LookupRoot   
)

Definition at line 241 of file vfs_test.cpp.

241 {
242 // 挂载 mock 文件系统作为根
243 MockFs mock_fs;
244 auto& mount_table = GetMountTable();
245
246 auto mount_result = mount_table.Mount("/", &mock_fs, nullptr);
247 EXPECT_TRUE(mount_result.has_value());
248
249 // 查找根目录
250 auto lookup_result = vfs::Lookup("/");
251 EXPECT_TRUE(lookup_result.has_value());
252 EXPECT_NE(lookup_result.value(), nullptr);
253}
auto GetMountTable() -> MountTable &
获取全局挂载表实例
Definition mount.cpp:234
Here is the call graph for this function: