alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, 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(); |
| 120 | |
| 121 | addIdentity(ndn::Name("/ndn/edu/test-site/%C1.Router/this-router")); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 122 | |
| 123 | // Initialize NLSR so a sync socket is created |
| 124 | nlsr.initialize(); |
| 125 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 126 | // Saving clock::now before any advanceClocks so that it will |
| 127 | // be the same value as what ChronoSync uses in setting the sessionName |
| 128 | sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()); |
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 | this->advanceClocks(ndn::time::milliseconds(10)); |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 131 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 132 | face.sentInterests.clear(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 133 | } |
| 134 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 135 | void sendInterestForPublishedData() |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 136 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 137 | // Need to send an interest now since ChronoSync |
| 138 | // no longer does face->put(*data) in publishData. |
| 139 | // Instead it does it in onInterest |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 140 | ndn::Name lsaInterestName = nlsr.getConfParameter().getLsaPrefix(); |
| 141 | lsaInterestName.append(nlsr.getConfParameter().getSiteName()); |
| 142 | lsaInterestName.append(nlsr.getConfParameter().getRouterName()); |
Nick Gordon | 727d483 | 2017-10-13 18:04:25 -0500 | [diff] [blame] | 143 | lsaInterestName.append(std::to_string(Lsa::Type::NAME)); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 144 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 145 | // The part after LSA is Chronosync getSession |
| 146 | lsaInterestName.append(sessionTime); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 147 | lsaInterestName.appendNumber(nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
| 148 | |
Nick Gordon | e98480b | 2017-05-24 11:23:03 -0500 | [diff] [blame] | 149 | std::shared_ptr<Interest> lsaInterest = std::make_shared<Interest>(lsaInterestName); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 150 | |
| 151 | face.receive(*lsaInterest); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 152 | this->advanceClocks(ndn::time::milliseconds(100)); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 153 | } |
| 154 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 155 | bool |
| 156 | wasRoutingUpdatePublished() |
| 157 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 158 | sendInterestForPublishedData(); |
| 159 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 160 | const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix(); |
| 161 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 162 | const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(), |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 163 | [lsaPrefix] (const ndn::Data& data) { |
| 164 | return lsaPrefix.isPrefixOf(data.getName()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 165 | } |
| 166 | ); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 167 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 168 | return (it != face.sentData.end()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 169 | } |
| 170 | |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 171 | void |
| 172 | checkResponseCode(const Name& commandPrefix, uint64_t expectedCode) |
| 173 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 174 | std::vector<Data>::iterator it = std::find_if(face.sentData.begin(), |
| 175 | face.sentData.end(), |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 176 | [commandPrefix] (const Data& data) { |
| 177 | return commandPrefix.isPrefixOf(data.getName()); |
| 178 | }); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 179 | BOOST_REQUIRE(it != face.sentData.end()); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 180 | |
| 181 | ndn::nfd::ControlResponse response(it->getContent().blockFromValue()); |
| 182 | BOOST_CHECK_EQUAL(response.getCode(), expectedCode); |
| 183 | } |
| 184 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 185 | public: |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 186 | ndn::util::DummyClientFace face; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 187 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 188 | ndn::Name siteIdentityName; |
| 189 | ndn::security::pib::Identity siteIdentity; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 190 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 191 | ndn::Name opIdentityName; |
| 192 | ndn::security::pib::Identity opIdentity; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 193 | |
| 194 | Nlsr nlsr; |
Laqin Fan | 54a43f0 | 2017-03-08 12:31:30 -0600 | [diff] [blame] | 195 | NamePrefixList& namePrefixList; |
| 196 | PrefixUpdateProcessor& updatePrefixUpdateProcessor; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 197 | |
| 198 | const boost::filesystem::path SITE_CERT_PATH; |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 199 | ndn::Name sessionTime; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture) |
| 203 | |
| 204 | BOOST_AUTO_TEST_CASE(Basic) |
| 205 | { |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 206 | uint64_t nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 207 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 208 | ndn::nfd::ControlParameters parameters; |
| 209 | parameters.setName("/prefix/to/advertise/"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 210 | |
| 211 | // Control Command format: /<prefix>/<management-module>/<command-verb>/<control-parameters> |
| 212 | // /<timestamp>/<random-value>/<signed-interests-components> |
| 213 | |
| 214 | // Advertise |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 215 | ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise"); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 216 | |
| 217 | // append /<control-parameters> |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 218 | advertiseCommand.append(parameters.wireEncode()); |
| 219 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 220 | ndn::security::CommandInterestSigner cis(m_keyChain); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 221 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 222 | // CommandInterestSigner::makeCommandInterest() will append the last |
| 223 | // three components: (<timestamp>/<random-value>/<signed-interests-components>) |
| 224 | ndn::Interest advertiseInterest = |
| 225 | cis.makeCommandInterest(advertiseCommand, |
| 226 | ndn::security::signingByIdentity(opIdentity)); |
| 227 | |
| 228 | face.receive(advertiseInterest); |
| 229 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 230 | this->advanceClocks(ndn::time::milliseconds(10)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 231 | |
| 232 | NamePrefixList& namePrefixList = nlsr.getNamePrefixList(); |
| 233 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 234 | BOOST_REQUIRE_EQUAL(namePrefixList.size(), 1); |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 235 | BOOST_CHECK_EQUAL(namePrefixList.getNames().front(), parameters.getName()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 236 | |
| 237 | BOOST_CHECK(wasRoutingUpdatePublished()); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 238 | BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 239 | |
| 240 | face.sentData.clear(); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 241 | nameLsaSeqNoBeforeInterest = nlsr.getLsdb().getSequencingManager().getNameLsaSeq(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 242 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 243 | //Withdraw |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 244 | ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw"); |
| 245 | withdrawCommand.append(parameters.wireEncode()); |
| 246 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 247 | ndn::Interest withdrawInterest |
| 248 | = cis.makeCommandInterest(withdrawCommand, |
| 249 | ndn::security::signingByIdentity(opIdentity)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 250 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 251 | face.receive(withdrawInterest); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 252 | this->advanceClocks(ndn::time::milliseconds(10)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 253 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 254 | BOOST_CHECK_EQUAL(namePrefixList.size(), 0); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 255 | |
| 256 | BOOST_CHECK(wasRoutingUpdatePublished()); |
Ashlesh Gawande | 3e105a0 | 2017-05-16 17:36:56 -0500 | [diff] [blame] | 257 | BOOST_CHECK(nameLsaSeqNoBeforeInterest < nlsr.getLsdb().getSequencingManager().getNameLsaSeq()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 258 | } |
| 259 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 260 | BOOST_AUTO_TEST_SUITE_END() |
| 261 | |
| 262 | } // namespace test |
| 263 | } // namespace update |
| 264 | } // namespace nlsr |