2015-03-12 14:27:51 +01:00
|
|
|
#include "../../../../implementations/ondisk/FileAlreadyExistsException.h"
|
|
|
|
#include "../../../../implementations/ondisk/OnDiskBlock.h"
|
2015-02-17 00:23:33 +01:00
|
|
|
#include "google/gtest/gtest.h"
|
2014-12-09 17:19:59 +01:00
|
|
|
|
2015-04-25 02:48:41 +02:00
|
|
|
#include <messmer/cpp-utils/tempfile/TempFile.h>
|
|
|
|
#include <messmer/cpp-utils/tempfile/TempDir.h>
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
using ::testing::Test;
|
|
|
|
using ::testing::WithParamInterface;
|
|
|
|
using ::testing::Values;
|
|
|
|
|
|
|
|
using std::unique_ptr;
|
2015-04-25 02:48:41 +02:00
|
|
|
using cpputils::Data;
|
|
|
|
using cpputils::TempFile;
|
|
|
|
using cpputils::TempDir;
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
using namespace blockstore;
|
|
|
|
using namespace blockstore::ondisk;
|
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
|
|
|
|
class OnDiskBlockCreateTest: public Test {
|
|
|
|
public:
|
|
|
|
OnDiskBlockCreateTest()
|
|
|
|
// Don't create the temp file yet (therefore pass false to the TempFile constructor)
|
2015-01-24 22:08:41 +01:00
|
|
|
: dir(),
|
|
|
|
key(Key::FromString("1491BB4932A389EE14BC7090AC772972")),
|
|
|
|
file(dir.path() / key.ToString(), false) {
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
2015-01-24 22:08:41 +01:00
|
|
|
TempDir dir;
|
|
|
|
Key key;
|
2014-12-09 17:19:59 +01:00
|
|
|
TempFile file;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(OnDiskBlockCreateTest, CreatingBlockCreatesFile) {
|
|
|
|
EXPECT_FALSE(bf::exists(file.path()));
|
|
|
|
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block = OnDiskBlock::CreateOnDisk(dir.path(), key, Data(0));
|
2014-12-09 17:19:59 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(bf::exists(file.path()));
|
|
|
|
EXPECT_TRUE(bf::is_regular_file(file.path()));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(OnDiskBlockCreateTest, CreatingExistingBlockReturnsNull) {
|
2015-04-18 14:47:12 +02:00
|
|
|
auto block1 = OnDiskBlock::CreateOnDisk(dir.path(), key, Data(0));
|
|
|
|
auto block2 = OnDiskBlock::CreateOnDisk(dir.path(), key, Data(0));
|
2014-12-09 17:19:59 +01:00
|
|
|
EXPECT_TRUE((bool)block1);
|
|
|
|
EXPECT_FALSE((bool)block2);
|
|
|
|
}
|
|
|
|
|
|
|
|
class OnDiskBlockCreateSizeTest: public OnDiskBlockCreateTest, public WithParamInterface<size_t> {
|
|
|
|
public:
|
|
|
|
unique_ptr<OnDiskBlock> block;
|
|
|
|
Data ZEROES;
|
|
|
|
|
|
|
|
OnDiskBlockCreateSizeTest():
|
2015-04-18 14:47:12 +02:00
|
|
|
block(OnDiskBlock::CreateOnDisk(dir.path(), key, std::move(Data(GetParam()).FillWithZeroes()))),
|
2014-12-09 17:19:59 +01:00
|
|
|
ZEROES(block->size())
|
|
|
|
{
|
|
|
|
ZEROES.FillWithZeroes();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
INSTANTIATE_TEST_CASE_P(OnDiskBlockCreateSizeTest, OnDiskBlockCreateSizeTest, Values(0, 1, 5, 1024, 10*1024*1024));
|
|
|
|
|
|
|
|
TEST_P(OnDiskBlockCreateSizeTest, OnDiskSizeIsCorrect) {
|
2015-04-25 02:48:41 +02:00
|
|
|
Data fileContent = Data::LoadFromFile(file.path()).value();
|
2014-12-09 17:19:59 +01:00
|
|
|
EXPECT_EQ(GetParam(), fileContent.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_P(OnDiskBlockCreateSizeTest, OnDiskBlockIsZeroedOut) {
|
2015-04-25 02:48:41 +02:00
|
|
|
Data fileContent = Data::LoadFromFile(file.path()).value();
|
2015-04-25 17:17:06 +02:00
|
|
|
EXPECT_EQ(ZEROES, fileContent);
|
2014-12-09 17:19:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// This test is also tested by OnDiskBlockStoreTest, but there the block is created using the BlockStore interface.
|
|
|
|
// Here, we create it using OnDiskBlock::CreateOnDisk()
|
|
|
|
TEST_P(OnDiskBlockCreateSizeTest, InMemorySizeIsCorrect) {
|
|
|
|
EXPECT_EQ(GetParam(), block->size());
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test is also tested by OnDiskBlockStoreTest, but there the block is created using the BlockStore interface.
|
|
|
|
// Here, we create it using OnDiskBlock::CreateOnDisk()
|
|
|
|
TEST_P(OnDiskBlockCreateSizeTest, InMemoryBlockIsZeroedOut) {
|
|
|
|
EXPECT_EQ(0, std::memcmp(ZEROES.data(), block->data(), block->size()));
|
|
|
|
}
|