Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 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" |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 30 | #include "lsa.hpp" |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 31 | |
| 32 | #include <boost/test/unit_test.hpp> |
| 33 | |
| 34 | namespace nlsr { |
| 35 | namespace test { |
| 36 | |
| 37 | class LsaSegmentStorageFixture : public UnitTestTimeFixture |
| 38 | { |
| 39 | public: |
| 40 | LsaSegmentStorageFixture() |
| 41 | : face(m_ioService, m_keyChain) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 42 | , conf(face) |
| 43 | , nlsr(face, m_keyChain, conf) |
| 44 | , lsdb(nlsr.m_lsdb) |
| 45 | , lsaStorage(lsdb.m_lsaStorage) |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 49 | std::string |
| 50 | makeLsaContent() |
| 51 | { |
| 52 | ndn::Name s1{"name1"}; |
| 53 | ndn::Name s2{"name2"}; |
| 54 | NamePrefixList npl1{s1, s2}; |
| 55 | NameLsa nameLsa("/ndn/other-site/%C1.Router/other-router", 12, |
Ashlesh Gawande | d65786a | 2018-03-30 01:17:58 -0500 | [diff] [blame] | 56 | ndn::time::system_clock::now() + ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), npl1); |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 57 | return nameLsa.serialize(); |
| 58 | } |
| 59 | |
| 60 | shared_ptr<ndn::Data> |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 61 | makeLsaSegment(const ndn::Name& baseName, uint64_t segmentNo, bool isFinal) |
| 62 | { |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 63 | ndn::Name lsaDataName(baseName); |
| 64 | lsaDataName.appendSegment(segmentNo); |
| 65 | auto lsaSegment = make_shared<ndn::Data>(ndn::Name(lsaDataName)); |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 66 | std::string content = makeLsaContent(); |
| 67 | lsaSegment->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.length()); |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 68 | if (isFinal) { |
Ashlesh Gawande | d65786a | 2018-03-30 01:17:58 -0500 | [diff] [blame] | 69 | lsaSegment->setFinalBlock(lsaSegment->getName()[-1]); |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return signData(lsaSegment); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | receiveLsaInterest(const ndn::Name& baseInterestName, uint64_t segmentNo, |
| 77 | bool isSegmentZero) |
| 78 | { |
| 79 | if (isSegmentZero) { |
| 80 | lsdb.processInterest(ndn::Name(), ndn::Interest(baseInterestName)); |
| 81 | } |
| 82 | else { |
| 83 | ndn::Name nextInterestName(baseInterestName); |
| 84 | nextInterestName.appendSegment(segmentNo); |
| 85 | lsdb.processInterest(ndn::Name(), ndn::Interest(nextInterestName)); |
| 86 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | public: |
| 91 | ndn::util::DummyClientFace face; |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame^] | 92 | ConfParameter conf; |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 93 | Nlsr nlsr; |
| 94 | Lsdb& lsdb; |
| 95 | LsaSegmentStorage& lsaStorage; |
| 96 | }; |
| 97 | |
| 98 | BOOST_FIXTURE_TEST_SUITE(TestLsaSegmentStorage, LsaSegmentStorageFixture) |
| 99 | |
| 100 | BOOST_AUTO_TEST_CASE(Basic) |
| 101 | { |
| 102 | ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME"); |
| 103 | lsaInterestName.appendNumber(12); |
| 104 | |
| 105 | ndn::Name lsaDataName(lsaInterestName); |
| 106 | lsaDataName.appendVersion(); |
| 107 | |
| 108 | for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) { |
| 109 | auto lsaData = makeLsaSegment(lsaDataName, segmentNo, segmentNo == 4); |
| 110 | lsaStorage.afterFetcherSignalEmitted(*lsaData); |
| 111 | } |
| 112 | |
| 113 | // receive interest for other-router's LSA that is stored in this router's storage |
| 114 | for (uint64_t segmentNo = 0; segmentNo < 4; ++segmentNo) { |
| 115 | receiveLsaInterest(lsaInterestName, segmentNo, segmentNo == 0); |
| 116 | } |
| 117 | |
| 118 | // 4 data segments should be sent in response to 4 interests |
| 119 | BOOST_CHECK_EQUAL(face.sentData.size(), 4); |
| 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(DeleteOldLsa) |
| 123 | { |
| 124 | ndn::Name lsaDataName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME"); |
| 125 | uint64_t segmentNo = 0; |
| 126 | |
| 127 | uint64_t oldSeqNo = 12; |
| 128 | ndn::Name oldLsaDataName(lsaDataName); |
| 129 | oldLsaDataName.appendNumber(oldSeqNo); |
| 130 | oldLsaDataName.appendVersion(); |
| 131 | |
| 132 | auto oldLsaData = makeLsaSegment(oldLsaDataName, segmentNo, true); |
| 133 | lsaStorage.afterFetcherSignalEmitted(*oldLsaData); |
| 134 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 135 | |
| 136 | uint64_t newSeqNo = 13; |
| 137 | ndn::Name newLsaDataName(lsaDataName); |
| 138 | newLsaDataName.appendNumber(newSeqNo); |
| 139 | newLsaDataName.appendVersion(); |
| 140 | |
| 141 | auto newLsaData = makeLsaSegment(newLsaDataName, segmentNo, true); |
| 142 | lsaStorage.afterFetcherSignalEmitted(*newLsaData); |
| 143 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 144 | |
| 145 | ndn::Name lsaInterestName(lsaDataName); |
| 146 | |
| 147 | ndn::Name oldLsaInterestName(lsaInterestName); |
| 148 | oldLsaInterestName.appendNumber(oldSeqNo); |
| 149 | receiveLsaInterest(oldLsaInterestName, segmentNo, true); |
| 150 | |
| 151 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 152 | |
| 153 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 154 | |
| 155 | ndn::Name newLsaInterestName(lsaInterestName); |
| 156 | newLsaInterestName.appendNumber(newSeqNo); |
| 157 | receiveLsaInterest(newLsaInterestName, segmentNo, true); |
| 158 | |
| 159 | advanceClocks(ndn::time::milliseconds(1), 10); |
| 160 | |
| 161 | BOOST_CHECK_EQUAL(face.sentData.size(), 1); |
| 162 | } |
| 163 | |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 164 | BOOST_AUTO_TEST_CASE(ScheduledDeletion) |
| 165 | { |
| 166 | ndn::Name lsaInterestName("/ndn/NLSR/LSA/other-site/%C1.Router/other-router/NAME"); |
| 167 | lsaInterestName.appendNumber(12); |
| 168 | |
| 169 | ndn::Name lsaDataName(lsaInterestName); |
| 170 | lsaDataName.appendVersion(); |
| 171 | |
| 172 | auto lsaData = makeLsaSegment(lsaDataName, 0, true); |
| 173 | lsaStorage.afterFetcherSignalEmitted(*lsaData); |
| 174 | |
| 175 | BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) != nullptr); |
| 176 | |
Ashlesh Gawande | d65786a | 2018-03-30 01:17:58 -0500 | [diff] [blame] | 177 | // Make sure it was not deleted earlier somehow |
| 178 | advanceClocks(ndn::time::seconds(100), 10); |
| 179 | BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) != nullptr); |
| 180 | |
| 181 | advanceClocks(ndn::time::seconds(LSA_REFRESH_TIME_DEFAULT), 10); |
Ashlesh Gawande | 8c6d8c8 | 2018-02-28 21:41:31 -0600 | [diff] [blame] | 182 | |
| 183 | BOOST_CHECK(lsaStorage.getLsaSegment(ndn::Interest(lsaInterestName)) == nullptr); |
| 184 | } |
| 185 | |
Muktadir Chowdhury | c3ea26f | 2018-01-05 21:40:59 +0000 | [diff] [blame] | 186 | BOOST_AUTO_TEST_SUITE_END() // TestLsaSegmentStorage |
| 187 | |
| 188 | } // namespace test |
| 189 | } // namespace nlsr |