File: OnDiskBlockCreateTest.cpp

package info (click to toggle)
cryfs 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,412 kB
  • sloc: cpp: 150,187; asm: 10,493; python: 1,455; javascript: 65; sh: 50; makefile: 17; xml: 7
file content (88 lines) | stat: -rw-r--r-- 3,052 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <gtest/gtest.h>

#include <cpp-utils/tempfile/TempFile.h>
#include <cpp-utils/tempfile/TempDir.h>

// TODO This should be ported to BlockStore2

/*
using ::testing::Test;
using ::testing::WithParamInterface;
using ::testing::Values;
using cpputils::Data;
using cpputils::TempFile;
using cpputils::TempDir;
using cpputils::unique_ref;

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)
  : dir(),
    key(BlockId::FromString("1491BB4932A389EE14BC7090AC772972")),
    file(dir.path() / blockId.ToString().substr(0,3) / blockId.ToString().substr(3), false) {
  }
  TempDir dir;
  BlockId key;
  TempFile file;
};

TEST_F(OnDiskBlockCreateTest, CreatingBlockCreatesFile) {
  EXPECT_FALSE(bf::exists(file.path()));

  auto block = OnDiskBlock::CreateOnDisk(dir.path(), blockId, Data(0));

  EXPECT_TRUE(bf::exists(file.path()));
  EXPECT_TRUE(bf::is_regular_file(file.path()));
}

TEST_F(OnDiskBlockCreateTest, CreatingExistingBlockReturnsNull) {
  auto block1 = OnDiskBlock::CreateOnDisk(dir.path(), blockId, Data(0));
  auto block2 = OnDiskBlock::CreateOnDisk(dir.path(), blockId, Data(0));
  EXPECT_TRUE((bool)block1);
  EXPECT_FALSE((bool)block2);
}

class OnDiskBlockCreateSizeTest: public OnDiskBlockCreateTest, public WithParamInterface<size_t> {
public:
  unique_ref<OnDiskBlock> block;
  Data ZEROES;

  OnDiskBlockCreateSizeTest():
    block(OnDiskBlock::CreateOnDisk(dir.path(), blockId, Data(GetParam()).FillWithZeroes()).value()),
    ZEROES(block->size())
  {
    ZEROES.FillWithZeroes();
  }
};
INSTANTIATE_TEST_SUITE_P(OnDiskBlockCreateSizeTest, OnDiskBlockCreateSizeTest, Values(0, 1, 5, 1024, 10*1024*1024));

TEST_P(OnDiskBlockCreateSizeTest, OnDiskSizeIsCorrect) {
  Data fileContent = Data::LoadFromFile(file.path()).value();
  EXPECT_EQ(GetParam() + OnDiskBlock::formatVersionHeaderSize(), fileContent.size());
}

TEST_P(OnDiskBlockCreateSizeTest, OnDiskBlockIsZeroedOut) {
  Data fileContent = Data::LoadFromFile(file.path()).value();
  Data fileContentWithoutHeader(fileContent.size() - OnDiskBlock::formatVersionHeaderSize());
  std::memcpy(fileContentWithoutHeader.data(), fileContent.dataOffset(OnDiskBlock::formatVersionHeaderSize()), fileContentWithoutHeader.size());
  EXPECT_EQ(ZEROES, fileContentWithoutHeader);
}

// 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()));
}
*/