fsfw/unittests/mocks/FilesystemMock.cpp

108 lines
3.6 KiB
C++
Raw Normal View History

#include "FilesystemMock.h"
2022-08-10 17:03:23 +02:00
#include "fsfw/serialize/SerializeIF.h"
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);
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t FilesystemMock::readFromFile(FileOpParams params, uint8_t **buffer, size_t &readSize,
size_t maxSize) {
std::string filename(params.path);
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()) {
return HasReturnvaluesIF::RETURN_OK;
}
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;
}
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t FilesystemMock::createFile(FilesystemParams params, const uint8_t *data,
size_t size) {
FileOpParams params2(params.path, size);
createOrAddToFile(params2, data);
return HasReturnvaluesIF::RETURN_OK;
}
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);
return HasReturnvaluesIF::RETURN_OK;
}
}
ReturnValue_t FilesystemMock::createDirectory(FilesystemParams params, bool createParentDirs) {
std::string dirPath = params.path;
dirMap[dirPath].createCallCount++;
dirMap[dirPath].wihParentDir.push(createParentDirs);
return HasReturnvaluesIF::RETURN_OK;
}
ReturnValue_t FilesystemMock::removeDirectory(FilesystemParams params, bool deleteRecurively) {
std::string dirPath = params.path;
dirMap[dirPath].delCallCount++;
dirMap[dirPath].recursiveDeletion.push(deleteRecurively);
return HasReturnvaluesIF::RETURN_OK;
}
2022-08-11 09:32:18 +02:00
ReturnValue_t FilesystemMock::rename(const char *oldPath, char *newPath, FileSystemArgsIF *args) {
2022-08-10 17:03:23 +02:00
renameQueue.push(RenameInfo(oldPath, newPath));
return HasReturnvaluesIF::RETURN_OK;
}
void FilesystemMock::createOrAddToFile(FileOpParams params, const uint8_t *data) {
2022-08-10 15:03:53 +02:00
std::string filename(params.path);
auto iter = fileMap.find(filename);
if (iter == fileMap.end()) {
FileSegmentQueue queue;
queue.push(FileWriteInfo(filename, params.offset, data, params.size));
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();
}