filesystem stuff works on linux/host

This commit is contained in:
Ulrich Mohr 2023-01-26 17:05:25 +01:00
parent 3e8446ba8b
commit c66fab90f9
2 changed files with 58 additions and 17 deletions

View File

@ -9,6 +9,8 @@ TEST_CASE("Filesystem Mock", "[mocks]") {
auto fsMock = FilesystemMock(); auto fsMock = FilesystemMock();
SECTION("Create File") { SECTION("Create File") {
std::filesystem::path myPath = "hello.txt";
std::filesystem::path myPath2("hello.txt");
FilesystemParams params("hello.txt"); FilesystemParams params("hello.txt");
CHECK(fsMock.createFile(params) == returnvalue::OK); CHECK(fsMock.createFile(params) == returnvalue::OK);
std::string filename_c("hello.txt"); std::string filename_c("hello.txt");

View File

@ -11,6 +11,16 @@
using namespace std; 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]") { TEST_CASE("Host Filesystem", "[hal][host]") {
namespace fs = filesystem; namespace fs = filesystem;
auto hostFs = HostFilesystem(); auto hostFs = HostFilesystem();
@ -26,14 +36,18 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
REQUIRE_NOTHROW(fs::remove_all(dir0)); REQUIRE_NOTHROW(fs::remove_all(dir0));
SECTION("Create file") { 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); REQUIRE(hostFs.createFile(params) == returnvalue::OK);
CHECK(fs::is_regular_file(file0)); CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0)); REQUIRE(fs::exists(file0));
} }
SECTION("Remove File") { 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); REQUIRE(hostFs.createFile(params) == returnvalue::OK);
CHECK(fs::is_regular_file(file0)); CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0)); REQUIRE(fs::exists(file0));
@ -42,14 +56,18 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
} }
SECTION("Create Directory") { 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); REQUIRE(hostFs.createDirectory(params) == returnvalue::OK);
CHECK(fs::is_directory(dir0)); CHECK(fs::is_directory(dir0));
REQUIRE(fs::exists(dir0)); REQUIRE(fs::exists(dir0));
} }
SECTION("Remove Directory") { 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(hostFs.createDirectory(params) == returnvalue::OK);
REQUIRE(fs::exists(dir0)); REQUIRE(fs::exists(dir0));
REQUIRE(hostFs.removeDirectory(params) == returnvalue::OK); REQUIRE(hostFs.removeDirectory(params) == returnvalue::OK);
@ -57,7 +75,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
} }
SECTION("Rename File") { 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); REQUIRE(hostFs.createFile(params) == returnvalue::OK);
CHECK(fs::is_regular_file(file0)); CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0)); REQUIRE(fs::exists(file0));
@ -66,7 +86,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
SECTION("Write To File") { SECTION("Write To File") {
std::string data = "hello world!"; 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); REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK);
CHECK(fs::is_regular_file(file0)); CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0)); REQUIRE(fs::exists(file0));
@ -88,7 +110,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
for (uint8_t& byte : randData) { for (uint8_t& byte : randData) {
byte = distU8(rng); 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); REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK);
CHECK(fs::is_regular_file(file0)); CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file0)); REQUIRE(fs::exists(file0));
@ -107,7 +131,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
SECTION("Read From File") { SECTION("Read From File") {
std::string data = "hello world!"; 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); REQUIRE(hostFs.createFile(params.fsParams) == returnvalue::OK);
CHECK(fs::is_regular_file(file0)); CHECK(fs::is_regular_file(file0));
ofstream of(file0); ofstream of(file0);
@ -138,7 +164,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
} }
SECTION("Create File but already exists") { 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) == returnvalue::OK);
REQUIRE(hostFs.createFile(params) == HasFileSystemIF::FILE_ALREADY_EXISTS); REQUIRE(hostFs.createFile(params) == HasFileSystemIF::FILE_ALREADY_EXISTS);
} }
@ -148,25 +176,32 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
} }
SECTION("Create Directory but already exists") { SECTION("Create Directory but already exists") {
FileOpParams params(file0.string().c_str(), 12); // See note at the top
REQUIRE(hostFs.createDirectory(params.fsParams) == returnvalue::OK); std::string dir0_string = dir0.string();
REQUIRE(hostFs.createDirectory(params.fsParams) == HasFileSystemIF::DIRECTORY_ALREADY_EXISTS); 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") { 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); REQUIRE(hostFs.removeDirectory(params) == HasFileSystemIF::DIRECTORY_DOES_NOT_EXIST);
} }
SECTION("Remove Directory but is file") { SECTION("Remove Directory but is file") {
ofstream of(file0); 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); REQUIRE(hostFs.removeDirectory(params) == HasFileSystemIF::NOT_A_DIRECTORY);
} }
SECTION("Read from file but does not exist") { SECTION("Read from file but does not exist") {
// See note at the top
std::string data = "hello world!"; 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<uint8_t, 10> readBuf{}; std::array<uint8_t, 10> readBuf{};
uint8_t* readPtr = readBuf.data(); uint8_t* readPtr = readBuf.data();
size_t readSize = 0; size_t readSize = 0;
@ -176,7 +211,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
SECTION("Write to file but does not exist") { SECTION("Write to file but does not exist") {
std::string data = "hello world!"; 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<const uint8_t*>(data.c_str())) == CHECK(hostFs.writeToFile(params, reinterpret_cast<const uint8_t*>(data.c_str())) ==
HasFileSystemIF::FILE_DOES_NOT_EXIST); HasFileSystemIF::FILE_DOES_NOT_EXIST);
} }
@ -210,7 +247,9 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
SECTION("Read but provided buffer too small") { SECTION("Read but provided buffer too small") {
std::string data = "hello world!"; 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); ofstream of(file0);
of.write(data.c_str(), static_cast<unsigned int>(data.size())); of.write(data.c_str(), static_cast<unsigned int>(data.size()));
of.close(); of.close();