unittest which would have caught this

This commit is contained in:
Robin Müller 2022-09-15 16:20:52 +02:00
parent 954c749de0
commit 2fee2fdff5
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
2 changed files with 33 additions and 5 deletions

View File

@ -86,8 +86,8 @@ class HasFileSystemIF {
* @brief Generic function to write to a file. * @brief Generic function to write to a file.
* *
* @details * @details
* Implementations should not truncate the file. This is equivalent to opening a file with "r+" on Unix systems * Implementations should not truncate the file. This is equivalent to opening a file with "r+"
* or using ios::out | ios::in with the C++ API. * on Unix systems or using ios::out | ios::in with the C++ API.
* @param fileOpInfo General information: File name, size to write, offset, additional arguments * @param fileOpInfo General information: File name, size to write, offset, additional arguments
* @param data The data to write to the file * @param data The data to write to the file
*/ */

View File

@ -1,6 +1,9 @@
#include <etl/crc32.h>
#include <catch2/catch_test_macros.hpp> #include <catch2/catch_test_macros.hpp>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <random>
#include "fsfw/serialize/SerializeIF.h" #include "fsfw/serialize/SerializeIF.h"
#include "fsfw_hal/host/HostFilesystem.h" #include "fsfw_hal/host/HostFilesystem.h"
@ -70,12 +73,37 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
returnvalue::OK); returnvalue::OK);
CHECK(fs::file_size(file0) == data.size()); CHECK(fs::file_size(file0) == data.size());
ifstream ifile(file0); ifstream ifile(file0);
char readBuf[524]{}; std::array<char, 524> readBuf{};
ifile.read(readBuf, sizeof(readBuf)); ifile.read(readBuf.data(), sizeof(readBuf));
std::string readBackString(readBuf); std::string readBackString(readBuf.data());
CHECK(data == readBackString); CHECK(data == readBackString);
} }
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);
}
FileOpParams params(file0.c_str(), randData.size() - 256);
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.cbegin()) == returnvalue::OK);
params.offset = 256;
CHECK(hostFs.writeToFile(params, randData.cbegin() + 256) == returnvalue::OK);
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]);
}
}
SECTION("Read From File") { SECTION("Read From File") {
std::string data = "hello world!"; std::string data = "hello world!";
FileOpParams params(file0.c_str(), data.size()); FileOpParams params(file0.c_str(), data.size());