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