From 12c452e7cef8caeb725d473cd56e11d812471174 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Wed, 17 Aug 2022 16:10:52 +0200 Subject: [PATCH] finished host FS unittests --- src/fsfw_hal/host/HostFilesystem.cpp | 4 +- unittests/hal/testHostFilesystem.cpp | 112 +++++++++++++++++++++++++-- 2 files changed, 106 insertions(+), 10 deletions(-) diff --git a/src/fsfw_hal/host/HostFilesystem.cpp b/src/fsfw_hal/host/HostFilesystem.cpp index ebe60444..7ebe5df8 100644 --- a/src/fsfw_hal/host/HostFilesystem.cpp +++ b/src/fsfw_hal/host/HostFilesystem.cpp @@ -90,9 +90,7 @@ ReturnValue_t HostFilesystem::createDirectory(FilesystemParams params, bool crea if (exists(dirPath)) { return HasFileSystemIF::DIRECTORY_ALREADY_EXISTS; } - if (is_regular_file(dirPath)) { - return HasFileSystemIF::NOT_A_DIRECTORY; - } + if (createParentDirs) { if (create_directories(dirPath, errorCode)) { return HasReturnvaluesIF::RETURN_OK; diff --git a/unittests/hal/testHostFilesystem.cpp b/unittests/hal/testHostFilesystem.cpp index bf2d49d5..2da185da 100644 --- a/unittests/hal/testHostFilesystem.cpp +++ b/unittests/hal/testHostFilesystem.cpp @@ -2,6 +2,7 @@ #include #include +#include "fsfw/serialize/SerializeIF.h" #include "fsfw_hal/host/HostFilesystem.h" using namespace std; @@ -13,16 +14,18 @@ TEST_CASE("Host Filesystem", "[hal][host]") { fs::path file0 = tmpDir / "hello.txt"; fs::path file1 = tmpDir / "hello2.txt"; fs::path dir0 = tmpDir / "test_dir"; + fs::path fileInDir0 = dir0 / "hello.txt"; + fs::path dirWithParent = dir0 / "test_dir"; + REQUIRE_NOTHROW(fs::remove(file0)); REQUIRE_NOTHROW(fs::remove(file1)); - REQUIRE_NOTHROW(fs::remove(dir0)); + REQUIRE_NOTHROW(fs::remove_all(dir0)); SECTION("Create file") { FilesystemParams params(file0.c_str()); REQUIRE(hostFs.createFile(params) == result::OK); CHECK(fs::is_regular_file(file0)); REQUIRE(fs::exists(file0)); - REQUIRE_NOTHROW(fs::remove(file0)); } SECTION("Remove File") { @@ -39,7 +42,6 @@ TEST_CASE("Host Filesystem", "[hal][host]") { REQUIRE(hostFs.createDirectory(params) == result::OK); CHECK(fs::is_directory(dir0)); REQUIRE(fs::exists(dir0)); - REQUIRE_NOTHROW(fs::remove(dir0)); } SECTION("Remove Directory") { @@ -56,7 +58,6 @@ TEST_CASE("Host Filesystem", "[hal][host]") { 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") { @@ -73,7 +74,6 @@ TEST_CASE("Host Filesystem", "[hal][host]") { ifile.read(readBuf, sizeof(readBuf)); std::string readBackString(readBuf); CHECK(data == readBackString); - REQUIRE_NOTHROW(fs::remove(file1)); } SECTION("Read From File") { @@ -94,10 +94,108 @@ TEST_CASE("Host Filesystem", "[hal][host]") { std::string readBackString(reinterpret_cast(readBuf.data())); CHECK(readSize == data.size()); CHECK(data == readBackString); - REQUIRE_NOTHROW(fs::remove(file1)); + } + + SECTION("Invalid Input does not crash") { + FileOpParams params(nullptr, 10); + REQUIRE(hostFs.createFile(params.fsParams) != result::OK); + REQUIRE(hostFs.createDirectory(params.fsParams) != result::OK); + REQUIRE(hostFs.createFile(params.fsParams) != result::OK); + REQUIRE(hostFs.removeDirectory(params.fsParams) != result::OK); + REQUIRE(hostFs.removeFile(nullptr) != result::OK); + REQUIRE(hostFs.rename(nullptr, nullptr) != result::OK); + REQUIRE(hostFs.writeToFile(params, nullptr) != result::OK); + size_t readLen = 0; + REQUIRE(hostFs.readFromFile(params, nullptr, readLen, 20) != result::OK); + } + + SECTION("Create File but already exists") { + FilesystemParams params(file0.c_str()); + REQUIRE(hostFs.createFile(params) == result::OK); + REQUIRE(hostFs.createFile(params) == HasFileSystemIF::FILE_ALREADY_EXISTS); + } + + SECTION("Remove File but does not exist") { + REQUIRE(hostFs.removeFile(file0.c_str()) == HasFileSystemIF::FILE_DOES_NOT_EXIST); + } + + SECTION("Create Directory but already exists") { + FileOpParams params(file0.c_str(), 12); + REQUIRE(hostFs.createDirectory(params.fsParams) == HasReturnvaluesIF::RETURN_OK); + REQUIRE(hostFs.createDirectory(params.fsParams) == HasFileSystemIF::DIRECTORY_ALREADY_EXISTS); + } + + SECTION("Remove Directory but does not exist") { + FilesystemParams params(dir0.c_str()); + REQUIRE(hostFs.removeDirectory(params) == HasFileSystemIF::DIRECTORY_DOES_NOT_EXIST); + } + + SECTION("Remove Directory but is file") { + ofstream of(file0); + FilesystemParams params(file0.c_str()); + REQUIRE(hostFs.removeDirectory(params) == HasFileSystemIF::NOT_A_DIRECTORY); + } + + SECTION("Read from file but does not exist") { + std::string data = "hello world!"; + FileOpParams params(file0.c_str(), data.size()); + std::array 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!"; + FileOpParams params(file0.c_str(), data.size()); + CHECK(hostFs.writeToFile(params, reinterpret_cast(data.c_str())) == + HasFileSystemIF::FILE_DOES_NOT_EXIST); + } + + SECTION("Remove recursively") { + fs::create_directory(dir0.c_str()); + ofstream of(fileInDir0); + CHECK(fs::is_directory(dir0)); + CHECK(fs::is_regular_file(fileInDir0)); + REQUIRE(hostFs.removeDirectory(FilesystemParams(dir0.c_str()), true) == result::OK); + CHECK(not fs::is_directory(dir0)); + CHECK(not fs::is_regular_file(fileInDir0)); + } + + SECTION("Non-Recursive Removal Fails") { + fs::create_directory(dir0.c_str()); + ofstream of(fileInDir0); + CHECK(fs::is_directory(dir0)); + CHECK(fs::is_regular_file(fileInDir0)); + REQUIRE(hostFs.removeDirectory(FilesystemParams(dir0.c_str())) == + HasFileSystemIF::DIRECTORY_NOT_EMPTY); + } + + SECTION("Create directory with parent directory") { + CHECK(hostFs.createDirectory(FilesystemParams(dirWithParent.c_str()), true) == result::OK); + CHECK(fs::is_directory(dir0)); + CHECK(fs::is_directory(dirWithParent)); + } + + SECTION("Read but provided buffer too small") { + std::string data = "hello world!"; + FileOpParams params(file0.c_str(), data.size()); + 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, 5) == SerializeIF::BUFFER_TOO_SHORT); + readSize = 10; + CHECK(hostFs.readFromFile(params, &readPtr, readSize, readBuf.size()) == + SerializeIF::BUFFER_TOO_SHORT); } REQUIRE_NOTHROW(fs::remove(file0)); REQUIRE_NOTHROW(fs::remove(file1)); - REQUIRE_NOTHROW(fs::remove(dir0)); + REQUIRE_NOTHROW(fs::remove_all(dir0)); } \ No newline at end of file