blob: adb13f49eeef693ad321597c6ebd184709ccd922 [file] [log] [blame]
alvy297f4162015-03-03 17:15:33 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, The University of Memphis,
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>
29#include <ndn-cxx/management/nfd-control-parameters.hpp>
30#include <ndn-cxx/security/key-chain.hpp>
31#include <ndn-cxx/util/dummy-client-face.hpp>
32
33#include <boost/filesystem.hpp>
34
35using namespace ndn;
36
37namespace nlsr {
38namespace update {
39namespace test {
40
41class PrefixUpdateFixture : public nlsr::test::BaseFixture
42{
43public:
44 PrefixUpdateFixture()
45 : face(ndn::util::makeDummyClientFace(g_ioService))
46 , siteIdentity(ndn::Name("/ndn/edu/test-site").appendVersion())
47 , opIdentity(ndn::Name(siteIdentity).append(ndn::Name("%C1.Operator")).appendVersion())
48 , nlsr(g_ioService, g_scheduler, *face)
49 , keyPrefix(("/ndn/broadcast"))
50 , updateProcessor(nlsr.getPrefixUpdateProcessor())
51 , SITE_CERT_PATH(boost::filesystem::current_path() / std::string("site.cert"))
52 {
53 createSiteCert();
54 BOOST_REQUIRE(siteCert != nullptr);
55
56 createOperatorCert();
57 BOOST_REQUIRE(opCert != nullptr);
58
59 const std::string CONFIG =
60 "rule\n"
61 "{\n"
62 " id \"NLSR ControlCommand Rule\"\n"
63 " for interest\n"
64 " filter\n"
65 " {\n"
66 " type name\n"
67 " regex ^<localhost><nlsr><prefix-update>[<advertise><withdraw>]<>$\n"
68 " }\n"
69 " checker\n"
70 " {\n"
71 " type customized\n"
72 " sig-type rsa-sha256\n"
73 " key-locator\n"
74 " {\n"
75 " type name\n"
76 " regex ^([^<KEY><%C1.Operator>]*)<%C1.Operator>[^<KEY>]*<KEY><ksk-.*><ID-CERT>$\n"
77 " }\n"
78 " }\n"
79 "}\n"
80 "rule\n"
81 "{\n"
82 " id \"NLSR Hierarchy Rule\"\n"
83 " for data\n"
84 " filter\n"
85 " {\n"
86 " type name\n"
87 " regex ^[^<KEY>]*<KEY><ksk-.*><ID-CERT><>$\n"
88 " }\n"
89 " checker\n"
90 " {\n"
91 " type hierarchical\n"
92 " sig-type rsa-sha256\n"
93 " }\n"
94 "}\n"
95 "trust-anchor\n"
96 "{\n"
97 " type file\n"
98 " file-name \"site.cert\"\n"
99 "}\n";
100
101 const boost::filesystem::path CONFIG_PATH =
102 (boost::filesystem::current_path() / std::string("unit-test.conf"));
103
104 updateProcessor.getValidator().load(CONFIG, CONFIG_PATH.native());
105
106 // Insert certs after the validator is loaded since ValidatorConfig::load() clears
107 // the certificate cache
108 nlsr.addCertificateToCache(opCert);
109
110 // Set the network so the LSA prefix is constructed
111 nlsr.getConfParameter().setNetwork("/ndn");
112
113 // Initialize NLSR so a sync socket is created
114 nlsr.initialize();
115
116 // Listen on localhost prefix
117 updateProcessor.startListening();
118
119 face->processEvents(ndn::time::milliseconds(1));
120 face->sentInterests.clear();
121 }
122
123 void
124 createSiteCert()
125 {
126 // Site cert
127 keyChain.createIdentity(siteIdentity);
128 siteCertName = keyChain.getDefaultCertificateNameForIdentity(siteIdentity);
129 siteCert = keyChain.getCertificate(siteCertName);
130
131 ndn::io::save(*siteCert, SITE_CERT_PATH.string());
132 }
133
134 void
135 createOperatorCert()
136 {
137 // Operator cert
138 ndn::Name keyName = keyChain.generateRsaKeyPairAsDefault(opIdentity, true);
139
140 opCert = ndn::make_shared<ndn::IdentityCertificate>();
141 ndn::shared_ptr<ndn::PublicKey> pubKey = keyChain.getPublicKey(keyName);
142 opCertName = keyName.getPrefix(-1);
143 opCertName.append("KEY").append(keyName.get(-1)).append("ID-CERT").appendVersion();
144 opCert->setName(opCertName);
145 opCert->setNotBefore(time::system_clock::now() - time::days(1));
146 opCert->setNotAfter(time::system_clock::now() + time::days(1));
147 opCert->setPublicKeyInfo(*pubKey);
148 opCert->addSubjectDescription(CertificateSubjectDescription(ndn::oid::ATTRIBUTE_NAME,
149 keyName.toUri()));
150 opCert->encode();
151
152 keyChain.signByIdentity(*opCert, siteIdentity);
153
154 keyChain.addCertificateAsIdentityDefault(*opCert);
155 }
156
157 bool
158 wasRoutingUpdatePublished()
159 {
160 const ndn::Name& lsaPrefix = nlsr.getConfParameter().getLsaPrefix();
161
162 const auto& it = std::find_if(face->sentDatas.begin(), face->sentDatas.end(),
163 [lsaPrefix] (const ndn::Data& data) {
164 return lsaPrefix.isPrefixOf(data.getName());
165 });
166
167 return (it != face->sentDatas.end());
168 }
169
170 ~PrefixUpdateFixture()
171 {
172 keyChain.deleteIdentity(siteIdentity);
173 keyChain.deleteIdentity(opIdentity);
174
175 boost::filesystem::remove(SITE_CERT_PATH);
176 }
177
178public:
179 shared_ptr<ndn::util::DummyClientFace> face;
180 ndn::KeyChain keyChain;
181
182 ndn::Name siteIdentity;
183 ndn::Name siteCertName;
184 shared_ptr<IdentityCertificate> siteCert;
185
186 ndn::Name opIdentity;
187 ndn::Name opCertName;
188 shared_ptr<IdentityCertificate> opCert;
189
190 Nlsr nlsr;
191 ndn::Name keyPrefix;
192 PrefixUpdateProcessor& updateProcessor;
193
194 const boost::filesystem::path SITE_CERT_PATH;
195};
196
197BOOST_FIXTURE_TEST_SUITE(TestPrefixUpdateProcessor, PrefixUpdateFixture)
198
199BOOST_AUTO_TEST_CASE(Basic)
200{
201 // Advertise
202 ndn::nfd::ControlParameters parameters;
203 parameters.setName("/prefix/to/advertise/");
204
205 ndn::Name advertiseCommand("/localhost/nlsr/prefix-update/advertise");
206 advertiseCommand.append(parameters.wireEncode());
207
208 shared_ptr<Interest> advertiseInterest = make_shared<Interest>(advertiseCommand);
209 keyChain.signByIdentity(*advertiseInterest, opIdentity);
210
211 face->receive(*advertiseInterest);
212 face->processEvents(ndn::time::milliseconds(1));
213
214 NamePrefixList& namePrefixList = nlsr.getNamePrefixList();
215
216 BOOST_REQUIRE_EQUAL(namePrefixList.getSize(), 1);
217 BOOST_CHECK_EQUAL(namePrefixList.getNameList().front(), parameters.getName());
218
219 BOOST_CHECK(wasRoutingUpdatePublished());
220 face->sentDatas.clear();
221
222 // Withdraw
223 ndn::Name withdrawCommand("/localhost/nlsr/prefix-update/withdraw");
224 withdrawCommand.append(parameters.wireEncode());
225
226 shared_ptr<Interest> withdrawInterest = make_shared<Interest>(withdrawCommand);
227 keyChain.signByIdentity(*withdrawInterest, opIdentity);
228
229 face->receive(*withdrawInterest);
230 face->processEvents(ndn::time::milliseconds(1));
231
232 BOOST_CHECK_EQUAL(namePrefixList.getSize(), 0);
233
234 BOOST_CHECK(wasRoutingUpdatePublished());
235}
236
237BOOST_AUTO_TEST_SUITE_END()
238
239} // namespace test
240} // namespace update
241} // namespace nlsr