Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, The University of Memphis, |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 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/>. |
| 20 | **/ |
| 21 | |
| 22 | #include "update/nfd-rib-command-processor.hpp" |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 23 | #include "adjacency-list.hpp" |
| 24 | #include "conf-parameter.hpp" |
| 25 | #include "nlsr.hpp" |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 26 | |
| 27 | #include "../test-common.hpp" |
| 28 | #include "../control-commands.hpp" |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 29 | |
| 30 | namespace nlsr { |
| 31 | namespace update { |
| 32 | namespace test { |
| 33 | |
| 34 | class NfdRibCommandProcessorFixture : public nlsr::test::UnitTestTimeFixture |
| 35 | { |
| 36 | public: |
| 37 | NfdRibCommandProcessorFixture() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 38 | : face(m_ioService, m_keyChain, {true, true}) |
| 39 | , nlsr(m_ioService, m_scheduler, face, m_keyChain) |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 40 | , namePrefixes(nlsr.getNamePrefixList()) |
| 41 | , processor(nlsr.getNfdRibCommandProcessor()) |
| 42 | { |
| 43 | // Set the network so the LSA prefix is constructed |
| 44 | nlsr.getConfParameter().setNetwork("/ndn"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 45 | nlsr.getConfParameter().setRouterName(ndn::Name("/This/router")); |
| 46 | |
| 47 | addIdentity(ndn::Name("/ndn/This/router")); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 48 | |
| 49 | // Initialize NLSR so a sync socket is created |
| 50 | nlsr.initialize(); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 51 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 52 | this->advanceClocks(ndn::time::milliseconds(10), 10); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 53 | face.sentInterests.clear(); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 54 | |
| 55 | nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq(); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 56 | } |
| 57 | |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 58 | void |
| 59 | sendCommand(ndn::Name prefix, const ndn::nfd::ControlParameters& parameters) |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 60 | { |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 61 | ndn::Interest interest(prefix.append(parameters.wireEncode())); |
| 62 | interest.setCanBePrefix(false); |
| 63 | face.receive(interest); |
| 64 | this->advanceClocks(ndn::time::milliseconds(10), 10); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 65 | } |
| 66 | |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 67 | void |
| 68 | sendInterestForPublishedData() |
| 69 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 70 | // Need to send an interest now since ChronoSync |
| 71 | // no longer does face->put(*data) in publishData. |
| 72 | // Instead it does it in onInterest |
Ashlesh Gawande | cba0ae2 | 2018-03-27 17:57:56 -0500 | [diff] [blame] | 73 | ndn::Name lsaInterestName("/localhop/ndn/nlsr/LSA/This/router"); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 74 | lsaInterestName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 75 | |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 76 | lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 77 | auto lsaInterest = make_shared<ndn::Interest>(lsaInterestName); |
| 78 | lsaInterest->setCanBePrefix(true); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 79 | face.receive(*lsaInterest); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 80 | this->advanceClocks(ndn::time::milliseconds(10), 10); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 81 | } |
| 82 | |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 83 | bool |
| 84 | wasRoutingUpdatePublished() |
| 85 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 86 | sendInterestForPublishedData(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 87 | |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 88 | const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix(); |
| 89 | |
| 90 | const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(), |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 91 | [&] (const ndn::Data& data) { |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 92 | return lsaPrefix.isPrefixOf(data.getName()); |
| 93 | }); |
| 94 | |
| 95 | return (it != face.sentData.end()); |
| 96 | } |
| 97 | |
| 98 | public: |
| 99 | ndn::util::DummyClientFace face; |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 100 | |
| 101 | Nlsr nlsr; |
| 102 | NamePrefixList& namePrefixes; |
| 103 | NfdRibCommandProcessor& processor; |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 104 | uint64_t nameLsaSeqNoBeforeInterest; |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | typedef boost::mpl::vector<NfdRibRegisterCommand, NfdRibUnregisterCommand> Commands; |
| 108 | |
| 109 | BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture) |
| 110 | |
| 111 | BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands) |
| 112 | { |
| 113 | ndn::nfd::ControlParameters parameters; |
| 114 | parameters.setName("/test/prefixA"); |
| 115 | |
| 116 | BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters)); |
| 117 | } |
| 118 | |
| 119 | BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands) |
| 120 | { |
| 121 | ndn::nfd::ControlParameters parameters; |
| 122 | parameters.setName("/test/prefixA").setCost(10); |
| 123 | |
| 124 | bool wasValidated = true; |
| 125 | try { |
| 126 | processor.validateParameters<NfdRibCommand>(parameters); |
| 127 | } |
| 128 | catch (...) { |
| 129 | wasValidated = false; |
| 130 | } |
| 131 | BOOST_CHECK(!wasValidated); |
| 132 | } |
| 133 | |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 134 | BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand) |
| 135 | { |
| 136 | ndn::Name name("/localhost/nlsr/rib/register"); |
| 137 | ndn::Name prefixName("/test/prefixA"); |
| 138 | ndn::nfd::ControlParameters parameters; |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 139 | parameters.setName(prefixName); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 140 | |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 141 | sendCommand(name, parameters); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 142 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 1); |
| 144 | std::list<ndn::Name> names = namePrefixes.getNames(); |
| 145 | auto itr = std::find(names.begin(), names.end(), prefixName); |
| 146 | if (itr == namePrefixes.getNames().end()) { |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 147 | BOOST_FAIL("Prefix was not inserted!"); |
| 148 | } |
| 149 | BOOST_CHECK_EQUAL((*itr), prefixName); |
| 150 | BOOST_CHECK(wasRoutingUpdatePublished()); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 151 | BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand) |
| 155 | { |
| 156 | ndn::Name name("/localhost/nlsr/rib/unregister"); |
| 157 | ndn::Name prefixName("/test/prefixA"); |
| 158 | ndn::nfd::ControlParameters parameters; |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 159 | parameters.setName(prefixName); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 160 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 161 | namePrefixes.insert(prefixName); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 162 | |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 163 | sendCommand(name, parameters); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 164 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 166 | BOOST_CHECK(wasRoutingUpdatePublished()); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 167 | BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix) |
| 171 | { |
| 172 | ndn::Name name("/localhost/invalid/rib/register"); |
| 173 | ndn::Name prefixName("/test/prefixA"); |
| 174 | ndn::nfd::ControlParameters parameters; |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 175 | parameters.setName(prefixName); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 176 | |
Junxiao Shi | 008c9b1 | 2019-01-13 23:15:56 +0000 | [diff] [blame] | 177 | sendCommand(name, parameters); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 178 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(namePrefixes.getNames().size(), 0); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 180 | |
| 181 | // Cannot use routingUpdatePublish test now since in |
| 182 | // initialize nlsr calls buildOwnNameLsa which publishes the routing update |
| 183 | BOOST_CHECK(nameLsaSeqNoBeforeInterest == nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | BOOST_AUTO_TEST_SUITE_END() |
| 187 | |
| 188 | } // namespace test |
| 189 | } // namespace update |
| 190 | } // namespace nlsr |