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