akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 2 | /** |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, The University of Memphis, |
Vince Lehman | c2e51f6 | 2015-01-20 15:03:11 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 20 | **/ |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 21 | |
| 22 | #include "test-common.hpp" |
| 23 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 24 | #include "lsdb.hpp" |
| 25 | #include "nlsr.hpp" |
| 26 | #include "lsa.hpp" |
| 27 | #include "name-prefix-list.hpp" |
| 28 | #include <boost/test/unit_test.hpp> |
| 29 | |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 31 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 32 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 33 | namespace nlsr { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 34 | namespace test { |
| 35 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 36 | using ndn::shared_ptr; |
| 37 | |
| 38 | class LsdbFixture : public BaseFixture |
| 39 | { |
| 40 | public: |
| 41 | LsdbFixture() |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 42 | : face(make_shared<ndn::util::DummyClientFace>(g_ioService)) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 43 | , nlsr(g_ioService, g_scheduler, ndn::ref(*face)) |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 44 | , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter(), nlsr.getSequencingManager()) |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 45 | , lsdb(nlsr.getLsdb()) |
| 46 | , conf(nlsr.getConfParameter()) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 47 | , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib") |
| 48 | , REGISTER_VERB("register") |
| 49 | { |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 50 | conf.setNetwork("/ndn"); |
| 51 | conf.setSiteName("/site"); |
| 52 | conf.setRouterName("/%C1.router/this-router"); |
| 53 | |
| 54 | nlsr.initialize(); |
| 55 | |
| 56 | face->processEvents(ndn::time::milliseconds(1)); |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 57 | face->sentInterests.clear(); |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 58 | |
| 59 | INIT_LOGGERS("/tmp", "DEBUG"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 62 | void |
| 63 | extractParameters(ndn::Interest& interest, ndn::Name::Component& verb, |
| 64 | ndn::nfd::ControlParameters& extractedParameters) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 65 | { |
| 66 | const ndn::Name& name = interest.getName(); |
| 67 | verb = name[REGISTER_COMMAND_PREFIX.size()]; |
| 68 | const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1]; |
| 69 | |
| 70 | ndn::Block rawParameters = parameterComponent.blockFromValue(); |
| 71 | extractedParameters.wireDecode(rawParameters); |
| 72 | } |
| 73 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 74 | void |
| 75 | areNamePrefixListsEqual(NamePrefixList& lhs, NamePrefixList& rhs) |
| 76 | { |
| 77 | typedef std::list<ndn::Name> NameList; |
| 78 | |
| 79 | NameList& lhsList = lhs.getNameList(); |
| 80 | NameList& rhsList = rhs.getNameList(); |
| 81 | |
| 82 | BOOST_REQUIRE_EQUAL(lhsList.size(), rhsList.size()); |
| 83 | |
| 84 | NameList::iterator i = lhsList.begin(); |
| 85 | NameList::iterator j = rhsList.begin(); |
| 86 | |
| 87 | for (; i != lhsList.end(); ++i, ++j) { |
| 88 | BOOST_CHECK_EQUAL(*i, *j); |
| 89 | } |
| 90 | } |
| 91 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 92 | public: |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 93 | shared_ptr<ndn::util::DummyClientFace> face; |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 94 | Nlsr nlsr; |
| 95 | SyncLogicHandler sync; |
| 96 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 97 | Lsdb& lsdb; |
| 98 | ConfParameter& conf; |
| 99 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 100 | ndn::Name REGISTER_COMMAND_PREFIX; |
| 101 | ndn::Name::Component REGISTER_VERB; |
| 102 | }; |
| 103 | |
| 104 | BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture) |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 105 | |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 106 | BOOST_AUTO_TEST_CASE(LsdbSync) |
| 107 | { |
| 108 | ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router2/name"); |
| 109 | uint64_t oldSeqNo = 82; |
| 110 | |
| 111 | ndn::Name oldInterestName = interestName; |
| 112 | oldInterestName.appendNumber(oldSeqNo); |
| 113 | |
| 114 | lsdb.expressInterest(oldInterestName, 0); |
| 115 | face->processEvents(ndn::time::milliseconds(1)); |
| 116 | |
Muktadir R Chowdhury | c69da0a | 2015-12-18 13:24:38 -0600 | [diff] [blame] | 117 | std::vector<ndn::Interest>& interests = face->sentInterests; |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 118 | |
| 119 | BOOST_REQUIRE(interests.size() > 0); |
| 120 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
| 121 | |
| 122 | BOOST_CHECK_EQUAL(it->getName(), oldInterestName); |
| 123 | interests.clear(); |
| 124 | |
| 125 | steady_clock::TimePoint deadline = steady_clock::now() + |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 126 | ndn::time::seconds(LSA_REFRESH_TIME_MAX); |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 127 | |
| 128 | // Simulate an LSA interest timeout |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 129 | lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout", |
| 130 | oldInterestName, 0, deadline, interestName, oldSeqNo); |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 131 | face->processEvents(ndn::time::milliseconds(1)); |
| 132 | |
| 133 | BOOST_REQUIRE(interests.size() > 0); |
| 134 | it = interests.begin(); |
| 135 | |
| 136 | BOOST_CHECK_EQUAL(it->getName(), oldInterestName); |
| 137 | interests.clear(); |
| 138 | |
| 139 | uint64_t newSeqNo = 83; |
| 140 | |
| 141 | ndn::Name newInterestName = interestName; |
| 142 | newInterestName.appendNumber(newSeqNo); |
| 143 | |
| 144 | lsdb.expressInterest(newInterestName, 0); |
| 145 | face->processEvents(ndn::time::milliseconds(1)); |
| 146 | |
| 147 | BOOST_REQUIRE(interests.size() > 0); |
| 148 | it = interests.begin(); |
| 149 | |
| 150 | BOOST_CHECK_EQUAL(it->getName(), newInterestName); |
| 151 | interests.clear(); |
| 152 | |
| 153 | // Simulate an LSA interest timeout where the sequence number is outdated |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 154 | lsdb.onFetchLsaError(ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT, "Timeout", |
| 155 | oldInterestName, 0, deadline, interestName, oldSeqNo); |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 156 | face->processEvents(ndn::time::milliseconds(1)); |
| 157 | |
| 158 | // Interest should not be expressed for outdated sequence number |
| 159 | BOOST_CHECK_EQUAL(interests.size(), 0); |
| 160 | } |
| 161 | |
Muktadir R Chowdhury | aa3b085 | 2015-08-06 13:08:56 -0500 | [diff] [blame] | 162 | BOOST_AUTO_TEST_CASE(SegmentLsaData) |
| 163 | { |
| 164 | ndn::Name router("/ndn/cs/%C1.Router/router1"); |
| 165 | uint64_t seqNo = 12; |
| 166 | NamePrefixList prefixList; |
| 167 | |
| 168 | NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList); |
| 169 | |
| 170 | ndn::Name prefix("/ndn/edu/memphis/netlab/research/nlsr/test/prefix/"); |
| 171 | |
| 172 | int nPrefixes = 0; |
| 173 | while (lsa.getData().size() < ndn::MAX_NDN_PACKET_SIZE) { |
| 174 | lsa.addName(ndn::Name(prefix).appendNumber(++nPrefixes)); |
| 175 | } |
| 176 | |
| 177 | std::string expectedDataContent = lsa.getData(); |
| 178 | lsdb.installNameLsa(lsa); |
| 179 | |
| 180 | ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/name/"); |
| 181 | interestName.appendNumber(seqNo); |
| 182 | |
| 183 | ndn::Interest interest(interestName); |
| 184 | |
| 185 | lsdb.processInterest(ndn::Name(), interest); |
| 186 | face->processEvents(ndn::time::milliseconds(1)); |
| 187 | |
| 188 | std::vector<ndn::Data> data = face->sentData; |
| 189 | std::string recvDataContent; |
| 190 | for (unsigned int i = 0; i < data.size(); ++i) |
| 191 | { |
| 192 | const ndn::Block& nameBlock = data[i].getContent(); |
| 193 | |
| 194 | std::string nameBlockContent(reinterpret_cast<char const*>(nameBlock.value()), |
| 195 | nameBlock.value_size()); |
| 196 | recvDataContent += nameBlockContent; |
| 197 | } |
| 198 | |
| 199 | BOOST_CHECK_EQUAL(expectedDataContent, recvDataContent); |
| 200 | } |
| 201 | |
| 202 | BOOST_AUTO_TEST_CASE(ReceiveSegmentedLsaData) |
| 203 | { |
| 204 | ndn::Name router("/ndn/cs/%C1.Router/router1"); |
| 205 | uint64_t seqNo = 12; |
| 206 | NamePrefixList prefixList; |
| 207 | |
| 208 | NameLsa lsa(router, seqNo, ndn::time::system_clock::now(), prefixList); |
| 209 | |
| 210 | ndn::Name prefix("/prefix/"); |
| 211 | |
| 212 | for (int nPrefixes = 0; nPrefixes < 3; ++nPrefixes) { |
| 213 | lsa.addName(ndn::Name(prefix).appendNumber(nPrefixes)); |
| 214 | } |
| 215 | |
| 216 | ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router1/name/"); |
| 217 | interestName.appendNumber(seqNo); |
| 218 | |
| 219 | const ndn::ConstBufferPtr bufferPtr = make_shared<ndn::Buffer>(lsa.getData().c_str(), |
| 220 | lsa.getData().size()); |
| 221 | lsdb.afterFetchLsa(bufferPtr, interestName); |
| 222 | |
| 223 | NameLsa* foundLsa = lsdb.findNameLsa(lsa.getKey()); |
| 224 | BOOST_REQUIRE(foundLsa != nullptr); |
| 225 | |
| 226 | BOOST_CHECK_EQUAL(foundLsa->getData(), lsa.getData()); |
| 227 | } |
| 228 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 229 | BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists) |
| 230 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 231 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 232 | NamePrefixList npl1; |
| 233 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 234 | std::string s1 = "name1"; |
| 235 | std::string s2 = "name2"; |
| 236 | std::string router1 = "router1/1"; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 237 | |
| 238 | npl1.insert(s1); |
| 239 | npl1.insert(s2); |
| 240 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 241 | //For NameLsa lsType is name. |
| 242 | //12 is seqNo, randomly generated. |
| 243 | //1800 is the default life time. |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 244 | NameLsa nlsa1(ndn::Name("/router1/1"), 12, testTimePoint, npl1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 245 | |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame] | 246 | Lsdb lsdb1(nlsr, g_scheduler, nlsr.getSyncLogicHandler()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 247 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 248 | lsdb1.installNameLsa(nlsa1); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 249 | lsdb1.writeNameLsdbLog(); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 250 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 251 | BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), NameLsa::TYPE_STRING)); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 252 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 253 | lsdb1.removeNameLsa(router1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 254 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 255 | BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), NameLsa::TYPE_STRING), false); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 256 | } |
| 257 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 258 | BOOST_AUTO_TEST_CASE(InstallNameLsa) |
| 259 | { |
| 260 | // Install lsa with name1 and name2 |
| 261 | ndn::Name name1("/ndn/name1"); |
| 262 | ndn::Name name2("/ndn/name2"); |
| 263 | |
| 264 | NamePrefixList prefixes; |
| 265 | prefixes.insert(name1); |
| 266 | prefixes.insert(name2); |
| 267 | |
| 268 | std::string otherRouter("/ndn/site/%C1.router/other-router"); |
| 269 | ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max(); |
| 270 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 271 | NameLsa lsa(otherRouter, 1, MAX_TIME, prefixes); |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 272 | lsdb.installNameLsa(lsa); |
| 273 | |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 274 | BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/name", NameLsa::TYPE_STRING), true); |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 275 | NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/name")->getNpl(); |
| 276 | |
| 277 | areNamePrefixListsEqual(nameList, prefixes); |
| 278 | |
| 279 | // Add a prefix: name3 |
| 280 | ndn::Name name3("/ndn/name3"); |
| 281 | prefixes.insert(name3); |
| 282 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 283 | NameLsa addLsa(otherRouter, 2, MAX_TIME, prefixes); |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 284 | lsdb.installNameLsa(addLsa); |
| 285 | |
| 286 | // Lsa should include name1, name2, and name3 |
| 287 | areNamePrefixListsEqual(nameList, prefixes); |
| 288 | |
| 289 | // Remove a prefix: name2 |
| 290 | prefixes.remove(name2); |
| 291 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 292 | NameLsa removeLsa(otherRouter, 3, MAX_TIME, prefixes); |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 293 | lsdb.installNameLsa(removeLsa); |
| 294 | |
| 295 | // Lsa should include name1 and name3 |
| 296 | areNamePrefixListsEqual(nameList, prefixes); |
| 297 | |
| 298 | // Add and remove a prefix: add name2, remove name3 |
| 299 | prefixes.insert(name2); |
| 300 | prefixes.remove(name3); |
| 301 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 302 | NameLsa addAndRemoveLsa(otherRouter, 4, MAX_TIME, prefixes); |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 303 | lsdb.installNameLsa(addAndRemoveLsa); |
| 304 | |
| 305 | // Lsa should include name1 and name2 |
| 306 | areNamePrefixListsEqual(nameList, prefixes); |
| 307 | |
| 308 | // Install a completely new list of prefixes |
| 309 | ndn::Name name4("/ndn/name4"); |
| 310 | ndn::Name name5("/ndn/name5"); |
| 311 | |
| 312 | NamePrefixList newPrefixes; |
| 313 | newPrefixes.insert(name4); |
| 314 | newPrefixes.insert(name5); |
| 315 | |
Ashlesh Gawande | d02c388 | 2015-12-29 16:02:51 -0600 | [diff] [blame] | 316 | NameLsa newLsa(otherRouter, 5, MAX_TIME, newPrefixes); |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 317 | lsdb.installNameLsa(newLsa); |
| 318 | |
| 319 | // Lsa should include name4 and name5 |
| 320 | areNamePrefixListsEqual(nameList, newPrefixes); |
| 321 | } |
| 322 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 323 | BOOST_AUTO_TEST_SUITE_END() |
| 324 | |
| 325 | } //namespace test |
| 326 | } //namespace nlsr |