peizhen guo | 0401e3e | 2014-08-12 13:24:30 -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 | * @author Peizhen Guo <patrick.guopz@gmail.com> |
| 20 | */ |
| 21 | #include <boost-test.hpp> |
| 22 | #include <iostream> |
| 23 | |
| 24 | #include "auditor.hpp" |
| 25 | #include "merkle-tree.hpp" |
| 26 | |
| 27 | namespace nsl { |
| 28 | |
| 29 | |
| 30 | boost::test_tools::predicate_result check_hash(ndn::ConstBufferPtr ptr1, ndn::ConstBufferPtr ptr2) |
| 31 | { |
| 32 | bool result = true; |
| 33 | for (int i = 0; i < ptr1->size(); i++) |
| 34 | { |
| 35 | if ((*ptr1)[i] != (*ptr2)[i]) |
| 36 | { |
| 37 | result = false; |
| 38 | break; |
| 39 | } |
| 40 | } |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | BOOST_AUTO_TEST_SUITE(TestAuditor) |
| 46 | |
| 47 | |
| 48 | BOOST_AUTO_TEST_CASE(TestVerify) |
| 49 | { |
| 50 | |
| 51 | std::string str1 = "peizhen"; |
| 52 | std::string str2 = "guo"; |
| 53 | std::string str3 = "is"; |
| 54 | std::string str4 = "building"; |
| 55 | std::string str5 = "this"; |
| 56 | std::string str6 = "logging"; |
| 57 | std::string str7 = "system"; |
| 58 | ndn::Buffer buf1; |
| 59 | ndn::Buffer buf2; |
| 60 | ndn::Buffer buf3; |
| 61 | ndn::Buffer buf4; |
| 62 | ndn::Buffer buf5; |
| 63 | ndn::Buffer buf6; |
| 64 | ndn::Buffer buf7; |
| 65 | for (int i=0; i < str1.size(); i++) |
| 66 | buf1.push_back(uint8_t(str1[i])); |
| 67 | for (int i=0; i < str2.size(); i++) |
| 68 | buf2.push_back(uint8_t(str2[i])); |
| 69 | for (int i=0; i < str3.size(); i++) |
| 70 | buf3.push_back(uint8_t(str3[i])); |
| 71 | for (int i=0; i < str4.size(); i++) |
| 72 | buf4.push_back(uint8_t(str4[i])); |
| 73 | for (int i=0; i < str5.size(); i++) |
| 74 | buf5.push_back(uint8_t(str5[i])); |
| 75 | for (int i=0; i < str6.size(); i++) |
| 76 | buf6.push_back(uint8_t(str6[i])); |
| 77 | for (int i=0; i < str7.size(); i++) |
| 78 | buf7.push_back(uint8_t(str7[i])); |
| 79 | ndn::ConstBufferPtr buf_p1 = boost::make_shared<ndn::Buffer>(buf1); |
| 80 | ndn::ConstBufferPtr buf_p2 = boost::make_shared<ndn::Buffer>(buf2); |
| 81 | ndn::ConstBufferPtr buf_p3 = boost::make_shared<ndn::Buffer>(buf3); |
| 82 | ndn::ConstBufferPtr buf_p4 = boost::make_shared<ndn::Buffer>(buf4); |
| 83 | ndn::ConstBufferPtr buf_p5 = boost::make_shared<ndn::Buffer>(buf5); |
| 84 | ndn::ConstBufferPtr buf_p6 = boost::make_shared<ndn::Buffer>(buf6); |
| 85 | ndn::ConstBufferPtr buf_p7 = boost::make_shared<ndn::Buffer>(buf7); |
| 86 | |
| 87 | // Test genProof function |
| 88 | Auditor validator; |
| 89 | MerkleTree merkle_tree; |
| 90 | Index version1, version2; |
| 91 | merkle_tree.addLeaf(buf_p1); |
| 92 | merkle_tree.addLeaf(buf_p2); |
| 93 | merkle_tree.addLeaf(buf_p3); |
| 94 | merkle_tree.addLeaf(buf_p4); |
| 95 | version1.number = 0; version1.level = merkle_tree.getLevel() - 1; |
| 96 | const Index ver1 = version1; |
| 97 | ndn::ConstBufferPtr rootHash1 = merkle_tree.getNode(ver1)->getHash(); |
| 98 | merkle_tree.addLeaf(buf_p5); |
| 99 | merkle_tree.addLeaf(buf_p6); |
| 100 | merkle_tree.addLeaf(buf_p7); |
| 101 | version2.number = 0; version2.level = merkle_tree.getLevel() - 1; |
| 102 | const Index ver2 = version2; |
| 103 | ndn::ConstBufferPtr rootHash2 = merkle_tree.getNode(ver2)->getHash(); |
| 104 | |
| 105 | std::vector<ConstNodePtr> evidence = merkle_tree.generateProof(3, 6); |
| 106 | bool isConsistent = validator.verifyConsistency(3, 6, rootHash1, rootHash2, evidence); |
| 107 | BOOST_CHECK(isConsistent== true); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_SUITE_END() |
| 111 | |
| 112 | } // namespace nsl |