Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 20 | */ |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 21 | |
| 22 | #include "communication/sync-logic-handler.hpp" |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 23 | #include "tests/test-common.hpp" |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 24 | #include "common.hpp" |
| 25 | #include "nlsr.hpp" |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 26 | |
| 27 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 28 | #include <boost/lexical_cast.hpp> |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 29 | |
| 30 | namespace nlsr { |
| 31 | namespace test { |
| 32 | |
| 33 | using std::shared_ptr; |
| 34 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 35 | class SyncLogicFixture : public UnitTestTimeFixture |
| 36 | { |
| 37 | public: |
| 38 | SyncLogicFixture() |
| 39 | : face(m_ioService, m_keyChain) |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 40 | , conf(face, m_keyChain) |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 41 | , confProcessor(conf, SYNC_PROTOCOL_PSYNC) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 42 | , testIsLsaNew([] (const ndn::Name& name, const Lsa::Type& lsaType, |
| 43 | const uint64_t sequenceNumber) { |
| 44 | return true; |
| 45 | }) |
| 46 | , sync(face, testIsLsaNew, conf) |
| 47 | , updateNamePrefix(this->conf.getLsaPrefix().toUri() + |
| 48 | this->conf.getSiteName().toUri() + |
| 49 | "/%C1.Router/other-router/") |
| 50 | { |
| 51 | addIdentity(conf.getRouterPrefix()); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | receiveUpdate(const std::string& prefix, uint64_t seqNo) |
| 56 | { |
| 57 | this->advanceClocks(ndn::time::milliseconds(1), 10); |
| 58 | face.sentInterests.clear(); |
| 59 | |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 60 | std::vector<psync::MissingDataInfo> updates; |
| 61 | updates.push_back({ndn::Name(prefix), 0, seqNo}); |
| 62 | sync.m_syncLogic.onPSyncUpdate(updates); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 63 | |
| 64 | this->advanceClocks(ndn::time::milliseconds(1), 10); |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | ndn::util::DummyClientFace face; |
| 69 | ConfParameter conf; |
| 70 | DummyConfFileProcessor confProcessor; |
| 71 | SyncLogicHandler::IsLsaNew testIsLsaNew; |
| 72 | SyncLogicHandler sync; |
| 73 | |
| 74 | const std::string updateNamePrefix; |
| 75 | const std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::ADJACENCY, |
| 76 | Lsa::Type::COORDINATE}; |
| 77 | }; |
| 78 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 79 | BOOST_AUTO_TEST_SUITE(TestSyncLogicHandler) |
| 80 | |
| 81 | /* Tests that when SyncLogicHandler receives an LSA of either Name or |
| 82 | Adjacency type that appears to be newer, it will emit to its signal |
| 83 | with those LSA details. |
| 84 | */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 85 | BOOST_FIXTURE_TEST_CASE(UpdateForOtherLS, SyncLogicFixture) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 86 | { |
| 87 | std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::ADJACENCY}; |
| 88 | |
| 89 | uint64_t syncSeqNo = 1; |
| 90 | |
| 91 | for (const Lsa::Type& lsaType : lsaTypes) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 92 | std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 93 | |
| 94 | // Actual testing done here -- signal function callback |
| 95 | ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect( |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 96 | [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber, |
| 97 | const ndn::Name& originRouter) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 98 | BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName); |
| 99 | BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo); |
| 100 | }); |
| 101 | |
| 102 | this->receiveUpdate(updateName, syncSeqNo); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* Tests that when SyncLogicHandler in HR mode receives an LSA of |
| 107 | either Coordinate or Name type that appears to be newer, it will |
| 108 | emit to its signal with those LSA details. |
| 109 | */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 110 | BOOST_FIXTURE_TEST_CASE(UpdateForOtherHR, SyncLogicFixture) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 111 | { |
| 112 | this->conf.setHyperbolicState(HYPERBOLIC_STATE_ON); |
| 113 | |
| 114 | uint64_t syncSeqNo = 1; |
| 115 | std::vector<Lsa::Type> lsaTypes = {Lsa::Type::NAME, Lsa::Type::COORDINATE}; |
| 116 | |
| 117 | for (const Lsa::Type& lsaType : lsaTypes) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 118 | std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 119 | |
| 120 | ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect( |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 121 | [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber, |
| 122 | const ndn::Name& originRouter) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName); |
| 124 | BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo); |
| 125 | }); |
| 126 | |
| 127 | this->receiveUpdate(updateName, syncSeqNo); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Tests that when SyncLogicHandler in HR-dry mode receives an LSA of |
| 132 | any type that appears to be newer, it will emit to its signal with |
| 133 | those LSA details. |
| 134 | */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 135 | BOOST_FIXTURE_TEST_CASE(UpdateForOtherHRDry, SyncLogicFixture) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 136 | { |
| 137 | this->conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN); |
| 138 | |
| 139 | uint64_t syncSeqNo = 1; |
| 140 | |
| 141 | for (const Lsa::Type& lsaType : this->lsaTypes) { |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 142 | std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 143 | |
| 144 | ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect( |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 145 | [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber, |
| 146 | const ndn::Name& originRouter) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 147 | BOOST_CHECK_EQUAL(ndn::Name{updateName}, routerName); |
| 148 | BOOST_CHECK_EQUAL(sequenceNumber, syncSeqNo); |
| 149 | }); |
| 150 | |
| 151 | this->receiveUpdate(updateName, syncSeqNo); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* Tests that when SyncLogicHandler receives an update for an LSA with |
| 156 | details matching this router's details, it will *not* emit to its |
| 157 | signal those LSA details. |
| 158 | */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 159 | BOOST_FIXTURE_TEST_CASE(NoUpdateForSelf, SyncLogicFixture) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 160 | { |
| 161 | const uint64_t sequenceNumber = 1; |
| 162 | |
| 163 | for (const Lsa::Type& lsaType : this->lsaTypes) { |
| 164 | // To ensure that we get correctly-separated components, create |
| 165 | // and modify a Name to hand off. |
| 166 | ndn::Name updateName = ndn::Name{this->conf.getLsaPrefix()}; |
| 167 | updateName.append(this->conf.getSiteName()) |
| 168 | .append(this->conf.getRouterName()) |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 169 | .append(boost::lexical_cast<std::string>(lsaType)); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 170 | |
| 171 | ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect( |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 172 | [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber, |
| 173 | const ndn::Name& originRouter) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 174 | BOOST_FAIL("Updates for self should not be emitted!"); |
| 175 | }); |
| 176 | |
| 177 | this->receiveUpdate(updateName.toUri(), sequenceNumber); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | /* Tests that when SyncLogicHandler receives an update for an LSA with |
| 182 | details that do not match the expected format, it will *not* emit |
| 183 | to its signal those LSA details. |
| 184 | */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 185 | BOOST_FIXTURE_TEST_CASE(MalformedUpdate, SyncLogicFixture) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 186 | { |
| 187 | const uint64_t sequenceNumber = 1; |
| 188 | |
| 189 | for (const Lsa::Type& lsaType : this->lsaTypes) { |
| 190 | ndn::Name updateName{this->conf.getSiteName()}; |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 191 | updateName.append(this->conf.getRouterName()).append(boost::lexical_cast<std::string>(lsaType)); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 192 | |
| 193 | ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect( |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 194 | [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber, |
| 195 | const ndn::Name& originRouter) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 196 | BOOST_FAIL("Malformed updates should not be emitted!"); |
| 197 | }); |
| 198 | |
| 199 | this->receiveUpdate(updateName.toUri(), sequenceNumber); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /* Tests that when SyncLogicHandler receives an update for an LSA with |
| 204 | details that do not appear to be new, it will *not* emit to its |
| 205 | signal those LSA details. |
| 206 | */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 207 | BOOST_FIXTURE_TEST_CASE(LsaNotNew, SyncLogicFixture) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 208 | { |
| 209 | auto testLsaAlwaysFalse = [] (const ndn::Name& routerName, const Lsa::Type& lsaType, |
| 210 | const uint64_t& sequenceNumber) { |
| 211 | return false; |
| 212 | }; |
| 213 | |
| 214 | const uint64_t sequenceNumber = 1; |
| 215 | SyncLogicHandler sync{this->face, testLsaAlwaysFalse, this->conf}; |
| 216 | ndn::util::signal::ScopedConnection connection = sync.onNewLsa->connect( |
Ashlesh Gawande | 08bce9c | 2019-04-05 11:08:07 -0500 | [diff] [blame] | 217 | [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber, |
| 218 | const ndn::Name& originRouter) { |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 219 | BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!"); |
| 220 | }); |
| 221 | |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 222 | std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(Lsa::Type::NAME); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 223 | |
| 224 | this->receiveUpdate(updateName, sequenceNumber); |
| 225 | } |
| 226 | |
| 227 | /* Tests that SyncLogicHandler successfully concatenates configured |
| 228 | variables together to form the necessary prefixes to advertise |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 229 | through sync. |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 230 | */ |
Ashlesh Gawande | 30d96e4 | 2021-03-21 19:15:33 -0700 | [diff] [blame^] | 231 | BOOST_FIXTURE_TEST_CASE(UpdatePrefix, SyncLogicFixture) |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 232 | { |
| 233 | ndn::Name expectedPrefix = this->conf.getLsaPrefix(); |
| 234 | expectedPrefix.append(this->conf.getSiteName()); |
| 235 | expectedPrefix.append(this->conf.getRouterName()); |
| 236 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 237 | BOOST_CHECK_EQUAL(this->sync.m_nameLsaUserPrefix, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 238 | ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::NAME))); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 239 | BOOST_CHECK_EQUAL(this->sync.m_adjLsaUserPrefix, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 240 | ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY))); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(this->sync.m_coorLsaUserPrefix, |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 242 | ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE))); |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 243 | } |
| 244 | |
Ashlesh Gawande | 85998a1 | 2017-12-07 22:22:13 -0600 | [diff] [blame] | 245 | BOOST_AUTO_TEST_SUITE_END() |
| 246 | |
| 247 | } // namespace test |
| 248 | } // namespace nlsr |