2015-03-12 14:27:51 +01:00
|
|
|
#include "../../../../implementations/ondisk/OnDiskBlock.h"
|
2015-04-25 16:43:52 +02:00
|
|
|
#include <messmer/cpp-utils/data/DataFixture.h>
|
2015-03-12 14:27:51 +01:00
|
|
|
#include "../../../../utils/FileDoesntExistException.h"
|
2015-02-17 00:23:33 +01:00
|
|
|
#include "google/gtest/gtest.h"
|
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
#include <messmer/cpp-utils/data/Data.h>
|
|
|
|
#include <messmer/cpp-utils/tempfile/TempFile.h>
|
|
|
|
#include <messmer/cpp-utils/tempfile/TempDir.h>
|
2015-07-21 14:50:52 +02:00
|
|
|
#include <messmer/cpp-utils/pointer/unique_ref_boost_optional_gtest_workaround.h>
|
2014-12-09 17:19:59 +01:00
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
using ::testing::Test;
|
|
|
|
using ::testing::WithParamInterface;
|
|
|
|
using ::testing::Values;
|
|
|
|
|
|
|
|
using std::ofstream;
|
|
|
|
using std::ios;
|
2015-04-25 02:48:41 +02:00
|
|
|
using cpputils::Data;
|
2015-04-25 16:43:52 +02:00
|
|
|
using cpputils::DataFixture;
|
2015-04-25 02:48:41 +02:00
|
|
|
using cpputils::TempFile;
|
|
|
|
using cpputils::TempDir;
|
2015-07-21 14:50:52 +02:00
|
|
|
using cpputils::unique_ref;
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
using namespace blockstore;
|
|
|
|
using namespace blockstore::ondisk;
|
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
|
|
|
|
class OnDiskBlockLoadTest: public Test, public WithParamInterface<size_t> {
|
|
|
|
public:
|
2015-01-24 22:08:41 +01:00
|
|
|
OnDiskBlockLoadTest():
|
|
|
|
dir(),
|
|
|
|
key(Key::FromString("1491BB4932A389EE14BC7090AC772972")),
|
|
|
|
file(dir.path() / key.ToString()) {
|
|
|
|
}
|
|
|
|
TempDir dir;
|
|
|
|
Key key;
|
2014-12-09 17:19:59 +01:00
|
|
|
TempFile file;
|
|
|
|
|
|
|
|
void SetFileSize(size_t size) {
|
|
|
|
Data data(size);
|
|
|
|
data.StoreToFile(file.path());
|
|
|
|
}
|
|
|
|
|
2015-04-25 16:43:52 +02:00
|
|
|
void StoreData(const Data &data) {
|
|
|
|
data.StoreToFile(file.path());
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
2015-07-21 14:50:52 +02:00
|
|
|
unique_ref<OnDiskBlock> LoadBlock() {
|
|
|
|
return OnDiskBlock::LoadFromDisk(dir.path(), key).value();
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
2015-04-25 16:43:52 +02:00
|
|
|
void EXPECT_BLOCK_DATA_EQ(const Data &expected, const OnDiskBlock &actual) {
|
2014-12-09 17:19:59 +01:00
|
|
|
EXPECT_EQ(expected.size(), actual.size());
|
|
|
|
EXPECT_EQ(0, std::memcmp(expected.data(), actual.data(), expected.size()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
INSTANTIATE_TEST_CASE_P(OnDiskBlockLoadTest, OnDiskBlockLoadTest, Values(0, 1, 5, 1024, 10*1024*1024));
|
|
|
|
|
|
|
|
TEST_P(OnDiskBlockLoadTest, FileSizeIsCorrect) {
|
|
|
|
SetFileSize(GetParam());
|
|
|
|
|
|
|
|
auto block = LoadBlock();
|
|
|
|
|
|
|
|
EXPECT_EQ(GetParam(), block->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(OnDiskBlockLoadTest, LoadedDataIsCorrect) {
|
2015-04-25 16:43:52 +02:00
|
|
|
Data randomData = DataFixture::generate(GetParam());
|
2014-12-09 17:19:59 +01:00
|
|
|
StoreData(randomData);
|
|
|
|
|
|
|
|
auto block = LoadBlock();
|
|
|
|
|
|
|
|
EXPECT_BLOCK_DATA_EQ(randomData, *block);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OnDiskBlockLoadTest, LoadNotExistingBlock) {
|
2015-01-24 22:08:41 +01:00
|
|
|
Key key2 = Key::FromString("272EE5517627CFA147A971A8E6E747E0");
|
2015-07-21 14:50:52 +02:00
|
|
|
EXPECT_EQ(boost::none, OnDiskBlock::LoadFromDisk(dir.path(), key2));
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|