blob: dddbf1d1bb4311ee3377d13c2163f45dd5c0eef8 [file] [log] [blame]
Vince Lehman904c2412014-09-23 19:36:11 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 **/
21
22#include "test-common.hpp"
23#include "dummy-face.hpp"
24
25#include "nlsr.hpp"
26#include "communication/sync-logic-handler.hpp"
27
28namespace nlsr {
29namespace test {
30
31using ndn::DummyFace;
32using ndn::shared_ptr;
33
34class SyncLogicFixture : public BaseFixture
35{
36public:
37 SyncLogicFixture()
38 : face(ndn::makeDummyFace())
39 , 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
52 receiveUpdate(std::string prefix, uint64_t seqNo)
53 {
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));
60 face->m_sentInterests.clear();
61
62 sync.onNsyncUpdate(updates, NULL);
63
64 face->processEvents(ndn::time::milliseconds(1));
65 }
66
67public:
68 shared_ptr<DummyFace> face;
69 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
79BOOST_AUTO_TEST_CASE(UpdateForOther)
80{
81 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
82 CONFIG_SITE + "/%C1.Router/other-router/";
83
84 receiveUpdate(updateName, 1);
85
86 std::vector<ndn::Interest>& interests = face->m_sentInterests;
87 BOOST_REQUIRE_EQUAL(interests.size(), 3);
88
89 std::vector<ndn::Interest>::iterator it = interests.begin();
90 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "name/");
91
92 ++it;
93 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "adjacency/");
94
95 ++it;
96 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "coordinate/");
97}
98
99BOOST_AUTO_TEST_CASE(NoUpdateForSelf)
100{
101 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
102 CONFIG_SITE + CONFIG_ROUTER_NAME;
103
104 receiveUpdate(updateName, 1);
105
106 std::vector<ndn::Interest>& interests = face->m_sentInterests;
107 BOOST_CHECK_EQUAL(interests.size(), 0);
108}
109
110BOOST_AUTO_TEST_CASE(MalformedUpdate)
111{
112 std::string updateName = CONFIG_SITE + nlsr.getConfParameter().getLsaPrefix().toUri() +
113 CONFIG_ROUTER_NAME;
114
115 std::vector<ndn::Interest>& interests = face->m_sentInterests;
116 BOOST_CHECK_EQUAL(interests.size(), 0);
117}
118
119BOOST_AUTO_TEST_CASE(SequenceNumber)
120{
121 std::string originRouter = CONFIG_NETWORK + CONFIG_SITE + "/%C1.Router/other-router/";
122
123 Lsdb& lsdb = nlsr.getLsdb();
124
125 // Install Name LSA
126 NamePrefixList nameList;
127 NameLsa lsa(originRouter, "name", 999, ndn::time::system_clock::TimePoint::max(), nameList);
128 lsdb.installNameLsa(lsa);
129
130 // Install Adj LSA
131 AdjacencyList adjList;
132 AdjLsa adjLsa(originRouter, "adjacency", 1000, ndn::time::system_clock::TimePoint::max(),
133 3 , adjList);
134 lsdb.installAdjLsa(adjLsa);
135
136 // Install Cor LSA
137 CoordinateLsa corLsa(originRouter, "coordinate", 1000, ndn::time::system_clock::TimePoint::max(),
138 0,0);
139 lsdb.installCoordinateLsa(corLsa);
140
141 std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() +
142 CONFIG_SITE + "/%C1.Router/other-router/";
143
144 // Lower NameLSA sequence number
145 uint64_t lowerSeqNo = static_cast<uint64_t>(998) << 40;
146 receiveUpdate(updateName, lowerSeqNo);
147
148 std::vector<ndn::Interest>& interests = face->m_sentInterests;
149 BOOST_REQUIRE_EQUAL(interests.size(), 0);
150
151 // Same NameLSA sequence number
152 uint64_t sameSeqNo = static_cast<uint64_t>(999) << 40;
153 receiveUpdate(updateName, sameSeqNo);
154
155 interests = face->m_sentInterests;
156 BOOST_REQUIRE_EQUAL(interests.size(), 0);
157
158 // Higher NameLSA sequence number
159 uint64_t higherSeqNo = static_cast<uint64_t>(1000) << 40;
160 receiveUpdate(updateName, higherSeqNo);
161
162 interests = face->m_sentInterests;
163 BOOST_REQUIRE_EQUAL(interests.size(), 1);
164
165 std::vector<ndn::Interest>::iterator it = interests.begin();
166 BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "name/");
167}
168
Vince Lehmanc11cc202015-01-20 11:41:33 -0600169BOOST_AUTO_TEST_CASE(UpdatePrefix)
170{
171 ndn::Name expectedPrefix = nlsr.getConfParameter().getLsaPrefix();
172 expectedPrefix.append(CONFIG_SITE);
173 expectedPrefix.append(CONFIG_ROUTER_NAME);
174
175 nlsr.initialize();
176
177 BOOST_CHECK_EQUAL(sync.m_updatePrefix, expectedPrefix);
178}
179
Vince Lehman904c2412014-09-23 19:36:11 -0500180BOOST_AUTO_TEST_SUITE_END()
181
182} // namespace test
183} // namespace nlsr