From 23f514039aa4587d4dd49065ad00308297dc707e Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 17 Aug 2022 11:39:15 +0200 Subject: [PATCH] host filesystem continued --- src/fsfw/filesystem/HasFileSystemIF.h | 8 ++--- src/fsfw/tcdistribution/PusDistributor.cpp | 3 +- src/fsfw_hal/host/HostFilesystem.cpp | 12 ++++---- unittests/hal/testHostFilesystem.cpp | 36 ++++++++++++++++++++-- unittests/util/CMakeLists.txt | 4 +-- 5 files changed, 44 insertions(+), 19 deletions(-) diff --git a/src/fsfw/filesystem/HasFileSystemIF.h b/src/fsfw/filesystem/HasFileSystemIF.h index 2f262285..078de7c0 100644 --- a/src/fsfw/filesystem/HasFileSystemIF.h +++ b/src/fsfw/filesystem/HasFileSystemIF.h @@ -19,13 +19,9 @@ struct FilesystemParams { struct FileOpParams { FileOpParams(const char* path, size_t size) : fsParams(path), size(size) {} - [[nodiscard]] const char* path() const { - return fsParams.path; - } + [[nodiscard]] const char* path() const { return fsParams.path; } - [[nodiscard]] FileSystemArgsIF* args() const { - return fsParams.args; - } + [[nodiscard]] FileSystemArgsIF* args() const { return fsParams.args; } FilesystemParams fsParams; size_t size; diff --git a/src/fsfw/tcdistribution/PusDistributor.cpp b/src/fsfw/tcdistribution/PusDistributor.cpp index d827d5b6..982a99bf 100644 --- a/src/fsfw/tcdistribution/PusDistributor.cpp +++ b/src/fsfw/tcdistribution/PusDistributor.cpp @@ -134,7 +134,8 @@ ReturnValue_t PusDistributor::initialize() { return ObjectManagerIF::CHILD_INIT_FAILED; } if (verifyChannel == nullptr) { - verifyChannel = ObjectManager::instance()->get(objects::VERIFICATION_REPORTER); + verifyChannel = + ObjectManager::instance()->get(objects::VERIFICATION_REPORTER); if (verifyChannel == nullptr) { return ObjectManagerIF::CHILD_INIT_FAILED; } diff --git a/src/fsfw_hal/host/HostFilesystem.cpp b/src/fsfw_hal/host/HostFilesystem.cpp index 72cff2ed..ebe60444 100644 --- a/src/fsfw_hal/host/HostFilesystem.cpp +++ b/src/fsfw_hal/host/HostFilesystem.cpp @@ -40,14 +40,14 @@ ReturnValue_t HostFilesystem::readFromFile(FileOpParams params, uint8_t **buffer if (file.fail()) { return HasFileSystemIF::GENERIC_FILE_ERROR; } - auto readLen = static_cast(params.offset); - file.seekg(readLen); - if (readSize + params.size > maxSize) { + auto sizeToRead = static_cast(params.size); + file.seekg(static_cast(params.offset)); + if (readSize + sizeToRead > maxSize) { return SerializeIF::BUFFER_TOO_SHORT; } - file.read(reinterpret_cast(*buffer), readLen); - readSize += readLen; - *buffer += readLen; + file.read(reinterpret_cast(*buffer), sizeToRead); + readSize += sizeToRead; + *buffer += sizeToRead; return HasReturnvaluesIF::RETURN_OK; } diff --git a/unittests/hal/testHostFilesystem.cpp b/unittests/hal/testHostFilesystem.cpp index ba3b1338..bf2d49d5 100644 --- a/unittests/hal/testHostFilesystem.cpp +++ b/unittests/hal/testHostFilesystem.cpp @@ -1,10 +1,13 @@ #include #include +#include #include "fsfw_hal/host/HostFilesystem.h" +using namespace std; + TEST_CASE("Host Filesystem", "[hal][host]") { - namespace fs = std::filesystem; + namespace fs = filesystem; auto hostFs = HostFilesystem(); auto tmpDir = fs::temp_directory_path(); fs::path file0 = tmpDir / "hello.txt"; @@ -62,8 +65,35 @@ TEST_CASE("Host Filesystem", "[hal][host]") { REQUIRE(hostFs.createFile(params.fsParams) == result::OK); CHECK(fs::is_regular_file(file0)); REQUIRE(fs::exists(file0)); - hostFs.writeToFile(params, reinterpret_cast(data.c_str())); - // TODO: Read back file and verify content + CHECK(hostFs.writeToFile(params, reinterpret_cast(data.c_str())) == + HasReturnvaluesIF::RETURN_OK); + CHECK(fs::file_size(file0) == data.size()); + ifstream ifile(file0); + char readBuf[524]{}; + ifile.read(readBuf, sizeof(readBuf)); + std::string readBackString(readBuf); + CHECK(data == readBackString); + REQUIRE_NOTHROW(fs::remove(file1)); + } + + SECTION("Read From 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)); + ofstream of(file0); + of.write(data.c_str(), static_cast(data.size())); + of.close(); + CHECK(fs::file_size(file0) == data.size()); + REQUIRE(fs::exists(file0)); + std::array readBuf{}; + uint8_t* readPtr = readBuf.data(); + size_t readSize = 0; + CHECK(hostFs.readFromFile(params, &readPtr, readSize, readBuf.size()) == + HasReturnvaluesIF::RETURN_OK); + std::string readBackString(reinterpret_cast(readBuf.data())); + CHECK(readSize == data.size()); + CHECK(data == readBackString); REQUIRE_NOTHROW(fs::remove(file1)); } diff --git a/unittests/util/CMakeLists.txt b/unittests/util/CMakeLists.txt index d4caa4d5..fb660d54 100644 --- a/unittests/util/CMakeLists.txt +++ b/unittests/util/CMakeLists.txt @@ -1,3 +1 @@ -target_sources(${FSFW_TEST_TGT} PRIVATE - testUnsignedByteField.cpp -) +target_sources(${FSFW_TEST_TGT} PRIVATE testUnsignedByteField.cpp)