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> |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 32 | #include <ndn-cxx/util/time.hpp> |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 33 | |
| 34 | namespace nlsr { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 35 | namespace test { |
| 36 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 37 | using ndn::DummyFace; |
| 38 | using ndn::shared_ptr; |
| 39 | |
| 40 | class LsdbFixture : public BaseFixture |
| 41 | { |
| 42 | public: |
| 43 | LsdbFixture() |
| 44 | : face(ndn::makeDummyFace()) |
| 45 | , nlsr(g_ioService, g_scheduler, ndn::ref(*face)) |
| 46 | , sync(*face, nlsr.getLsdb(), nlsr.getConfParameter()) |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 47 | , lsdb(nlsr.getLsdb()) |
| 48 | , conf(nlsr.getConfParameter()) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 49 | , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib") |
| 50 | , REGISTER_VERB("register") |
| 51 | { |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 52 | conf.setNetwork("/ndn"); |
| 53 | conf.setSiteName("/site"); |
| 54 | conf.setRouterName("/%C1.router/this-router"); |
| 55 | |
| 56 | nlsr.initialize(); |
| 57 | |
| 58 | face->processEvents(ndn::time::milliseconds(1)); |
| 59 | face->m_sentInterests.clear(); |
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: |
| 93 | shared_ptr<DummyFace> face; |
| 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 | |
| 106 | BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists) |
| 107 | { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 108 | INIT_LOGGERS("/tmp", "DEBUG"); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 109 | |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 110 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 111 | NamePrefixList npl1; |
| 112 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 113 | std::string s1 = "name1"; |
| 114 | std::string s2 = "name2"; |
| 115 | std::string router1 = "router1/1"; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 116 | |
| 117 | npl1.insert(s1); |
| 118 | npl1.insert(s2); |
| 119 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 120 | //For NameLsa lsType is name. |
| 121 | //12 is seqNo, randomly generated. |
| 122 | //1800 is the default life time. |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 123 | NameLsa nlsa1(ndn::Name("/router1/1"), std::string("name"), 12, testTimePoint, npl1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 124 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 125 | Lsdb lsdb1(nlsr, g_scheduler); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 126 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 127 | lsdb1.installNameLsa(nlsa1); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 128 | lsdb1.writeNameLsdbLog(); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 129 | |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 130 | BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), std::string("name"))); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 131 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 132 | lsdb1.removeNameLsa(router1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 133 | |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 134 | 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] | 135 | } |
| 136 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 137 | BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild) |
| 138 | { |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 139 | // Should register Sync prefix |
| 140 | lsdb.buildAndInstallOwnAdjLsa(); |
| 141 | face->processEvents(ndn::time::milliseconds(1)); |
| 142 | |
| 143 | std::vector<ndn::Interest>& interests = face->m_sentInterests; |
| 144 | |
| 145 | BOOST_REQUIRE(interests.size() > 0); |
| 146 | |
| 147 | ndn::nfd::ControlParameters extractedParameters; |
| 148 | ndn::Name::Component verb; |
| 149 | extractParameters(interests[0], verb, extractedParameters); |
| 150 | |
| 151 | BOOST_CHECK_EQUAL(verb, REGISTER_VERB); |
| 152 | BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix()); |
| 153 | |
| 154 | // Should not register Sync prefix |
| 155 | face->m_sentInterests.clear(); |
| 156 | lsdb.buildAndInstallOwnAdjLsa(); |
| 157 | face->processEvents(ndn::time::milliseconds(1)); |
| 158 | |
| 159 | BOOST_CHECK_EQUAL(interests.size(), 0); |
| 160 | } |
| 161 | |
Vince Lehman | f1aa523 | 2014-10-06 17:57:35 -0500 | [diff] [blame] | 162 | BOOST_AUTO_TEST_CASE(InstallNameLsa) |
| 163 | { |
| 164 | // Install lsa with name1 and name2 |
| 165 | ndn::Name name1("/ndn/name1"); |
| 166 | ndn::Name name2("/ndn/name2"); |
| 167 | |
| 168 | NamePrefixList prefixes; |
| 169 | prefixes.insert(name1); |
| 170 | prefixes.insert(name2); |
| 171 | |
| 172 | std::string otherRouter("/ndn/site/%C1.router/other-router"); |
| 173 | ndn::time::system_clock::TimePoint MAX_TIME = ndn::time::system_clock::TimePoint::max(); |
| 174 | |
| 175 | NameLsa lsa(otherRouter, "name", 1, MAX_TIME, prefixes); |
| 176 | lsdb.installNameLsa(lsa); |
| 177 | |
| 178 | BOOST_REQUIRE_EQUAL(lsdb.doesLsaExist(otherRouter + "/name", "name"), true); |
| 179 | NamePrefixList& nameList = lsdb.findNameLsa(otherRouter + "/name")->getNpl(); |
| 180 | |
| 181 | areNamePrefixListsEqual(nameList, prefixes); |
| 182 | |
| 183 | // Add a prefix: name3 |
| 184 | ndn::Name name3("/ndn/name3"); |
| 185 | prefixes.insert(name3); |
| 186 | |
| 187 | NameLsa addLsa(otherRouter, "name", 2, MAX_TIME, prefixes); |
| 188 | lsdb.installNameLsa(addLsa); |
| 189 | |
| 190 | // Lsa should include name1, name2, and name3 |
| 191 | areNamePrefixListsEqual(nameList, prefixes); |
| 192 | |
| 193 | // Remove a prefix: name2 |
| 194 | prefixes.remove(name2); |
| 195 | |
| 196 | NameLsa removeLsa(otherRouter, "name", 3, MAX_TIME, prefixes); |
| 197 | lsdb.installNameLsa(removeLsa); |
| 198 | |
| 199 | // Lsa should include name1 and name3 |
| 200 | areNamePrefixListsEqual(nameList, prefixes); |
| 201 | |
| 202 | // Add and remove a prefix: add name2, remove name3 |
| 203 | prefixes.insert(name2); |
| 204 | prefixes.remove(name3); |
| 205 | |
| 206 | NameLsa addAndRemoveLsa(otherRouter, "name", 4, MAX_TIME, prefixes); |
| 207 | lsdb.installNameLsa(addAndRemoveLsa); |
| 208 | |
| 209 | // Lsa should include name1 and name2 |
| 210 | areNamePrefixListsEqual(nameList, prefixes); |
| 211 | |
| 212 | // Install a completely new list of prefixes |
| 213 | ndn::Name name4("/ndn/name4"); |
| 214 | ndn::Name name5("/ndn/name5"); |
| 215 | |
| 216 | NamePrefixList newPrefixes; |
| 217 | newPrefixes.insert(name4); |
| 218 | newPrefixes.insert(name5); |
| 219 | |
| 220 | NameLsa newLsa(otherRouter, "name", 5, MAX_TIME, newPrefixes); |
| 221 | lsdb.installNameLsa(newLsa); |
| 222 | |
| 223 | // Lsa should include name4 and name5 |
| 224 | areNamePrefixListsEqual(nameList, newPrefixes); |
| 225 | } |
| 226 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 227 | BOOST_AUTO_TEST_SUITE_END() |
| 228 | |
| 229 | } //namespace test |
| 230 | } //namespace nlsr |