blob: e836976c605c64f86834ec189ef93878103afe28 [file] [log] [blame]
Vince Lehman904c2412014-09-23 19:36:11 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
Vince Lehman904c2412014-09-23 19:36:11 -05006 *
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/>.
Vince Lehman904c2412014-09-23 19:36:11 -050020 **/
21
22#include "test-common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050023
24#include "nlsr.hpp"
25#include "communication/sync-logic-handler.hpp"
26
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060027#include <ndn-cxx/util/dummy-client-face.hpp>
28
Vince Lehman904c2412014-09-23 19:36:11 -050029namespace nlsr {
30namespace test {
31
Vince Lehman904c2412014-09-23 19:36:11 -050032using ndn::shared_ptr;
33
34class SyncLogicFixture : public BaseFixture
35{
36public:
37 SyncLogicFixture()
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060038 : face(make_shared<ndn::util::DummyClientFace>())
Vince Lehman904c2412014-09-23 19:36:11 -050039 , nlsr(g_ioService, g_scheduler, ndn::ref(*face))
Vince Lehmanc11cc202015-01-20 11:41:33 -060040 , sync(nlsr.getSyncLogicHandler())
Vince Lehman904c2412014-09-23 19:36:11 -050041 , CONFIG_NETWORK("/ndn")
42 , CONFIG_SITE("/site")
43 , CONFIG_ROUTER_NAME("/%C1.Router/this-router")
44 {
45 nlsr.getConfParameter().setNetwork(CONFIG_NETWORK);
46 nlsr.getConfParameter().setSiteName(CONFIG_SITE);
47 nlsr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME);
48 nlsr.getConfParameter().buildRouterPrefix();
49 }
50
51 void
Nick Gordon5c467f02016-07-13 13:40:10 -050052 receiveUpdate(std::string prefix, uint64_t seqNo, SyncLogicHandler& p_sync)
Vince Lehman904c2412014-09-23 19:36:11 -050053 {
54 Sync::MissingDataInfo info = {prefix, 0, seqNo};
55
56 std::vector<Sync::MissingDataInfo> updates;
57 updates.push_back(info);
58
59 face->processEvents(ndn::time::milliseconds(1));
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060060 face->sentInterests.clear();
Vince Lehman904c2412014-09-23 19:36:11 -050061
Nick Gordon5c467f02016-07-13 13:40:10 -050062 p_sync.onNsyncUpdate(updates, NULL);
Vince Lehman904c2412014-09-23 19:36:11 -050063
64 face->processEvents(ndn::time::milliseconds(1));
65 }
66
67public:
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060068 shared_ptr<ndn::util::DummyClientFace> face;
Vince Lehman904c2412014-09-23 19:36:11 -050069 Nlsr nlsr;
Vince Lehmanc11cc202015-01-20 11:41:33 -060070 SyncLogicHandler& sync;
Vince Lehman904c2412014-09-23 19:36:11 -050071
72 const std::string CONFIG_NETWORK;
73 const std::string CONFIG_SITE;
74 const std::string CONFIG_ROUTER_NAME;
75};
76
77BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture)
78
Nick Gordon5c467f02016-07-13 13:40:10 -050079BOOST_AUTO_TEST_CASE(UpdateForOtherLS)
Vince Lehman904c2412014-09-23 19:36:11 -050080{
81 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
82 CONFIG_SITE + "/%C1.Router/other-router/";
83
Nick Gordon5c467f02016-07-13 13:40:10 -050084 uint64_t syncSeqNo = 1;
85 receiveUpdate(updateName, syncSeqNo, sync);
Vince Lehman904c2412014-09-23 19:36:11 -050086
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060087 std::vector<ndn::Interest>& interests = face->sentInterests;
Nick Gordone8e03ac2016-07-07 14:24:38 -050088
Nick Gordon5c467f02016-07-13 13:40:10 -050089 std::vector<ndn::Interest>::iterator it = interests.begin();
90
91 BOOST_REQUIRE_EQUAL(interests.size(), 2);
92
93 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + NameLsa::TYPE_STRING + "/");
94
95 ++it;
96 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + AdjLsa::TYPE_STRING + "/");
97}
98
99BOOST_AUTO_TEST_CASE(UpdateForOtherHR)
100{
101 Nlsr nlsr_hr(g_ioService, g_scheduler, ndn::ref(*face));
102 SyncLogicHandler& sync_hr(nlsr_hr.getSyncLogicHandler());
103
104 nlsr_hr.getConfParameter().setNetwork(CONFIG_NETWORK);
105 nlsr_hr.getConfParameter().setSiteName(CONFIG_SITE);
106 nlsr_hr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME);
107 nlsr_hr.getConfParameter().buildRouterPrefix();
108
109 nlsr_hr.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_ON);
110
111 nlsr_hr.initialize();
112
113 std::string updateName = nlsr_hr.getConfParameter().getLsaPrefix().toUri() +
114 CONFIG_SITE + "/%C1.Router/other-router/";
115
116 uint64_t syncSeqNo = 0;
117 syncSeqNo = syncSeqNo | (static_cast<uint64_t>(1) << 20);
118
119 receiveUpdate(updateName, syncSeqNo, sync_hr);
120
121 std::vector<ndn::Interest>& interests = face->sentInterests;
122 std::vector<ndn::Interest>::iterator it = interests.begin();
123
124 BOOST_REQUIRE_EQUAL(interests.size(), 2);
125
126 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + NameLsa::TYPE_STRING + "/");
127
128 ++it;
129 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + CoordinateLsa::TYPE_STRING + "/");
130}
131
132BOOST_AUTO_TEST_CASE(UpdateForOtherHRDry)
133{
134
135 Nlsr nlsr_hrdry(g_ioService, g_scheduler, ndn::ref(*face));
136 SyncLogicHandler& sync_hrdry(nlsr_hrdry.getSyncLogicHandler());
137
138 nlsr_hrdry.getConfParameter().setNetwork(CONFIG_NETWORK);
139 nlsr_hrdry.getConfParameter().setSiteName(CONFIG_SITE);
140 nlsr_hrdry.getConfParameter().setRouterName(CONFIG_ROUTER_NAME);
141 nlsr_hrdry.getConfParameter().buildRouterPrefix();
142
143 nlsr_hrdry.getConfParameter().setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
144
145 nlsr_hrdry.initialize();
146
147 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
148 CONFIG_SITE + "/%C1.Router/other-router/";
149
150
151 uint64_t syncSeqNo = 1;
152 syncSeqNo = syncSeqNo | (static_cast<uint64_t>(1) << 20);
153 receiveUpdate(updateName, syncSeqNo, sync_hrdry);
154
155 std::vector<ndn::Interest>& interests = face->sentInterests;
156 std::vector<ndn::Interest>::iterator it = interests.begin();
157
Vince Lehman904c2412014-09-23 19:36:11 -0500158 BOOST_REQUIRE_EQUAL(interests.size(), 3);
159
alvy49b1c0c2014-12-19 13:57:46 -0600160 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + NameLsa::TYPE_STRING + "/");
Vince Lehman904c2412014-09-23 19:36:11 -0500161
Nick Gordone8e03ac2016-07-07 14:24:38 -0500162 if (nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
163 ++it;
164 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + CoordinateLsa::TYPE_STRING + "/");
165 }
166 else if (nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
167 ++it;
168 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + AdjLsa::TYPE_STRING + "/");
169 }
170 else {
171 ++it;
172 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + CoordinateLsa::TYPE_STRING + "/");
Vince Lehman904c2412014-09-23 19:36:11 -0500173
Nick Gordone8e03ac2016-07-07 14:24:38 -0500174 ++it;
175 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + AdjLsa::TYPE_STRING + "/");
176 }
Vince Lehman904c2412014-09-23 19:36:11 -0500177}
178
179BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
180{
181 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
182 CONFIG_SITE + CONFIG_ROUTER_NAME;
183
Nick Gordon5c467f02016-07-13 13:40:10 -0500184 receiveUpdate(updateName, 1, sync);
Vince Lehman904c2412014-09-23 19:36:11 -0500185
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -0600186 std::vector<ndn::Interest>& interests = face->sentInterests;
Vince Lehman904c2412014-09-23 19:36:11 -0500187 BOOST_CHECK_EQUAL(interests.size(), 0);
188}
189
190BOOST_AUTO_TEST_CASE(MalformedUpdate)
191{
192 std::string updateName = CONFIG_SITE + nlsr.getConfParameter().getLsaPrefix().toUri() +
193 CONFIG_ROUTER_NAME;
194
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -0600195 std::vector<ndn::Interest>& interests = face->sentInterests;
Vince Lehman904c2412014-09-23 19:36:11 -0500196 BOOST_CHECK_EQUAL(interests.size(), 0);
197}
198
199BOOST_AUTO_TEST_CASE(SequenceNumber)
200{
201 std::string originRouter = CONFIG_NETWORK + CONFIG_SITE + "/%C1.Router/other-router/";
202
203 Lsdb& lsdb = nlsr.getLsdb();
204
205 // Install Name LSA
206 NamePrefixList nameList;
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600207 NameLsa lsa(originRouter, 999, ndn::time::system_clock::TimePoint::max(), nameList);
Vince Lehman904c2412014-09-23 19:36:11 -0500208 lsdb.installNameLsa(lsa);
209
210 // Install Adj LSA
211 AdjacencyList adjList;
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600212 AdjLsa adjLsa(originRouter, 1000, ndn::time::system_clock::TimePoint::max(),
Vince Lehman904c2412014-09-23 19:36:11 -0500213 3 , adjList);
214 lsdb.installAdjLsa(adjLsa);
215
216 // Install Cor LSA
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600217 CoordinateLsa corLsa(originRouter, 1000, ndn::time::system_clock::TimePoint::max(),
Vince Lehman904c2412014-09-23 19:36:11 -0500218 0,0);
219 lsdb.installCoordinateLsa(corLsa);
220
221 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
222 CONFIG_SITE + "/%C1.Router/other-router/";
223
224 // Lower NameLSA sequence number
225 uint64_t lowerSeqNo = static_cast<uint64_t>(998) << 40;
Nick Gordon5c467f02016-07-13 13:40:10 -0500226 receiveUpdate(updateName, lowerSeqNo, sync);
Vince Lehman904c2412014-09-23 19:36:11 -0500227
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -0600228 std::vector<ndn::Interest>& interests = face->sentInterests;
Vince Lehman904c2412014-09-23 19:36:11 -0500229 BOOST_REQUIRE_EQUAL(interests.size(), 0);
230
231 // Same NameLSA sequence number
232 uint64_t sameSeqNo = static_cast<uint64_t>(999) << 40;
Nick Gordon5c467f02016-07-13 13:40:10 -0500233 receiveUpdate(updateName, sameSeqNo, sync);
Vince Lehman904c2412014-09-23 19:36:11 -0500234
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -0600235 interests = face->sentInterests;
Vince Lehman904c2412014-09-23 19:36:11 -0500236 BOOST_REQUIRE_EQUAL(interests.size(), 0);
237
238 // Higher NameLSA sequence number
239 uint64_t higherSeqNo = static_cast<uint64_t>(1000) << 40;
Nick Gordon5c467f02016-07-13 13:40:10 -0500240 receiveUpdate(updateName, higherSeqNo, sync);
Vince Lehman904c2412014-09-23 19:36:11 -0500241
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -0600242 interests = face->sentInterests;
Vince Lehman904c2412014-09-23 19:36:11 -0500243 BOOST_REQUIRE_EQUAL(interests.size(), 1);
244
245 std::vector<ndn::Interest>::iterator it = interests.begin();
246 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "name/");
247}
248
Vince Lehmanc11cc202015-01-20 11:41:33 -0600249BOOST_AUTO_TEST_CASE(UpdatePrefix)
250{
251 ndn::Name expectedPrefix = nlsr.getConfParameter().getLsaPrefix();
252 expectedPrefix.append(CONFIG_SITE);
253 expectedPrefix.append(CONFIG_ROUTER_NAME);
254
255 nlsr.initialize();
256
257 BOOST_CHECK_EQUAL(sync.m_updatePrefix, expectedPrefix);
258}
259
Vince Lehman9d097802015-03-16 17:55:59 -0500260BOOST_AUTO_TEST_CASE(CreateSyncSocketOnInitialization) // Bug #2649
261{
262 nlsr.initialize();
263
264 // Make sure an adjacency LSA has not been built yet
265 ndn::Name key = ndn::Name(nlsr.getConfParameter().getRouterPrefix()).append(AdjLsa::TYPE_STRING);
266 AdjLsa* lsa = nlsr.getLsdb().findAdjLsa(key);
267 BOOST_REQUIRE(lsa == nullptr);
268
269 // Publish a routing update before an Adjacency LSA is built
270 BOOST_CHECK_NO_THROW(sync.publishRoutingUpdate());
271}
272
Vince Lehman904c2412014-09-23 19:36:11 -0500273BOOST_AUTO_TEST_SUITE_END()
274
275} // namespace test
276} // namespace nlsr