From c66fab90f9a5c3258849a1f8139332a303dfb620 Mon Sep 17 00:00:00 2001 From: Ulrich Mohr Date: Thu, 26 Jan 2023 17:05:25 +0100 Subject: [PATCH] filesystem stuff works on linux/host --- unittests/hal/testFsMock.cpp | 2 + unittests/hal/testHostFilesystem.cpp | 73 +++++++++++++++++++++------- 2 files changed, 58 insertions(+), 17 deletions(-) diff --git a/unittests/hal/testFsMock.cpp b/unittests/hal/testFsMock.cpp index bd15fb59e..1f71e3118 100644 --- a/unittests/hal/testFsMock.cpp +++ b/unittests/hal/testFsMock.cpp @@ -9,6 +9,8 @@ TEST_CASE("Filesystem Mock", "[mocks]") { auto fsMock = FilesystemMock(); SECTION("Create File") { + std::filesystem::path myPath = "hello.txt"; + std::filesystem::path myPath2("hello.txt"); FilesystemParams params("hello.txt"); CHECK(fsMock.createFile(params) == returnvalue::OK); std::string filename_c("hello.txt"); diff --git a/unittests/hal/testHostFilesystem.cpp b/unittests/hal/testHostFilesystem.cpp index e011eaa65..06d4f3747 100644 --- a/unittests/hal/testHostFilesystem.cpp +++ b/unittests/hal/testHostFilesystem.cpp @@ -11,6 +11,16 @@ using namespace std; +/** + * paths are a bit complex here: + * - std::filesystem is using path::value_type which can be using both char* and wchar*, + * depending on platform + * - fsfw::HasFilesystemIF is using char* + * - To be safe, whenever there is a path object to be passed as C string into the FSFW API, + * it needs to be converted to std::string and then .c_str() can be used to get a C string + * Note: path.string().c_string() does not work as it is returning a pointer to a temporary +*/ + TEST_CASE("Host Filesystem", "[hal][host]") { namespace fs = filesystem; auto hostFs = HostFilesystem(); @@ -26,14 +36,18 @@ TEST_CASE("Host Filesystem", "[hal][host]") { REQUIRE_NOTHROW(fs::remove_all(dir0)); SECTION("Create file") { - FilesystemParams params(file0.string().c_str()); + // See note at the top + std::string file0_string = file0.string(); + FilesystemParams params(file0_string.c_str()); REQUIRE(hostFs.createFile(params) == returnvalue::OK); CHECK(fs::is_regular_file(file0)); REQUIRE(fs::exists(file0)); } SECTION("Remove File") { - FilesystemParams params(file0.string().c_str()); + // See note at the top + std::string file0_string = file0.string(); + FilesystemParams params(file0_string.c_str()); REQUIRE(hostFs.createFile(params) == returnvalue::OK); CHECK(fs::is_regular_file(file0)); REQUIRE(fs::exists(file0)); @@ -42,14 +56,18 @@ TEST_CASE("Host Filesystem", "[hal][host]") { } SECTION("Create Directory") { - FilesystemParams params(dir0.string().c_str()); + // See note at the top + std::string dir0_string = dir0.string(); + FilesystemParams params(dir0_string.c_str()); REQUIRE(hostFs.createDirectory(params) == returnvalue::OK); CHECK(fs::is_directory(dir0)); REQUIRE(fs::exists(dir0)); } SECTION("Remove Directory") { - FilesystemParams params(dir0.string().c_str()); + // See note at the top + std::string dir0_string = dir0.string(); + FilesystemParams params(dir0_string.c_str()); REQUIRE(hostFs.createDirectory(params) == returnvalue::OK); REQUIRE(fs::exists(dir0)); REQUIRE(hostFs.removeDirectory(params) == returnvalue::OK); @@ -57,7 +75,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") { } SECTION("Rename File") { - FilesystemParams params(file0.string().c_str()); + // See note at the top + std::string file0_string = file0.string(); + FilesystemParams params(file0_string.c_str()); REQUIRE(hostFs.createFile(params) == returnvalue::OK); CHECK(fs::is_regular_file(file0)); REQUIRE(fs::exists(file0)); @@ -66,7 +86,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") { SECTION("Write To File") { std::string data = "hello world!"; - FileOpParams params(file0.string().c_str(), data.size()); + // See note at the top + std::string file0_string = file0.string(); + FileOpParams params(file0_string.c_str(), data.size()); REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK); CHECK(fs::is_regular_file(file0)); REQUIRE(fs::exists(file0)); @@ -88,7 +110,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") { for (uint8_t& byte : randData) { byte = distU8(rng); } - FileOpParams params(file0.string().c_str(), randData.size() - 256); + // See note at the top + std::string file0_string = file0.string(); + FileOpParams params(file0_string.c_str(), randData.size() - 256); REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK); CHECK(fs::is_regular_file(file0)); REQUIRE(fs::exists(file0)); @@ -107,7 +131,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") { SECTION("Read From File") { std::string data = "hello world!"; - FileOpParams params(file0.string().c_str(), data.size()); + // See note at the top + std::string file0_string = file0.string(); + FileOpParams params(file0_string.c_str(), data.size()); REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK); CHECK(fs::is_regular_file(file0)); ofstream of(file0); @@ -138,7 +164,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") { } SECTION("Create File but already exists") { - FilesystemParams params(file0.string().c_str()); + // See note at the top + std::string file0_string = file0.string(); + FilesystemParams params(file0_string.c_str()); REQUIRE(hostFs.createFile(params) == returnvalue::OK); REQUIRE(hostFs.createFile(params) == HasFileSystemIF::FILE_ALREADY_EXISTS); } @@ -148,25 +176,32 @@ TEST_CASE("Host Filesystem", "[hal][host]") { } SECTION("Create Directory but already exists") { - FileOpParams params(file0.string().c_str(), 12); - REQUIRE(hostFs.createDirectory(params.fsParams) == returnvalue::OK); - REQUIRE(hostFs.createDirectory(params.fsParams) == HasFileSystemIF::DIRECTORY_ALREADY_EXISTS); + // See note at the top + std::string dir0_string = dir0.string(); + FilesystemParams params(dir0_string.c_str()); + REQUIRE(hostFs.createDirectory(params) == returnvalue::OK); + REQUIRE(hostFs.createDirectory(params) == HasFileSystemIF::DIRECTORY_ALREADY_EXISTS); } SECTION("Remove Directory but does not exist") { - FilesystemParams params(dir0.string().c_str()); + // See note at the top + std::string dir0_string = dir0.string(); + FilesystemParams params(dir0_string.c_str()); REQUIRE(hostFs.removeDirectory(params) == HasFileSystemIF::DIRECTORY_DOES_NOT_EXIST); } SECTION("Remove Directory but is file") { ofstream of(file0); - FilesystemParams params(file0.string().c_str()); + std::string file0_string = file0.string(); + FilesystemParams params(file0_string.c_str()); REQUIRE(hostFs.removeDirectory(params) == HasFileSystemIF::NOT_A_DIRECTORY); } SECTION("Read from file but does not exist") { + // See note at the top std::string data = "hello world!"; - FileOpParams params(file0.string().c_str(), data.size()); + std::string file0_string = file0.string(); + FileOpParams params(file0_string.c_str(), data.size()); std::array readBuf{}; uint8_t* readPtr = readBuf.data(); size_t readSize = 0; @@ -176,7 +211,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") { SECTION("Write to file but does not exist") { std::string data = "hello world!"; - FileOpParams params(file0.string().c_str(), data.size()); + // See note at the top + std::string file0_string = file0.string(); + FileOpParams params(file0_string.c_str(), data.size()); CHECK(hostFs.writeToFile(params, reinterpret_cast(data.c_str())) == HasFileSystemIF::FILE_DOES_NOT_EXIST); } @@ -210,7 +247,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") { SECTION("Read but provided buffer too small") { std::string data = "hello world!"; - FileOpParams params(file0.string().c_str(), data.size()); + // See note at the top + std::string file0_string = file0.string(); + FileOpParams params(file0_string.c_str(), data.size()); ofstream of(file0); of.write(data.c_str(), static_cast(data.size())); of.close();