fsfw/unittests/mocks/FilesystemMock.cpp

141 lines
4.5 KiB
C++
Raw Normal View History

#include "FilesystemMock.h"
2022-08-17 16:47:46 +02:00
#include <filesystem>
2022-08-10 17:03:23 +02:00
#include "fsfw/serialize/SerializeIF.h"
2022-08-17 16:47:46 +02:00
ReturnValue_t FilesystemMock::feedFile(const std::string &filename, std::ifstream &file) {
if (not std::filesystem::exists(filename)) {
2022-08-22 16:35:53 +02:00
return returnvalue::FAILED;
2022-08-17 16:47:46 +02:00
}
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());
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-17 16:47:46 +02:00
}
2022-08-10 15:03:53 +02:00
ReturnValue_t FilesystemMock::writeToFile(FileOpParams params, const uint8_t *data) {
2022-08-10 17:03:23 +02:00
createOrAddToFile(params, data);
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-10 17:03:23 +02:00
}
ReturnValue_t FilesystemMock::readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize,
size_t maxSize) {
2022-08-11 10:19:25 +02:00
std::string filename(params.path());
2022-08-10 17:03:23 +02:00
auto iter = fileMap.find(filename);
if (iter == fileMap.end()) {
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
} else {
FileInfo &info = iter->second;
size_t readLen = params.size;
if (params.offset + params.size > info.fileRaw.size()) {
if (params.offset > info.fileRaw.size()) {
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-10 17:03:23 +02:00
}
readLen = info.fileRaw.size() - params.offset;
}
if (readSize + readLen > maxSize) {
return SerializeIF::STREAM_TOO_SHORT;
}
std::copy(info.fileRaw.data() + params.offset, info.fileRaw.data() + readLen, *buffer);
*buffer += readLen;
readSize += readLen;
}
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-10 17:03:23 +02:00
}
ReturnValue_t FilesystemMock::createFile(FilesystemParams params, const uint8_t *data,
size_t size) {
FileOpParams params2(params.path, size);
createOrAddToFile(params2, data);
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-10 17:03:23 +02:00
}
ReturnValue_t FilesystemMock::removeFile(const char *path, FileSystemArgsIF *args) {
std::string filename(path);
auto iter = fileMap.find(filename);
if (iter == fileMap.end()) {
return HasFileSystemIF::FILE_DOES_NOT_EXIST;
} else {
fileMap.erase(iter);
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-10 17:03:23 +02:00
}
}
ReturnValue_t FilesystemMock::createDirectory(FilesystemParams params, bool createParentDirs) {
std::string dirPath = params.path;
dirMap[dirPath].createCallCount++;
dirMap[dirPath].wihParentDir.push(createParentDirs);
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-10 17:03:23 +02:00
}
ReturnValue_t FilesystemMock::removeDirectory(FilesystemParams params, bool deleteRecurively) {
std::string dirPath = params.path;
dirMap[dirPath].delCallCount++;
dirMap[dirPath].recursiveDeletion.push(deleteRecurively);
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-10 17:03:23 +02:00
}
2022-08-11 10:10:05 +02:00
ReturnValue_t FilesystemMock::rename(const char *oldPath, const char *newPath,
FileSystemArgsIF *args) {
2022-08-10 17:03:23 +02:00
renameQueue.push(RenameInfo(oldPath, newPath));
2022-08-22 16:35:53 +02:00
return returnvalue::OK;
2022-08-10 17:03:23 +02:00
}
void FilesystemMock::createOrAddToFile(FileOpParams params, const uint8_t *data) {
2022-08-11 10:19:25 +02:00
std::string filename(params.path());
2022-08-10 15:03:53 +02:00
auto iter = fileMap.find(filename);
if (iter == fileMap.end()) {
FileSegmentQueue queue;
2022-08-17 16:47:46 +02:00
if (params.size > 0) {
queue.push(FileWriteInfo(filename, params.offset, data, params.size));
}
2022-08-10 15:03:53 +02:00
FileInfo info;
info.fileSegQueue = queue;
2022-08-10 17:03:23 +02:00
if (data != nullptr) {
info.fileRaw.insert(info.fileRaw.end(), data, data + params.size);
}
2022-08-10 15:03:53 +02:00
fileMap.emplace(filename, info);
} else {
2022-08-10 17:03:23 +02:00
FileInfo &info = iter->second;
2022-08-10 15:03:53 +02:00
info.fileSegQueue.push(FileWriteInfo(filename, params.offset, data, params.size));
2022-08-10 17:03:23 +02:00
if (data == nullptr) {
return;
}
2022-08-10 15:03:53 +02:00
// Easiest case: append data to the end
2022-08-10 17:03:23 +02:00
if (params.offset == info.fileRaw.size()) {
2022-08-10 15:03:53 +02:00
info.fileRaw.insert(info.fileRaw.end(), data, data + params.size);
} else {
size_t totalNewLen = params.offset + params.size;
2022-08-10 17:03:23 +02:00
if (totalNewLen > info.fileRaw.size()) {
2022-08-10 15:03:53 +02:00
info.fileRaw.resize(params.offset + params.size);
}
2022-08-10 17:03:23 +02:00
std::copy(data, data + params.size,
info.fileRaw.begin() + static_cast<unsigned int>(params.offset));
2022-08-10 15:03:53 +02:00
}
}
}
2022-08-10 17:03:23 +02:00
void FilesystemMock::reset() {
fileMap.clear();
dirMap.clear();
2022-08-17 16:47:46 +02:00
std::queue<RenameInfo> empty;
std::swap(renameQueue, empty);
}
2022-09-05 17:42:56 +02:00
bool FilesystemMock::fileExists(FilesystemParams params) {
std::string filename(params.path);
auto iter = fileMap.find(filename);
if (iter == fileMap.end()) {
return false;
}
return true;
}
ReturnValue_t FilesystemMock::truncateFile(FilesystemParams params) {
truncateCalledOnFile = params.path;
return returnvalue::OK;
}