File: chunk_signature_unittest.cc

package info (click to toggle)
lizardfs 3.12.0+dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 8,064 kB
  • sloc: cpp: 91,899; sh: 9,341; python: 3,878; ansic: 3,109; pascal: 128; makefile: 57
file content (130 lines) | stat: -rw-r--r-- 5,566 bytes parent folder | download | duplicates (5)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
   Copyright 2013-2016 Skytechnology sp. z o.o.

   This file is part of LizardFS.

   LizardFS is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, version 3.

   LizardFS is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with LizardFS. If not, see <http://www.gnu.org/licenses/>.
 */

#include "common/platform.h"
#include "chunkserver/chunk_signature.h"

#include <fcntl.h>
#include <unistd.h>
#include <fstream>
#include <string>
#include <gtest/gtest.h>

#include "common/chunk_part_type.h"
#include "common/slice_traits.h"
#include "unittests/chunk_type_constants.h"
#include "unittests/TemporaryDirectory.h"

TEST(ChunkSignatureTests, ReadingOldSignatureFromFile) {
	const size_t signatureOffset = 5;
	const uint64_t chunkId = 0x0102030405060708;
	const uint32_t version = 0x04030201;
	const uint8_t chunkTypeId = legacy::ChunkPartType(slice_traits::xors::getSliceType(4), 1).getId();

	// For the data listed above the contents should look like this:
	std::vector<uint8_t> chunkFileContents = {
		0, 1, 2, 3, 4,                          // 5 (headerOffset) bytes of garbage
		'L', 'I', 'Z', 'C', ' ', '1', '.', '0', // signature = LIZC 1.0
		1, 2, 3, 4, 5, 6, 7, 8,                 // id        = 0x0102030405060708
		4, 3, 2, 1,                             // version   = 0x04030201
		chunkTypeId,                            // legacy type ID
		0                                       // padding
	};

	// Create a file
	TemporaryDirectory temp("/tmp", this->test_info_->name());
	std::string chunkFileName(temp.name() + "/" + "chunk");
	std::ofstream file(chunkFileName);
	ASSERT_TRUE((bool)file) << "Cannot create a file " << chunkFileName;
	file.write(reinterpret_cast<const char*>(chunkFileContents.data()), chunkFileContents.size());
	file.close();
	ASSERT_TRUE((bool)file) << "Cannot write data to file " << chunkFileName;

	// Open file and read header contents
	int fd = open(chunkFileName.c_str(), O_RDONLY);
	ASSERT_NE(fd, -1) << "Cannot open file " << chunkFileName << " after creating it";

	ChunkSignature chunkSignature;
	ASSERT_TRUE(chunkSignature.readFromDescriptor(fd, signatureOffset)) << "Cannot read signature";
	ASSERT_TRUE(chunkSignature.hasValidSignatureId());
	ASSERT_EQ(chunkId, chunkSignature.chunkId());
	ASSERT_EQ(version, chunkSignature.chunkVersion());
	ASSERT_EQ(slice_traits::xors::ChunkPartType(4, 1), chunkSignature.chunkType());
}


TEST(ChunkSignatureTests, ReadingFromFile) {
	const size_t signatureOffset = 5;
	const uint64_t chunkId = 0x0102030405060708;
	const uint32_t version = 0x04030201;
	const uint16_t chunkTypeId = slice_traits::xors::ChunkPartType(3, 1).getId();

	// For the data listed above the contents should look like this:
	std::vector<uint8_t> chunkFileContents = {
		0, 1, 2, 3, 4,                          // 5 (headerOffset) bytes of garbage
		'L', 'I', 'Z', 'C', ' ', '1', '.', '1', // signature = LIZC 1.1
		1, 2, 3, 4, 5, 6, 7, 8,                 // id        = 0x0102030405060708
		4, 3, 2, 1,                             // version   = 0x04030201
		(uint8_t)(chunkTypeId >> 8),            // type ID (hi byte)
		(uint8_t)(chunkTypeId & 0xFF)           // type ID (lo byte)
	};

	// Create a file
	TemporaryDirectory temp("/tmp", this->test_info_->name());
	std::string chunkFileName(temp.name() + "/" + "chunk");
	std::ofstream file(chunkFileName);
	ASSERT_TRUE((bool)file) << "Cannot create a file " << chunkFileName;
	file.write(reinterpret_cast<const char*>(chunkFileContents.data()), chunkFileContents.size());
	file.close();
	ASSERT_TRUE((bool)file) << "Cannot write data to file " << chunkFileName;

	// Open file and read header contents
	int fd = open(chunkFileName.c_str(), O_RDONLY);
	ASSERT_NE(fd, -1) << "Cannot open file " << chunkFileName << " after creating it";

	ChunkSignature chunkSignature;
	ASSERT_TRUE(chunkSignature.readFromDescriptor(fd, signatureOffset)) << "Cannot read signature";
	ASSERT_TRUE(chunkSignature.hasValidSignatureId());
	ASSERT_EQ(chunkId, chunkSignature.chunkId());
	ASSERT_EQ(version, chunkSignature.chunkVersion());
	ASSERT_EQ(chunkTypeId, chunkSignature.chunkType().getId());
}

// This test verifies if signature has proper size, because existing chunks
// created by previous versions of LizardFS have 21-byte signatures.
TEST(ChunkSignatureTests, SerializedSize) {
	ASSERT_EQ(22U, ChunkSignature(0x0102030405060708, 0x04030201, xor_1_of_3).serializedSize());
}

// This test verifies if serialized signature has proper content, because existing chunks
// created by previous versions of LizardFS already have signatures in this format
TEST(ChunkSignatureTests, Serialize) {
	// Serialize some signature
	std::vector<uint8_t> data;
	serialize(data, ChunkSignature(0x0102030405060708, 0x04030201, xor_1_of_3));

	// And test if it looks like it should look like
	std::vector<uint8_t> expectedData = {
			'L', 'I', 'Z', 'C', ' ', '1', '.', '1', // signature = LIZC 1.0
			1, 2, 3, 4, 5, 6, 7, 8,                 // id        = 0x0102030405060708
			4, 3, 2, 1,                             // version   = 0x04030201
			(uint8_t)(xor_1_of_3.getId() >> 8),     // type ID (hi byte)
			(uint8_t)(xor_1_of_3.getId() & 0xFF)    // type ID (lo byte)
	};
	ASSERT_EQ(expectedData, data);
}