Atlas-Launcher/external/rvfs/tests/test-restriction.cpp

37 lines
1.3 KiB
C++

#include <iostream>
#include <RestrictedVirtualFileSystem.hpp>
int main() {
// TODO(Faraphel): use a real unit test framework ?
// create a basic rvfs
rvfs::RestrictedVirtualFileSystem fs;
// mount a directory into our system
fs.mountDirectory(QDir("/assets/"), QDir("./_assets/"));
// TEST: check if a file outside of an allowed mounted directory can't be resolved
try {
const QString& path = fs.resolvePath("/content.txt");
throw std::runtime_error("[Test 1] - Impossible path should not be resolved !");
} catch (const std::runtime_error& exception) {}
// TEST: check if a file in a mounted directory can be resolved
try {
const QString& path = fs.resolvePath("/assets/content.txt");
auto file = QFile(path);
file.open(QIODeviceBase::Text | QIODevice::ReadOnly);
std::cout << file.readAll().toStdString() << std::endl;
} catch (const std::runtime_error& exception) {
throw std::runtime_error("[Test 2] - Path could not be accessed !");
}
// TEST: cannot bypass security with the ".." parent directory syntax
try {
const QString& path = fs.resolvePath("/assets/../content.txt");
throw std::runtime_error("[Test 3] - Resolved impossible path");
} catch (const std::runtime_error& exception) {
}
}