Nick Gordon | 4d2c6c0 | 2017-01-20 13:18:46 -0600 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, The University of Memphis, |
| 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" |
| 23 | |
| 24 | #include "../test-common.hpp" |
| 25 | #include "../control-commands.hpp" |
| 26 | #include "conf-parameter.hpp" |
| 27 | #include "adjacency-list.hpp" |
| 28 | #include "nlsr.hpp" |
| 29 | |
| 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 31 | |
| 32 | #include <ndn-cxx/security/key-chain.hpp> |
| 33 | |
| 34 | namespace nlsr { |
| 35 | namespace update { |
| 36 | namespace test { |
| 37 | |
| 38 | class NfdRibCommandProcessorFixture : public nlsr::test::UnitTestTimeFixture |
| 39 | { |
| 40 | public: |
| 41 | NfdRibCommandProcessorFixture() |
| 42 | : face(g_ioService, keyChain, {true, true}) |
| 43 | , nlsr(g_ioService, g_scheduler, face, g_keyChain) |
| 44 | , namePrefixes(nlsr.getNamePrefixList()) |
| 45 | , processor(nlsr.getNfdRibCommandProcessor()) |
| 46 | { |
| 47 | // Set the network so the LSA prefix is constructed |
| 48 | nlsr.getConfParameter().setNetwork("/ndn"); |
| 49 | |
| 50 | // Initialize NLSR so a sync socket is created |
| 51 | nlsr.initialize(); |
| 52 | this->advanceClocks(ndn::time::milliseconds(10), 1); |
| 53 | face.sentInterests.clear(); |
| 54 | } |
| 55 | |
| 56 | std::shared_ptr<ndn::Interest> |
| 57 | makeInterest(const ndn::Name& name, uint32_t nonce) |
| 58 | { |
| 59 | auto interest = std::make_shared<ndn::Interest>(name); |
| 60 | if (nonce != 0) { |
| 61 | interest->setNonce(nonce); |
| 62 | } |
| 63 | return interest; |
| 64 | } |
| 65 | |
| 66 | bool |
| 67 | wasRoutingUpdatePublished() |
| 68 | { |
| 69 | const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix(); |
| 70 | |
| 71 | const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(), |
| 72 | [lsaPrefix] (const ndn::Data& data) { |
| 73 | return lsaPrefix.isPrefixOf(data.getName()); |
| 74 | }); |
| 75 | |
| 76 | return (it != face.sentData.end()); |
| 77 | } |
| 78 | |
| 79 | public: |
| 80 | ndn::util::DummyClientFace face; |
| 81 | ndn::KeyChain keyChain; |
| 82 | |
| 83 | Nlsr nlsr; |
| 84 | NamePrefixList& namePrefixes; |
| 85 | NfdRibCommandProcessor& processor; |
| 86 | }; |
| 87 | |
| 88 | typedef boost::mpl::vector<NfdRibRegisterCommand, NfdRibUnregisterCommand> Commands; |
| 89 | |
| 90 | BOOST_FIXTURE_TEST_SUITE(TestNfdRibCommandProcessor, NfdRibCommandProcessorFixture) |
| 91 | |
| 92 | BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersSuccess, NfdRibCommand, Commands) |
| 93 | { |
| 94 | ndn::nfd::ControlParameters parameters; |
| 95 | parameters.setName("/test/prefixA"); |
| 96 | |
| 97 | BOOST_CHECK(processor.validateParameters<NfdRibCommand>(parameters)); |
| 98 | } |
| 99 | |
| 100 | BOOST_AUTO_TEST_CASE_TEMPLATE(ValidateParametersFailure, NfdRibCommand, Commands) |
| 101 | { |
| 102 | ndn::nfd::ControlParameters parameters; |
| 103 | parameters.setName("/test/prefixA").setCost(10); |
| 104 | |
| 105 | bool wasValidated = true; |
| 106 | try { |
| 107 | processor.validateParameters<NfdRibCommand>(parameters); |
| 108 | } |
| 109 | catch (...) { |
| 110 | wasValidated = false; |
| 111 | } |
| 112 | BOOST_CHECK(!wasValidated); |
| 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_CASE(InsertPrefix) |
| 116 | { |
| 117 | ndn::nfd::ControlParameters parameters; |
| 118 | ndn::Name prefixName("/test/prefixA"); |
| 119 | parameters.setName(prefixName); |
| 120 | |
| 121 | processor.insertPrefix(parameters); |
| 122 | this->advanceClocks(ndn::time::milliseconds(10), 1); |
| 123 | |
| 124 | BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1); |
| 125 | auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(), |
| 126 | prefixName); |
| 127 | if (itr == namePrefixes.getNameList().end()) { |
| 128 | BOOST_FAIL("Prefix was not inserted!"); |
| 129 | } |
| 130 | BOOST_CHECK_EQUAL((*itr), parameters.getName()); |
| 131 | BOOST_CHECK(wasRoutingUpdatePublished()); |
| 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(RemovePrefix) |
| 135 | { |
| 136 | ndn::Name prefixName("/test/prefixA"); |
| 137 | namePrefixes.getNameList().push_back(prefixName); |
| 138 | ndn::nfd::ControlParameters parameters; |
| 139 | parameters.setName("/test/prefixA"); |
| 140 | |
| 141 | BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1); |
| 142 | processor.removePrefix(parameters); |
| 143 | this->advanceClocks(ndn::time::milliseconds(10), 1); |
| 144 | |
| 145 | BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0); |
| 146 | auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(), |
| 147 | prefixName); |
| 148 | if (itr != namePrefixes.getNameList().end()) { |
| 149 | BOOST_FAIL("Prefix was not removed!"); |
| 150 | } |
| 151 | BOOST_CHECK(wasRoutingUpdatePublished()); |
| 152 | } |
| 153 | |
| 154 | BOOST_AUTO_TEST_CASE(onReceiveInterestRegisterCommand) |
| 155 | { |
| 156 | ndn::Name name("/localhost/nlsr/rib/register"); |
| 157 | ndn::Name prefixName("/test/prefixA"); |
| 158 | ndn::nfd::ControlParameters parameters; |
| 159 | |
| 160 | shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName) |
| 161 | .wireEncode()), 0); |
| 162 | |
| 163 | face.receive(*command); |
| 164 | this->advanceClocks(ndn::time::milliseconds(10), 1); |
| 165 | |
| 166 | BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 1); |
| 167 | auto itr = std::find(namePrefixes.getNameList().begin(), namePrefixes.getNameList().end(), |
| 168 | prefixName); |
| 169 | if (itr == namePrefixes.getNameList().end()) { |
| 170 | BOOST_FAIL("Prefix was not inserted!"); |
| 171 | } |
| 172 | BOOST_CHECK_EQUAL((*itr), prefixName); |
| 173 | BOOST_CHECK(wasRoutingUpdatePublished()); |
| 174 | } |
| 175 | |
| 176 | BOOST_AUTO_TEST_CASE(onReceiveInterestUnregisterCommand) |
| 177 | { |
| 178 | ndn::Name name("/localhost/nlsr/rib/unregister"); |
| 179 | ndn::Name prefixName("/test/prefixA"); |
| 180 | ndn::nfd::ControlParameters parameters; |
| 181 | |
| 182 | namePrefixes.getNameList().push_back(prefixName); |
| 183 | |
| 184 | shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName) |
| 185 | .wireEncode()), 0); |
| 186 | |
| 187 | face.receive(ndn::Interest(name)); |
| 188 | this->advanceClocks(ndn::time::milliseconds(10), 1); |
| 189 | |
| 190 | BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0); |
| 191 | BOOST_CHECK(wasRoutingUpdatePublished()); |
| 192 | } |
| 193 | |
| 194 | BOOST_AUTO_TEST_CASE(onReceiveInterestInvalidPrefix) |
| 195 | { |
| 196 | ndn::Name name("/localhost/invalid/rib/register"); |
| 197 | ndn::Name prefixName("/test/prefixA"); |
| 198 | ndn::nfd::ControlParameters parameters; |
| 199 | |
| 200 | shared_ptr<ndn::Interest> command = makeInterest(name.append(parameters.setName(prefixName) |
| 201 | .wireEncode()), 0); |
| 202 | |
| 203 | face.receive(ndn::Interest(name)); |
| 204 | this->advanceClocks(ndn::time::milliseconds(10), 1); |
| 205 | |
| 206 | BOOST_CHECK_EQUAL(namePrefixes.getNameList().size(), 0); |
| 207 | BOOST_CHECK(!wasRoutingUpdatePublished()); |
| 208 | } |
| 209 | |
| 210 | BOOST_AUTO_TEST_SUITE_END() |
| 211 | |
| 212 | } // namespace test |
| 213 | } // namespace update |
| 214 | } // namespace nlsr |