alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Ashlesh Gawande | f7da9c5 | 2018-02-06 17:36:46 -0600 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, The University of Memphis, |
alvy | 297f416 | 2015-03-03 17:15:33 -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/prefix-update-processor.hpp" |
| 23 | |
| 24 | #include "../control-commands.hpp" |
| 25 | #include "../test-common.hpp" |
| 26 | #include "nlsr.hpp" |
| 27 | |
| 28 | #include <ndn-cxx/interest.hpp> |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 29 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
| 30 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 31 | #include <ndn-cxx/security/key-chain.hpp> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 32 | #include <ndn-cxx/security/pib/identity.hpp> |
| 33 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 34 | #include <ndn-cxx/security/command-interest-signer.hpp> |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 35 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 36 | #include <ndn-cxx/lp/tags.hpp> |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 37 | |
| 38 | #include <boost/filesystem.hpp> |
| 39 | |
| 40 | using namespace ndn; |
| 41 | |
| 42 | namespace nlsr { |
| 43 | namespace update { |
| 44 | namespace test { |
| 45 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 46 | class PrefixUpdateFixture : public nlsr::test::UnitTestTimeFixture |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 47 | { |
| 48 | public: |
| 49 | PrefixUpdateFixture() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 50 | : face(m_ioService, m_keyChain, {true, true}) |
| 51 | , siteIdentityName(ndn::Name("/edu/test-site")) |
| 52 | , opIdentityName(ndn::Name("/edu/test-site").append(ndn::Name("%C1.Operator"))) |
| 53 | , nlsr(m_ioService, m_scheduler, face, m_keyChain) |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 54 | , namePrefixList(nlsr.getNamePrefixList()) |
| 55 | , updatePrefixUpdateProcessor(nlsr.getPrefixUpdateProcessor()) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 56 | , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert")) |
| 57 | { |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 58 | // Site cert |
| 59 | siteIdentity = addIdentity(siteIdentityName); |
| 60 | saveCertificate(siteIdentity, SITE_CERT_PATH.string()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 61 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 62 | // Operator cert |
| 63 | opIdentity = addSubCertificate(opIdentityName, siteIdentity); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 64 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 65 | const std::string CONFIG = R"CONF( |
| 66 | rule |
| 67 | { |
| 68 | id "NLSR ControlCommand Rule" |
| 69 | for interest |
| 70 | filter |
| 71 | { |
| 72 | type name |
| 73 | regex ^(<localhost><nlsr>)<prefix-update>[<advertise><withdraw>]<><><>$ |
| 74 | } |
| 75 | checker |
| 76 | { |
| 77 | type customized |
| 78 | sig-type rsa-sha256 |
| 79 | key-locator |
| 80 | { |
| 81 | type name |
| 82 | regex ^<>*<KEY><>$ |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | rule |
| 87 | { |
| 88 | id "NLSR Hierarchy Rule" |
| 89 | for data |
| 90 | filter |
| 91 | { |
| 92 | type name |
| 93 | regex ^[^<KEY>]*<KEY><><><>$ |
| 94 | } |
| 95 | checker |
| 96 | { |
| 97 | type hierarchical |
| 98 | sig-type rsa-sha256 |
| 99 | } |
| 100 | } |
| 101 | trust-anchor |
| 102 | { |
| 103 | type file |
| 104 | file-name "site.cert" |
| 105 | } |
| 106 | )CONF"; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 107 | |
| 108 | const boost::filesystem::path CONFIG_PATH = |
| 109 | (boost::filesystem::current_path() / std::string("unit-test.conf")); |
| 110 | |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 111 | updatePrefixUpdateProcessor.getValidator().load(CONFIG, CONFIG_PATH.native()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 112 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 113 | nlsr.loadCertToPublish(opIdentity.getDefaultKey().getDefaultCertificate()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 114 | |
| 115 | // Set the network so the LSA prefix is constructed |
| 116 | nlsr.getConfParameter().setNetwork("/ndn"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 117 | nlsr.getConfParameter().setSiteName("/edu/test-site"); |
| 118 | nlsr.getConfParameter().setRouterName("/%C1.Router/this-router"); |
| 119 | nlsr.getConfParameter().buildRouterPrefix(); |
Ashlesh Gawande | f7da9c5 | 2018-02-06 17:36:46 -0600 | [diff] [blame^] | 120 | // Otherwise code coverage node fails with default 60 seconds lifetime |
| 121 | nlsr.getConfParameter().setSyncInterestLifetime(1000); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 122 | |
| 123 | addIdentity(ndn::Name("/ndn/edu/test-site/%C1.Router/this-router")); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 124 | |
| 125 | // Initialize NLSR so a sync socket is created |
| 126 | nlsr.initialize(); |
| 127 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 128 | // Saving clock::now before any advanceClocks so that it will |
| 129 | // be the same value as what ChronoSync uses in setting the sessionName |
| 130 | sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 131 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 132 | this->advanceClocks(ndn::time::milliseconds(10)); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 133 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 134 | face.sentInterests.clear(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 135 | } |
| 136 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 137 | void sendInterestForPublishedData() |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 138 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 139 | // Need to send an interest now since ChronoSync |
| 140 | // no longer does face->put(*data) in publishData. |
| 141 | // Instead it does it in onInterest |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 142 | ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix(); |
| 143 | lsaInterestName.append(nlsr.getConfParameter().getSiteName()); |
| 144 | lsaInterestName.append(nlsr.getConfParameter().getRouterName()); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 145 | lsaInterestName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 146 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 147 | // The part after LSA is Chronosync getSession |
| 148 | lsaInterestName.append(sessionTime); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 149 | lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
| 150 | |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 151 | std::shared_ptr<Interest> lsaInterest = std::make_shared<Interest>(lsaInterestName); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 152 | |
| 153 | face.receive(*lsaInterest); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 154 | this->advanceClocks(ndn::time::milliseconds(100)); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 155 | } |
| 156 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 157 | bool |
| 158 | wasRoutingUpdatePublished() |
| 159 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 160 | sendInterestForPublishedData(); |
| 161 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 162 | const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix(); |
| 163 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 164 | const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(), |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 165 | [lsaPrefix] (const ndn::Data& data) { |
| 166 | return lsaPrefix.isPrefixOf(data.getName()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 167 | } |
| 168 | ); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 169 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 170 | return (it != face.sentData.end()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 171 | } |
| 172 | |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 173 | void |
| 174 | checkResponseCode(const Name& commandPrefix, uint64_t expectedCode) |
| 175 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 176 | std::vector<Data>::iterator it = std::find_if(face.sentData.begin(), |
| 177 | face.sentData.end(), |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 178 | [commandPrefix] (const Data& data) { |
| 179 | return commandPrefix.isPrefixOf(data.getName()); |
| 180 | }); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 181 | BOOST_REQUIRE(it != face.sentData.end()); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 182 | |
| 183 | ndn::nfd::ControlResponse response(it->getContent().blockFromValue()); |
| 184 | BOOST_CHECK_EQUAL(response.getCode(), expectedCode); |
| 185 | } |
| 186 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 187 | public: |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 188 | ndn::util::DummyClientFace face; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 189 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 190 | ndn::Name siteIdentityName; |
| 191 | ndn::security::pib::Identity siteIdentity; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 192 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 193 | ndn::Name opIdentityName; |
| 194 | ndn::security::pib::Identity opIdentity; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 195 | |
| 196 | Nlsr nlsr; |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 197 | NamePrefixList& namePrefixList; |
| 198 | PrefixUpdateProcessor& updatePrefixUpdateProcessor; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 199 | |
| 200 | const boost::filesystem::path SITE_CERT_PATH; |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 201 | ndn::Name sessionTime; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture) |
| 205 | |
| 206 | BOOST_AUTO_TEST_CASE(Basic) |
| 207 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 208 | uint64_t nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 209 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 210 | ndn::nfd::ControlParameters parameters; |
| 211 | parameters.setName("/prefix/to/advertise/"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 212 | |
| 213 | // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters> |
| 214 | // /<timestamp>/<random-value>/<signed-interests-components> |
| 215 | |
| 216 | // Advertise |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 217 | ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 218 | |
| 219 | // append /<control-parameters> |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 220 | advertiseCommand.append(parameters.wireEncode()); |
| 221 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 222 | ndn::security::CommandInterestSigner cis(m_keyChain); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 223 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 224 | // CommandInterestSigner::makeCommandInterest() will append the last |
| 225 | // three components: (<timestamp>/<random-value>/<signed-interests-components>) |
| 226 | ndn::Interest advertiseInterest = |
| 227 | cis.makeCommandInterest(advertiseCommand, |
| 228 | ndn::security::signingByIdentity(opIdentity)); |
| 229 | |
| 230 | face.receive(advertiseInterest); |
| 231 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 232 | this->advanceClocks(ndn::time::milliseconds(10)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 233 | |
| 234 | NamePrefixList& namePrefixList = nlsr.getNamePrefixList(); |
| 235 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 236 | BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1); |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 237 | BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 238 | |
| 239 | BOOST_CHECK(wasRoutingUpdatePublished()); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 240 | BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 241 | |
| 242 | face.sentData.clear(); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 243 | nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 244 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 245 | //Withdraw |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 246 | ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw"); |
| 247 | withdrawCommand.append(parameters.wireEncode()); |
| 248 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 249 | ndn::Interest withdrawInterest |
| 250 | = cis.makeCommandInterest(withdrawCommand, |
| 251 | ndn::security::signingByIdentity(opIdentity)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 252 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 253 | face.receive(withdrawInterest); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 254 | this->advanceClocks(ndn::time::milliseconds(10)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 255 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 256 | BOOST_CHECK_EQUAL(namePrefixList.size(), 0); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 257 | |
| 258 | BOOST_CHECK(wasRoutingUpdatePublished()); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 259 | BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 260 | } |
| 261 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 262 | BOOST_AUTO_TEST_SUITE_END() |
| 263 | |
| 264 | } // namespace test |
| 265 | } // namespace update |
| 266 | } // namespace nlsr |