blob: cc6dfda307c26489f7f4253763e26ad656aa2586 [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 "tree-generator.hpp"
23#include <ndn-cxx/util/digest.hpp>
24
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -070025namespace ndn {
26namespace delorean {
Yingdi Yu0c3e5912015-03-17 14:22:38 -070027namespace tests {
28
29const Name TreeGenerator::LOGGER_NAME("/logger/name");
30ndn::ConstBufferPtr TreeGenerator::LEAF_HASH;
31
32ndn::ConstBufferPtr
33TreeGenerator::getHash(const Node::Index& idx,
34 const NonNegativeInteger& nextLeafSeqNo,
35 bool useEmpty)
36{
37 if (idx.level == 0) {
38 if (useEmpty)
39 return Node::getEmptyHash();
40
41 return Node::getEmptyHash();
42 }
43
44 NonNegativeInteger leftChildSeqNo = idx.seqNo;
45 NonNegativeInteger rightChildSeqNo = idx.seqNo + (idx.range >> 1);
46
47 if (idx.seqNo == 0 && nextLeafSeqNo <= rightChildSeqNo) {
48 BOOST_ASSERT(false);
49 }
50
51 ndn::util::Sha256 sha256;
52 sha256 << idx.level << idx.seqNo;
53
54 auto hash1 = getHash(Node::Index(leftChildSeqNo, idx.level - 1),
55 nextLeafSeqNo,
56 useEmpty);
57 sha256.update(hash1->buf(), hash1->size());
58
59 if (nextLeafSeqNo > rightChildSeqNo) {
60 auto hash2 = getHash(Node::Index(rightChildSeqNo, idx.level - 1),
61 nextLeafSeqNo,
62 useEmpty);
63 sha256.update(hash2->buf(), hash2->size());
64 }
65 else {
66 auto hash2 = Node::getEmptyHash();
67 sha256.update(hash2->buf(), hash2->size());
68 }
69 return sha256.computeDigest();
70}
71
72shared_ptr<SubTreeBinary>
73TreeGenerator::getSubTreeBinary(const Node::Index& index,
74 const NonNegativeInteger& nextLeafSeqNo,
75 bool useEmpty)
76{
77 auto subtree = make_shared<SubTreeBinary>(LOGGER_NAME, index,
78 [&] (const Node::Index&) {},
79 [&] (const Node::Index&,
80 const NonNegativeInteger&,
81 ndn::ConstBufferPtr) {});
82
83 size_t leafLevel = index.level + 1 - SubTreeBinary::SUB_TREE_DEPTH;
84 NonNegativeInteger step = 1 << leafLevel;
85
86 for (NonNegativeInteger i = index.seqNo; i < nextLeafSeqNo - step; i += step) {
87 auto node = make_shared<Node>(i, leafLevel, i + step,
88 getHash(Node::Index(i, leafLevel),
89 i + step,
90 useEmpty));
91 subtree->addLeaf(node);
92 }
93
94 NonNegativeInteger childSeqNo = ((nextLeafSeqNo - 1) >> leafLevel) << leafLevel;
95 auto node = make_shared<Node>(childSeqNo, leafLevel, nextLeafSeqNo,
96 getHash(Node::Index(childSeqNo, leafLevel),
97 nextLeafSeqNo,
98 useEmpty));
99 subtree->addLeaf(node);
100
101 return subtree;
102}
103
104ndn::ConstBufferPtr
105TreeGenerator::getLeafHash()
106{
107 if (LEAF_HASH == nullptr) {
108 ndn::util::Sha256 sha256;
109 sha256 << 1;
110 LEAF_HASH = sha256.computeDigest();
111 }
112
113 return LEAF_HASH;
114}
115
116} // namespace tests
Alexander Afanasyev49e2e4c2017-05-06 13:42:57 -0700117} // namespace delorean
118} // namespace ndn