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