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()) |
| 47 | , REGISTER_COMMAND_PREFIX("/localhost/nfd/rib") |
| 48 | , REGISTER_VERB("register") |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void extractParameters(ndn::Interest& interest, ndn::Name::Component& verb, |
| 53 | ndn::nfd::ControlParameters& extractedParameters) |
| 54 | { |
| 55 | const ndn::Name& name = interest.getName(); |
| 56 | verb = name[REGISTER_COMMAND_PREFIX.size()]; |
| 57 | const ndn::Name::Component& parameterComponent = name[REGISTER_COMMAND_PREFIX.size() + 1]; |
| 58 | |
| 59 | ndn::Block rawParameters = parameterComponent.blockFromValue(); |
| 60 | extractedParameters.wireDecode(rawParameters); |
| 61 | } |
| 62 | |
| 63 | public: |
| 64 | shared_ptr<DummyFace> face; |
| 65 | Nlsr nlsr; |
| 66 | SyncLogicHandler sync; |
| 67 | |
| 68 | ndn::Name REGISTER_COMMAND_PREFIX; |
| 69 | ndn::Name::Component REGISTER_VERB; |
| 70 | }; |
| 71 | |
| 72 | BOOST_FIXTURE_TEST_SUITE(TestLsdb, LsdbFixture) |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 73 | |
| 74 | BOOST_AUTO_TEST_CASE(LsdbRemoveAndExists) |
| 75 | { |
Vince Lehman | f99b87f | 2014-08-26 15:54:27 -0500 | [diff] [blame] | 76 | INIT_LOGGERS("/tmp", "DEBUG"); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 77 | |
akmhoque | c7a79b2 | 2014-05-26 08:06:19 -0500 | [diff] [blame] | 78 | ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now(); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 79 | NamePrefixList npl1; |
| 80 | |
akmhoque | fdbddb1 | 2014-05-02 18:35:19 -0500 | [diff] [blame] | 81 | std::string s1 = "name1"; |
| 82 | std::string s2 = "name2"; |
| 83 | std::string router1 = "router1/1"; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 84 | |
| 85 | npl1.insert(s1); |
| 86 | npl1.insert(s2); |
| 87 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 88 | //For NameLsa lsType is name. |
| 89 | //12 is seqNo, randomly generated. |
| 90 | //1800 is the default life time. |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 91 | NameLsa nlsa1(ndn::Name("/router1/1"), std::string("name"), 12, testTimePoint, npl1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 92 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 93 | Lsdb lsdb1(nlsr, g_scheduler); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 94 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 95 | lsdb1.installNameLsa(nlsa1); |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 96 | lsdb1.writeNameLsdbLog(); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 97 | |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 98 | BOOST_CHECK(lsdb1.doesLsaExist(ndn::Name("/router1/1/name"), std::string("name"))); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 99 | |
akmhoque | 31d1d4b | 2014-05-05 22:08:14 -0500 | [diff] [blame] | 100 | lsdb1.removeNameLsa(router1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 101 | |
Alexander Afanasyev | 411ee4b | 2014-08-16 23:17:03 -0700 | [diff] [blame] | 102 | 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] | 103 | } |
| 104 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(RegisterSyncPrefixOnFirstAdjLsaBuild) |
| 106 | { |
| 107 | ConfParameter& conf = nlsr.getConfParameter(); |
| 108 | conf.setNetwork("/ndn"); |
| 109 | conf.setSiteName("/site"); |
| 110 | conf.setRouterName("/%C1.router/this-router"); |
| 111 | |
| 112 | nlsr.initialize(); |
| 113 | |
| 114 | Lsdb& lsdb = nlsr.getLsdb(); |
| 115 | face->processEvents(ndn::time::milliseconds(1)); |
| 116 | face->m_sentInterests.clear(); |
| 117 | |
| 118 | // Should register Sync prefix |
| 119 | lsdb.buildAndInstallOwnAdjLsa(); |
| 120 | face->processEvents(ndn::time::milliseconds(1)); |
| 121 | |
| 122 | std::vector<ndn::Interest>& interests = face->m_sentInterests; |
| 123 | |
| 124 | BOOST_REQUIRE(interests.size() > 0); |
| 125 | |
| 126 | ndn::nfd::ControlParameters extractedParameters; |
| 127 | ndn::Name::Component verb; |
| 128 | extractParameters(interests[0], verb, extractedParameters); |
| 129 | |
| 130 | BOOST_CHECK_EQUAL(verb, REGISTER_VERB); |
| 131 | BOOST_CHECK_EQUAL(extractedParameters.getName(), conf.getChronosyncPrefix()); |
| 132 | |
| 133 | // Should not register Sync prefix |
| 134 | face->m_sentInterests.clear(); |
| 135 | lsdb.buildAndInstallOwnAdjLsa(); |
| 136 | face->processEvents(ndn::time::milliseconds(1)); |
| 137 | |
| 138 | BOOST_CHECK_EQUAL(interests.size(), 0); |
| 139 | } |
| 140 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 141 | BOOST_AUTO_TEST_SUITE_END() |
| 142 | |
| 143 | } //namespace test |
| 144 | } //namespace nlsr |