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 "auditor.hpp" |
| 23 | #include "../tree-generator.hpp" |
| 24 | #include "cryptopp.hpp" |
| 25 | |
| 26 | #include <boost/mpl/list.hpp> |
| 27 | #include "boost-test.hpp" |
| 28 | |
| 29 | namespace nsl { |
| 30 | namespace tests { |
| 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(TestAuditor) |
| 33 | |
| 34 | void |
| 35 | printHex(const uint8_t* buf, size_t size) |
| 36 | { |
| 37 | using namespace CryptoPP; |
| 38 | StringSource ss(buf, size, true, new HexEncoder(new FileSink(std::cerr), false)); |
| 39 | std::cerr << std::endl; |
| 40 | } |
| 41 | |
| 42 | BOOST_AUTO_TEST_CASE(LoadProofTests) |
| 43 | { |
| 44 | std::vector<shared_ptr<Data>> proofs; |
| 45 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 32)->encode()); |
| 46 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(32, 5), 64)->encode()); |
| 47 | |
| 48 | std::map<Node::Index, ConstSubTreeBinaryPtr> tree1; |
| 49 | |
| 50 | BOOST_CHECK(Auditor::loadProof(tree1, proofs, TreeGenerator::LOGGER_NAME)); |
| 51 | |
| 52 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(32, 5), 64)->encode()); |
| 53 | std::map<Node::Index, ConstSubTreeBinaryPtr> tree2; |
| 54 | BOOST_CHECK_EQUAL(Auditor::loadProof(tree2, proofs, TreeGenerator::LOGGER_NAME), false); |
| 55 | } |
| 56 | |
| 57 | size_t |
| 58 | getRootLevel(const NonNegativeInteger& leafSeqNo) { |
| 59 | size_t rootLevel = 0; |
| 60 | NonNegativeInteger seqNo = leafSeqNo; |
| 61 | while (seqNo != 0) { |
| 62 | seqNo = seqNo >> 1; |
| 63 | rootLevel++; |
| 64 | } |
| 65 | |
| 66 | return rootLevel; |
| 67 | } |
| 68 | |
| 69 | template<NonNegativeInteger L, NonNegativeInteger O, NonNegativeInteger N> |
| 70 | class AuditorProofParam1 |
| 71 | { |
| 72 | public: |
| 73 | void |
| 74 | createProof() |
| 75 | { |
| 76 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 32, true)->encode()); |
| 77 | |
| 78 | leafHash = TreeGenerator::getHash(Node::Index(L, 0), L + 1, false); |
| 79 | oldHash = TreeGenerator::getHash(Node::Index(0, getRootLevel(O - 1)), O, false); |
| 80 | newHash = TreeGenerator::getHash(Node::Index(0, getRootLevel(N - 1)), N, false); |
| 81 | } |
| 82 | |
| 83 | std::vector<shared_ptr<Data>> proofs; |
| 84 | const NonNegativeInteger leafSeqNo = L; |
| 85 | ndn::ConstBufferPtr leafHash; |
| 86 | const NonNegativeInteger oldNextSeqNo = O; |
| 87 | ndn::ConstBufferPtr oldHash; |
| 88 | const NonNegativeInteger newNextSeqNo = N; |
| 89 | ndn::ConstBufferPtr newHash; |
| 90 | }; |
| 91 | |
| 92 | template<NonNegativeInteger L, NonNegativeInteger O, NonNegativeInteger N> |
| 93 | class AuditorProofParam2 |
| 94 | { |
| 95 | public: |
| 96 | void |
| 97 | createProof() |
| 98 | { |
| 99 | // proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 32, true)->encode()); |
| 100 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(32, 5), 64, true)->encode()); |
| 101 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(0, 10), 64, true)->encode()); |
| 102 | |
| 103 | leafHash = TreeGenerator::getHash(Node::Index(L, 0), L + 1, false); |
| 104 | oldHash = TreeGenerator::getHash(Node::Index(0, getRootLevel(O - 1)), O, false); |
| 105 | newHash = TreeGenerator::getHash(Node::Index(0, getRootLevel(N - 1)), N, false); |
| 106 | } |
| 107 | |
| 108 | std::vector<shared_ptr<Data>> proofs; |
| 109 | const NonNegativeInteger leafSeqNo = L; |
| 110 | ndn::ConstBufferPtr leafHash; |
| 111 | const NonNegativeInteger oldNextSeqNo = O; |
| 112 | ndn::ConstBufferPtr oldHash; |
| 113 | const NonNegativeInteger newNextSeqNo = N; |
| 114 | ndn::ConstBufferPtr newHash; |
| 115 | }; |
| 116 | |
| 117 | template<NonNegativeInteger L, NonNegativeInteger O, NonNegativeInteger N> |
| 118 | class AuditorProofParam3 |
| 119 | { |
| 120 | public: |
| 121 | void |
| 122 | createProof() |
| 123 | { |
| 124 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 32, true)->encode()); |
| 125 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(32, 5), 33, true)->encode()); |
| 126 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(0, 10), 33, true)->encode()); |
| 127 | |
| 128 | leafHash = TreeGenerator::getHash(Node::Index(L, 0), L + 1, false); |
| 129 | oldHash = TreeGenerator::getHash(Node::Index(0, getRootLevel(O - 1)), O, false); |
| 130 | newHash = TreeGenerator::getHash(Node::Index(0, getRootLevel(N - 1)), N, false); |
| 131 | } |
| 132 | |
| 133 | std::vector<shared_ptr<Data>> proofs; |
| 134 | const NonNegativeInteger leafSeqNo = L; |
| 135 | ndn::ConstBufferPtr leafHash; |
| 136 | const NonNegativeInteger oldNextSeqNo = O; |
| 137 | ndn::ConstBufferPtr oldHash; |
| 138 | const NonNegativeInteger newNextSeqNo = N; |
| 139 | ndn::ConstBufferPtr newHash; |
| 140 | }; |
| 141 | |
| 142 | typedef boost::mpl::list<AuditorProofParam1<0, 1, 1>, |
| 143 | AuditorProofParam1<0, 2, 2>, |
| 144 | AuditorProofParam1<0, 4, 4>, |
| 145 | AuditorProofParam1<1, 2, 2>, |
| 146 | AuditorProofParam1<1, 4, 4>, |
| 147 | AuditorProofParam1<2, 4, 4>, |
| 148 | AuditorProofParam1<3, 4, 4>, |
| 149 | AuditorProofParam1<4, 6, 6>, |
| 150 | AuditorProofParam1<31, 32, 32>, |
| 151 | AuditorProofParam3<0, 33, 33>, |
| 152 | AuditorProofParam2<32, 33, 33>, |
| 153 | AuditorProofParam2<48, 64, 64>> ExistenceProofTestParams; |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE_TEMPLATE(ExistenceProof, P, ExistenceProofTestParams) |
| 156 | { |
| 157 | P params; |
| 158 | params.createProof(); |
| 159 | |
| 160 | BOOST_CHECK(Auditor::doesExist(params.leafSeqNo, params.leafHash, |
| 161 | params.newNextSeqNo, params.newHash, |
| 162 | params.proofs, TreeGenerator::LOGGER_NAME)); |
| 163 | } |
| 164 | |
| 165 | template<NonNegativeInteger L, NonNegativeInteger O, NonNegativeInteger N> |
| 166 | class AuditorProofParam4 |
| 167 | { |
| 168 | public: |
| 169 | void |
| 170 | createProof() |
| 171 | { |
| 172 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(0, 5), 32, true)->encode()); |
| 173 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(32, 5), 64, true)->encode()); |
| 174 | proofs.push_back(TreeGenerator::getSubTreeBinary(Node::Index(0, 10), 64, true)->encode()); |
| 175 | |
| 176 | leafHash = TreeGenerator::getHash(Node::Index(L, 0), L + 1, false); |
| 177 | oldHash = TreeGenerator::getHash(Node::Index(0, getRootLevel(O - 1)), O, false); |
| 178 | newHash = TreeGenerator::getHash(Node::Index(0, getRootLevel(N - 1)), N, false); |
| 179 | } |
| 180 | |
| 181 | std::vector<shared_ptr<Data>> proofs; |
| 182 | const NonNegativeInteger leafSeqNo = L; |
| 183 | ndn::ConstBufferPtr leafHash; |
| 184 | const NonNegativeInteger oldNextSeqNo = O; |
| 185 | ndn::ConstBufferPtr oldHash; |
| 186 | const NonNegativeInteger newNextSeqNo = N; |
| 187 | ndn::ConstBufferPtr newHash; |
| 188 | }; |
| 189 | |
| 190 | typedef boost::mpl::list<AuditorProofParam1<0, 1, 1>, |
| 191 | AuditorProofParam1<0, 1, 2>, |
| 192 | AuditorProofParam1<0, 1, 32>, |
| 193 | AuditorProofParam1<0, 2, 32>, |
| 194 | AuditorProofParam1<0, 31, 32>, |
| 195 | AuditorProofParam4<0, 32, 64>, |
| 196 | AuditorProofParam3<0, 1, 33>, |
| 197 | AuditorProofParam3<0, 31, 33>, |
| 198 | AuditorProofParam4<0, 1, 64>> ConsistencyProofTestParams; |
| 199 | |
| 200 | BOOST_AUTO_TEST_CASE_TEMPLATE(ConsistencyProof, P, ConsistencyProofTestParams) |
| 201 | { |
| 202 | P params; |
| 203 | params.createProof(); |
| 204 | |
| 205 | BOOST_CHECK(Auditor::isConsistent(params.oldNextSeqNo, params.oldHash, |
| 206 | params.newNextSeqNo, params.newHash, |
| 207 | params.proofs, TreeGenerator::LOGGER_NAME)); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_SUITE_END() |
| 211 | |
| 212 | } // namespace tests |
| 213 | } // namespace nsl |