libcryfs/fstest/testutils/FileTest.h

52 lines
1.8 KiB
C
Raw Normal View History

2015-10-15 13:04:57 +02:00
#pragma once
#ifndef MESSMER_FSPP_FSTEST_TESTUTILS_FILETEST_H_
#define MESSMER_FSPP_FSTEST_TESTUTILS_FILETEST_H_
#include "FileSystemTest.h"
2015-04-25 03:36:30 +02:00
#include <messmer/cpp-utils/data/Data.h>
2015-06-21 17:44:30 +02:00
#include <messmer/cpp-utils/pointer/unique_ref.h>
template<class ConcreteFileSystemTestFixture>
class FileTest: public FileSystemTest<ConcreteFileSystemTestFixture> {
public:
2015-10-17 21:34:15 +02:00
FileTest(): file_root(), file_nested() {
2015-04-25 03:36:30 +02:00
this->LoadDir("/")->createAndOpenFile("myfile", this->MODE_PUBLIC, 0, 0);
file_root = cpputils::to_unique_ptr(this->LoadFile("/myfile"));
2015-04-25 03:36:30 +02:00
this->LoadDir("/")->createDir("mydir", this->MODE_PUBLIC, 0, 0);
this->LoadDir("/mydir")->createAndOpenFile("mynestedfile", this->MODE_PUBLIC, 0, 0);
file_nested = cpputils::to_unique_ptr(this->LoadFile("/mydir/mynestedfile"));
}
std::unique_ptr<fspp::File> file_root;
std::unique_ptr<fspp::File> file_nested;
void EXPECT_SIZE(uint64_t expectedSize, const fspp::File &file) {
EXPECT_SIZE_IN_FILE(expectedSize, file);
auto openFile = file.open(O_RDONLY);
EXPECT_SIZE_IN_OPEN_FILE(expectedSize, *openFile);
EXPECT_NUMBYTES_READABLE(expectedSize, *openFile);
}
void EXPECT_SIZE_IN_FILE(uint64_t expectedSize, const fspp::File &file) {
struct stat st;
file.stat(&st);
EXPECT_EQ(expectedSize, st.st_size);
}
void EXPECT_SIZE_IN_OPEN_FILE(uint64_t expectedSize, const fspp::OpenFile &file) {
struct stat st;
file.stat(&st);
EXPECT_EQ(expectedSize, st.st_size);
}
void EXPECT_NUMBYTES_READABLE(uint64_t expectedSize, const fspp::OpenFile &file) {
cpputils::Data data(expectedSize);
//Try to read one byte more than the expected size
ssize_t readBytes = file.read(data.data(), expectedSize+1, 0);
//and check that it only read the expected size (but also not less)
EXPECT_EQ(expectedSize, readBytes);
}
};
#endif