blob: 55007b8e907ccc61614e61f1b8b1eae68a8efff5 [file] [log] [blame]
Ashlesh Gawande85998a12017-12-07 22:22:13 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande30d96e42021-03-21 19:15:33 -07002/*
3 * Copyright (c) 2014-2021, The University of Memphis,
Ashlesh Gawande85998a12017-12-07 22:22:13 -06004 * 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 Gawande30d96e42021-03-21 19:15:33 -070020 */
Ashlesh Gawande85998a12017-12-07 22:22:13 -060021
22#include "communication/sync-logic-handler.hpp"
Saurab Dulal427e0122019-11-28 11:58:02 -060023#include "tests/test-common.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060024#include "common.hpp"
25#include "nlsr.hpp"
Ashlesh Gawande85998a12017-12-07 22:22:13 -060026
27#include <ndn-cxx/util/dummy-client-face.hpp>
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070028#include <boost/lexical_cast.hpp>
Ashlesh Gawande85998a12017-12-07 22:22:13 -060029
30namespace nlsr {
31namespace test {
32
33using std::shared_ptr;
34
Ashlesh Gawande85998a12017-12-07 22:22:13 -060035class SyncLogicFixture : public UnitTestTimeFixture
36{
37public:
38 SyncLogicFixture()
39 : face(m_ioService, m_keyChain)
Saurab Dulal427e0122019-11-28 11:58:02 -060040 , conf(face, m_keyChain)
Ashlesh Gawande30d96e42021-03-21 19:15:33 -070041 , confProcessor(conf, SYNC_PROTOCOL_PSYNC)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060042 , 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 Gawande30d96e42021-03-21 19:15:33 -070060 std::vector<psync::MissingDataInfo> updates;
61 updates.push_back({ndn::Name(prefix), 0, seqNo});
62 sync.m_syncLogic.onPSyncUpdate(updates);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060063
64 this->advanceClocks(ndn::time::milliseconds(1), 10);
65 }
66
67public:
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 Gawande85998a12017-12-07 22:22:13 -060079BOOST_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 Gawande30d96e42021-03-21 19:15:33 -070085BOOST_FIXTURE_TEST_CASE(UpdateForOtherLS, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -060086{
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 Gawande0db4d4d2020-02-05 20:30:02 -080092 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -060093
94 // Actual testing done here -- signal function callback
95 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -050096 [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber,
97 const ndn::Name& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -060098 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 Gawande30d96e42021-03-21 19:15:33 -0700110BOOST_FIXTURE_TEST_CASE(UpdateForOtherHR, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600111{
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 Gawande0db4d4d2020-02-05 20:30:02 -0800118 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600119
120 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500121 [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber,
122 const ndn::Name& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600123 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 Gawande30d96e42021-03-21 19:15:33 -0700135BOOST_FIXTURE_TEST_CASE(UpdateForOtherHRDry, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600136{
137 this->conf.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
138
139 uint64_t syncSeqNo = 1;
140
141 for (const Lsa::Type& lsaType : this->lsaTypes) {
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800142 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(lsaType);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600143
144 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500145 [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber,
146 const ndn::Name& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600147 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 Gawande30d96e42021-03-21 19:15:33 -0700159BOOST_FIXTURE_TEST_CASE(NoUpdateForSelf, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600160{
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 Gawande0db4d4d2020-02-05 20:30:02 -0800169 .append(boost::lexical_cast<std::string>(lsaType));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600170
171 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500172 [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber,
173 const ndn::Name& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600174 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 Gawande30d96e42021-03-21 19:15:33 -0700185BOOST_FIXTURE_TEST_CASE(MalformedUpdate, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600186{
187 const uint64_t sequenceNumber = 1;
188
189 for (const Lsa::Type& lsaType : this->lsaTypes) {
190 ndn::Name updateName{this->conf.getSiteName()};
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800191 updateName.append(this->conf.getRouterName()).append(boost::lexical_cast<std::string>(lsaType));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600192
193 ndn::util::signal::ScopedConnection connection = this->sync.onNewLsa->connect(
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500194 [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber,
195 const ndn::Name& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600196 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 Gawande30d96e42021-03-21 19:15:33 -0700207BOOST_FIXTURE_TEST_CASE(LsaNotNew, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600208{
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 Gawande08bce9c2019-04-05 11:08:07 -0500217 [&] (const ndn::Name& routerName, const uint64_t& sequenceNumber,
218 const ndn::Name& originRouter) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600219 BOOST_FAIL("An update for an LSA with non-new sequence number should not emit!");
220 });
221
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800222 std::string updateName = this->updateNamePrefix + boost::lexical_cast<std::string>(Lsa::Type::NAME);
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600223
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 Gawande30d96e42021-03-21 19:15:33 -0700229 through sync.
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600230 */
Ashlesh Gawande30d96e42021-03-21 19:15:33 -0700231BOOST_FIXTURE_TEST_CASE(UpdatePrefix, SyncLogicFixture)
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600232{
233 ndn::Name expectedPrefix = this->conf.getLsaPrefix();
234 expectedPrefix.append(this->conf.getSiteName());
235 expectedPrefix.append(this->conf.getRouterName());
236
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600237 BOOST_CHECK_EQUAL(this->sync.m_nameLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800238 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::NAME)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600239 BOOST_CHECK_EQUAL(this->sync.m_adjLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800240 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::ADJACENCY)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600241 BOOST_CHECK_EQUAL(this->sync.m_coorLsaUserPrefix,
Ashlesh Gawande0db4d4d2020-02-05 20:30:02 -0800242 ndn::Name(expectedPrefix).append(boost::lexical_cast<std::string>(Lsa::Type::COORDINATE)));
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600243}
244
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600245BOOST_AUTO_TEST_SUITE_END()
246
247} // namespace test
248} // namespace nlsr