Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2018, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "lsa-segment-storage.hpp" |
| 27 | #include "test-common.hpp" |
| 28 | #include "nlsr.hpp" |
| 29 | #include "name-prefix-list.hpp" |
| 30 | |
| 31 | #include <boost/test/unit_test.hpp> |
| 32 | |
| 33 | namespace nlsr { |
| 34 | namespace test { |
| 35 | |
| 36 | class LsaSegmentStorageFixture : public UnitTestTimeFixture |
| 37 | { |
| 38 | public: |
| 39 | LsaSegmentStorageFixture() |
| 40 | : face(m_ioService, m_keyChain) |
| 41 | , nlsr(m_ioService, m_scheduler, face, m_keyChain) |
| 42 | , lsdb(nlsr.getLsdb()) |
| 43 | , lsaStorage(lsdb.getLsaStorage()) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | static shared_ptr<ndn::Data> |
| 48 | makeLsaSegment(const ndn::Name& baseName, uint64_t segmentNo, bool isFinal) |
| 49 | { |
| 50 | const uint8_t buffer[] = "Hello, world!"; |
| 51 | |
| 52 | ndn::Name lsaDataName(baseName); |
| 53 | lsaDataName.appendSegment(segmentNo); |
| 54 | auto lsaSegment = make_shared<ndn::Data>(ndn::Name(lsaDataName)); |
| 55 | lsaSegment->setContent(buffer, sizeof(buffer)); |
| 56 | if (isFinal) { |
| 57 | lsaSegment->setFinalBlockId(lsaSegment->getName()[-1]); |
| 58 | } |
| 59 | |
| 60 | return signData(lsaSegment); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | receiveLsaInterest(const ndn::Name& baseInterestName, uint64_t segmentNo, |
| 65 | bool isSegmentZero) |
| 66 | { |
| 67 | if (isSegmentZero) { |
| 68 | lsdb.processInterest(ndn::Name(), ndn::Interest(baseInterestName)); |
| 69 | } |
| 70 | else { |
| 71 | ndn::Name nextInterestName(baseInterestName); |
| 72 | nextInterestName.appendSegment(segmentNo); |
| 73 | lsdb.processInterest(ndn::Name(), ndn::Interest(nextInterestName)); |
| 74 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public: |
| 79 | ndn::util::DummyClientFace face; |
| 80 | Nlsr nlsr; |
| 81 | Lsdb& lsdb; |
| 82 | LsaSegmentStorage& lsaStorage; |
| 83 | }; |
| 84 | |
| 85 | BOOST_FIXTURE_TEST_SUITE(TestLsaSegmentStorage, LsaSegmentStorageFixture) |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(Basic) |
| 88 | { |
| 89 | ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME"); |
| 90 | lsaInterestName.appendNumber(12); |
| 91 | |
| 92 | ndn::Name lsaDataName(lsaInterestName); |
| 93 | lsaDataName.appendVersion(); |
| 94 | |
| 95 | for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) { |
| 96 | auto lsaData = makeLsaSegment(lsaDataName, segmentNo, segmentNo == 4); |
| 97 | lsaStorage.afterFetcherSignalEmitted(*lsaData); |
| 98 | } |
| 99 | |
| 100 | // receive interest for other-router's LSA that is stored in this router's storage |
| 101 | for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) { |
| 102 | receiveLsaInterest(lsaInterestName, segmentNo, segmentNo == 0); |
| 103 | } |
| 104 | |
| 105 | // 4 data segments should be sent in response to 4 interests |
| 106 | BOOST_CHECK_EQUAL(face.sentData.size(), 4); |
| 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_CASE(DeleteOldLsa) |
| 110 | { |
| 111 | ndn::Name lsaDataName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME"); |
| 112 | uint64_t segmentNo = 0; |
| 113 | |
| 114 | uint64_t oldSeqNo = 12; |
| 115 | ndn::Name oldLsaDataName(lsaDataName); |
| 116 | oldLsaDataName.appendNumber(oldSeqNo); |
| 117 | oldLsaDataName.appendVersion(); |
| 118 | |
| 119 | auto oldLsaData = makeLsaSegment(oldLsaDataName, segmentNo, true); |
| 120 | lsaStorage.afterFetcherSignalEmitted(*oldLsaData); |
| 121 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 122 | |
| 123 | uint64_t newSeqNo = 13; |
| 124 | ndn::Name newLsaDataName(lsaDataName); |
| 125 | newLsaDataName.appendNumber(newSeqNo); |
| 126 | newLsaDataName.appendVersion(); |
| 127 | |
| 128 | auto newLsaData = makeLsaSegment(newLsaDataName, segmentNo, true); |
| 129 | lsaStorage.afterFetcherSignalEmitted(*newLsaData); |
| 130 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 131 | |
| 132 | ndn::Name lsaInterestName(lsaDataName); |
| 133 | |
| 134 | ndn::Name oldLsaInterestName(lsaInterestName); |
| 135 | oldLsaInterestName.appendNumber(oldSeqNo); |
| 136 | receiveLsaInterest(oldLsaInterestName, segmentNo, true); |
| 137 | |
| 138 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 139 | |
| 140 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 141 | |
| 142 | ndn::Name newLsaInterestName(lsaInterestName); |
| 143 | newLsaInterestName.appendNumber(newSeqNo); |
| 144 | receiveLsaInterest(newLsaInterestName, segmentNo, true); |
| 145 | |
| 146 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 147 | |
| 148 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_SUITE_END() // TestLsaSegmentStorage |
| 152 | |
| 153 | } // namespace test |
| 154 | } // namespace nlsr |