From c12492df03da5cb529190336bd8269bd75bc6ba0 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 11 Aug 2022 10:19:25 +0200 Subject: [PATCH] continued host FS test --- src/fsfw/filesystem/HasFileSystemIF.h | 13 +++++- src/fsfw_hal/host/HostFilesystem.cpp | 8 ++-- unittests/hal/testHostFilesystem.cpp | 67 ++++++++++++++++----------- unittests/mocks/FilesystemMock.cpp | 4 +- 4 files changed, 56 insertions(+), 36 deletions(-) diff --git a/src/fsfw/filesystem/HasFileSystemIF.h b/src/fsfw/filesystem/HasFileSystemIF.h index 9771dda01..2f2622852 100644 --- a/src/fsfw/filesystem/HasFileSystemIF.h +++ b/src/fsfw/filesystem/HasFileSystemIF.h @@ -16,9 +16,18 @@ struct FilesystemParams { FileSystemArgsIF* args = nullptr; }; -struct FileOpParams : public FilesystemParams { - FileOpParams(const char* path, size_t size) : FilesystemParams(path), size(size) {} +struct FileOpParams { + FileOpParams(const char* path, size_t size) : fsParams(path), size(size) {} + [[nodiscard]] const char* path() const { + return fsParams.path; + } + + [[nodiscard]] FileSystemArgsIF* args() const { + return fsParams.args; + } + + FilesystemParams fsParams; size_t size; size_t offset = 0; }; diff --git a/src/fsfw_hal/host/HostFilesystem.cpp b/src/fsfw_hal/host/HostFilesystem.cpp index 8a861d479..72cff2ede 100644 --- a/src/fsfw_hal/host/HostFilesystem.cpp +++ b/src/fsfw_hal/host/HostFilesystem.cpp @@ -11,10 +11,10 @@ using namespace std; HostFilesystem::HostFilesystem() = default; ReturnValue_t HostFilesystem::writeToFile(FileOpParams params, const uint8_t *data) { - if (params.path == nullptr) { + if (params.path() == nullptr) { return HasReturnvaluesIF::RETURN_FAILED; } - path path(params.path); + path path(params.path()); if (not exists(path)) { return HasFileSystemIF::FILE_DOES_NOT_EXIST; } @@ -29,10 +29,10 @@ ReturnValue_t HostFilesystem::writeToFile(FileOpParams params, const uint8_t *da ReturnValue_t HostFilesystem::readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize, size_t maxSize) { - if (params.path == nullptr) { + if (params.path() == nullptr) { return HasReturnvaluesIF::RETURN_FAILED; } - path path(params.path); + path path(params.path()); if (not exists(path)) { return HasFileSystemIF::FILE_DOES_NOT_EXIST; } diff --git a/unittests/hal/testHostFilesystem.cpp b/unittests/hal/testHostFilesystem.cpp index d3fd880a2..ba3b1338f 100644 --- a/unittests/hal/testHostFilesystem.cpp +++ b/unittests/hal/testHostFilesystem.cpp @@ -8,55 +8,66 @@ TEST_CASE("Host Filesystem", "[hal][host]") { auto hostFs = HostFilesystem(); auto tmpDir = fs::temp_directory_path(); fs::path file0 = tmpDir / "hello.txt"; - fs::path file1 = tmpDir / "hello.txt"; + fs::path file1 = tmpDir / "hello2.txt"; + fs::path dir0 = tmpDir / "test_dir"; REQUIRE_NOTHROW(fs::remove(file0)); REQUIRE_NOTHROW(fs::remove(file1)); + REQUIRE_NOTHROW(fs::remove(dir0)); SECTION("Create file") { - fs::path file = tmpDir / "hello.txt"; - FilesystemParams params(file.c_str()); + FilesystemParams params(file0.c_str()); REQUIRE(hostFs.createFile(params) == result::OK); - CHECK(fs::is_regular_file(file)); - REQUIRE(fs::exists(file)); - REQUIRE_NOTHROW(fs::remove(file)); + CHECK(fs::is_regular_file(file0)); + REQUIRE(fs::exists(file0)); + REQUIRE_NOTHROW(fs::remove(file0)); } SECTION("Remove File") { - fs::path file = tmpDir / "hello.txt"; - FilesystemParams params(file.c_str()); + FilesystemParams params(file0.c_str()); REQUIRE(hostFs.createFile(params) == result::OK); - CHECK(fs::is_regular_file(file)); - REQUIRE(fs::exists(file)); - REQUIRE(hostFs.removeFile(file.c_str()) == result::OK); - REQUIRE(not fs::exists(file)); + CHECK(fs::is_regular_file(file0)); + REQUIRE(fs::exists(file0)); + REQUIRE(hostFs.removeFile(file0.c_str()) == result::OK); + REQUIRE(not fs::exists(file0)); } SECTION("Create Directory") { - fs::path dirPath = tmpDir / "test_dir"; - FilesystemParams params(dirPath.c_str()); + FilesystemParams params(dir0.c_str()); REQUIRE(hostFs.createDirectory(params) == result::OK); - CHECK(fs::is_directory(dirPath)); - REQUIRE(fs::exists(dirPath)); - REQUIRE_NOTHROW(fs::remove(dirPath)); + CHECK(fs::is_directory(dir0)); + REQUIRE(fs::exists(dir0)); + REQUIRE_NOTHROW(fs::remove(dir0)); } SECTION("Remove Directory") { - fs::path dirPath = tmpDir / "test_dir"; - FilesystemParams params(dirPath.c_str()); + FilesystemParams params(dir0.c_str()); REQUIRE(hostFs.createDirectory(params) == result::OK); - REQUIRE(fs::exists(dirPath)); + REQUIRE(fs::exists(dir0)); REQUIRE(hostFs.removeDirectory(params) == result::OK); - REQUIRE(not fs::exists(dirPath)); + REQUIRE(not fs::exists(dir0)); } SECTION("Rename File") { - fs::path file = tmpDir / "hello.txt"; - fs::path newFile = tmpDir / "hello2.txt"; - FilesystemParams params(file.c_str()); + FilesystemParams params(file0.c_str()); REQUIRE(hostFs.createFile(params) == result::OK); - CHECK(fs::is_regular_file(file)); - REQUIRE(fs::exists(file)); - REQUIRE(hostFs.rename(file.c_str(), newFile.c_str()) == result::OK); - REQUIRE_NOTHROW(fs::remove(newFile)); + 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)); } + + 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(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)); } \ No newline at end of file diff --git a/unittests/mocks/FilesystemMock.cpp b/unittests/mocks/FilesystemMock.cpp index 5b195ffc2..02620fd54 100644 --- a/unittests/mocks/FilesystemMock.cpp +++ b/unittests/mocks/FilesystemMock.cpp @@ -9,7 +9,7 @@ ReturnValue_t FilesystemMock::writeToFile(FileOpParams params, const uint8_t *da ReturnValue_t FilesystemMock::readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize, size_t maxSize) { - std::string filename(params.path); + std::string filename(params.path()); auto iter = fileMap.find(filename); if (iter == fileMap.end()) { return HasFileSystemIF::FILE_DOES_NOT_EXIST; @@ -71,7 +71,7 @@ ReturnValue_t FilesystemMock::rename(const char *oldPath, const char *newPath, } void FilesystemMock::createOrAddToFile(FileOpParams params, const uint8_t *data) { - std::string filename(params.path); + std::string filename(params.path()); auto iter = fileMap.find(filename); if (iter == fileMap.end()) { FileSegmentQueue queue;