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