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