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> |
| 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 33 | |
| 34 | #include <boost/filesystem.hpp> |
| 35 | |
| 36 | using namespace ndn; |
| 37 | |
| 38 | namespace nlsr { |
| 39 | namespace update { |
| 40 | namespace test { |
| 41 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 42 | class PrefixUpdateFixture : public nlsr::test::UnitTestTimeFixture |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 43 | { |
| 44 | public: |
| 45 | PrefixUpdateFixture() |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 46 | : face(g_ioService, keyChain, {true, true}) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 47 | , siteIdentity(ndn::Name("/ndn/edu/test-site").appendVersion()) |
| 48 | , opIdentity(ndn::Name(siteIdentity).append(ndn::Name("%C1.Operator")).appendVersion()) |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 49 | , nlsr(g_ioService, g_scheduler, face, g_keyChain) |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 50 | , keyPrefix(("/ndn/broadcast")) |
| 51 | , updateProcessor(nlsr.getPrefixUpdateProcessor()) |
| 52 | , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert")) |
| 53 | { |
| 54 | createSiteCert(); |
| 55 | BOOST_REQUIRE(siteCert != nullptr); |
| 56 | |
| 57 | createOperatorCert(); |
| 58 | BOOST_REQUIRE(opCert != nullptr); |
| 59 | |
| 60 | const std::string CONFIG = |
| 61 | "rule\n" |
| 62 | "{\n" |
| 63 | " id \"NLSR ControlCommand Rule\"\n" |
| 64 | " for interest\n" |
| 65 | " filter\n" |
| 66 | " {\n" |
| 67 | " type name\n" |
| 68 | " regex ^<localhost><nlsr><prefix-update>[<advertise><withdraw>]<>$\n" |
| 69 | " }\n" |
| 70 | " checker\n" |
| 71 | " {\n" |
| 72 | " type customized\n" |
| 73 | " sig-type rsa-sha256\n" |
| 74 | " key-locator\n" |
| 75 | " {\n" |
| 76 | " type name\n" |
| 77 | " regex ^([^<KEY><%C1.Operator>]*)<%C1.Operator>[^<KEY>]*<KEY><ksk-.*><ID-CERT>$\n" |
| 78 | " }\n" |
| 79 | " }\n" |
| 80 | "}\n" |
| 81 | "rule\n" |
| 82 | "{\n" |
| 83 | " id \"NLSR Hierarchy Rule\"\n" |
| 84 | " for data\n" |
| 85 | " filter\n" |
| 86 | " {\n" |
| 87 | " type name\n" |
| 88 | " regex ^[^<KEY>]*<KEY><ksk-.*><ID-CERT><>$\n" |
| 89 | " }\n" |
| 90 | " checker\n" |
| 91 | " {\n" |
| 92 | " type hierarchical\n" |
| 93 | " sig-type rsa-sha256\n" |
| 94 | " }\n" |
| 95 | "}\n" |
| 96 | "trust-anchor\n" |
| 97 | "{\n" |
| 98 | " type file\n" |
| 99 | " file-name \"site.cert\"\n" |
| 100 | "}\n"; |
| 101 | |
| 102 | const boost::filesystem::path CONFIG_PATH = |
| 103 | (boost::filesystem::current_path() / std::string("unit-test.conf")); |
| 104 | |
| 105 | updateProcessor.getValidator().load(CONFIG, CONFIG_PATH.native()); |
| 106 | |
| 107 | // Insert certs after the validator is loaded since ValidatorConfig::load() clears |
| 108 | // the certificate cache |
| 109 | nlsr.addCertificateToCache(opCert); |
| 110 | |
| 111 | // Set the network so the LSA prefix is constructed |
| 112 | nlsr.getConfParameter().setNetwork("/ndn"); |
| 113 | |
| 114 | // Initialize NLSR so a sync socket is created |
| 115 | nlsr.initialize(); |
| 116 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 117 | // Saving clock::now before any advanceClocks so that it will |
| 118 | // be the same value as what ChronoSync uses in setting the sessionName |
| 119 | sessionTime.appendNumber(ndn::time::toUnixTimestamp(ndn::time::system_clock::now()).count()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 120 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 121 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 122 | face.sentInterests.clear(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void |
| 126 | createSiteCert() |
| 127 | { |
| 128 | // Site cert |
| 129 | keyChain.createIdentity(siteIdentity); |
| 130 | siteCertName = keyChain.getDefaultCertificateNameForIdentity(siteIdentity); |
| 131 | siteCert = keyChain.getCertificate(siteCertName); |
| 132 | |
| 133 | ndn::io::save(*siteCert, SITE_CERT_PATH.string()); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | createOperatorCert() |
| 138 | { |
| 139 | // Operator cert |
| 140 | ndn::Name keyName = keyChain.generateRsaKeyPairAsDefault(opIdentity, true); |
| 141 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 142 | opCert = std::make_shared<ndn::IdentityCertificate>(); |
| 143 | std::shared_ptr<ndn::PublicKey> pubKey = keyChain.getPublicKey(keyName); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 144 | opCertName = keyName.getPrefix(-1); |
| 145 | opCertName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion(); |
| 146 | opCert->setName(opCertName); |
| 147 | opCert->setNotBefore(time::system_clock::now() - time::days(1)); |
| 148 | opCert->setNotAfter(time::system_clock::now() + time::days(1)); |
| 149 | opCert->setPublicKeyInfo(*pubKey); |
Alexander Afanasyev | 4c2bb36 | 2017-03-14 12:29:19 -0700 | [diff] [blame] | 150 | opCert->addSubjectDescription(ndn::security::v1::CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME, |
| 151 | keyName.toUri())); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 152 | opCert->encode(); |
| 153 | |
| 154 | keyChain.signByIdentity(*opCert, siteIdentity); |
| 155 | |
| 156 | keyChain.addCertificateAsIdentityDefault(*opCert); |
| 157 | } |
| 158 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 159 | void sendInterestForPublishedData() { |
| 160 | // Need to send an interest now since ChronoSync |
| 161 | // no longer does face->put(*data) in publishData. |
| 162 | // Instead it does it in onInterest |
| 163 | ndn::Name lsaInterestName("/localhop/ndn/NLSR/LSA"); |
| 164 | // The part after LSA is Chronosync getSession |
| 165 | lsaInterestName.append(sessionTime); |
| 166 | lsaInterestName.appendNumber(nlsr.getSequencingManager().getCombinedSeqNo()); |
| 167 | shared_ptr<Interest> lsaInterest = make_shared<Interest>(lsaInterestName); |
| 168 | |
| 169 | face.receive(*lsaInterest); |
| 170 | this->advanceClocks(ndn::time::milliseconds(10)); |
| 171 | } |
| 172 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 173 | bool |
| 174 | wasRoutingUpdatePublished() |
| 175 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 176 | sendInterestForPublishedData(); |
| 177 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 178 | const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix(); |
| 179 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 180 | const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(), |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 181 | [lsaPrefix] (const ndn::Data& data) { |
| 182 | return lsaPrefix.isPrefixOf(data.getName()); |
| 183 | }); |
| 184 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 185 | return (it != face.sentData.end()); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 186 | } |
| 187 | |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 188 | void |
| 189 | checkResponseCode(const Name& commandPrefix, uint64_t expectedCode) |
| 190 | { |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 191 | std::vector<Data>::iterator it = std::find_if(face.sentData.begin(), |
| 192 | face.sentData.end(), |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 193 | [commandPrefix] (const Data& data) { |
| 194 | return commandPrefix.isPrefixOf(data.getName()); |
| 195 | }); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 196 | BOOST_REQUIRE(it != face.sentData.end()); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 197 | |
| 198 | ndn::nfd::ControlResponse response(it->getContent().blockFromValue()); |
| 199 | BOOST_CHECK_EQUAL(response.getCode(), expectedCode); |
| 200 | } |
| 201 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 202 | ~PrefixUpdateFixture() |
| 203 | { |
| 204 | keyChain.deleteIdentity(siteIdentity); |
| 205 | keyChain.deleteIdentity(opIdentity); |
| 206 | |
| 207 | boost::filesystem::remove(SITE_CERT_PATH); |
| 208 | } |
| 209 | |
| 210 | public: |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 211 | ndn::util::DummyClientFace face; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 212 | ndn::KeyChain keyChain; |
| 213 | |
| 214 | ndn::Name siteIdentity; |
| 215 | ndn::Name siteCertName; |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 216 | std::shared_ptr<IdentityCertificate> siteCert; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 217 | |
| 218 | ndn::Name opIdentity; |
| 219 | ndn::Name opCertName; |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 220 | std::shared_ptr<IdentityCertificate> opCert; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 221 | |
| 222 | Nlsr nlsr; |
| 223 | ndn::Name keyPrefix; |
| 224 | PrefixUpdateProcessor& updateProcessor; |
| 225 | |
| 226 | const boost::filesystem::path SITE_CERT_PATH; |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 227 | ndn::Name sessionTime; |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture) |
| 231 | |
| 232 | BOOST_AUTO_TEST_CASE(Basic) |
| 233 | { |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 234 | updateProcessor.enable(); |
| 235 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 236 | // Advertise |
| 237 | ndn::nfd::ControlParameters parameters; |
| 238 | parameters.setName("/prefix/to/advertise/"); |
| 239 | |
| 240 | ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise"); |
| 241 | advertiseCommand.append(parameters.wireEncode()); |
| 242 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 243 | std::shared_ptr<Interest> advertiseInterest = std::make_shared<Interest>(advertiseCommand); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 244 | keyChain.signByIdentity(*advertiseInterest, opIdentity); |
| 245 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 246 | face.receive(*advertiseInterest); |
| 247 | this->advanceClocks(ndn::time::milliseconds(10)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 248 | |
| 249 | NamePrefixList& namePrefixList = nlsr.getNamePrefixList(); |
| 250 | |
| 251 | BOOST_REQUIRE_EQUAL(namePrefixList.getSize(), 1); |
| 252 | BOOST_CHECK_EQUAL(namePrefixList.getNameList().front(), parameters.getName()); |
| 253 | |
| 254 | BOOST_CHECK(wasRoutingUpdatePublished()); |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 255 | |
| 256 | face.sentData.clear(); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 257 | |
| 258 | // Withdraw |
| 259 | ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw"); |
| 260 | withdrawCommand.append(parameters.wireEncode()); |
| 261 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 262 | std::shared_ptr<Interest> withdrawInterest = std::make_shared<Interest>(withdrawCommand); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 263 | keyChain.signByIdentity(*withdrawInterest, opIdentity); |
| 264 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 265 | face.receive(*withdrawInterest); |
| 266 | this->advanceClocks(ndn::time::milliseconds(10)); |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 267 | |
| 268 | BOOST_CHECK_EQUAL(namePrefixList.getSize(), 0); |
| 269 | |
| 270 | BOOST_CHECK(wasRoutingUpdatePublished()); |
| 271 | } |
| 272 | |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 273 | BOOST_AUTO_TEST_CASE(DisabledAndEnabled) |
| 274 | { |
| 275 | ndn::nfd::ControlParameters parameters; |
| 276 | parameters.setName("/prefix/to/advertise/"); |
| 277 | |
| 278 | ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise"); |
| 279 | advertiseCommand.append(parameters.wireEncode()); |
| 280 | |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 281 | std::shared_ptr<Interest> advertiseInterest = std::make_shared<Interest>(advertiseCommand); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 282 | keyChain.signByIdentity(*advertiseInterest, opIdentity); |
| 283 | |
| 284 | // Command should be rejected |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 285 | face.receive(*advertiseInterest); |
| 286 | this->advanceClocks(ndn::time::milliseconds(10)); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 287 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 288 | BOOST_REQUIRE(!face.sentData.empty()); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 289 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 290 | const ndn::MetaInfo& metaInfo = face.sentData.front().getMetaInfo(); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 291 | BOOST_CHECK_EQUAL(metaInfo.getType(), ndn::tlv::ContentType_Nack); |
| 292 | |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 293 | face.sentData.clear(); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 294 | |
| 295 | // Enable PrefixUpdateProcessor so commands will be processed |
| 296 | updateProcessor.enable(); |
| 297 | |
| 298 | // Command should be accepted |
Ashlesh Gawande | 415676b | 2016-12-22 00:26:23 -0600 | [diff] [blame] | 299 | face.receive(*advertiseInterest); |
| 300 | this->advanceClocks(ndn::time::milliseconds(10)); |
Vince Lehman | d33e5bc | 2015-06-22 15:27:50 -0500 | [diff] [blame] | 301 | |
| 302 | checkResponseCode(advertiseCommand, 200); |
| 303 | } |
| 304 | |
alvy | 297f416 | 2015-03-03 17:15:33 -0600 | [diff] [blame] | 305 | BOOST_AUTO_TEST_SUITE_END() |
| 306 | |
| 307 | } // namespace test |
| 308 | } // namespace update |
| 309 | } // namespace nlsr |