blob: 169fc48c03ac0a6f6d92c0bba766c5cadde47926 [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
alvy297f4162015-03-03 17:15:33 -06004 * 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 Shi3e5120c2016-09-10 16:58:34 +000029#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
30#include <ndn-cxx/mgmt/nfd/control-response.hpp>
alvy297f4162015-03-03 17:15:33 -060031#include <ndn-cxx/security/key-chain.hpp>
32#include <ndn-cxx/util/dummy-client-face.hpp>
33
34#include <boost/filesystem.hpp>
35
36using namespace ndn;
37
38namespace nlsr {
39namespace update {
40namespace test {
41
Ashlesh Gawande415676b2016-12-22 00:26:23 -060042class PrefixUpdateFixture : public nlsr::test::UnitTestTimeFixture
alvy297f4162015-03-03 17:15:33 -060043{
44public:
45 PrefixUpdateFixture()
Ashlesh Gawande415676b2016-12-22 00:26:23 -060046 : face(g_ioService, keyChain, {true, true})
alvy297f4162015-03-03 17:15:33 -060047 , siteIdentity(ndn::Name("/ndn/edu/test-site").appendVersion())
48 , opIdentity(ndn::Name(siteIdentity).append(ndn::Name("%C1.Operator")).appendVersion())
Ashlesh Gawande415676b2016-12-22 00:26:23 -060049 , nlsr(g_ioService, g_scheduler, face, g_keyChain)
alvy297f4162015-03-03 17:15:33 -060050 , 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 Gawande415676b2016-12-22 00:26:23 -0600117 // 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());
alvy297f4162015-03-03 17:15:33 -0600120
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600121 this->advanceClocks(ndn::time::milliseconds(10));
122 face.sentInterests.clear();
alvy297f4162015-03-03 17:15:33 -0600123 }
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
dmcoomes9f936662017-03-02 10:33:09 -0600142 opCert = std::make_shared<ndn::IdentityCertificate>();
143 std::shared_ptr<ndn::PublicKey> pubKey = keyChain.getPublicKey(keyName);
alvy297f4162015-03-03 17:15:33 -0600144 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 Afanasyev4c2bb362017-03-14 12:29:19 -0700150 opCert->addSubjectDescription(ndn::security::v1::CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
151 keyName.toUri()));
alvy297f4162015-03-03 17:15:33 -0600152 opCert->encode();
153
154 keyChain.signByIdentity(*opCert, siteIdentity);
155
156 keyChain.addCertificateAsIdentityDefault(*opCert);
157 }
158
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600159 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
alvy297f4162015-03-03 17:15:33 -0600173 bool
174 wasRoutingUpdatePublished()
175 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600176 sendInterestForPublishedData();
177
alvy297f4162015-03-03 17:15:33 -0600178 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
179
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600180 const auto& it = std::find_if(face.sentData.begin(), face.sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600181 [lsaPrefix] (const ndn::Data& data) {
182 return lsaPrefix.isPrefixOf(data.getName());
183 });
184
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600185 return (it != face.sentData.end());
alvy297f4162015-03-03 17:15:33 -0600186 }
187
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500188 void
189 checkResponseCode(const Name& commandPrefix, uint64_t expectedCode)
190 {
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600191 std::vector<Data>::iterator it = std::find_if(face.sentData.begin(),
192 face.sentData.end(),
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500193 [commandPrefix] (const Data& data) {
194 return commandPrefix.isPrefixOf(data.getName());
195 });
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600196 BOOST_REQUIRE(it != face.sentData.end());
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500197
198 ndn::nfd::ControlResponse response(it->getContent().blockFromValue());
199 BOOST_CHECK_EQUAL(response.getCode(), expectedCode);
200 }
201
alvy297f4162015-03-03 17:15:33 -0600202 ~PrefixUpdateFixture()
203 {
204 keyChain.deleteIdentity(siteIdentity);
205 keyChain.deleteIdentity(opIdentity);
206
207 boost::filesystem::remove(SITE_CERT_PATH);
208 }
209
210public:
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600211 ndn::util::DummyClientFace face;
alvy297f4162015-03-03 17:15:33 -0600212 ndn::KeyChain keyChain;
213
214 ndn::Name siteIdentity;
215 ndn::Name siteCertName;
dmcoomes9f936662017-03-02 10:33:09 -0600216 std::shared_ptr<IdentityCertificate> siteCert;
alvy297f4162015-03-03 17:15:33 -0600217
218 ndn::Name opIdentity;
219 ndn::Name opCertName;
dmcoomes9f936662017-03-02 10:33:09 -0600220 std::shared_ptr<IdentityCertificate> opCert;
alvy297f4162015-03-03 17:15:33 -0600221
222 Nlsr nlsr;
223 ndn::Name keyPrefix;
224 PrefixUpdateProcessor& updateProcessor;
225
226 const boost::filesystem::path SITE_CERT_PATH;
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600227 ndn::Name sessionTime;
alvy297f4162015-03-03 17:15:33 -0600228};
229
230BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
231
232BOOST_AUTO_TEST_CASE(Basic)
233{
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500234 updateProcessor.enable();
235
alvy297f4162015-03-03 17:15:33 -0600236 // 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
dmcoomes9f936662017-03-02 10:33:09 -0600243 std::shared_ptr<Interest> advertiseInterest = std::make_shared<Interest>(advertiseCommand);
alvy297f4162015-03-03 17:15:33 -0600244 keyChain.signByIdentity(*advertiseInterest, opIdentity);
245
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600246 face.receive(*advertiseInterest);
247 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600248
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 Gawande415676b2016-12-22 00:26:23 -0600255
256 face.sentData.clear();
alvy297f4162015-03-03 17:15:33 -0600257
258 // Withdraw
259 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
260 withdrawCommand.append(parameters.wireEncode());
261
dmcoomes9f936662017-03-02 10:33:09 -0600262 std::shared_ptr<Interest> withdrawInterest = std::make_shared<Interest>(withdrawCommand);
alvy297f4162015-03-03 17:15:33 -0600263 keyChain.signByIdentity(*withdrawInterest, opIdentity);
264
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600265 face.receive(*withdrawInterest);
266 this->advanceClocks(ndn::time::milliseconds(10));
alvy297f4162015-03-03 17:15:33 -0600267
268 BOOST_CHECK_EQUAL(namePrefixList.getSize(), 0);
269
270 BOOST_CHECK(wasRoutingUpdatePublished());
271}
272
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500273BOOST_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
dmcoomes9f936662017-03-02 10:33:09 -0600281 std::shared_ptr<Interest> advertiseInterest = std::make_shared<Interest>(advertiseCommand);
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500282 keyChain.signByIdentity(*advertiseInterest, opIdentity);
283
284 // Command should be rejected
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600285 face.receive(*advertiseInterest);
286 this->advanceClocks(ndn::time::milliseconds(10));
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500287
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600288 BOOST_REQUIRE(!face.sentData.empty());
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500289
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600290 const ndn::MetaInfo& metaInfo = face.sentData.front().getMetaInfo();
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500291 BOOST_CHECK_EQUAL(metaInfo.getType(), ndn::tlv::ContentType_Nack);
292
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600293 face.sentData.clear();
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500294
295 // Enable PrefixUpdateProcessor so commands will be processed
296 updateProcessor.enable();
297
298 // Command should be accepted
Ashlesh Gawande415676b2016-12-22 00:26:23 -0600299 face.receive(*advertiseInterest);
300 this->advanceClocks(ndn::time::milliseconds(10));
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500301
302 checkResponseCode(advertiseCommand, 200);
303}
304
alvy297f4162015-03-03 17:15:33 -0600305BOOST_AUTO_TEST_SUITE_END()
306
307} // namespace test
308} // namespace update
309} // namespace nlsr