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