Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 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 | **/ |
| 21 | |
| 22 | #include "test-common.hpp" |
| 23 | #include "dummy-face.hpp" |
| 24 | |
| 25 | #include "nlsr.hpp" |
| 26 | #include "communication/sync-logic-handler.hpp" |
| 27 | |
| 28 | namespace nlsr { |
| 29 | namespace test { |
| 30 | |
| 31 | using ndn::DummyFace; |
| 32 | using ndn::shared_ptr; |
| 33 | |
| 34 | class SyncLogicFixture : public BaseFixture |
| 35 | { |
| 36 | public: |
| 37 | SyncLogicFixture() |
| 38 | : face(ndn::makeDummyFace()) |
| 39 | , nlsr(g_ioService, g_scheduler, ndn::ref(*face)) |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 40 | , sync(nlsr.getSyncLogicHandler()) |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 41 | , CONFIG_NETWORK("/ndn") |
| 42 | , CONFIG_SITE("/site") |
| 43 | , CONFIG_ROUTER_NAME("/%C1.Router/this-router") |
| 44 | { |
| 45 | nlsr.getConfParameter().setNetwork(CONFIG_NETWORK); |
| 46 | nlsr.getConfParameter().setSiteName(CONFIG_SITE); |
| 47 | nlsr.getConfParameter().setRouterName(CONFIG_ROUTER_NAME); |
| 48 | nlsr.getConfParameter().buildRouterPrefix(); |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | receiveUpdate(std::string prefix, uint64_t seqNo) |
| 53 | { |
| 54 | Sync::MissingDataInfo info = {prefix, 0, seqNo}; |
| 55 | |
| 56 | std::vector<Sync::MissingDataInfo> updates; |
| 57 | updates.push_back(info); |
| 58 | |
| 59 | face->processEvents(ndn::time::milliseconds(1)); |
| 60 | face->m_sentInterests.clear(); |
| 61 | |
| 62 | sync.onNsyncUpdate(updates, NULL); |
| 63 | |
| 64 | face->processEvents(ndn::time::milliseconds(1)); |
| 65 | } |
| 66 | |
| 67 | public: |
| 68 | shared_ptr<DummyFace> face; |
| 69 | Nlsr nlsr; |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 70 | SyncLogicHandler& sync; |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 71 | |
| 72 | const std::string CONFIG_NETWORK; |
| 73 | const std::string CONFIG_SITE; |
| 74 | const std::string CONFIG_ROUTER_NAME; |
| 75 | }; |
| 76 | |
| 77 | BOOST_FIXTURE_TEST_SUITE(TestSyncLogicHandler, SyncLogicFixture) |
| 78 | |
| 79 | BOOST_AUTO_TEST_CASE(UpdateForOther) |
| 80 | { |
| 81 | std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() + |
| 82 | CONFIG_SITE + "/%C1.Router/other-router/"; |
| 83 | |
| 84 | receiveUpdate(updateName, 1); |
| 85 | |
| 86 | std::vector<ndn::Interest>& interests = face->m_sentInterests; |
| 87 | BOOST_REQUIRE_EQUAL(interests.size(), 3); |
| 88 | |
| 89 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + NameLsa::TYPE_STRING + "/"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 91 | |
| 92 | ++it; |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + AdjLsa::TYPE_STRING + "/"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 94 | |
| 95 | ++it; |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + CoordinateLsa::TYPE_STRING + "/"); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | BOOST_AUTO_TEST_CASE(NoUpdateForSelf) |
| 100 | { |
| 101 | std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() + |
| 102 | CONFIG_SITE + CONFIG_ROUTER_NAME; |
| 103 | |
| 104 | receiveUpdate(updateName, 1); |
| 105 | |
| 106 | std::vector<ndn::Interest>& interests = face->m_sentInterests; |
| 107 | BOOST_CHECK_EQUAL(interests.size(), 0); |
| 108 | } |
| 109 | |
| 110 | BOOST_AUTO_TEST_CASE(MalformedUpdate) |
| 111 | { |
| 112 | std::string updateName = CONFIG_SITE + nlsr.getConfParameter().getLsaPrefix().toUri() + |
| 113 | CONFIG_ROUTER_NAME; |
| 114 | |
| 115 | std::vector<ndn::Interest>& interests = face->m_sentInterests; |
| 116 | BOOST_CHECK_EQUAL(interests.size(), 0); |
| 117 | } |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE(SequenceNumber) |
| 120 | { |
| 121 | std::string originRouter = CONFIG_NETWORK + CONFIG_SITE + "/%C1.Router/other-router/"; |
| 122 | |
| 123 | Lsdb& lsdb = nlsr.getLsdb(); |
| 124 | |
| 125 | // Install Name LSA |
| 126 | NamePrefixList nameList; |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 127 | NameLsa lsa(originRouter, NameLsa::TYPE_STRING, 999, ndn::time::system_clock::TimePoint::max(), nameList); |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 128 | lsdb.installNameLsa(lsa); |
| 129 | |
| 130 | // Install Adj LSA |
| 131 | AdjacencyList adjList; |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 132 | AdjLsa adjLsa(originRouter, AdjLsa::TYPE_STRING, 1000, ndn::time::system_clock::TimePoint::max(), |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 133 | 3 , adjList); |
| 134 | lsdb.installAdjLsa(adjLsa); |
| 135 | |
| 136 | // Install Cor LSA |
alvy | 49b1c0c | 2014-12-19 13:57:46 -0600 | [diff] [blame] | 137 | CoordinateLsa corLsa(originRouter, CoordinateLsa::TYPE_STRING, 1000, ndn::time::system_clock::TimePoint::max(), |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 138 | 0,0); |
| 139 | lsdb.installCoordinateLsa(corLsa); |
| 140 | |
| 141 | std::string updateName = nlsr.getConfParameter().getLsaPrefix().toUri() + |
| 142 | CONFIG_SITE + "/%C1.Router/other-router/"; |
| 143 | |
| 144 | // Lower NameLSA sequence number |
| 145 | uint64_t lowerSeqNo = static_cast<uint64_t>(998) << 40; |
| 146 | receiveUpdate(updateName, lowerSeqNo); |
| 147 | |
| 148 | std::vector<ndn::Interest>& interests = face->m_sentInterests; |
| 149 | BOOST_REQUIRE_EQUAL(interests.size(), 0); |
| 150 | |
| 151 | // Same NameLSA sequence number |
| 152 | uint64_t sameSeqNo = static_cast<uint64_t>(999) << 40; |
| 153 | receiveUpdate(updateName, sameSeqNo); |
| 154 | |
| 155 | interests = face->m_sentInterests; |
| 156 | BOOST_REQUIRE_EQUAL(interests.size(), 0); |
| 157 | |
| 158 | // Higher NameLSA sequence number |
| 159 | uint64_t higherSeqNo = static_cast<uint64_t>(1000) << 40; |
| 160 | receiveUpdate(updateName, higherSeqNo); |
| 161 | |
| 162 | interests = face->m_sentInterests; |
| 163 | BOOST_REQUIRE_EQUAL(interests.size(), 1); |
| 164 | |
| 165 | std::vector<ndn::Interest>::iterator it = interests.begin(); |
| 166 | BOOST_CHECK_EQUAL(it->getName().getPrefix(-1), updateName + "name/"); |
| 167 | } |
| 168 | |
Vince Lehman | c11cc20 | 2015-01-20 11:41:33 -0600 | [diff] [blame] | 169 | BOOST_AUTO_TEST_CASE(UpdatePrefix) |
| 170 | { |
| 171 | ndn::Name expectedPrefix = nlsr.getConfParameter().getLsaPrefix(); |
| 172 | expectedPrefix.append(CONFIG_SITE); |
| 173 | expectedPrefix.append(CONFIG_ROUTER_NAME); |
| 174 | |
| 175 | nlsr.initialize(); |
| 176 | |
| 177 | BOOST_CHECK_EQUAL(sync.m_updatePrefix, expectedPrefix); |
| 178 | } |
| 179 | |
Vince Lehman | 904c241 | 2014-09-23 19:36:11 -0500 | [diff] [blame] | 180 | BOOST_AUTO_TEST_SUITE_END() |
| 181 | |
| 182 | } // namespace test |
| 183 | } // namespace nlsr |