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 | /** |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 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 | * \author Ashlesh Gawande <agawande@memphis.edu> |
| 21 | * |
| 22 | **/ |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 23 | |
| 24 | #include "test-common.hpp" |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 25 | #include "dummy-face.hpp" |
Vince Lehman | 7c60329 | 2014-09-11 17:48:16 -0500 | [diff] [blame] | 26 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 27 | #include "lsdb.hpp" |
| 28 | #include "nlsr.hpp" |
| 29 | #include "lsa.hpp" |
| 30 | #include "name-prefix-list.hpp" |
| 31 | #include <boost/test/unit_test.hpp> |
| 32 | |
| 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::DummyFace; |
| 37 | using ndn::shared_ptr; |
| 38 | |
| 39 | class LsdbFixture : public BaseFixture |
| 40 | { |
| 41 | public: |
| 42 | LsdbFixture() |
| 43 | : face(ndn::makeDummyFace()) |
| 44 | , nlsr(g_ioService, g_scheduler, ndn::ref(*face)) |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame^] | 45 | , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter(), nlsr.getSequencingManager()) |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 46 | , lsdb(nlsr.getLsdb()) |
| 47 | , conf(nlsr.getConfParameter()) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 48 | , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib") |
| 49 | , REGISTER_VERB("register") |
| 50 | { |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 51 | conf.setNetwork("/ndn"); |
| 52 | conf.setSiteName("/site"); |
| 53 | conf.setRouterName("/%C1.router/this-router"); |
| 54 | |
| 55 | nlsr.initialize(); |
| 56 | |
| 57 | face->processEvents(ndn::time::milliseconds(1)); |
| 58 | face->m_sentInterests.clear(); |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 59 | |
| 60 | INIT_LOGGERS("/tmp", "DEBUG"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 63 | void |
| 64 | extractParameters(ndn::Interest& interest, ndn::Name::Component& verb, |
| 65 | ndn::nfd::ControlParameters& extractedParameters) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 66 | { |
| 67 | const ndn::Name& name = interest.getName(); |
| 68 | verb = name[REGISTER_COMMAND_PREFIX.size()]; |
| 69 | const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1]; |
| 70 | |
| 71 | ndn::Block rawParameters = parameterComponent.blockFromValue(); |
| 72 | extractedParameters.wireDecode(rawParameters); |
| 73 | } |
| 74 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 75 | void |
| 76 | areNamePrefixListsEqual(NamePrefixList& lhs, NamePrefixList& rhs) |
| 77 | { |
| 78 | typedef std::list<ndn::Name> NameList; |
| 79 | |
| 80 | NameList& lhsList = lhs.getNameList(); |
| 81 | NameList& rhsList = rhs.getNameList(); |
| 82 | |
| 83 | BOOST_REQUIRE_EQUAL(lhsList.size(), rhsList.size()); |
| 84 | |
| 85 | NameList::iterator i = lhsList.begin(); |
| 86 | NameList::iterator j = rhsList.begin(); |
| 87 | |
| 88 | for (; i != lhsList.end(); ++i, ++j) { |
| 89 | BOOST_CHECK_EQUAL(*i, *j); |
| 90 | } |
| 91 | } |
| 92 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 93 | public: |
| 94 | shared_ptr<DummyFace> face; |
| 95 | Nlsr nlsr; |
| 96 | SyncLogicHandler sync; |
| 97 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 98 | Lsdb& lsdb; |
| 99 | ConfParameter& conf; |
| 100 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 101 | ndn::Name REGISTER_COMMAND_PREFIX; |
| 102 | ndn::Name::Component REGISTER_VERB; |
| 103 | }; |
| 104 | |
| 105 | BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture) |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 106 | |
Ashlesh Gawande | 5bf8317 | 2014-09-19 12:38:17 -0500 | [diff] [blame] | 107 | BOOST_AUTO_TEST_CASE(LsdbSync) |
| 108 | { |
| 109 | ndn::Name interestName("/ndn/NLSR/LSA/cs/%C1.Router/router2/name"); |
| 110 | uint64_t oldSeqNo = 82; |
| 111 | |
| 112 | ndn::Name oldInterestName = interestName; |
| 113 | oldInterestName.appendNumber(oldSeqNo); |
| 114 | |
| 115 | lsdb.expressInterest(oldInterestName, 0); |
| 116 | face->processEvents(ndn::time::milliseconds(1)); |
| 117 | |
| 118 | std::vector<ndn::Interest>& interests = face->m_sentInterests; |
| 119 | |
| 120 | BOOST_REQUIRE(interests.size() > 0); |
| 121 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
| 122 | |
| 123 | BOOST_CHECK_EQUAL(it->getName(), oldInterestName); |
| 124 | interests.clear(); |
| 125 | |
| 126 | steady_clock::TimePoint deadline = steady_clock::now() + |
| 127 | ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX)); |
| 128 | |
| 129 | // Simulate an LSA interest timeout |
| 130 | lsdb.processInterestTimedOut(oldInterestName, 0, deadline, interestName, oldSeqNo); |
| 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 |
| 154 | lsdb.processInterestTimedOut(oldInterestName, 0, deadline, interestName, oldSeqNo); |
| 155 | face->processEvents(ndn::time::milliseconds(1)); |
| 156 | |
| 157 | // Interest should not be expressed for outdated sequence number |
| 158 | BOOST_CHECK_EQUAL(interests.size(), 0); |
| 159 | } |
| 160 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 161 | BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists) |
| 162 | { |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 163 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 164 | NamePrefixList npl1; |
| 165 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 166 | std::string s1 = "name1"; |
| 167 | std::string s2 = "name2"; |
| 168 | std::string router1 = "router1/1"; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 169 | |
| 170 | npl1.insert(s1); |
| 171 | npl1.insert(s2); |
| 172 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 173 | //For NameLsa lsType is name. |
| 174 | //12 is seqNo, randomly generated. |
| 175 | //1800 is the default life time. |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 176 | NameLsa nlsa1(ndn::Name("/router1/1"), std::string("name"), 12, testTimePoint, npl1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 177 | |
Vince Lehman | 0bcf9a3 | 2014-12-10 11:24:45 -0600 | [diff] [blame^] | 178 | Lsdb lsdb1(nlsr, g_scheduler, nlsr.getSyncLogicHandler()); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 179 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 180 | lsdb1.installNameLsa(nlsa1); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 181 | lsdb1.writeNameLsdbLog(); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 182 | |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 183 | BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), std::string("name"))); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 184 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 185 | lsdb1.removeNameLsa(router1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 186 | |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 187 | BOOST_CHECK_EQUAL(lsdb1.doesLsaExist(ndn::Name("/router1/1"), std::string("name")), false); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 190 | BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild) |
| 191 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 192 | // Should register Sync prefix |
| 193 | lsdb.buildAndInstallOwnAdjLsa(); |
| 194 | face->processEvents(ndn::time::milliseconds(1)); |
| 195 | |
| 196 | std::vector<ndn::Interest>& interests = face->m_sentInterests; |
| 197 | |
| 198 | BOOST_REQUIRE(interests.size() > 0); |
| 199 | |
| 200 | ndn::nfd::ControlParameters extractedParameters; |
| 201 | ndn::Name::Component verb; |
| 202 | extractParameters(interests[0], verb, extractedParameters); |
| 203 | |
| 204 | BOOST_CHECK_EQUAL(verb, REGISTER_VERB); |
| 205 | BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix()); |
| 206 | |
| 207 | // Should not register Sync prefix |
| 208 | face->m_sentInterests.clear(); |
| 209 | lsdb.buildAndInstallOwnAdjLsa(); |
| 210 | face->processEvents(ndn::time::milliseconds(1)); |
| 211 | |
| 212 | BOOST_CHECK_EQUAL(interests.size(), 0); |
| 213 | } |
| 214 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 215 | BOOST_AUTO_TEST_CASE(InstallNameLsa) |
| 216 | { |
| 217 | // Install lsa with name1 and name2 |
| 218 | ndn::Name name1("/ndn/name1"); |
| 219 | ndn::Name name2("/ndn/name2"); |
| 220 | |
| 221 | NamePrefixList prefixes; |
| 222 | prefixes.insert(name1); |
| 223 | prefixes.insert(name2); |
| 224 | |
| 225 | std::string otherRouter("/ndn/site/%C1.router/other-router"); |
| 226 | ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max(); |
| 227 | |
| 228 | NameLsa lsa(otherRouter, "name", 1, MAX_TIME, prefixes); |
| 229 | lsdb.installNameLsa(lsa); |
| 230 | |
| 231 | BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/name", "name"), true); |
| 232 | NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/name")->getNpl(); |
| 233 | |
| 234 | areNamePrefixListsEqual(nameList, prefixes); |
| 235 | |
| 236 | // Add a prefix: name3 |
| 237 | ndn::Name name3("/ndn/name3"); |
| 238 | prefixes.insert(name3); |
| 239 | |
| 240 | NameLsa addLsa(otherRouter, "name", 2, MAX_TIME, prefixes); |
| 241 | lsdb.installNameLsa(addLsa); |
| 242 | |
| 243 | // Lsa should include name1, name2, and name3 |
| 244 | areNamePrefixListsEqual(nameList, prefixes); |
| 245 | |
| 246 | // Remove a prefix: name2 |
| 247 | prefixes.remove(name2); |
| 248 | |
| 249 | NameLsa removeLsa(otherRouter, "name", 3, MAX_TIME, prefixes); |
| 250 | lsdb.installNameLsa(removeLsa); |
| 251 | |
| 252 | // Lsa should include name1 and name3 |
| 253 | areNamePrefixListsEqual(nameList, prefixes); |
| 254 | |
| 255 | // Add and remove a prefix: add name2, remove name3 |
| 256 | prefixes.insert(name2); |
| 257 | prefixes.remove(name3); |
| 258 | |
| 259 | NameLsa addAndRemoveLsa(otherRouter, "name", 4, MAX_TIME, prefixes); |
| 260 | lsdb.installNameLsa(addAndRemoveLsa); |
| 261 | |
| 262 | // Lsa should include name1 and name2 |
| 263 | areNamePrefixListsEqual(nameList, prefixes); |
| 264 | |
| 265 | // Install a completely new list of prefixes |
| 266 | ndn::Name name4("/ndn/name4"); |
| 267 | ndn::Name name5("/ndn/name5"); |
| 268 | |
| 269 | NamePrefixList newPrefixes; |
| 270 | newPrefixes.insert(name4); |
| 271 | newPrefixes.insert(name5); |
| 272 | |
| 273 | NameLsa newLsa(otherRouter, "name", 5, MAX_TIME, newPrefixes); |
| 274 | lsdb.installNameLsa(newLsa); |
| 275 | |
| 276 | // Lsa should include name4 and name5 |
| 277 | areNamePrefixListsEqual(nameList, newPrefixes); |
| 278 | } |
| 279 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 280 | BOOST_AUTO_TEST_SUITE_END() |
| 281 | |
| 282 | } //namespace test |
| 283 | } //namespace nlsr |