2022-08-10 17:03:23 +02:00
|
|
|
#include <catch2/catch_test_macros.hpp>
|
2022-08-11 10:10:05 +02:00
|
|
|
#include <filesystem>
|
2022-08-10 17:03:23 +02:00
|
|
|
|
|
|
|
#include "fsfw_hal/host/HostFilesystem.h"
|
|
|
|
|
2022-08-11 09:59:14 +02:00
|
|
|
TEST_CASE("Host Filesystem", "[hal][host]") {
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
auto hostFs = HostFilesystem();
|
|
|
|
auto tmpDir = fs::temp_directory_path();
|
2022-08-11 10:10:05 +02:00
|
|
|
fs::path file0 = tmpDir / "hello.txt";
|
2022-08-11 10:19:25 +02:00
|
|
|
fs::path file1 = tmpDir / "hello2.txt";
|
|
|
|
fs::path dir0 = tmpDir / "test_dir";
|
2022-08-11 10:10:05 +02:00
|
|
|
REQUIRE_NOTHROW(fs::remove(file0));
|
|
|
|
REQUIRE_NOTHROW(fs::remove(file1));
|
2022-08-11 10:19:25 +02:00
|
|
|
REQUIRE_NOTHROW(fs::remove(dir0));
|
2022-08-11 09:59:14 +02:00
|
|
|
|
|
|
|
SECTION("Create file") {
|
2022-08-11 10:19:25 +02:00
|
|
|
FilesystemParams params(file0.c_str());
|
2022-08-11 09:59:14 +02:00
|
|
|
REQUIRE(hostFs.createFile(params) == result::OK);
|
2022-08-11 10:19:25 +02:00
|
|
|
CHECK(fs::is_regular_file(file0));
|
|
|
|
REQUIRE(fs::exists(file0));
|
|
|
|
REQUIRE_NOTHROW(fs::remove(file0));
|
2022-08-11 09:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Remove File") {
|
2022-08-11 10:19:25 +02:00
|
|
|
FilesystemParams params(file0.c_str());
|
2022-08-11 09:59:14 +02:00
|
|
|
REQUIRE(hostFs.createFile(params) == result::OK);
|
2022-08-11 10:19:25 +02:00
|
|
|
CHECK(fs::is_regular_file(file0));
|
|
|
|
REQUIRE(fs::exists(file0));
|
|
|
|
REQUIRE(hostFs.removeFile(file0.c_str()) == result::OK);
|
|
|
|
REQUIRE(not fs::exists(file0));
|
2022-08-11 09:59:14 +02:00
|
|
|
}
|
|
|
|
|
2022-08-11 10:10:05 +02:00
|
|
|
SECTION("Create Directory") {
|
2022-08-11 10:19:25 +02:00
|
|
|
FilesystemParams params(dir0.c_str());
|
2022-08-11 10:10:05 +02:00
|
|
|
REQUIRE(hostFs.createDirectory(params) == result::OK);
|
2022-08-11 10:19:25 +02:00
|
|
|
CHECK(fs::is_directory(dir0));
|
|
|
|
REQUIRE(fs::exists(dir0));
|
|
|
|
REQUIRE_NOTHROW(fs::remove(dir0));
|
2022-08-11 10:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Remove Directory") {
|
2022-08-11 10:19:25 +02:00
|
|
|
FilesystemParams params(dir0.c_str());
|
2022-08-11 10:10:05 +02:00
|
|
|
REQUIRE(hostFs.createDirectory(params) == result::OK);
|
2022-08-11 10:19:25 +02:00
|
|
|
REQUIRE(fs::exists(dir0));
|
2022-08-11 10:10:05 +02:00
|
|
|
REQUIRE(hostFs.removeDirectory(params) == result::OK);
|
2022-08-11 10:19:25 +02:00
|
|
|
REQUIRE(not fs::exists(dir0));
|
2022-08-11 10:10:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Rename File") {
|
2022-08-11 10:19:25 +02:00
|
|
|
FilesystemParams params(file0.c_str());
|
2022-08-11 10:10:05 +02:00
|
|
|
REQUIRE(hostFs.createFile(params) == result::OK);
|
2022-08-11 10:19:25 +02:00
|
|
|
CHECK(fs::is_regular_file(file0));
|
|
|
|
REQUIRE(fs::exists(file0));
|
|
|
|
REQUIRE(hostFs.rename(file0.c_str(), file1.c_str()) == result::OK);
|
|
|
|
REQUIRE_NOTHROW(fs::remove(file1));
|
2022-08-11 10:10:05 +02:00
|
|
|
}
|
2022-08-11 10:19:25 +02:00
|
|
|
|
|
|
|
SECTION("Write To File") {
|
|
|
|
std::string data = "hello world!";
|
|
|
|
FileOpParams params(file0.c_str(), data.size());
|
|
|
|
REQUIRE(hostFs.createFile(params.fsParams) == result::OK);
|
|
|
|
CHECK(fs::is_regular_file(file0));
|
|
|
|
REQUIRE(fs::exists(file0));
|
|
|
|
hostFs.writeToFile(params, reinterpret_cast<const uint8_t*>(data.c_str()));
|
|
|
|
// TODO: Read back file and verify content
|
|
|
|
REQUIRE_NOTHROW(fs::remove(file1));
|
|
|
|
}
|
|
|
|
|
|
|
|
REQUIRE_NOTHROW(fs::remove(file0));
|
|
|
|
REQUIRE_NOTHROW(fs::remove(file1));
|
|
|
|
REQUIRE_NOTHROW(fs::remove(dir0));
|
2022-08-11 09:59:14 +02:00
|
|
|
}
|