blob: c29f8340c85997f6dfbb11f4186614251959ad45 [file] [log] [blame]
Yingdi Yu0c3e5912015-03-17 14:22:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07003 * Copyright (c) 2014-2017, Regents of the University of California
Yingdi Yu0c3e5912015-03-17 14:22:38 -07004 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07005 * This file is part of NDN DeLorean, An Authentication System for Data Archives in
6 * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors
7 * and contributors.
Yingdi Yu0c3e5912015-03-17 14:22:38 -07008 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -07009 * NDN DeLorean is free software: you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
Yingdi Yu0c3e5912015-03-17 14:22:38 -070013 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070014 * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
Yingdi Yu0c3e5912015-03-17 14:22:38 -070017 *
Alexander Afanasyevbe998ac2017-05-06 13:11:42 -070018 * You should have received a copy of the GNU General Public License along with NDN
19 * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yingdi Yu0c3e5912015-03-17 14:22:38 -070020 */
21
22#include "sub-tree-binary.hpp"
23
24#include <ndn-cxx/encoding/buffer-stream.hpp>
25#include <ndn-cxx/util/digest.hpp>
26#include "boost-test.hpp"
27
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070028namespace ndn {
29namespace delorean {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070030namespace tests {
31
32class SubTreeBinaryTestFixture
33{
34public:
35 NonNegativeInteger nextSeqNo;
36 NonNegativeInteger seqNoCount;
37
38 size_t nCompleteCalls;
39 size_t nUpdateCalls;
40
41 ndn::ConstBufferPtr eventualHash;
42};
43
44BOOST_FIXTURE_TEST_SUITE(TestSubTreeBinary, SubTreeBinaryTestFixture)
45
46ndn::ConstBufferPtr
47getTestHashRoot(const Node::Index& idx)
48{
49 if (idx.level == 0)
50 return Node::getEmptyHash();
51
52 auto hash1 = getTestHashRoot(Node::Index(idx.seqNo, idx.level - 1));
53 auto hash2 = getTestHashRoot(Node::Index(idx.seqNo + (idx.range >> 1), idx.level - 1));
54
55 ndn::util::Sha256 sha256;
56 sha256 << idx.level << idx.seqNo;
57 sha256.update(hash1->buf(), hash1->size());
58 sha256.update(hash2->buf(), hash2->size());
59
60 return sha256.computeDigest();
61}
62
63void
64printHex(const uint8_t* buf, size_t size)
65{
66 using namespace CryptoPP;
67 StringSource ss(buf, size, true, new HexEncoder(new FileSink(std::cerr), false));
68 std::cerr << std::endl;
69}
70
71void
72printByte(const uint8_t* buf, size_t size)
73{
74 std::stringstream ss;
75 using namespace CryptoPP;
76 StringSource is(buf, size, true, new HexEncoder(new FileSink(ss), false));
77
78 std::string output = ss.str();
79 for (size_t i = 0; i < output.size(); i++) {
80 std::cerr << "0x" << output.at(i);
81 std::cerr << output.at(++i) << ", ";
82 if ((i + 1) % 32 == 0)
83 std::cerr << std::endl;
84 }
85}
86
87
88BOOST_AUTO_TEST_CASE(BasicTest1)
89{
90 nextSeqNo = 0;
91 seqNoCount = 0;
92 nCompleteCalls = 0;
93 nUpdateCalls = 0;
94
95 Name loggerName("/logger/name");
96
97 Node::Index idx(0, 5);
98 SubTreeBinary subTree(loggerName,
99 idx,
100 [&] (const Node::Index& index) {
101 BOOST_CHECK_EQUAL(this->seqNoCount, idx.range);
102 this->nCompleteCalls++;
103 },
104 [&] (const Node::Index&,
105 const NonNegativeInteger& seqNo,
106 ndn::ConstBufferPtr hash) {
107 BOOST_CHECK_EQUAL(this->nextSeqNo, seqNo);
108 this->nUpdateCalls++;
109 this->eventualHash = hash;
110 });
111
112 BOOST_CHECK(subTree.getPeakIndex() == idx);
113 BOOST_CHECK_EQUAL(subTree.getMinSeqNo(), 0);
114 BOOST_CHECK_EQUAL(subTree.getMaxSeqNo(), 32);
115 BOOST_CHECK_EQUAL(subTree.getLeafLevel(), 0);
116 BOOST_CHECK_EQUAL(subTree.getNextLeafSeqNo(), 0);
117
118 for (int i = 0; i < 32; i++) {
119 seqNoCount++;
120 nextSeqNo++;
121 BOOST_CHECK_EQUAL(subTree.isFull(), false);
122 auto node = make_shared<Node>(i, 0, i + 1, Node::getEmptyHash());
123 BOOST_CHECK(subTree.addLeaf(node));
124 BOOST_CHECK_EQUAL(subTree.getNextLeafSeqNo(), i + 1);
125 }
126 BOOST_CHECK_EQUAL(subTree.isFull(), true);
127
128 BOOST_CHECK_EQUAL(nCompleteCalls, 1);
129 BOOST_CHECK_EQUAL(nUpdateCalls, 32);
130
131 auto actualHash = subTree.getRoot()->getHash();
132 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
133 eventualHash->begin(), eventualHash->end());
134
135 {
136 using namespace CryptoPP;
137
138 ndn::OBufferStream os;
139 std::string rootHash("989551ef13ce660c1c5ccdda770f4769966a6faf83722c91dfeac597c6fa2782");
140 StringSource ss(reinterpret_cast<const uint8_t*>(rootHash.c_str()), rootHash.size(),
141 true, new HexDecoder(new FileSink(os)));
142 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
143 os.buf()->begin(), os.buf()->end());
144 }
145
146}
147
148BOOST_AUTO_TEST_CASE(BasicTest2)
149{
150 nextSeqNo = 32;
151 seqNoCount = 0;
152 nCompleteCalls = 0;
153 nUpdateCalls = 0;
154
155 Name loggerName("/logger/name");
156
157 Node::Index idx(32, 5);
158 SubTreeBinary subTree(loggerName,
159 idx,
160 [&] (const Node::Index& index) {
161 BOOST_CHECK_EQUAL(this->seqNoCount, idx.range);
162 this->nCompleteCalls++;
163 },
164 [&] (const Node::Index&,
165 const NonNegativeInteger& seqNo,
166 ndn::ConstBufferPtr hash) {
167 BOOST_CHECK(this->nextSeqNo >= (1 << (idx.level - 1)));
168 BOOST_CHECK_EQUAL(this->nextSeqNo, seqNo);
169 this->nUpdateCalls++;
170 this->eventualHash = hash;
171 });
172
173 BOOST_CHECK(subTree.getPeakIndex() == idx);
174 BOOST_CHECK_EQUAL(subTree.getMinSeqNo(), 32);
175 BOOST_CHECK_EQUAL(subTree.getMaxSeqNo(), 64);
176 BOOST_CHECK_EQUAL(subTree.getLeafLevel(), 0);
177 BOOST_CHECK_EQUAL(subTree.getNextLeafSeqNo(), 32);
178
179 for (int i = 32; i < 64; i++) {
180 seqNoCount++;
181 nextSeqNo++;
182 BOOST_CHECK_EQUAL(subTree.isFull(), false);
183 auto node = make_shared<Node>(i, 0, i + 1, Node::getEmptyHash());
184 BOOST_CHECK(subTree.addLeaf(node));
185 BOOST_CHECK_EQUAL(subTree.getNextLeafSeqNo(), i + 1);
186 }
187 BOOST_CHECK_EQUAL(subTree.isFull(), true);
188
189 BOOST_CHECK_EQUAL(nCompleteCalls, 1);
190 BOOST_CHECK_EQUAL(nUpdateCalls, 32);
191
192 auto actualHash = subTree.getRoot()->getHash();
193 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
194 eventualHash->begin(), eventualHash->end());
195
196 {
197 using namespace CryptoPP;
198
199 ndn::OBufferStream os;
200 std::string rootHash("2657cd81c3acb8eb4489f0a2559d42532644ce737ae494f49f30452f47bcff53");
201 StringSource ss(reinterpret_cast<const uint8_t*>(rootHash.c_str()), rootHash.size(),
202 true, new HexDecoder(new FileSink(os)));
203 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
204 os.buf()->begin(), os.buf()->end());
205 }
206}
207
208BOOST_AUTO_TEST_CASE(BasicTest3)
209{
210 nextSeqNo = 0;
211 seqNoCount = 0;
212 nCompleteCalls = 0;
213 nUpdateCalls = 0;
214
215 Name loggerName("/logger/name");
216
217 Node::Index idx(0, 10);
218 SubTreeBinary subTree(loggerName,
219 idx,
220 [&] (const Node::Index& index) {
221 BOOST_CHECK_EQUAL(this->seqNoCount, 32);
222 this->nCompleteCalls++;
223 },
224 [&] (const Node::Index&,
225 const NonNegativeInteger& seqNo,
226 ndn::ConstBufferPtr hash) {
227 BOOST_CHECK_EQUAL(this->nextSeqNo, seqNo);
228 this->nUpdateCalls++;
229 this->eventualHash = hash;
230 });
231
232 BOOST_CHECK(subTree.getPeakIndex() == idx);
233 BOOST_CHECK_EQUAL(subTree.getMinSeqNo(), 0);
234 BOOST_CHECK_EQUAL(subTree.getMaxSeqNo(), 1024);
235 BOOST_CHECK_EQUAL(subTree.getLeafLevel(), 5);
236 BOOST_CHECK_EQUAL(subTree.getNextLeafSeqNo(), 0);
237
238 for (int i = 0; i < 1024; i += 32) {
239 seqNoCount++;
240 nextSeqNo += 32;
241 BOOST_CHECK_EQUAL(subTree.isFull(), false);
242 auto node = make_shared<Node>(i, 5, i + 32, getTestHashRoot(Node::Index(i, 5)));
243 BOOST_CHECK(subTree.addLeaf(node));
244 BOOST_CHECK_EQUAL(subTree.getNextLeafSeqNo(), i + 32);
245 }
246 BOOST_CHECK_EQUAL(subTree.isFull(), true);
247
248 BOOST_CHECK_EQUAL(nCompleteCalls, 1);
249 BOOST_CHECK_EQUAL(nUpdateCalls, 32);
250
251 auto actualHash = subTree.getRoot()->getHash();
252 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
253 eventualHash->begin(), eventualHash->end());
254
255 {
256 using namespace CryptoPP;
257
258 ndn::OBufferStream os;
259 std::string rootHash("dc138a319c197bc4ede89902ed9b46e4e17d732b5ace9fa3b8a398db5edb1e36");
260 StringSource ss(reinterpret_cast<const uint8_t*>(rootHash.c_str()), rootHash.size(),
261 true, new HexDecoder(new FileSink(os)));
262 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
263 os.buf()->begin(), os.buf()->end());
264 }
265}
266
267BOOST_AUTO_TEST_CASE(AddLeaf1)
268{
269 Name loggerName("/logger/name");
270
271 Node::Index idx(0, 10);
272 SubTreeBinary subTree(loggerName,
273 idx,
274 [&] (const Node::Index&) {},
275 [&] (const Node::Index&,
276 const NonNegativeInteger&,
277 ndn::ConstBufferPtr) {});
278
279 auto node_0_5 = make_shared<Node>(0, 5, 32, getTestHashRoot(Node::Index(0, 5)));
280 auto node_32_5 = make_shared<Node>(32, 5, 64, getTestHashRoot(Node::Index(32, 5)));
281 auto node_64_5 = make_shared<Node>(64, 5, 96, getTestHashRoot(Node::Index(64, 5)));
282
283 Node::Index idx2(32, 5);
284 SubTreeBinary subTree2(loggerName,
285 idx2,
286 [&] (const Node::Index&) {},
287 [&] (const Node::Index&,
288 const NonNegativeInteger&,
289 ndn::ConstBufferPtr) {});
290
291 auto node_32_0 = make_shared<Node>(32, 0, 33, Node::getEmptyHash());
292 auto node_33_0 = make_shared<Node>(33, 0, 34, Node::getEmptyHash());
293 auto node_34_0 = make_shared<Node>(34, 0, 35, Node::getEmptyHash());
294 BOOST_REQUIRE(subTree2.addLeaf(node_32_0));
295 BOOST_REQUIRE(subTree2.getRoot() != nullptr);
296 BOOST_REQUIRE(subTree2.getRoot()->getHash() != nullptr);
297 auto node_32_5_33 = make_shared<Node>(32, 5, 33, subTree2.getRoot()->getHash());
298 BOOST_REQUIRE(subTree2.addLeaf(node_33_0));
299 auto node_32_5_34 = make_shared<Node>(32, 5, 34, subTree2.getRoot()->getHash());
300 BOOST_REQUIRE(subTree2.addLeaf(node_34_0));
301 auto node_32_5_35 = make_shared<Node>(32, 5, 35, subTree2.getRoot()->getHash());
302
303 BOOST_CHECK_EQUAL(subTree.addLeaf(node_32_5), false);
304 BOOST_CHECK_EQUAL(subTree.addLeaf(node_0_5), true);
305 BOOST_CHECK_EQUAL(subTree.addLeaf(node_32_5_33), true);
306 BOOST_CHECK_EQUAL(subTree.updateLeaf(34, node_32_5_34->getHash()), true);
307 BOOST_CHECK_EQUAL(subTree.updateLeaf(35, node_32_5_35->getHash()), true);
308 BOOST_CHECK_EQUAL(subTree.addLeaf(node_32_5), false);
309 BOOST_CHECK_EQUAL(subTree.addLeaf(node_64_5), false);
310 BOOST_CHECK_EQUAL(subTree.updateLeaf(64, node_32_5->getHash()), true);
311 BOOST_CHECK_EQUAL(subTree.addLeaf(node_64_5), true);
312
313 for (int i = 96; i < 1024; i += 32) {
314 BOOST_CHECK_EQUAL(subTree.isFull(), false);
315 auto node = make_shared<Node>(i, 5, i + 32, getTestHashRoot(Node::Index(i, 5)));
316 BOOST_CHECK(subTree.addLeaf(node));
317 }
318 BOOST_CHECK_EQUAL(subTree.isFull(), true);
319
320 auto actualHash = subTree.getRoot()->getHash();
321 {
322 using namespace CryptoPP;
323
324 ndn::OBufferStream os;
325 std::string rootHash("dc138a319c197bc4ede89902ed9b46e4e17d732b5ace9fa3b8a398db5edb1e36");
326 StringSource ss(reinterpret_cast<const uint8_t*>(rootHash.c_str()), rootHash.size(),
327 true, new HexDecoder(new FileSink(os)));
328 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
329 os.buf()->begin(), os.buf()->end());
330 }
331}
332
333
334uint8_t SUBTREE_DATA[] = {
335 0x06, 0xfd, 0x04, 0x6f, // Data
336 0x07, 0x40, // Name /logger/name/5/0/complete/....
337 0x08, 0x06, 0x6c, 0x6f, 0x67, 0x67, 0x65, 0x72,
338 0x08, 0x04, 0x6e, 0x61, 0x6d, 0x65,
339 0x08, 0x01, 0x05,
340 0x08, 0x01, 0x00,
341 0x08, 0x08, 0x63, 0x6f, 0x6d, 0x70, 0x6c, 0x65, 0x74, 0x65,
342 0x08, 0x20,
343 0x98, 0x95, 0x51, 0xef, 0x13, 0xce, 0x66, 0x0c,
344 0x1c, 0x5c, 0xcd, 0xda, 0x77, 0x0f, 0x47, 0x69,
345 0x96, 0x6a, 0x6f, 0xaf, 0x83, 0x72, 0x2c, 0x91,
346 0xdf, 0xea, 0xc5, 0x97, 0xc6, 0xfa, 0x27, 0x82,
347 0x14, 0x00, // MetaInfo
348 0x15, 0xfd, 0x04, 0x00, // Content
349 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
350 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
351 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
352 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
353 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
354 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
355 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
356 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
357 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
358 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
359 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
360 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
361 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
362 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
363 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
364 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
365 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
366 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
367 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
368 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
369 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
370 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
371 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
372 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
373 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
374 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
375 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
376 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
377 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
378 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
379 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
380 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
381 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
382 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
383 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
384 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
385 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
386 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
387 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
388 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
389 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
390 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
391 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
392 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
393 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
394 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
395 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
396 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
397 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
398 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
399 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
400 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
401 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
402 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
403 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
404 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
405 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
406 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
407 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
408 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
409 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
410 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
411 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
412 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
413 0x16, 0x03, 0x1b, 0x01, 0x00, // SigInfo
414 0x17, 0x20, // SigValue
415 0x2d, 0xda, 0xd1, 0xd3, 0x25, 0xd1, 0x7d, 0xf5, 0x64, 0xab, 0x58, 0x74, 0x3a, 0x01, 0xb9, 0x31,
416 0x52, 0xcd, 0x55, 0xd2, 0xce, 0xea, 0xbc, 0x7c, 0x1a, 0x61, 0xe4, 0x7e, 0xff, 0x4a, 0x1f, 0xe7
417};
418
419BOOST_AUTO_TEST_CASE(Encoding1)
420{
421 Name loggerName("/logger/name");
422
423 Node::Index idx(0, 5);
424 SubTreeBinary subTree(loggerName,
425 idx,
426 [&] (const Node::Index&) {},
427 [&] (const Node::Index&,
428 const NonNegativeInteger&,
429 ndn::ConstBufferPtr) {});
430
431 for (int i = 0; i < 32; i++) {
432 auto node = make_shared<Node>(i, 0, i + 1, Node::getEmptyHash());
433 subTree.addLeaf(node);
434 }
435
436 shared_ptr<Data> data = subTree.encode();
437 BOOST_REQUIRE(data != nullptr);
438
439 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().wire(),
440 data->wireEncode().wire() + data->wireEncode().size(),
441 SUBTREE_DATA,
442 SUBTREE_DATA + sizeof(SUBTREE_DATA));
443}
444
445BOOST_AUTO_TEST_CASE(Decoding1)
446{
447 Name loggerName("/logger/name");
448 SubTreeBinary subtree(loggerName,
449 [&] (const Node::Index&) {},
450 [&] (const Node::Index&,
451 const NonNegativeInteger&,
452 ndn::ConstBufferPtr) {});
453
454 Block block(SUBTREE_DATA, sizeof(SUBTREE_DATA));
455 Data data(block);
456
457 BOOST_REQUIRE_NO_THROW(subtree.decode(data));
458}
459
460uint8_t SUBTREE_DATA2[] = {
461 0x06, 0xaa, // Data
462 0x07, 0x39, // Name /logger/name/6/0/.../35
463 0x08, 0x06, 0x6c, 0x6f, 0x67, 0x67, 0x65, 0x72,
464 0x08, 0x04, 0x6e, 0x61, 0x6d, 0x65,
465 0x08, 0x01, 0x06,
466 0x08, 0x01, 0x00,
467 0x08, 0x01, 0x23,
468 0x08, 0x20,
469 0x44, 0xb2, 0x25, 0x95, 0x79, 0x99, 0x8c, 0xd7,
470 0xd9, 0x56, 0xc5, 0x22, 0x32, 0x53, 0xd0, 0x7f,
471 0xf0, 0x09, 0x12, 0xd2, 0x17, 0x54, 0x81, 0x79,
472 0xfc, 0xad, 0x40, 0x2f, 0x86, 0x0e, 0xa2, 0xef,
473 0x14, 0x04, // MetaInfo
474 0x19, 0x02, 0xea, 0x60, // 60000 ms
475 0x15, 0x40, // Content
476 0x98, 0x95, 0x51, 0xef, 0x13, 0xce, 0x66, 0x0c, 0x1c, 0x5c, 0xcd, 0xda, 0x77, 0x0f, 0x47, 0x69,
477 0x96, 0x6a, 0x6f, 0xaf, 0x83, 0x72, 0x2c, 0x91, 0xdf, 0xea, 0xc5, 0x97, 0xc6, 0xfa, 0x27, 0x82,
478 0xf8, 0x30, 0x5d, 0x94, 0xfa, 0x23, 0xe2, 0x49, 0x08, 0x73, 0x5a, 0xc2, 0x22, 0x34, 0xa1, 0xfd,
479 0xc4, 0x46, 0xec, 0x07, 0x7c, 0x6c, 0xa2, 0x7e, 0x51, 0x70, 0x68, 0xa9, 0xbb, 0xc6, 0x56, 0x89,
480 0x16, 0x03, // SigInfo
481 0x1b, 0x01, 0x00,
482 0x17, 0x20, // SigValue
483 0xad, 0x00, 0xce, 0x0b, 0x31, 0x06, 0x9d, 0xee, 0x90, 0x28, 0x03, 0xbe, 0x3f, 0xcc, 0x0a, 0xd6,
484 0x1b, 0x3e, 0xf6, 0x26, 0x07, 0x63, 0x9b, 0xdf, 0xb9, 0x5e, 0x82, 0xd4, 0xb0, 0xce, 0xc0, 0x9f
485};
486
487BOOST_AUTO_TEST_CASE(Encoding2)
488{
489 Name loggerName("/logger/name");
490
491 Node::Index idx(0, 10);
492 SubTreeBinary subTree(loggerName,
493 idx,
494 [&] (const Node::Index&) {},
495 [&] (const Node::Index&,
496 const NonNegativeInteger&,
497 ndn::ConstBufferPtr) {});
498
499 auto node_0_5 = make_shared<Node>(0, 5, 32, getTestHashRoot(Node::Index(0, 5)));
500 auto node_32_5 = make_shared<Node>(32, 5, 64, getTestHashRoot(Node::Index(32, 5)));
501 auto node_64_5 = make_shared<Node>(64, 5, 96, getTestHashRoot(Node::Index(64, 5)));
502
503 Node::Index idx2(32, 5);
504 SubTreeBinary subTree2(loggerName,
505 idx2,
506 [&] (const Node::Index&) {},
507 [&] (const Node::Index&,
508 const NonNegativeInteger&,
509 ndn::ConstBufferPtr) {});
510
511 auto node_32_0 = make_shared<Node>(32, 0, 33, Node::getEmptyHash());
512 auto node_33_0 = make_shared<Node>(33, 0, 34, Node::getEmptyHash());
513 auto node_34_0 = make_shared<Node>(34, 0, 35, Node::getEmptyHash());
514 BOOST_REQUIRE(subTree2.addLeaf(node_32_0));
515 BOOST_REQUIRE(subTree2.getRoot() != nullptr);
516 BOOST_REQUIRE(subTree2.getRoot()->getHash() != nullptr);
517 auto node_32_5_33 = make_shared<Node>(32, 5, 33, subTree2.getRoot()->getHash());
518 BOOST_REQUIRE(subTree2.addLeaf(node_33_0));
519 auto node_32_5_34 = make_shared<Node>(32, 5, 34, subTree2.getRoot()->getHash());
520 BOOST_REQUIRE(subTree2.addLeaf(node_34_0));
521 auto node_32_5_35 = make_shared<Node>(32, 5, 35, subTree2.getRoot()->getHash());
522
523 BOOST_CHECK_EQUAL(subTree.addLeaf(node_32_5), false);
524 BOOST_CHECK_EQUAL(subTree.addLeaf(node_0_5), true);
525 BOOST_CHECK_EQUAL(subTree.addLeaf(node_32_5_33), true);
526 BOOST_CHECK_EQUAL(subTree.updateLeaf(34, node_32_5_34->getHash()), true);
527 BOOST_CHECK_EQUAL(subTree.updateLeaf(35, node_32_5_35->getHash()), true);
528
529 shared_ptr<Data> data = subTree.encode();
530 BOOST_REQUIRE(data != nullptr);
531
532 BOOST_CHECK_EQUAL(data->getName().get(SubTreeBinary::OFFSET_COMPLETE).toNumber(), 35);
533 BOOST_CHECK_EQUAL(data->getFreshnessPeriod(), time::milliseconds(60000));
534 BOOST_CHECK_EQUAL(data->getContent().value_size(), 32 * 2);
535
536 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().wire(),
537 data->wireEncode().wire() + data->wireEncode().size(),
538 SUBTREE_DATA2,
539 SUBTREE_DATA2 + sizeof(SUBTREE_DATA2));
540}
541
542BOOST_AUTO_TEST_CASE(Decoding2)
543{
544 Name loggerName("/logger/name");
545 SubTreeBinary subTree(loggerName,
546 [&] (const Node::Index&) {},
547 [&] (const Node::Index&,
548 const NonNegativeInteger&,
549 ndn::ConstBufferPtr) {});
550
551 Block block(SUBTREE_DATA2, sizeof(SUBTREE_DATA2));
552 Data data(block);
553
554 BOOST_REQUIRE_NO_THROW(subTree.decode(data));
555
556 auto node_32_5 = make_shared<Node>(32, 5, 64, getTestHashRoot(Node::Index(32, 5)));
557 BOOST_CHECK_EQUAL(subTree.updateLeaf(64, node_32_5->getHash()), true);
558
559 for (int i = 64; i < 1024; i += 32) {
560 BOOST_CHECK_EQUAL(subTree.isFull(), false);
561 auto node = make_shared<Node>(i, 5, i + 32, getTestHashRoot(Node::Index(i, 5)));
562 BOOST_CHECK(subTree.addLeaf(node));
563 }
564 BOOST_CHECK_EQUAL(subTree.isFull(), true);
565
566 auto actualHash = subTree.getRoot()->getHash();
567 {
568 using namespace CryptoPP;
569
570 ndn::OBufferStream os;
571 std::string rootHash("dc138a319c197bc4ede89902ed9b46e4e17d732b5ace9fa3b8a398db5edb1e36");
572 StringSource ss(reinterpret_cast<const uint8_t*>(rootHash.c_str()), rootHash.size(),
573 true, new HexDecoder(new FileSink(os)));
574 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
575 os.buf()->begin(), os.buf()->end());
576 }
577}
578
579uint8_t SUBTREE_DATA3[] = {
580 0x06, 0x69,
581 0x07, 0x39,
582 0x08, 0x06, 0x6c, 0x6f, 0x67, 0x67, 0x65, 0x72,
583 0x08, 0x04, 0x6e, 0x61, 0x6d, 0x65,
584 0x08, 0x01, 0x05,
585 0x08, 0x01, 0x00,
586 0x08, 0x01, 0x00,
587 0x08, 0x20,
588 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
589 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
590 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
591 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55,
592 0x14, 0x03,
593 0x19, 0x01, 0x00,
594 0x15, 0x00,
595 0x16, 0x03,
596 0x1b, 0x01, 0x00,
597 0x17, 0x20,
598 0x42, 0x3d, 0x4b, 0xb2, 0xe8, 0x24, 0xd3, 0xf6, 0xb7, 0x20, 0x69, 0x8f, 0x70, 0xb3, 0x9f, 0xfb,
599 0xdf, 0x71, 0x05, 0xdd, 0xcf, 0xdc, 0x4d, 0x08, 0xbb, 0x22, 0x2e, 0x89, 0x1a, 0x81, 0xef, 0xce
600};
601
602BOOST_AUTO_TEST_CASE(Encoding3)
603{
604 Name loggerName("/logger/name");
605
606 Node::Index idx(0, 5);
607 SubTreeBinary subTree(loggerName,
608 idx,
609 [&] (const Node::Index&) {},
610 [&] (const Node::Index&,
611 const NonNegativeInteger&,
612 ndn::ConstBufferPtr) {});
613
614 shared_ptr<Data> data = subTree.encode();
615 BOOST_REQUIRE(data != nullptr);
616
617 BOOST_CHECK_EQUAL(data->getName().get(SubTreeBinary::OFFSET_COMPLETE).toNumber(), 0);
618 BOOST_CHECK_EQUAL(data->getFreshnessPeriod(), time::milliseconds(0));
619 BOOST_CHECK_EQUAL(data->getContent().value_size(), 0);
620
621 BOOST_CHECK_EQUAL_COLLECTIONS(data->wireEncode().wire(),
622 data->wireEncode().wire() + data->wireEncode().size(),
623 SUBTREE_DATA3,
624 SUBTREE_DATA3 + sizeof(SUBTREE_DATA3));
625}
626
627BOOST_AUTO_TEST_CASE(Decoding3)
628{
629 Name loggerName("/logger/name");
630 SubTreeBinary subTree(loggerName,
631 [&] (const Node::Index&) {},
632 [&] (const Node::Index&,
633 const NonNegativeInteger&,
634 ndn::ConstBufferPtr) {});
635
636 Block block(SUBTREE_DATA3, sizeof(SUBTREE_DATA3));
637 Data data(block);
638
639 try {
640 subTree.decode(data);
641 }
642 catch (std::runtime_error& e) {
643 std::cerr << e.what() << std::endl;
644 }
645
646 BOOST_REQUIRE_NO_THROW(subTree.decode(data));
647 BOOST_CHECK(subTree.getRoot() == nullptr);
648 BOOST_CHECK(subTree.getPeakIndex() == Node::Index(0, 5));
649 BOOST_CHECK_EQUAL(subTree.getLeafLevel(), 0);
650 BOOST_CHECK_EQUAL(subTree.isFull(), false);
651
652 for (int i = 0; i < 32; i ++) {
653 BOOST_CHECK_EQUAL(subTree.isFull(), false);
654 auto node = make_shared<Node>(i, 0, i + 1, Node::getEmptyHash());
655 BOOST_CHECK(subTree.addLeaf(node));
656 }
657 BOOST_CHECK_EQUAL(subTree.isFull(), true);
658
659 auto actualHash = subTree.getRoot()->getHash();
660 {
661 using namespace CryptoPP;
662
663 ndn::OBufferStream os;
664 std::string rootHash("989551ef13ce660c1c5ccdda770f4769966a6faf83722c91dfeac597c6fa2782");
665 StringSource ss(reinterpret_cast<const uint8_t*>(rootHash.c_str()), rootHash.size(),
666 true, new HexDecoder(new FileSink(os)));
667 BOOST_CHECK_EQUAL_COLLECTIONS(actualHash->begin(), actualHash->end(),
668 os.buf()->begin(), os.buf()->end());
669 }
670}
671
672BOOST_AUTO_TEST_CASE(SubTreePeakIndexConvert)
673{
674 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(0, 0)) == Node::Index(0, 5));
675 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(0, 1)) == Node::Index(0, 5));
676 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(0, 5), false) == Node::Index(0, 5));
677 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(0, 5)) == Node::Index(0, 10));
678 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(1, 0)) == Node::Index(0, 5));
679 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(2, 1)) == Node::Index(0, 5));
680
681 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(32, 0)) == Node::Index(32, 5));
682 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(32, 1)) == Node::Index(32, 5));
683 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(32, 5), false) == Node::Index(32, 5));
684 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(32, 5)) == Node::Index(0, 10));
685 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(33, 0)) == Node::Index(32, 5));
686 BOOST_CHECK(SubTreeBinary::toSubTreePeakIndex(Node::Index(34, 1)) == Node::Index(32, 5));
687}
688
689
690BOOST_AUTO_TEST_SUITE_END()
691
692} // namespace tests
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700693} // namespace delorean
694} // namespace ndn