continued host FS test

This commit is contained in:
Robin Müller 2022-08-11 10:19:25 +02:00
parent 8aaabc5d73
commit c12492df03
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
4 changed files with 56 additions and 36 deletions

View File

@ -16,9 +16,18 @@ struct FilesystemParams {
FileSystemArgsIF* args = nullptr; FileSystemArgsIF* args = nullptr;
}; };
struct FileOpParams : public FilesystemParams { struct FileOpParams {
FileOpParams(const char* path, size_t size) : FilesystemParams(path), size(size) {} 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 size;
size_t offset = 0; size_t offset = 0;
}; };

View File

@ -11,10 +11,10 @@ using namespace std;
HostFilesystem::HostFilesystem() = default; HostFilesystem::HostFilesystem() = default;
ReturnValue_t HostFilesystem::writeToFile(FileOpParams params, const uint8_t *data) { ReturnValue_t HostFilesystem::writeToFile(FileOpParams params, const uint8_t *data) {
if (params.path == nullptr) { if (params.path() == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
path path(params.path); path path(params.path());
if (not exists(path)) { if (not exists(path)) {
return HasFileSystemIF::FILE_DOES_NOT_EXIST; 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, ReturnValue_t HostFilesystem::readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize,
size_t maxSize) { size_t maxSize) {
if (params.path == nullptr) { if (params.path() == nullptr) {
return HasReturnvaluesIF::RETURN_FAILED; return HasReturnvaluesIF::RETURN_FAILED;
} }
path path(params.path); path path(params.path());
if (not exists(path)) { if (not exists(path)) {
return HasFileSystemIF::FILE_DOES_NOT_EXIST; return HasFileSystemIF::FILE_DOES_NOT_EXIST;
} }

View File

@ -8,55 +8,66 @@ TEST_CASE("Host Filesystem", "[hal][host]") {
auto hostFs = HostFilesystem(); auto hostFs = HostFilesystem();
auto tmpDir = fs::temp_directory_path(); auto tmpDir = fs::temp_directory_path();
fs::path file0 = tmpDir / "hello.txt"; 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(file0));
REQUIRE_NOTHROW(fs::remove(file1)); REQUIRE_NOTHROW(fs::remove(file1));
REQUIRE_NOTHROW(fs::remove(dir0));
SECTION("Create file") { SECTION("Create file") {
fs::path file = tmpDir / "hello.txt"; FilesystemParams params(file0.c_str());
FilesystemParams params(file.c_str());
REQUIRE(hostFs.createFile(params) == result::OK); REQUIRE(hostFs.createFile(params) == result::OK);
CHECK(fs::is_regular_file(file)); CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file)); REQUIRE(fs::exists(file0));
REQUIRE_NOTHROW(fs::remove(file)); REQUIRE_NOTHROW(fs::remove(file0));
} }
SECTION("Remove File") { SECTION("Remove File") {
fs::path file = tmpDir / "hello.txt"; FilesystemParams params(file0.c_str());
FilesystemParams params(file.c_str());
REQUIRE(hostFs.createFile(params) == result::OK); REQUIRE(hostFs.createFile(params) == result::OK);
CHECK(fs::is_regular_file(file)); CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file)); REQUIRE(fs::exists(file0));
REQUIRE(hostFs.removeFile(file.c_str()) == result::OK); REQUIRE(hostFs.removeFile(file0.c_str()) == result::OK);
REQUIRE(not fs::exists(file)); REQUIRE(not fs::exists(file0));
} }
SECTION("Create Directory") { SECTION("Create Directory") {
fs::path dirPath = tmpDir / "test_dir"; FilesystemParams params(dir0.c_str());
FilesystemParams params(dirPath.c_str());
REQUIRE(hostFs.createDirectory(params) == result::OK); REQUIRE(hostFs.createDirectory(params) == result::OK);
CHECK(fs::is_directory(dirPath)); CHECK(fs::is_directory(dir0));
REQUIRE(fs::exists(dirPath)); REQUIRE(fs::exists(dir0));
REQUIRE_NOTHROW(fs::remove(dirPath)); REQUIRE_NOTHROW(fs::remove(dir0));
} }
SECTION("Remove Directory") { SECTION("Remove Directory") {
fs::path dirPath = tmpDir / "test_dir"; FilesystemParams params(dir0.c_str());
FilesystemParams params(dirPath.c_str());
REQUIRE(hostFs.createDirectory(params) == result::OK); REQUIRE(hostFs.createDirectory(params) == result::OK);
REQUIRE(fs::exists(dirPath)); REQUIRE(fs::exists(dir0));
REQUIRE(hostFs.removeDirectory(params) == result::OK); REQUIRE(hostFs.removeDirectory(params) == result::OK);
REQUIRE(not fs::exists(dirPath)); REQUIRE(not fs::exists(dir0));
} }
SECTION("Rename File") { SECTION("Rename File") {
fs::path file = tmpDir / "hello.txt"; FilesystemParams params(file0.c_str());
fs::path newFile = tmpDir / "hello2.txt";
FilesystemParams params(file.c_str());
REQUIRE(hostFs.createFile(params) == result::OK); REQUIRE(hostFs.createFile(params) == result::OK);
CHECK(fs::is_regular_file(file)); CHECK(fs::is_regular_file(file0));
REQUIRE(fs::exists(file)); REQUIRE(fs::exists(file0));
REQUIRE(hostFs.rename(file.c_str(), newFile.c_str()) == result::OK); REQUIRE(hostFs.rename(file0.c_str(), file1.c_str()) == result::OK);
REQUIRE_NOTHROW(fs::remove(newFile)); 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<const uint8_t*>(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));
} }

View File

@ -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, ReturnValue_t FilesystemMock::readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize,
size_t maxSize) { size_t maxSize) {
std::string filename(params.path); std::string filename(params.path());
auto iter = fileMap.find(filename); auto iter = fileMap.find(filename);
if (iter == fileMap.end()) { if (iter == fileMap.end()) {
return HasFileSystemIF::FILE_DOES_NOT_EXIST; 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) { void FilesystemMock::createOrAddToFile(FileOpParams params, const uint8_t *data) {
std::string filename(params.path); std::string filename(params.path());
auto iter = fileMap.find(filename); auto iter = fileMap.find(filename);
if (iter == fileMap.end()) { if (iter == fileMap.end()) {
FileSegmentQueue queue; FileSegmentQueue queue;