Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 1 | /* -*- 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 "merkle-tree.hpp" |
| 23 | #include "../tree-generator.hpp" |
| 24 | #include "db-fixture.hpp" |
| 25 | |
| 26 | #include <boost/mpl/list.hpp> |
| 27 | #include "boost-test.hpp" |
| 28 | |
| 29 | namespace nsl { |
| 30 | namespace tests { |
| 31 | |
| 32 | BOOST_FIXTURE_TEST_SUITE(TestMerkleTree, DbFixture) |
| 33 | |
| 34 | BOOST_AUTO_TEST_CASE(Basic) |
| 35 | { |
| 36 | MerkleTree merkleTree(TreeGenerator::LOGGER_NAME, db); |
| 37 | BOOST_CHECK_EQUAL(merkleTree.getNextLeafSeqNo(), 0); |
| 38 | BOOST_CHECK(merkleTree.getRootHash() == nullptr); |
| 39 | } |
| 40 | |
| 41 | template<NonNegativeInteger N, size_t L> |
| 42 | struct MerkleTreeTestParam |
| 43 | { |
| 44 | const NonNegativeInteger leafNo = N; |
| 45 | const size_t rootLevel = L; |
| 46 | }; |
| 47 | |
| 48 | typedef boost::mpl::list<MerkleTreeTestParam<5, 3>, |
| 49 | MerkleTreeTestParam<32, 5>, |
| 50 | MerkleTreeTestParam<33, 6>, |
| 51 | MerkleTreeTestParam<1024, 10>, |
| 52 | MerkleTreeTestParam<1025, 11>> AddLeafTestParams; |
| 53 | |
| 54 | BOOST_AUTO_TEST_CASE_TEMPLATE(AddLeaf, T, AddLeafTestParams) |
| 55 | { |
| 56 | T param; |
| 57 | |
| 58 | NonNegativeInteger leafNo = param.leafNo; |
| 59 | size_t rootLevel = param.rootLevel; |
| 60 | |
| 61 | MerkleTree merkleTree(TreeGenerator::LOGGER_NAME, db); |
| 62 | for (NonNegativeInteger i = 0; i < leafNo ; i++) { |
| 63 | BOOST_REQUIRE(merkleTree.addLeaf(i, Node::getEmptyHash())); |
| 64 | } |
| 65 | |
| 66 | auto hash1 = TreeGenerator::getHash(Node::Index(0, rootLevel), leafNo); |
| 67 | auto hash2 = merkleTree.getRootHash(); |
| 68 | |
| 69 | BOOST_REQUIRE(hash1 != nullptr); |
| 70 | BOOST_REQUIRE(hash2 != nullptr); |
| 71 | |
| 72 | BOOST_CHECK_EQUAL_COLLECTIONS(hash1->begin(), hash1->end(), hash2->begin(), hash2->end()); |
| 73 | } |
| 74 | |
| 75 | class MerkleTreeLoadTestParam1 |
| 76 | { |
| 77 | public: |
| 78 | void |
| 79 | insertData(Db& db) |
| 80 | { |
| 81 | // partial first sub-tree |
| 82 | auto subtree1 = TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 5); |
| 83 | db.insertSubTreeData(5, 0, *subtree1->encode(), false, 5); |
| 84 | } |
| 85 | |
| 86 | const NonNegativeInteger seqNo = 0; |
| 87 | const size_t level = 3; |
| 88 | const NonNegativeInteger nextLeafSeqNo = 5; |
| 89 | }; |
| 90 | |
| 91 | class MerkleTreeLoadTestParam2 |
| 92 | { |
| 93 | public: |
| 94 | void |
| 95 | insertData(Db& db) |
| 96 | { |
| 97 | // full first sub-tree |
| 98 | auto subtree1 = TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 32); |
| 99 | auto subtree1Data = subtree1->encode(); |
| 100 | db.insertSubTreeData(5, 0, *subtree1Data); |
| 101 | |
| 102 | auto subtree2 = TreeGenerator::getSubTreeBinary(Node::Index(0, 10), 32); |
| 103 | auto subtree2Data = subtree2->encode(); |
| 104 | db.insertSubTreeData(10, 0, *subtree2Data, false, 32); |
| 105 | |
| 106 | auto subtree3 = make_shared<SubTreeBinary>(TreeGenerator::LOGGER_NAME, |
| 107 | Node::Index(32, 5), |
| 108 | [&] (const Node::Index&) {}, |
| 109 | [&] (const Node::Index&, |
| 110 | const NonNegativeInteger&, |
| 111 | ndn::ConstBufferPtr) {}); |
| 112 | auto subtree3Data = subtree3->encode(); |
| 113 | |
| 114 | db.insertSubTreeData(5, 32, *subtree3Data, false, 32); |
| 115 | } |
| 116 | |
| 117 | const NonNegativeInteger seqNo = 0; |
| 118 | const size_t level = 5; |
| 119 | const NonNegativeInteger nextLeafSeqNo = 32; |
| 120 | }; |
| 121 | |
| 122 | class MerkleTreeLoadTestParam3 |
| 123 | { |
| 124 | public: |
| 125 | void |
| 126 | insertData(Db& db) |
| 127 | { |
| 128 | auto subtree1 = TreeGenerator::getSubTreeBinary(Node::Index(0, 15), 1025); |
| 129 | auto subtree1Data = subtree1->encode(); |
| 130 | db.insertSubTreeData(15, 0, *subtree1Data, false, 1025); |
| 131 | |
| 132 | auto subtree2 = TreeGenerator::getSubTreeBinary(Node::Index(1024, 10), 1025); |
| 133 | auto subtree2Data = subtree2->encode(); |
| 134 | db.insertSubTreeData(10, 1024, *subtree2Data, false, 1025); |
| 135 | |
| 136 | auto subtree3 = TreeGenerator::getSubTreeBinary(Node::Index(1024, 5), 1025); |
| 137 | auto subtree3Data = subtree3->encode(); |
| 138 | db.insertSubTreeData(5, 1024, *subtree3Data, false, 1025); |
| 139 | } |
| 140 | |
| 141 | const NonNegativeInteger seqNo = 0; |
| 142 | const size_t level = 11; |
| 143 | const NonNegativeInteger nextLeafSeqNo = 1025; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | typedef boost::mpl::list<MerkleTreeLoadTestParam1, |
| 148 | MerkleTreeLoadTestParam2, |
| 149 | MerkleTreeLoadTestParam3> DbLoadTestParams; |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE_TEMPLATE(DbLoad, T, DbLoadTestParams) |
| 152 | { |
| 153 | T param; |
| 154 | |
| 155 | param.insertData(db); |
| 156 | |
| 157 | MerkleTree merkleTree(TreeGenerator::LOGGER_NAME, db); |
| 158 | |
| 159 | auto hash1 = TreeGenerator::getHash(Node::Index(param.seqNo, param.level), param.nextLeafSeqNo); |
| 160 | auto hash2 = merkleTree.getRootHash(); |
| 161 | |
| 162 | BOOST_REQUIRE(hash1 != nullptr); |
| 163 | BOOST_REQUIRE(hash2 != nullptr); |
| 164 | |
| 165 | BOOST_CHECK_EQUAL_COLLECTIONS(hash1->begin(), hash1->end(), hash2->begin(), hash2->end()); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_CASE(DbSave1) |
| 169 | { |
| 170 | MerkleTree merkleTree(TreeGenerator::LOGGER_NAME, db); |
| 171 | for (NonNegativeInteger i = 0; i < 5 ; i++) { |
| 172 | BOOST_REQUIRE(merkleTree.addLeaf(i, Node::getEmptyHash())); |
| 173 | } |
| 174 | |
| 175 | merkleTree.savePendingTree(); |
| 176 | auto data1 = db.getPendingSubTrees()[0]; |
| 177 | auto data2 = TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 5)->encode(); |
| 178 | |
| 179 | BOOST_CHECK(data1->wireEncode() == data2->wireEncode()); |
| 180 | } |
| 181 | |
| 182 | BOOST_AUTO_TEST_CASE(DbSave2) |
| 183 | { |
| 184 | MerkleTree merkleTree(TreeGenerator::LOGGER_NAME, db); |
| 185 | for (NonNegativeInteger i = 0; i < 32 ; i++) { |
| 186 | BOOST_REQUIRE(merkleTree.addLeaf(i, Node::getEmptyHash())); |
| 187 | } |
| 188 | |
| 189 | merkleTree.savePendingTree(); |
| 190 | auto data1 = db.getPendingSubTrees()[0]; |
| 191 | auto data2 = TreeGenerator::getSubTreeBinary(Node::Index(0, 10), 32)->encode(); |
| 192 | |
| 193 | auto data3 = db.getPendingSubTrees()[1]; |
| 194 | auto subtree = make_shared<SubTreeBinary>(TreeGenerator::LOGGER_NAME, |
| 195 | Node::Index(32, 5), |
| 196 | [&] (const Node::Index&) {}, |
| 197 | [&] (const Node::Index&, |
| 198 | const NonNegativeInteger&, |
| 199 | ndn::ConstBufferPtr) {}); |
| 200 | auto data4 = subtree->encode(); |
| 201 | |
| 202 | BOOST_CHECK(data1->wireEncode() == data2->wireEncode()); |
| 203 | BOOST_CHECK(data3->wireEncode() == data4->wireEncode()); |
| 204 | |
| 205 | auto dataA = TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 32)->encode(); |
| 206 | auto dataB = db.getSubTreeData(5, 0); |
| 207 | |
| 208 | BOOST_CHECK(dataA->wireEncode() == dataB->wireEncode()); |
| 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_CASE(DbSave3) |
| 212 | { |
| 213 | MerkleTree merkleTree(TreeGenerator::LOGGER_NAME, db); |
| 214 | for (NonNegativeInteger i = 0; i < 1025 ; i++) { |
| 215 | BOOST_REQUIRE(merkleTree.addLeaf(i, Node::getEmptyHash())); |
| 216 | } |
| 217 | |
| 218 | merkleTree.savePendingTree(); |
| 219 | |
| 220 | auto data1 = db.getPendingSubTrees()[0]; |
| 221 | auto data2 = TreeGenerator::getSubTreeBinary(Node::Index(0, 15), 1025)->encode(); |
| 222 | |
| 223 | auto data3 = db.getPendingSubTrees()[1]; |
| 224 | auto data4 = TreeGenerator::getSubTreeBinary(Node::Index(1024, 10), 1025)->encode(); |
| 225 | |
| 226 | auto data5 = db.getPendingSubTrees()[2]; |
| 227 | auto data6 = TreeGenerator::getSubTreeBinary(Node::Index(1024, 5), 1025)->encode(); |
| 228 | |
| 229 | BOOST_CHECK(data1->wireEncode() == data2->wireEncode()); |
| 230 | BOOST_CHECK(data3->wireEncode() == data4->wireEncode()); |
| 231 | BOOST_CHECK(data5->wireEncode() == data6->wireEncode()); |
| 232 | |
| 233 | for (NonNegativeInteger i = 0; i < 1024 ; i += 32) { |
| 234 | auto dataA = TreeGenerator::getSubTreeBinary(Node::Index(i, 5), i + 32)->encode(); |
| 235 | auto dataB = db.getSubTreeData(5, i); |
| 236 | |
| 237 | BOOST_CHECK(dataA->wireEncode() == dataB->wireEncode()); |
| 238 | } |
| 239 | |
| 240 | auto dataA = TreeGenerator::getSubTreeBinary(Node::Index(0, 10), 1024)->encode(); |
| 241 | auto dataB = db.getSubTreeData(10, 0); |
| 242 | |
| 243 | BOOST_CHECK(dataA->wireEncode() == dataB->wireEncode()); |
| 244 | } |
| 245 | |
| 246 | BOOST_AUTO_TEST_SUITE_END() |
| 247 | |
| 248 | } // namespace tests |
| 249 | } // namespace nsl |