fsfw/unittests/mock/FilesystemMock.h

97 lines
3.3 KiB
C
Raw Normal View History

#ifndef FSFW_MOCKS_FILESYSTEMMOCK_H
#define FSFW_MOCKS_FILESYSTEMMOCK_H
2022-08-17 16:47:46 +02:00
#include <fstream>
2022-08-10 17:03:23 +02:00
#include <map>
2022-08-10 15:03:53 +02:00
#include <queue>
#include <string>
#include <utility>
#include "fsfw/filesystem.h"
2022-08-10 17:03:23 +02:00
/**
* This mock models a filesystem in the RAM. It can be used to verify correct behaviour of
* a component using a filesystem without relying on an actual OS filesystem implementation.
*
* Please note that this object does not actually check paths for validity. The file API was
* built in a way to allow reading a file back after it was written while also remembering
* the specific file segments which were inserted in write calls.
*/
class FilesystemMock : public HasFileSystemIF {
public:
2022-08-10 15:03:53 +02:00
struct FileWriteInfo {
2024-05-24 14:29:42 +02:00
FileWriteInfo(std::string filename, size_t offset, const uint8_t* data, size_t len)
2022-08-10 17:03:23 +02:00
: filename(std::move(filename)), offset(offset) {
2022-08-10 15:03:53 +02:00
this->data.insert(this->data.end(), data, data + len);
}
std::string filename;
size_t offset;
std::vector<uint8_t> data;
};
using FileSegmentQueue = std::queue<FileWriteInfo>;
struct FileInfo {
FileSegmentQueue fileSegQueue;
std::vector<uint8_t> fileRaw;
};
std::map<std::string, FileInfo> fileMap;
2022-08-10 17:03:23 +02:00
struct DirInfo {
size_t createCallCount = 0;
size_t delCallCount = 0;
std::queue<bool> wihParentDir;
std::queue<bool> recursiveDeletion;
};
std::map<std::string, DirInfo> dirMap;
struct RenameInfo {
RenameInfo(std::string oldName, std::string newName)
: oldName(std::move(oldName)), newName(std::move(newName)) {}
std::string oldName;
std::string newName;
};
std::queue<RenameInfo> renameQueue;
2022-09-05 17:42:56 +02:00
std::string truncateCalledOnFile;
2024-05-24 14:29:42 +02:00
ReturnValue_t feedFile(const std::string& filename, std::ifstream& file);
2022-09-05 17:42:56 +02:00
2024-05-24 14:29:42 +02:00
ReturnValue_t getBaseFilename(const char* path, char* nameBuf, size_t maxLen,
size_t& baseNameLen) override;
2024-05-24 14:29:42 +02:00
ReturnValue_t isDirectory(const char* path, bool& isDirectory) override;
2022-09-05 17:42:56 +02:00
2024-05-24 14:29:42 +02:00
bool fileExists(const char* path, FileSystemArgsIF* args) override;
ReturnValue_t truncateFile(const char* path, FileSystemArgsIF* args) override;
ReturnValue_t getFileSize(const char* path, uint64_t& fileSize, FileSystemArgsIF* args) override;
ReturnValue_t writeToFile(const char* path, size_t offset, const uint8_t* data,
size_t size) override;
ReturnValue_t readFromFile(const char* path, size_t offset, size_t size, uint8_t* buffer,
size_t& readSize, size_t maxSize) override;
ReturnValue_t createFile(const char* path, const uint8_t* data, size_t size) override;
ReturnValue_t removeFile(const char* path, FileSystemArgsIF* args) override;
ReturnValue_t createDirectory(const char* path, bool createParentDirs,
FileSystemArgsIF* args) override;
ReturnValue_t removeDirectory(const char* path, bool deleteRecurively,
FileSystemArgsIF* args) override;
ReturnValue_t rename(const char* oldPath, const char* newPath, FileSystemArgsIF* args) override;
2022-08-10 17:03:23 +02:00
void reset();
2022-08-17 16:47:46 +02:00
using HasFileSystemIF::createDirectory;
using HasFileSystemIF::createFile;
using HasFileSystemIF::readFromFile;
2022-08-10 17:03:23 +02:00
private:
2024-05-24 14:29:42 +02:00
void createOrAddToFile(const char* path, size_t offset, const uint8_t* data, size_t dataSize);
};
#endif // FSFW_MOCKS_FILESYSTEMMOCK_H