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 "db.hpp" |
| 23 | #include "db-fixture.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/security/digest-sha256.hpp> |
| 26 | #include <ndn-cxx/encoding/buffer-stream.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_FIXTURE_TEST_SUITE(TestDb, DbFixture) |
| 34 | |
| 35 | BOOST_AUTO_TEST_CASE(Basic) |
| 36 | { |
| 37 | ndn::DigestSha256 digest; |
| 38 | ndn::ConstBufferPtr hash = make_shared<ndn::Buffer>(32); |
| 39 | Data data1(Name("/logger/name/5/0/abcdabcdabcdabcdabcd/complete")); |
| 40 | data1.setSignature(digest); |
| 41 | data1.setSignatureValue(Block(tlv::SignatureValue, hash)); |
| 42 | Data data2(Name("/logger/name/5/32/abcdabcdabcdabcdabcd/complete")); |
| 43 | data2.setSignature(digest); |
| 44 | data2.setSignatureValue(Block(tlv::SignatureValue, hash)); |
| 45 | Data data3(Name("/logger/name/5/32/abcdabcdabcdabcdabcd/33")); |
| 46 | data3.setSignature(digest); |
| 47 | data3.setSignatureValue(Block(tlv::SignatureValue, hash)); |
| 48 | |
| 49 | BOOST_CHECK_EQUAL(db.getPendingSubTrees().size(), 0); |
| 50 | BOOST_CHECK(db.getSubTreeData(5, 0) == nullptr); |
| 51 | db.insertSubTreeData(5, 0, data1); |
| 52 | BOOST_REQUIRE(db.getSubTreeData(5, 0) != nullptr); |
| 53 | BOOST_CHECK(db.getSubTreeData(5, 0)->wireEncode() == data1.wireEncode()); |
| 54 | BOOST_CHECK_EQUAL(db.getPendingSubTrees().size(), 0); |
| 55 | |
| 56 | BOOST_CHECK(db.getSubTreeData(5, 32) == nullptr); |
| 57 | db.insertSubTreeData(5, 32, data3, false, 33); |
| 58 | BOOST_REQUIRE(db.getSubTreeData(5, 32) != nullptr); |
| 59 | BOOST_CHECK(db.getSubTreeData(5, 32)->wireEncode() == data3.wireEncode()); |
| 60 | BOOST_CHECK_EQUAL(db.getPendingSubTrees().size(), 1); |
| 61 | |
| 62 | db.insertSubTreeData(5, 32, data2); |
| 63 | BOOST_REQUIRE(db.getSubTreeData(5, 32) != nullptr); |
| 64 | BOOST_CHECK(db.getSubTreeData(5, 32)->wireEncode() == data2.wireEncode()); |
| 65 | BOOST_CHECK_EQUAL(db.getPendingSubTrees().size(), 0); |
| 66 | } |
| 67 | |
| 68 | BOOST_AUTO_TEST_CASE(Basic2) |
| 69 | { |
| 70 | ndn::DigestSha256 digest; |
| 71 | ndn::ConstBufferPtr hash = make_shared<ndn::Buffer>(32); |
| 72 | Data data1(Name("/logger/name/10/0/abcdabcdabcdabcdabcd/33")); |
| 73 | data1.setSignature(digest); |
| 74 | data1.setSignatureValue(Block(tlv::SignatureValue, hash)); |
| 75 | Data data2(Name("/logger/name/5/32/abcdabcdabcdabcdabcd/33")); |
| 76 | data2.setSignature(digest); |
| 77 | data2.setSignatureValue(Block(tlv::SignatureValue, hash)); |
| 78 | |
| 79 | db.insertSubTreeData(5, 32, data2, false, 33); |
| 80 | db.insertSubTreeData(10, 0, data1, false, 33); |
| 81 | std::vector<shared_ptr<Data>> subtrees = db.getPendingSubTrees(); |
| 82 | |
| 83 | BOOST_CHECK_EQUAL(subtrees.size(), 2); |
| 84 | BOOST_CHECK(subtrees[0]->wireEncode() == data1.wireEncode()); |
| 85 | BOOST_CHECK(subtrees[1]->wireEncode() == data2.wireEncode()); |
| 86 | } |
| 87 | |
| 88 | const uint8_t Data1[] = { |
| 89 | 0x06, 0xc5, // NDN Data |
| 90 | 0x07, 0x14, // Name |
| 91 | 0x08, 0x05, |
| 92 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 93 | 0x08, 0x03, |
| 94 | 0x6e, 0x64, 0x6e, |
| 95 | 0x08, 0x06, |
| 96 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78, |
| 97 | 0x14, 0x04, // MetaInfo |
| 98 | 0x19, 0x02, // FreshnessPeriod |
| 99 | 0x27, 0x10, |
| 100 | 0x15, 0x08, // Content |
| 101 | 0x53, 0x55, 0x43, 0x43, 0x45, 0x53, 0x53, 0x21, |
| 102 | 0x16, 0x1b, // SignatureInfo |
| 103 | 0x1b, 0x01, // SignatureType |
| 104 | 0x01, |
| 105 | 0x1c, 0x16, // KeyLocator |
| 106 | 0x07, 0x14, // Name |
| 107 | 0x08, 0x04, |
| 108 | 0x74, 0x65, 0x73, 0x74, |
| 109 | 0x08, 0x03, |
| 110 | 0x6b, 0x65, 0x79, |
| 111 | 0x08, 0x07, |
| 112 | 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x6f, 0x72, |
| 113 | 0x17, 0x80, // SignatureValue |
| 114 | 0x2f, 0xd6, 0xf1, 0x6e, 0x80, 0x6f, 0x10, 0xbe, 0xb1, 0x6f, 0x3e, 0x31, 0xec, |
| 115 | 0xe3, 0xb9, 0xea, 0x83, 0x30, 0x40, 0x03, 0xfc, 0xa0, 0x13, 0xd9, 0xb3, 0xc6, |
| 116 | 0x25, 0x16, 0x2d, 0xa6, 0x58, 0x41, 0x69, 0x62, 0x56, 0xd8, 0xb3, 0x6a, 0x38, |
| 117 | 0x76, 0x56, 0xea, 0x61, 0xb2, 0x32, 0x70, 0x1c, 0xb6, 0x4d, 0x10, 0x1d, 0xdc, |
| 118 | 0x92, 0x8e, 0x52, 0xa5, 0x8a, 0x1d, 0xd9, 0x96, 0x5e, 0xc0, 0x62, 0x0b, 0xcf, |
| 119 | 0x3a, 0x9d, 0x7f, 0xca, 0xbe, 0xa1, 0x41, 0x71, 0x85, 0x7a, 0x8b, 0x5d, 0xa9, |
| 120 | 0x64, 0xd6, 0x66, 0xb4, 0xe9, 0x8d, 0x0c, 0x28, 0x43, 0xee, 0xa6, 0x64, 0xe8, |
| 121 | 0x55, 0xf6, 0x1c, 0x19, 0x0b, 0xef, 0x99, 0x25, 0x1e, 0xdc, 0x78, 0xb3, 0xa7, |
| 122 | 0xaa, 0x0d, 0x14, 0x58, 0x30, 0xe5, 0x37, 0x6a, 0x6d, 0xdb, 0x56, 0xac, 0xa3, |
| 123 | 0xfc, 0x90, 0x7a, 0xb8, 0x66, 0x9c, 0x0e, 0xf6, 0xb7, 0x64, 0xd1 |
| 124 | }; |
| 125 | |
| 126 | BOOST_AUTO_TEST_CASE(Basic3) |
| 127 | { |
| 128 | Name loggerName("/test/logger"); |
| 129 | Name dataName("/test/data"); |
| 130 | Block block(Data1, sizeof(Data1)); |
| 131 | Data data(block); |
| 132 | |
| 133 | BOOST_CHECK_EQUAL(db.getMaxLeafSeq(), 0); |
| 134 | |
| 135 | Leaf leaf(dataName, 1, 0, 0, loggerName); |
| 136 | BOOST_CHECK(db.insertLeafData(leaf, data)); |
| 137 | BOOST_CHECK_EQUAL(db.getMaxLeafSeq(), 1); |
| 138 | |
| 139 | auto result = db.getLeaf(0); |
| 140 | BOOST_CHECK_EQUAL(result.first->getDataName(), dataName); |
| 141 | BOOST_CHECK_EQUAL(result.first->getTimestamp(), 1); |
| 142 | BOOST_CHECK_EQUAL(result.first->getDataSeqNo(), 0); |
| 143 | BOOST_CHECK_EQUAL(result.first->getSignerSeqNo(), 0); |
| 144 | BOOST_REQUIRE(result.second != nullptr); |
| 145 | BOOST_CHECK_EQUAL(result.second->getName(), data.getName()); |
| 146 | |
| 147 | Leaf leaf2(dataName, 2, 1, 0, loggerName); |
| 148 | BOOST_CHECK(db.insertLeafData(leaf2)); |
| 149 | BOOST_CHECK_EQUAL(db.getMaxLeafSeq(), 2); |
| 150 | |
| 151 | result = db.getLeaf(1); |
| 152 | BOOST_REQUIRE(result.first != nullptr); |
| 153 | BOOST_CHECK_EQUAL(result.first->getDataName(), dataName); |
| 154 | BOOST_CHECK_EQUAL(result.first->getTimestamp(), 2); |
| 155 | BOOST_CHECK_EQUAL(result.first->getDataSeqNo(), 1); |
| 156 | BOOST_CHECK_EQUAL(result.first->getSignerSeqNo(), 0); |
| 157 | BOOST_REQUIRE(result.second == nullptr); |
| 158 | |
| 159 | Leaf leaf3(dataName, 2, 5, 0, loggerName); |
| 160 | BOOST_CHECK_EQUAL(db.insertLeafData(leaf), false); |
| 161 | BOOST_CHECK_EQUAL(db.insertLeafData(leaf2), false); |
| 162 | BOOST_CHECK_EQUAL(db.insertLeafData(leaf3), false); |
| 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_SUITE_END() |
| 166 | |
| 167 | } // namespace tests |
Alexander Afanasyev | 49e2e4c | 2017-05-06 13:42:57 -0700 | [diff] [blame^] | 168 | } // namespace delorean |
| 169 | } // namespace ndn |