fsfw/unittests/hal/testHostFilesystem.cpp

272 lines
10 KiB
C++
Raw Permalink Normal View History

2022-09-15 16:20:52 +02:00
#include <etl/crc32.h>
2023-01-26 13:40:44 +01:00
#include <array>
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-17 11:39:15 +02:00
#include <fstream>
2022-09-15 16:20:52 +02:00
#include <random>
2022-08-10 17:03:23 +02:00
2022-08-17 16:10:52 +02:00
#include "fsfw/serialize/SerializeIF.h"
2022-08-10 17:03:23 +02:00
#include "fsfw_hal/host/HostFilesystem.h"
2022-08-17 11:39:15 +02:00
using namespace std;
2023-01-26 17:05:25 +01:00
/**
* paths are a bit complex here:
* - std::filesystem is using path::value_type which can be using both char* and wchar*,
* depending on platform
* - fsfw::HasFilesystemIF is using char*
* - To be safe, whenever there is a path object to be passed as C string into the FSFW API,
* it needs to be converted to std::string and then .c_str() can be used to get a C string
* Note: path.string().c_string() does not work as it is returning a pointer to a temporary
*/
2023-01-26 17:05:25 +01:00
2022-08-11 09:59:14 +02:00
TEST_CASE("Host Filesystem", "[hal][host]") {
2022-08-17 11:39:15 +02:00
namespace fs = filesystem;
2022-08-11 09:59:14 +02:00
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-17 16:10:52 +02:00
fs::path fileInDir0 = dir0 / "hello.txt";
fs::path dirWithParent = dir0 / "test_dir";
2022-08-11 10:10:05 +02:00
REQUIRE_NOTHROW(fs::remove(file0));
REQUIRE_NOTHROW(fs::remove(file1));
2022-08-17 16:10:52 +02:00
REQUIRE_NOTHROW(fs::remove_all(dir0));
2022-08-11 09:59:14 +02:00
SECTION("Create file") {
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FilesystemParams params(file0_string.c_str());
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createFile(params) == returnvalue::OK);
2022-08-11 10:19:25 +02:00
CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0));
2022-08-11 09:59:14 +02:00
}
SECTION("Remove File") {
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FilesystemParams params(file0_string.c_str());
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createFile(params) == returnvalue::OK);
2022-08-11 10:19:25 +02:00
CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0));
2023-01-26 15:30:23 +01:00
REQUIRE(hostFs.removeFile(file0.string().c_str()) == returnvalue::OK);
2022-08-11 10:19:25 +02:00
REQUIRE(not fs::exists(file0));
2022-08-11 09:59:14 +02:00
}
2022-08-11 10:10:05 +02:00
SECTION("Create Directory") {
2023-01-26 17:05:25 +01:00
// See note at the top
std::string dir0_string = dir0.string();
FilesystemParams params(dir0_string.c_str());
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createDirectory(params) == returnvalue::OK);
2022-08-11 10:19:25 +02:00
CHECK(fs::is_directory(dir0));
REQUIRE(fs::exists(dir0));
2022-08-11 10:10:05 +02:00
}
SECTION("Remove Directory") {
2023-01-26 17:05:25 +01:00
// See note at the top
std::string dir0_string = dir0.string();
FilesystemParams params(dir0_string.c_str());
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createDirectory(params) == returnvalue::OK);
2022-08-11 10:19:25 +02:00
REQUIRE(fs::exists(dir0));
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.removeDirectory(params) == returnvalue::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") {
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FilesystemParams params(file0_string.c_str());
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createFile(params) == returnvalue::OK);
2022-08-11 10:19:25 +02:00
CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0));
2023-01-26 15:30:23 +01:00
REQUIRE(hostFs.rename(file0.string().c_str(), file1.string().c_str()) == returnvalue::OK);
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!";
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FileOpParams params(file0_string.c_str(), data.size());
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK);
2022-08-11 10:19:25 +02:00
CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0));
2022-08-17 11:39:15 +02:00
CHECK(hostFs.writeToFile(params, reinterpret_cast<const uint8_t*>(data.c_str())) ==
2022-08-22 16:35:53 +02:00
returnvalue::OK);
2022-08-17 11:39:15 +02:00
CHECK(fs::file_size(file0) == data.size());
ifstream ifile(file0);
2022-09-15 16:20:52 +02:00
std::array<char, 524> readBuf{};
ifile.read(readBuf.data(), sizeof(readBuf));
std::string readBackString(readBuf.data());
2022-08-17 11:39:15 +02:00
CHECK(data == readBackString);
}
2022-09-15 16:20:52 +02:00
SECTION("Write To File, Check Not Truncated") {
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> distU8(1, 255);
std::array<uint8_t, 512> randData{};
for (uint8_t& byte : randData) {
byte = distU8(rng);
}
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FileOpParams params(file0_string.c_str(), randData.size() - 256);
2022-09-15 16:20:52 +02:00
REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK);
CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0));
// Write first file chunk
CHECK(hostFs.writeToFile(params, randData.data()) == returnvalue::OK);
2022-09-15 16:20:52 +02:00
params.offset = 256;
CHECK(hostFs.writeToFile(params, randData.data() + 256) == returnvalue::OK);
2022-09-15 16:20:52 +02:00
std::ifstream rf(file0, ios::binary);
std::array<uint8_t, 512> readBack{};
REQUIRE(std::filesystem::file_size(file0) == 512);
rf.read(reinterpret_cast<char*>(readBack.data()), readBack.size());
for (size_t i = 0; i < 512; i++) {
CHECK(randData[i] == readBack[i]);
}
}
2022-08-17 11:39:15 +02:00
SECTION("Read From File") {
std::string data = "hello world!";
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FileOpParams params(file0_string.c_str(), data.size());
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK);
2022-08-17 11:39:15 +02:00
CHECK(fs::is_regular_file(file0));
ofstream of(file0);
of.write(data.c_str(), static_cast<unsigned int>(data.size()));
of.close();
CHECK(fs::file_size(file0) == data.size());
REQUIRE(fs::exists(file0));
std::array<uint8_t, 256> readBuf{};
uint8_t* readPtr = readBuf.data();
size_t readSize = 0;
2022-08-22 16:35:53 +02:00
CHECK(hostFs.readFromFile(params, &readPtr, readSize, readBuf.size()) == returnvalue::OK);
2022-08-17 11:39:15 +02:00
std::string readBackString(reinterpret_cast<const char*>(readBuf.data()));
CHECK(readSize == data.size());
CHECK(data == readBackString);
2022-08-17 16:10:52 +02:00
}
SECTION("Invalid Input does not crash") {
FileOpParams params(nullptr, 10);
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createFile(params.fsParams) != returnvalue::OK);
REQUIRE(hostFs.createDirectory(params.fsParams) != returnvalue::OK);
REQUIRE(hostFs.createFile(params.fsParams) != returnvalue::OK);
REQUIRE(hostFs.removeDirectory(params.fsParams) != returnvalue::OK);
REQUIRE(hostFs.removeFile(nullptr) != returnvalue::OK);
REQUIRE(hostFs.rename(nullptr, nullptr) != returnvalue::OK);
REQUIRE(hostFs.writeToFile(params, nullptr) != returnvalue::OK);
2022-08-17 16:10:52 +02:00
size_t readLen = 0;
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.readFromFile(params, nullptr, readLen, 20) != returnvalue::OK);
2022-08-17 16:10:52 +02:00
}
SECTION("Create File but already exists") {
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FilesystemParams params(file0_string.c_str());
2022-08-22 16:35:53 +02:00
REQUIRE(hostFs.createFile(params) == returnvalue::OK);
2022-08-17 16:10:52 +02:00
REQUIRE(hostFs.createFile(params) == HasFileSystemIF::FILE_ALREADY_EXISTS);
}
SECTION("Remove File but does not exist") {
2023-01-26 15:30:23 +01:00
REQUIRE(hostFs.removeFile(file0.string().c_str()) == HasFileSystemIF::FILE_DOES_NOT_EXIST);
2022-08-17 16:10:52 +02:00
}
SECTION("Create Directory but already exists") {
2023-01-26 17:05:25 +01:00
// See note at the top
std::string dir0_string = dir0.string();
FilesystemParams params(dir0_string.c_str());
REQUIRE(hostFs.createDirectory(params) == returnvalue::OK);
REQUIRE(hostFs.createDirectory(params) == HasFileSystemIF::DIRECTORY_ALREADY_EXISTS);
2022-08-17 16:10:52 +02:00
}
SECTION("Remove Directory but does not exist") {
2023-01-26 17:05:25 +01:00
// See note at the top
std::string dir0_string = dir0.string();
FilesystemParams params(dir0_string.c_str());
2022-08-17 16:10:52 +02:00
REQUIRE(hostFs.removeDirectory(params) == HasFileSystemIF::DIRECTORY_DOES_NOT_EXIST);
}
SECTION("Remove Directory but is file") {
ofstream of(file0);
2023-01-26 17:05:25 +01:00
std::string file0_string = file0.string();
FilesystemParams params(file0_string.c_str());
2022-08-17 16:10:52 +02:00
REQUIRE(hostFs.removeDirectory(params) == HasFileSystemIF::NOT_A_DIRECTORY);
}
SECTION("Read from file but does not exist") {
2023-01-26 17:05:25 +01:00
// See note at the top
2022-08-17 16:10:52 +02:00
std::string data = "hello world!";
2023-01-26 17:05:25 +01:00
std::string file0_string = file0.string();
FileOpParams params(file0_string.c_str(), data.size());
2022-08-17 16:10:52 +02:00
std::array<uint8_t, 10> readBuf{};
uint8_t* readPtr = readBuf.data();
size_t readSize = 0;
CHECK(hostFs.readFromFile(params, &readPtr, readSize, readBuf.size()) ==
HasFileSystemIF::FILE_DOES_NOT_EXIST);
}
SECTION("Write to file but does not exist") {
std::string data = "hello world!";
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FileOpParams params(file0_string.c_str(), data.size());
2022-08-17 16:10:52 +02:00
CHECK(hostFs.writeToFile(params, reinterpret_cast<const uint8_t*>(data.c_str())) ==
HasFileSystemIF::FILE_DOES_NOT_EXIST);
}
SECTION("Remove recursively") {
2023-01-26 15:30:23 +01:00
fs::create_directory(dir0.string().c_str());
2022-08-17 16:10:52 +02:00
ofstream of(fileInDir0);
2023-01-26 18:14:35 +01:00
of.close();
2022-08-17 16:10:52 +02:00
CHECK(fs::is_directory(dir0));
CHECK(fs::is_regular_file(fileInDir0));
2023-01-26 18:14:35 +01:00
// See note at the top
std::string dir0_string = dir0.string();
REQUIRE(hostFs.removeDirectory(FilesystemParams(dir0_string.c_str()), true) == returnvalue::OK);
2022-08-17 16:10:52 +02:00
CHECK(not fs::is_directory(dir0));
CHECK(not fs::is_regular_file(fileInDir0));
}
SECTION("Non-Recursive Removal Fails") {
2023-01-26 15:30:23 +01:00
fs::create_directory(dir0.string().c_str());
2022-08-17 16:10:52 +02:00
ofstream of(fileInDir0);
CHECK(fs::is_directory(dir0));
CHECK(fs::is_regular_file(fileInDir0));
2023-01-26 15:30:23 +01:00
REQUIRE(hostFs.removeDirectory(FilesystemParams(dir0.string().c_str())) ==
2022-08-17 16:10:52 +02:00
HasFileSystemIF::DIRECTORY_NOT_EMPTY);
}
SECTION("Create directory with parent directory") {
2023-01-26 15:30:23 +01:00
CHECK(hostFs.createDirectory(FilesystemParams(dirWithParent.string().c_str()), true) ==
returnvalue::OK);
2022-08-17 16:10:52 +02:00
CHECK(fs::is_directory(dir0));
CHECK(fs::is_directory(dirWithParent));
}
SECTION("Read but provided buffer too small") {
std::string data = "hello world!";
2023-01-26 17:05:25 +01:00
// See note at the top
std::string file0_string = file0.string();
FileOpParams params(file0_string.c_str(), data.size());
2022-08-17 16:10:52 +02:00
ofstream of(file0);
of.write(data.c_str(), static_cast<unsigned int>(data.size()));
of.close();
CHECK(fs::file_size(file0) == data.size());
REQUIRE(fs::exists(file0));
std::array<uint8_t, 15> readBuf{};
uint8_t* readPtr = readBuf.data();
size_t readSize = 0;
CHECK(hostFs.readFromFile(params, &readPtr, readSize, 5) == SerializeIF::BUFFER_TOO_SHORT);
readSize = 10;
CHECK(hostFs.readFromFile(params, &readPtr, readSize, readBuf.size()) ==
SerializeIF::BUFFER_TOO_SHORT);
2022-08-11 10:19:25 +02:00
}
REQUIRE_NOTHROW(fs::remove(file0));
REQUIRE_NOTHROW(fs::remove(file1));
2022-08-17 16:10:52 +02:00
REQUIRE_NOTHROW(fs::remove_all(dir0));
2022-08-11 09:59:14 +02:00
}