From 7f979dacd414f896f4fc0cfea09ba3d887646231 Mon Sep 17 00:00:00 2001 From: neru Date: Mon, 11 May 2026 08:24:36 -0300 Subject: [PATCH] feat: add VFS test --- src/test/tests/vfs.cpp | 126 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/test/tests/vfs.cpp diff --git a/src/test/tests/vfs.cpp b/src/test/tests/vfs.cpp new file mode 100644 index 0000000..0b57f0c --- /dev/null +++ b/src/test/tests/vfs.cpp @@ -0,0 +1,126 @@ +#include "tests.h" + +#include + +#include +#include +#include + +using namespace seallib; + +/* + in-memory file provider +*/ +class MemoryFileProvider : public IFileProvider +{ + std::map 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 listDirectory(const std::string& path) const override + { + std::vector 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(); + auto prov2 = std::make_unique(); + + /* + 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(); + 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;