blob: 983f698bc1c69e0e23c5570dd94cf80e786594f9 [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
42class PrefixUpdateFixture : public nlsr::test::BaseFixture
43{
44public:
45 PrefixUpdateFixture()
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060046 : face(make_shared<ndn::util::DummyClientFace>(g_ioService))
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())
49 , nlsr(g_ioService, g_scheduler, *face)
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
117 // Listen on localhost prefix
118 updateProcessor.startListening();
119
120 face->processEvents(ndn::time::milliseconds(1));
121 face->sentInterests.clear();
122 }
123
124 void
125 createSiteCert()
126 {
127 // Site cert
128 keyChain.createIdentity(siteIdentity);
129 siteCertName = keyChain.getDefaultCertificateNameForIdentity(siteIdentity);
130 siteCert = keyChain.getCertificate(siteCertName);
131
132 ndn::io::save(*siteCert, SITE_CERT_PATH.string());
133 }
134
135 void
136 createOperatorCert()
137 {
138 // Operator cert
139 ndn::Name keyName = keyChain.generateRsaKeyPairAsDefault(opIdentity, true);
140
141 opCert = ndn::make_shared<ndn::IdentityCertificate>();
142 ndn::shared_ptr<ndn::PublicKey> pubKey = keyChain.getPublicKey(keyName);
143 opCertName = keyName.getPrefix(-1);
144 opCertName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
145 opCert->setName(opCertName);
146 opCert->setNotBefore(time::system_clock::now() - time::days(1));
147 opCert->setNotAfter(time::system_clock::now() + time::days(1));
148 opCert->setPublicKeyInfo(*pubKey);
149 opCert->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
150 keyName.toUri()));
151 opCert->encode();
152
153 keyChain.signByIdentity(*opCert, siteIdentity);
154
155 keyChain.addCertificateAsIdentityDefault(*opCert);
156 }
157
158 bool
159 wasRoutingUpdatePublished()
160 {
161 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
162
Junxiao Shic778e812016-07-14 15:44:26 +0000163 const auto& it = std::find_if(face->sentData.begin(), face->sentData.end(),
alvy297f4162015-03-03 17:15:33 -0600164 [lsaPrefix] (const ndn::Data& data) {
165 return lsaPrefix.isPrefixOf(data.getName());
166 });
167
Junxiao Shic778e812016-07-14 15:44:26 +0000168 return (it != face->sentData.end());
alvy297f4162015-03-03 17:15:33 -0600169 }
170
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500171 void
172 checkResponseCode(const Name& commandPrefix, uint64_t expectedCode)
173 {
Junxiao Shic778e812016-07-14 15:44:26 +0000174 std::vector<Data>::iterator it = std::find_if(face->sentData.begin(),
175 face->sentData.end(),
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500176 [commandPrefix] (const Data& data) {
177 return commandPrefix.isPrefixOf(data.getName());
178 });
Junxiao Shic778e812016-07-14 15:44:26 +0000179 BOOST_REQUIRE(it != face->sentData.end());
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500180
181 ndn::nfd::ControlResponse response(it->getContent().blockFromValue());
182 BOOST_CHECK_EQUAL(response.getCode(), expectedCode);
183 }
184
alvy297f4162015-03-03 17:15:33 -0600185 ~PrefixUpdateFixture()
186 {
187 keyChain.deleteIdentity(siteIdentity);
188 keyChain.deleteIdentity(opIdentity);
189
190 boost::filesystem::remove(SITE_CERT_PATH);
191 }
192
193public:
194 shared_ptr<ndn::util::DummyClientFace> face;
195 ndn::KeyChain keyChain;
196
197 ndn::Name siteIdentity;
198 ndn::Name siteCertName;
199 shared_ptr<IdentityCertificate> siteCert;
200
201 ndn::Name opIdentity;
202 ndn::Name opCertName;
203 shared_ptr<IdentityCertificate> opCert;
204
205 Nlsr nlsr;
206 ndn::Name keyPrefix;
207 PrefixUpdateProcessor& updateProcessor;
208
209 const boost::filesystem::path SITE_CERT_PATH;
210};
211
212BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
213
214BOOST_AUTO_TEST_CASE(Basic)
215{
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500216 updateProcessor.enable();
217
alvy297f4162015-03-03 17:15:33 -0600218 // Advertise
219 ndn::nfd::ControlParameters parameters;
220 parameters.setName("/prefix/to/advertise/");
221
222 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
223 advertiseCommand.append(parameters.wireEncode());
224
225 shared_ptr<Interest> advertiseInterest = make_shared<Interest>(advertiseCommand);
226 keyChain.signByIdentity(*advertiseInterest, opIdentity);
227
228 face->receive(*advertiseInterest);
229 face->processEvents(ndn::time::milliseconds(1));
230
231 NamePrefixList& namePrefixList = nlsr.getNamePrefixList();
232
233 BOOST_REQUIRE_EQUAL(namePrefixList.getSize(), 1);
234 BOOST_CHECK_EQUAL(namePrefixList.getNameList().front(), parameters.getName());
235
236 BOOST_CHECK(wasRoutingUpdatePublished());
Junxiao Shic778e812016-07-14 15:44:26 +0000237 face->sentData.clear();
alvy297f4162015-03-03 17:15:33 -0600238
239 // Withdraw
240 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
241 withdrawCommand.append(parameters.wireEncode());
242
243 shared_ptr<Interest> withdrawInterest = make_shared<Interest>(withdrawCommand);
244 keyChain.signByIdentity(*withdrawInterest, opIdentity);
245
246 face->receive(*withdrawInterest);
247 face->processEvents(ndn::time::milliseconds(1));
248
249 BOOST_CHECK_EQUAL(namePrefixList.getSize(), 0);
250
251 BOOST_CHECK(wasRoutingUpdatePublished());
252}
253
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500254BOOST_AUTO_TEST_CASE(DisabledAndEnabled)
255{
256 ndn::nfd::ControlParameters parameters;
257 parameters.setName("/prefix/to/advertise/");
258
259 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
260 advertiseCommand.append(parameters.wireEncode());
261
262 shared_ptr<Interest> advertiseInterest = make_shared<Interest>(advertiseCommand);
263 keyChain.signByIdentity(*advertiseInterest, opIdentity);
264
265 // Command should be rejected
266 face->receive(*advertiseInterest);
267 face->processEvents(ndn::time::milliseconds(1));
268
Junxiao Shic778e812016-07-14 15:44:26 +0000269 BOOST_REQUIRE(!face->sentData.empty());
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500270
Junxiao Shic778e812016-07-14 15:44:26 +0000271 const ndn::MetaInfo& metaInfo = face->sentData.front().getMetaInfo();
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500272 BOOST_CHECK_EQUAL(metaInfo.getType(), ndn::tlv::ContentType_Nack);
273
Junxiao Shic778e812016-07-14 15:44:26 +0000274 face->sentData.clear();
Vince Lehmand33e5bc2015-06-22 15:27:50 -0500275
276 // Enable PrefixUpdateProcessor so commands will be processed
277 updateProcessor.enable();
278
279 // Command should be accepted
280 face->receive(*advertiseInterest);
281 face->processEvents(ndn::time::milliseconds(1));
282
283 checkResponseCode(advertiseCommand, 200);
284}
285
alvy297f4162015-03-03 17:15:33 -0600286BOOST_AUTO_TEST_SUITE_END()
287
288} // namespace test
289} // namespace update
290} // namespace nlsr