feat: add VFS test

This commit is contained in:
2026-05-11 08:24:36 -03:00
parent b51f253705
commit 7f979dacd4
+126
View File
@@ -0,0 +1,126 @@
#include "tests.h"
#include <seallib/vfs.h>
#include <string>
#include <iostream>
#include <map>
using namespace seallib;
/*
in-memory file provider
*/
class MemoryFileProvider : public IFileProvider
{
std::map<std::string, std::string> files;
public:
bool exists(const std::string& path) const override { return files.count(path); }
VFSResult readFile(const std::string& path, FileBuffer& outBuffer) const override
{
if (!exists(path)) return VFSResult::FileNotFound;
outBuffer = FileBuffer::fromString(files.at(path));
return VFSResult::Success;
}
VFSResult writeFile(const std::string& path, const FileBuffer& buffer) override
{
files[path] = buffer.toString();
return VFSResult::Success;
}
VFSResult deleteFile(const std::string& path) override
{
return files.erase(path) ? VFSResult::Success : VFSResult::FileNotFound;
}
VFSResult createDirectory(const std::string& path) override { return VFSResult::Success; }
std::vector<std::string> listDirectory(const std::string& path) const override
{
std::vector<std::string> result;
for (auto const& [key, val] : files)
result.push_back(key);
return result;
}
std::string getProviderName() const override { return "MemoryProvider"; }
};
class VFSTest : ITest
{
public:
VFSTest() : ITest() {};
virtual void run() override
{
VirtualFileSystem vfs;
auto prov1 = std::make_unique<MemoryFileProvider>();
auto prov2 = std::make_unique<MemoryFileProvider>();
/*
mnt test
*/
vfs.mount("scripts", std::move(prov1));
vfs.mount("config/network", std::move(prov2));
/*
write / read tests
*/
logInfo("Writing to different mount points");
vfs.writeFile("scripts/main.lua", FileBuffer::fromString("print('hello')"));
vfs.writeFile("config/network/settings.json", FileBuffer::fromString("{ \"ip\": \"127.0.0.1\" }"));
logInfo("Reading from mount points");
FileBuffer b1, b2;
if (vfs.readFile("scripts/main.lua", b1) == VFSResult::Success) logInfo("Read Scripts: {}", b1.toString());
/*
existence ands crosstalk
*/
if (!vfs.exists("scripts/main.lua")) logError("scripts/main.lua is missing?");
if (vfs.exists("scripts/settings.json")) logError("wrong provider, settings.json should be inaccessible");
logInfo("Path isolation verified");
/*
priority / overlapping mounts
*/
auto overrideProv = std::make_unique<MemoryFileProvider>();
overrideProv->writeFile("patch.txt", FileBuffer::fromString("Hotfix Data"));
vfs.mount("scripts", std::move(overrideProv), 10); // higher priority
FileBuffer b3;
if (vfs.readFile("scripts/patch.txt", b3) == VFSResult::Success)
logInfo("Priority mount successful: {}", b3.toString());
/*
directory listing
*/
logInfo("Listing 'scripts' folder");
auto list = vfs.listDirectory("scripts");
for (const auto& file : list)
logInfo("\t* {}", file);
/*
deletion
*/
VFSResult delRes = vfs.deleteFile("scripts/main.lua");
logInfo("Deletion result: {}", vfsResultToString(delRes));
if (vfs.exists("scripts/main.lua")) logError("Failed to delete file, scrips/main.lua still accessible");
/*
unmounting
*/
vfs.unmount("scripts");
if (!vfs.exists("scripts/patch.txt")) logInfo("Unmount successful");
}
virtual const char* getName() override { return "VFSTest"; }
};
static VFSTest g_vfsTest;