blob: 616b6e80ef0bc39f8e20d44744bb274d2bc517b4 [file] [log] [blame]
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento21353752020-11-20 00:43:44 -05003 * Copyright (c) 2014-2020, Regents of the University of California,
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +00004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050026#include "mgmt/rib-manager.hpp"
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000027
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040028#include "tests/test-common.hpp"
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040029#include "tests/key-chain-fixture.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shidf1dc652019-08-30 19:03:19 +000031#include "tests/daemon/rib/fib-updates-common.hpp"
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040032
Davide Pesavento21353752020-11-20 00:43:44 -050033#include <ndn-cxx/security/transform/base64-encode.hpp>
34#include <ndn-cxx/security/transform/buffer-source.hpp>
35#include <ndn-cxx/security/transform/stream-sink.hpp>
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000036#include <ndn-cxx/util/dummy-client-face.hpp>
37
Teng Liang18c2b292019-10-18 14:31:04 -070038#include <boost/property_tree/info_parser.hpp>
39
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000040namespace nfd {
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000041namespace tests {
42
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050043using rib::Route;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000044
Teng Liang18c2b292019-10-18 14:31:04 -070045static ConfigSection
46makeSection(const std::string& config)
47{
48 std::istringstream inputStream(config);
49 ConfigSection section;
50 boost::property_tree::read_info(inputStream, section);
51 return section;
52}
53
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040054class RibManagerSlAnnounceFixture : public GlobalIoTimeFixture, public KeyChainFixture
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000055{
56public:
57 using SlAnnounceResult = RibManager::SlAnnounceResult;
58
59 RibManagerSlAnnounceFixture()
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040060 : m_face(g_io, m_keyChain)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000061 , m_nfdController(m_face, m_keyChain)
62 , m_dispatcher(m_face, m_keyChain)
63 , m_fibUpdater(rib, m_nfdController)
64 , m_trustedSigner(m_keyChain.createIdentity("/trusted", ndn::RsaKeyParams()))
65 , m_untrustedSigner(m_keyChain.createIdentity("/untrusted", ndn::RsaKeyParams()))
66 {
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000067 // Face, Controller, Dispatcher are irrelevant to SlAnnounce functions but required by
68 // RibManager construction, so they are private. RibManager is a pointer to avoid code style
69 // rule 1.4 violation.
Davide Pesavento0a71dd32019-03-17 20:36:18 -040070 manager = make_unique<RibManager>(rib, m_face, m_keyChain, m_nfdController, m_dispatcher);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000071
Teng Liang18c2b292019-10-18 14:31:04 -070072 loadDefaultPaConfig();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000073 }
74
75 template<typename ...T>
76 ndn::PrefixAnnouncement
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050077 makeTrustedAnn(T&&... args)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000078 {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050079 return signPrefixAnn(makePrefixAnn(std::forward<T>(args)...), m_keyChain, m_trustedSigner);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000080 }
81
82 template<typename ...T>
83 ndn::PrefixAnnouncement
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050084 makeUntrustedAnn(T&&... args)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000085 {
Davide Pesavento8a05c7f2019-02-28 02:26:19 -050086 return signPrefixAnn(makePrefixAnn(std::forward<T>(args)...), m_keyChain, m_untrustedSigner);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000087 }
88
89 /** \brief Invoke manager->slAnnounce and wait for result.
90 */
91 SlAnnounceResult
92 slAnnounceSync(const ndn::PrefixAnnouncement& pa, uint64_t faceId, time::milliseconds maxLifetime)
93 {
94 optional<SlAnnounceResult> result;
95 manager->slAnnounce(pa, faceId, maxLifetime,
96 [&] (RibManager::SlAnnounceResult res) {
97 BOOST_CHECK(!result);
98 result = res;
99 });
100
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400101 g_io.poll();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000102 BOOST_CHECK(result);
103 return result.value_or(SlAnnounceResult::ERROR);
104 }
105
106 /** \brief Invoke manager->slRenew and wait for result.
107 */
108 SlAnnounceResult
109 slRenewSync(const Name& name, uint64_t faceId, time::milliseconds maxLifetime)
110 {
111 optional<SlAnnounceResult> result;
112 manager->slRenew(name, faceId, maxLifetime,
113 [&] (RibManager::SlAnnounceResult res) {
114 BOOST_CHECK(!result);
115 result = res;
116 });
117
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400118 g_io.poll();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000119 BOOST_CHECK(result);
120 return result.value_or(SlAnnounceResult::ERROR);
121 }
122
123 /** \brief Invoke manager->slFindAnn and wait for result.
124 */
125 optional<ndn::PrefixAnnouncement>
126 slFindAnnSync(const Name& name)
127 {
128 optional<optional<ndn::PrefixAnnouncement>> result;
129 manager->slFindAnn(name,
130 [&] (optional<ndn::PrefixAnnouncement> found) {
131 BOOST_CHECK(!result);
132 result = found;
133 });
134
Davide Pesaventoe1bdc082018-10-11 21:20:23 -0400135 g_io.poll();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000136 BOOST_CHECK(result);
137 return result.value_or(nullopt);
138 }
139
140 /** \brief Lookup a route with PREFIXANN origin.
141 */
142 Route*
Davide Pesavento21353752020-11-20 00:43:44 -0500143 findAnnRoute(const Name& name, uint64_t faceId) const
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000144 {
145 Route routeQuery;
146 routeQuery.faceId = faceId;
147 routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN;
148 return rib.find(name, routeQuery);
149 }
150
Davide Pesavento21353752020-11-20 00:43:44 -0500151 /** \brief Retrieve an identity certificate as a base64 string.
152 */
153 std::string
154 getIdentityCertificateBase64(const Name& identity) const
155 {
156 auto cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
157 const auto& block = cert.wireEncode();
158
159 namespace tr = ndn::security::transform;
160 std::ostringstream oss;
161 tr::bufferSource(block.wire(), block.size()) >> tr::base64Encode(false) >> tr::streamSink(oss);
162
163 return oss.str();
164 }
165
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000166private:
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000167 void
Teng Liang18c2b292019-10-18 14:31:04 -0700168 loadDefaultPaConfig()
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000169 {
Teng Liang18c2b292019-10-18 14:31:04 -0700170 const std::string CONFIG = R"CONFIG(
171 trust-anchor
172 {
173 type any
174 }
175 )CONFIG";
176 manager->applyPaConfig(makeSection(CONFIG), "default");
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000177 }
178
Davide Pesavento21353752020-11-20 00:43:44 -0500179protected:
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500180 rib::Rib rib;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000181 unique_ptr<RibManager> manager;
182
183private:
184 ndn::util::DummyClientFace m_face;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000185 ndn::nfd::Controller m_nfdController;
Davide Pesavento78ddcab2019-02-28 22:00:03 -0500186 Dispatcher m_dispatcher;
Junxiao Shidf1dc652019-08-30 19:03:19 +0000187 rib::tests::MockFibUpdater m_fibUpdater;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000188
189 ndn::security::SigningInfo m_trustedSigner;
190 ndn::security::SigningInfo m_untrustedSigner;
191};
192
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500193BOOST_AUTO_TEST_SUITE(Mgmt)
194BOOST_AUTO_TEST_SUITE(TestRibManager)
195BOOST_FIXTURE_TEST_SUITE(SlAnnounce, RibManagerSlAnnounceFixture)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000196
Teng Liang18c2b292019-10-18 14:31:04 -0700197BOOST_AUTO_TEST_CASE(AnnounceWithDefaultConfig)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000198{
Teng Liang18c2b292019-10-18 14:31:04 -0700199 auto pa = makeTrustedAnn("/fMXN7UeB", 1_h);
200 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 3275, 1_h), SlAnnounceResult::OK);
201 BOOST_CHECK(findAnnRoute("/fMXN7UeB", 3275) != nullptr);
202
203 auto pa2 = makeUntrustedAnn("/1nzAe0Y4", 1_h);
204 BOOST_CHECK_EQUAL(slAnnounceSync(pa2, 2959, 1_h), SlAnnounceResult::OK);
205 BOOST_CHECK(findAnnRoute("/1nzAe0Y4", 2959) != nullptr);
206}
207
208BOOST_AUTO_TEST_CASE(AnnounceWithEmptyConfig)
209{
210 manager->applyPaConfig(makeSection(""), "empty");
211
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000212 auto pa = makeTrustedAnn("/fMXN7UeB", 1_h);
213 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 3275, 1_h), SlAnnounceResult::VALIDATION_FAILURE);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000214 BOOST_CHECK(findAnnRoute("/fMXN7UeB", 3275) == nullptr);
Teng Liang18c2b292019-10-18 14:31:04 -0700215
216 auto pa2 = makeUntrustedAnn("/1nzAe0Y4", 1_h);
217 BOOST_CHECK_EQUAL(slAnnounceSync(pa2, 2959, 1_h), SlAnnounceResult::VALIDATION_FAILURE);
218 BOOST_CHECK(findAnnRoute("/1nzAe0Y4", 2959) == nullptr);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000219}
220
221BOOST_AUTO_TEST_CASE(AnnounceValidationError)
222{
Teng Liang18c2b292019-10-18 14:31:04 -0700223 ConfigSection section;
224 section.put("rule.id", "PA");
225 section.put("rule.for", "data");
226 section.put("rule.checker.type", "customized");
227 section.put("rule.checker.sig-type", "rsa-sha256");
228 section.put("rule.checker.key-locator.type", "name");
229 section.put("rule.checker.key-locator.name", "/trusted");
230 section.put("rule.checker.key-locator.relation", "is-prefix-of");
231 section.put("trust-anchor.type", "base64");
232 section.put("trust-anchor.base64-string", getIdentityCertificateBase64("/trusted"));
233 manager->applyPaConfig(section, "trust-schema.section");
234
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000235 auto pa = makeUntrustedAnn("/1nzAe0Y4", 1_h);
236 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 2959, 1_h), SlAnnounceResult::VALIDATION_FAILURE);
237
238 BOOST_CHECK(findAnnRoute("/1nzAe0Y4", 2959) == nullptr);
239}
240
241BOOST_AUTO_TEST_CASE(AnnounceInsert_AnnLifetime)
242{
243 auto pa = makeTrustedAnn("/EHJYmJz9", 1_h);
244 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 1641, 2_h), SlAnnounceResult::OK);
245
246 Route* route = findAnnRoute("/EHJYmJz9", 1641);
247 BOOST_REQUIRE(route != nullptr);
248 BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 1_h);
249 BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 1_h);
250}
251
252BOOST_AUTO_TEST_CASE(AnnounceInsert_ArgLifetime)
253{
254 auto pa = makeTrustedAnn("/BU9Fec9E", 2_h);
255 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 1282, 1_h), SlAnnounceResult::OK);
256
257 Route* route = findAnnRoute("/BU9Fec9E", 1282);
258 BOOST_REQUIRE(route != nullptr);
259 BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 2_h);
260 BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 1_h);
261}
262
263BOOST_AUTO_TEST_CASE(AnnounceReplace)
264{
265 auto pa = makeTrustedAnn("/HsBFGvL3", 1_h);
266 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 2813, 1_h), SlAnnounceResult::OK);
267
268 pa = makeTrustedAnn("/HsBFGvL3", 2_h);
269 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 2813, 2_h), SlAnnounceResult::OK);
270
271 Route* route = findAnnRoute("/HsBFGvL3", 2813);
272 BOOST_REQUIRE(route != nullptr);
273 BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 2_h);
274 BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 2_h);
275}
276
277BOOST_AUTO_TEST_CASE(AnnounceExpired)
278{
279 auto pa = makeTrustedAnn("/awrVv6V7", 1_h, std::make_pair(-3_h, -1_h));
280 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 9087, 1_h), SlAnnounceResult::EXPIRED);
281
282 BOOST_CHECK(findAnnRoute("/awrVv6V7", 9087) == nullptr);
283}
284
285BOOST_AUTO_TEST_CASE(RenewNotFound)
286{
287 BOOST_CHECK_EQUAL(slRenewSync("IAYigN73", 1070, 1_h), SlAnnounceResult::NOT_FOUND);
288
289 BOOST_CHECK(findAnnRoute("/IAYigN73", 1070) == nullptr);
290}
291
292BOOST_AUTO_TEST_CASE(RenewProlong_ArgLifetime)
293{
294 auto pa = makeTrustedAnn("/P2IYFqtr", 4_h);
295 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 4506, 2_h), SlAnnounceResult::OK);
296 advanceClocks(1_h); // Route has 1_h remaining lifetime
297
Teng Lianga4e6ec32018-10-21 09:25:00 -0700298 BOOST_CHECK_EQUAL(slRenewSync("/P2IYFqtr/2321", 4506, 2_h), SlAnnounceResult::OK);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000299
300 Route* route = findAnnRoute("/P2IYFqtr", 4506);
301 BOOST_REQUIRE(route != nullptr);
302 BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 3_h);
303 BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 2_h); // set by slRenew
304}
305
306BOOST_AUTO_TEST_CASE(RenewProlong_AnnLifetime)
307{
308 auto pa = makeTrustedAnn("/be01Yiba", 4_h);
309 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 1589, 2_h), SlAnnounceResult::OK);
310 advanceClocks(1_h); // Route has 1_h remaining lifetime
311
Teng Lianga4e6ec32018-10-21 09:25:00 -0700312 BOOST_CHECK_EQUAL(slRenewSync("/be01Yiba/4324", 1589, 5_h), SlAnnounceResult::OK);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000313
314 Route* route = findAnnRoute("/be01Yiba", 1589);
315 BOOST_REQUIRE(route != nullptr);
316 BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 3_h);
317 BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 3_h); // capped by annExpires
318}
319
320BOOST_AUTO_TEST_CASE(RenewShorten)
321{
322 auto pa = makeTrustedAnn("/5XCHYCAd", 4_h);
323 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 3851, 4_h), SlAnnounceResult::OK);
324 advanceClocks(1_h); // Route has 3_h remaining lifetime
325
Teng Lianga4e6ec32018-10-21 09:25:00 -0700326 BOOST_CHECK_EQUAL(slRenewSync("/5XCHYCAd/98934", 3851, 1_h), SlAnnounceResult::OK);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000327
328 Route* route = findAnnRoute("/5XCHYCAd", 3851);
329 BOOST_REQUIRE(route != nullptr);
330 BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 3_h);
331 BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 1_h); // set by slRenew
332}
333
334BOOST_AUTO_TEST_CASE(RenewShorten_Zero)
335{
336 auto pa = makeTrustedAnn("/cdQ7KPNw", 4_h);
337 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 8031, 4_h), SlAnnounceResult::OK);
338 advanceClocks(1_h); // Route has 3_h remaining lifetime
339
Teng Lianga4e6ec32018-10-21 09:25:00 -0700340 BOOST_CHECK_EQUAL(slRenewSync("/cdQ7KPNw/8023", 8031, 0_s), SlAnnounceResult::EXPIRED);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000341
342 BOOST_CHECK(findAnnRoute("/cdQ7KPNw", 8031) == nullptr);
343}
344
345BOOST_AUTO_TEST_CASE(FindExisting)
346{
347 auto pa = makeTrustedAnn("/JHugsjjr", 1_h);
348 BOOST_CHECK_EQUAL(slAnnounceSync(pa, 2363, 1_h), SlAnnounceResult::OK);
349
350 auto found = slFindAnnSync("/JHugsjjr");
351 BOOST_REQUIRE(found);
352 BOOST_CHECK_EQUAL(found->getAnnouncedName(), "/JHugsjjr");
353 BOOST_CHECK(found->getData());
354
355 auto found2 = slFindAnnSync("/JHugsjjr/StvXhKR5");
356 BOOST_CHECK(found == found2);
357}
358
359BOOST_AUTO_TEST_CASE(FindNew)
360{
361 Route route;
362 route.faceId = 1367;
363 route.origin = ndn::nfd::ROUTE_ORIGIN_APP;
364 rib.insert("/dLY1pRhR", route);
365
366 auto pa = slFindAnnSync("/dLY1pRhR/3qNK9Ngn");
367 BOOST_REQUIRE(pa);
368 BOOST_CHECK_EQUAL(pa->getAnnouncedName(), "/dLY1pRhR");
369}
370
371BOOST_AUTO_TEST_CASE(FindNone)
372{
373 auto pa = slFindAnnSync("/2YNeYuV2");
374 BOOST_CHECK(!pa);
375}
376
Davide Pesavento8a05c7f2019-02-28 02:26:19 -0500377BOOST_AUTO_TEST_SUITE_END() // SlAnnounce
378BOOST_AUTO_TEST_SUITE_END() // TestRibManager
379BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000380
381} // namespace tests
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000382} // namespace nfd