add fs mock feed function

This commit is contained in:
Robin Müller 2022-08-17 16:47:46 +02:00
parent 12c452e7ce
commit 0bb82e0da2
No known key found for this signature in database
GPG Key ID: 11D4952C8CCEF814
4 changed files with 75 additions and 2 deletions

View File

@ -1,2 +1,2 @@
target_sources(${FSFW_TEST_TGT} PRIVATE testCommandExecutor.cpp target_sources(${FSFW_TEST_TGT} PRIVATE testCommandExecutor.cpp
testHostFilesystem.cpp) testHostFilesystem.cpp testFsMock.cpp)

View File

@ -0,0 +1,49 @@
#include <catch2/catch_test_macros.hpp>
#include "fsfw/serialize/SerializeIF.h"
#include "mocks/FilesystemMock.h"
using namespace std;
TEST_CASE("Filesystem Mock", "[mocks]") {
auto fsMock = FilesystemMock();
SECTION("Create File") {
FilesystemParams params("hello.txt");
CHECK(fsMock.createFile(params) == result::OK);
auto iter = fsMock.fileMap.find("hello.txt");
REQUIRE(iter != fsMock.fileMap.end());
FilesystemMock::FileInfo &stats = iter->second;
CHECK(stats.fileSegQueue.empty());
CHECK(stats.fileRaw.empty());
}
SECTION("Write to File") {
std::string testData = "test data";
FileOpParams params("hello.txt", testData.size());
CHECK(fsMock.writeToFile(params, reinterpret_cast<const uint8_t *>(testData.data())) ==
HasReturnvaluesIF::RETURN_OK);
auto iter = fsMock.fileMap.find("hello.txt");
REQUIRE(iter != fsMock.fileMap.end());
FilesystemMock::FileInfo &stats = iter->second;
CHECK(not stats.fileSegQueue.empty());
CHECK(not stats.fileRaw.empty());
auto &segment = stats.fileSegQueue.back();
CHECK(segment.offset == 0);
CHECK(std::string(reinterpret_cast<const char *>(segment.data.data()), segment.data.size()) ==
testData);
CHECK(std::string(reinterpret_cast<const char *>(stats.fileRaw.data()), segment.data.size()) ==
testData);
}
SECTION("Create Directory") {
FilesystemParams params("hello");
CHECK(fsMock.createDirectory(params) == result::OK);
REQUIRE(not fsMock.dirMap.empty());
auto iter = fsMock.dirMap.find("hello");
REQUIRE(iter != fsMock.dirMap.end());
auto &dirInfo = iter->second;
CHECK(dirInfo.createCallCount == 1);
CHECK(dirInfo.delCallCount == 0);
}
}

View File

@ -1,7 +1,21 @@
#include "FilesystemMock.h" #include "FilesystemMock.h"
#include <filesystem>
#include "fsfw/serialize/SerializeIF.h" #include "fsfw/serialize/SerializeIF.h"
ReturnValue_t FilesystemMock::feedFile(const std::string &filename, std::ifstream &file) {
if (not std::filesystem::exists(filename)) {
return HasReturnvaluesIF::RETURN_FAILED;
}
size_t fileSize = std::filesystem::file_size(filename);
FileOpParams params(filename.c_str(), fileSize);
std::vector<uint8_t> rawData(fileSize);
file.read(reinterpret_cast<char *>(rawData.data()), static_cast<unsigned int>(rawData.size()));
createOrAddToFile(params, rawData.data());
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t FilesystemMock::writeToFile(FileOpParams params, const uint8_t *data) { ReturnValue_t FilesystemMock::writeToFile(FileOpParams params, const uint8_t *data) {
createOrAddToFile(params, data); createOrAddToFile(params, data);
return HasReturnvaluesIF::RETURN_OK; return HasReturnvaluesIF::RETURN_OK;
@ -75,7 +89,9 @@ void FilesystemMock::createOrAddToFile(FileOpParams params, const uint8_t *data)
auto iter = fileMap.find(filename); auto iter = fileMap.find(filename);
if (iter == fileMap.end()) { if (iter == fileMap.end()) {
FileSegmentQueue queue; FileSegmentQueue queue;
if (params.size > 0) {
queue.push(FileWriteInfo(filename, params.offset, data, params.size)); queue.push(FileWriteInfo(filename, params.offset, data, params.size));
}
FileInfo info; FileInfo info;
info.fileSegQueue = queue; info.fileSegQueue = queue;
if (data != nullptr) { if (data != nullptr) {
@ -105,4 +121,6 @@ void FilesystemMock::createOrAddToFile(FileOpParams params, const uint8_t *data)
void FilesystemMock::reset() { void FilesystemMock::reset() {
fileMap.clear(); fileMap.clear();
dirMap.clear(); dirMap.clear();
std::queue<RenameInfo> empty;
std::swap(renameQueue, empty);
} }

View File

@ -1,6 +1,7 @@
#ifndef FSFW_MOCKS_FILESYSTEMMOCK_H #ifndef FSFW_MOCKS_FILESYSTEMMOCK_H
#define FSFW_MOCKS_FILESYSTEMMOCK_H #define FSFW_MOCKS_FILESYSTEMMOCK_H
#include <fstream>
#include <map> #include <map>
#include <queue> #include <queue>
#include <string> #include <string>
@ -53,6 +54,7 @@ class FilesystemMock : public HasFileSystemIF {
}; };
std::queue<RenameInfo> renameQueue; std::queue<RenameInfo> renameQueue;
ReturnValue_t feedFile(const std::string &filename, std::ifstream &file);
ReturnValue_t writeToFile(FileOpParams params, const uint8_t *data) override; ReturnValue_t writeToFile(FileOpParams params, const uint8_t *data) override;
ReturnValue_t readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize, ReturnValue_t readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize,
size_t maxSize) override; size_t maxSize) override;
@ -64,6 +66,10 @@ class FilesystemMock : public HasFileSystemIF {
void reset(); void reset();
using HasFileSystemIF::createDirectory;
using HasFileSystemIF::createFile;
using HasFileSystemIF::readFromFile;
private: private:
void createOrAddToFile(FileOpParams params, const uint8_t *data); void createOrAddToFile(FileOpParams params, const uint8_t *data);
}; };