From 2fee2fdff591c0ca44480b493d90b4c86f78a622 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 15 Sep 2022 16:20:52 +0200 Subject: [PATCH] unittest which would have caught this --- src/fsfw/filesystem/HasFileSystemIF.h | 4 ++-- unittests/hal/testHostFilesystem.cpp | 34 ++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/fsfw/filesystem/HasFileSystemIF.h b/src/fsfw/filesystem/HasFileSystemIF.h index c86d12b9..6f7112ad 100644 --- a/src/fsfw/filesystem/HasFileSystemIF.h +++ b/src/fsfw/filesystem/HasFileSystemIF.h @@ -86,8 +86,8 @@ class HasFileSystemIF { * @brief Generic function to write to a file. * * @details - * Implementations should not truncate the file. This is equivalent to opening a file with "r+" on Unix systems - * or using ios::out | ios::in with the C++ API. + * Implementations should not truncate the file. This is equivalent to opening a file with "r+" + * 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 data The data to write to the file */ diff --git a/unittests/hal/testHostFilesystem.cpp b/unittests/hal/testHostFilesystem.cpp index 3873d5e9..e33b30cc 100644 --- a/unittests/hal/testHostFilesystem.cpp +++ b/unittests/hal/testHostFilesystem.cpp @@ -1,6 +1,9 @@ +#include + #include #include #include +#include #include "fsfw/serialize/SerializeIF.h" #include "fsfw_hal/host/HostFilesystem.h" @@ -70,12 +73,37 @@ TEST_CASE("Host Filesystem", "[hal][host]") { returnvalue::OK); CHECK(fs::file_size(file0) == data.size()); ifstream ifile(file0); - char readBuf[524]{}; - ifile.read(readBuf, sizeof(readBuf)); - std::string readBackString(readBuf); + std::array readBuf{}; + ifile.read(readBuf.data(), sizeof(readBuf)); + std::string readBackString(readBuf.data()); CHECK(data == readBackString); } + SECTION("Write To File, Check Not Truncated") { + std::random_device dev; + std::mt19937 rng(dev()); + std::uniform_int_distribution distU8(1, 255); + std::array 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 readBack{}; + REQUIRE(std::filesystem::file_size(file0) == 512); + rf.read(reinterpret_cast(readBack.data()), readBack.size()); + for (size_t i = 0; i < 512; i++) { + CHECK(randData[i] == readBack[i]); + } + } + SECTION("Read From File") { std::string data = "hello world!"; FileOpParams params(file0.c_str(), data.size());