| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2014-2022, Regents of the University of California, |
| * Arizona Board of Regents, |
| * Colorado State University, |
| * University Pierre & Marie Curie, Sorbonne University, |
| * Washington University in St. Louis, |
| * Beijing Institute of Technology, |
| * The University of Memphis. |
| * |
| * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| * See AUTHORS.md for complete list of NFD authors and contributors. |
| * |
| * NFD is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #include "mgmt/rib-manager.hpp" |
| |
| #include "tests/test-common.hpp" |
| #include "tests/key-chain-fixture.hpp" |
| #include "tests/daemon/global-io-fixture.hpp" |
| #include "tests/daemon/rib/fib-updates-common.hpp" |
| |
| #include <ndn-cxx/security/transform/base64-encode.hpp> |
| #include <ndn-cxx/security/transform/buffer-source.hpp> |
| #include <ndn-cxx/security/transform/stream-sink.hpp> |
| #include <ndn-cxx/util/dummy-client-face.hpp> |
| |
| #include <boost/property_tree/info_parser.hpp> |
| |
| namespace nfd::tests { |
| |
| using rib::Route; |
| |
| static ConfigSection |
| makeSection(const std::string& config) |
| { |
| std::istringstream inputStream(config); |
| ConfigSection section; |
| boost::property_tree::read_info(inputStream, section); |
| return section; |
| } |
| |
| class RibManagerSlAnnounceFixture : public GlobalIoTimeFixture, public KeyChainFixture |
| { |
| public: |
| using SlAnnounceResult = RibManager::SlAnnounceResult; |
| |
| RibManagerSlAnnounceFixture() |
| : m_face(g_io, m_keyChain) |
| , m_nfdController(m_face, m_keyChain) |
| , m_dispatcher(m_face, m_keyChain) |
| , m_fibUpdater(rib, m_nfdController) |
| , m_trustedSigner(m_keyChain.createIdentity("/trusted", ndn::RsaKeyParams())) |
| , m_untrustedSigner(m_keyChain.createIdentity("/untrusted", ndn::RsaKeyParams())) |
| { |
| // Face, Controller, Dispatcher are irrelevant to SlAnnounce functions but required by |
| // RibManager construction, so they are private. RibManager is a pointer to avoid code style |
| // rule 1.4 violation. |
| manager = make_unique<RibManager>(rib, m_face, m_keyChain, m_nfdController, m_dispatcher); |
| |
| loadDefaultPaConfig(); |
| } |
| |
| template<typename ...T> |
| ndn::PrefixAnnouncement |
| makeTrustedAnn(T&&... args) |
| { |
| return signPrefixAnn(makePrefixAnn(std::forward<T>(args)...), m_keyChain, m_trustedSigner); |
| } |
| |
| template<typename ...T> |
| ndn::PrefixAnnouncement |
| makeUntrustedAnn(T&&... args) |
| { |
| return signPrefixAnn(makePrefixAnn(std::forward<T>(args)...), m_keyChain, m_untrustedSigner); |
| } |
| |
| /** \brief Invoke manager->slAnnounce and wait for result. |
| */ |
| SlAnnounceResult |
| slAnnounceSync(const ndn::PrefixAnnouncement& pa, uint64_t faceId, time::milliseconds maxLifetime) |
| { |
| std::optional<SlAnnounceResult> result; |
| manager->slAnnounce(pa, faceId, maxLifetime, |
| [&] (RibManager::SlAnnounceResult res) { |
| BOOST_CHECK(!result); |
| result = res; |
| }); |
| |
| g_io.poll(); |
| BOOST_CHECK(result); |
| return result.value_or(SlAnnounceResult::ERROR); |
| } |
| |
| /** \brief Invoke manager->slRenew and wait for result. |
| */ |
| SlAnnounceResult |
| slRenewSync(const Name& name, uint64_t faceId, time::milliseconds maxLifetime) |
| { |
| std::optional<SlAnnounceResult> result; |
| manager->slRenew(name, faceId, maxLifetime, |
| [&] (RibManager::SlAnnounceResult res) { |
| BOOST_CHECK(!result); |
| result = res; |
| }); |
| |
| g_io.poll(); |
| BOOST_CHECK(result); |
| return result.value_or(SlAnnounceResult::ERROR); |
| } |
| |
| /** \brief Invoke manager->slFindAnn and wait for result. |
| */ |
| std::optional<ndn::PrefixAnnouncement> |
| slFindAnnSync(const Name& name) |
| { |
| std::optional<std::optional<ndn::PrefixAnnouncement>> result; |
| manager->slFindAnn(name, |
| [&] (std::optional<ndn::PrefixAnnouncement> found) { |
| BOOST_CHECK(!result); |
| result = found; |
| }); |
| |
| g_io.poll(); |
| BOOST_CHECK(result); |
| return result.value_or(std::nullopt); |
| } |
| |
| /** \brief Lookup a route with PREFIXANN origin. |
| */ |
| Route* |
| findAnnRoute(const Name& name, uint64_t faceId) const |
| { |
| Route routeQuery; |
| routeQuery.faceId = faceId; |
| routeQuery.origin = ndn::nfd::ROUTE_ORIGIN_PREFIXANN; |
| return rib.find(name, routeQuery); |
| } |
| |
| /** \brief Retrieve an identity certificate as a base64 string. |
| */ |
| std::string |
| getIdentityCertificateBase64(const Name& identity) const |
| { |
| namespace tr = ndn::security::transform; |
| |
| auto cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate(); |
| std::ostringstream oss; |
| tr::bufferSource(cert.wireEncode()) >> tr::base64Encode(false) >> tr::streamSink(oss); |
| return oss.str(); |
| } |
| |
| private: |
| void |
| loadDefaultPaConfig() |
| { |
| const std::string CONFIG = R"CONFIG( |
| trust-anchor |
| { |
| type any |
| } |
| )CONFIG"; |
| manager->applyPaConfig(makeSection(CONFIG), "default"); |
| } |
| |
| protected: |
| rib::Rib rib; |
| unique_ptr<RibManager> manager; |
| |
| private: |
| ndn::util::DummyClientFace m_face; |
| ndn::nfd::Controller m_nfdController; |
| Dispatcher m_dispatcher; |
| MockFibUpdater m_fibUpdater; |
| |
| ndn::security::SigningInfo m_trustedSigner; |
| ndn::security::SigningInfo m_untrustedSigner; |
| }; |
| |
| BOOST_AUTO_TEST_SUITE(Mgmt) |
| BOOST_AUTO_TEST_SUITE(TestRibManager) |
| BOOST_FIXTURE_TEST_SUITE(SlAnnounce, RibManagerSlAnnounceFixture) |
| |
| BOOST_AUTO_TEST_CASE(AnnounceWithDefaultConfig) |
| { |
| auto pa = makeTrustedAnn("/fMXN7UeB", 1_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 3275, 1_h), SlAnnounceResult::OK); |
| BOOST_CHECK(findAnnRoute("/fMXN7UeB", 3275) != nullptr); |
| |
| auto pa2 = makeUntrustedAnn("/1nzAe0Y4", 1_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa2, 2959, 1_h), SlAnnounceResult::OK); |
| BOOST_CHECK(findAnnRoute("/1nzAe0Y4", 2959) != nullptr); |
| } |
| |
| BOOST_AUTO_TEST_CASE(AnnounceWithEmptyConfig) |
| { |
| manager->applyPaConfig(makeSection(""), "empty"); |
| |
| auto pa = makeTrustedAnn("/fMXN7UeB", 1_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 3275, 1_h), SlAnnounceResult::VALIDATION_FAILURE); |
| BOOST_CHECK(findAnnRoute("/fMXN7UeB", 3275) == nullptr); |
| |
| auto pa2 = makeUntrustedAnn("/1nzAe0Y4", 1_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa2, 2959, 1_h), SlAnnounceResult::VALIDATION_FAILURE); |
| BOOST_CHECK(findAnnRoute("/1nzAe0Y4", 2959) == nullptr); |
| } |
| |
| BOOST_AUTO_TEST_CASE(AnnounceValidationError) |
| { |
| ConfigSection section; |
| section.put("rule.id", "PA"); |
| section.put("rule.for", "data"); |
| section.put("rule.checker.type", "customized"); |
| section.put("rule.checker.sig-type", "rsa-sha256"); |
| section.put("rule.checker.key-locator.type", "name"); |
| section.put("rule.checker.key-locator.name", "/trusted"); |
| section.put("rule.checker.key-locator.relation", "is-prefix-of"); |
| section.put("trust-anchor.type", "base64"); |
| section.put("trust-anchor.base64-string", getIdentityCertificateBase64("/trusted")); |
| manager->applyPaConfig(section, "trust-schema.section"); |
| |
| auto pa = makeUntrustedAnn("/1nzAe0Y4", 1_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 2959, 1_h), SlAnnounceResult::VALIDATION_FAILURE); |
| |
| BOOST_CHECK(findAnnRoute("/1nzAe0Y4", 2959) == nullptr); |
| } |
| |
| BOOST_AUTO_TEST_CASE(AnnounceInsert_AnnLifetime) |
| { |
| auto pa = makeTrustedAnn("/EHJYmJz9", 1_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 1641, 2_h), SlAnnounceResult::OK); |
| |
| Route* route = findAnnRoute("/EHJYmJz9", 1641); |
| BOOST_REQUIRE(route != nullptr); |
| BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 1_h); |
| BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 1_h); |
| } |
| |
| BOOST_AUTO_TEST_CASE(AnnounceInsert_ArgLifetime) |
| { |
| auto pa = makeTrustedAnn("/BU9Fec9E", 2_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 1282, 1_h), SlAnnounceResult::OK); |
| |
| Route* route = findAnnRoute("/BU9Fec9E", 1282); |
| BOOST_REQUIRE(route != nullptr); |
| BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 2_h); |
| BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 1_h); |
| } |
| |
| BOOST_AUTO_TEST_CASE(AnnounceReplace) |
| { |
| auto pa = makeTrustedAnn("/HsBFGvL3", 1_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 2813, 1_h), SlAnnounceResult::OK); |
| |
| pa = makeTrustedAnn("/HsBFGvL3", 2_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 2813, 2_h), SlAnnounceResult::OK); |
| |
| Route* route = findAnnRoute("/HsBFGvL3", 2813); |
| BOOST_REQUIRE(route != nullptr); |
| BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 2_h); |
| BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 2_h); |
| } |
| |
| BOOST_AUTO_TEST_CASE(AnnounceExpired) |
| { |
| auto pa = makeTrustedAnn("/awrVv6V7", 1_h, std::pair(-3_h, -1_h)); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 9087, 1_h), SlAnnounceResult::EXPIRED); |
| |
| BOOST_CHECK(findAnnRoute("/awrVv6V7", 9087) == nullptr); |
| } |
| |
| BOOST_AUTO_TEST_CASE(RenewNotFound) |
| { |
| BOOST_CHECK_EQUAL(slRenewSync("IAYigN73", 1070, 1_h), SlAnnounceResult::NOT_FOUND); |
| |
| BOOST_CHECK(findAnnRoute("/IAYigN73", 1070) == nullptr); |
| } |
| |
| BOOST_AUTO_TEST_CASE(RenewProlong_ArgLifetime) |
| { |
| auto pa = makeTrustedAnn("/P2IYFqtr", 4_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 4506, 2_h), SlAnnounceResult::OK); |
| advanceClocks(1_h); // Route has 1_h remaining lifetime |
| |
| BOOST_CHECK_EQUAL(slRenewSync("/P2IYFqtr/2321", 4506, 2_h), SlAnnounceResult::OK); |
| |
| Route* route = findAnnRoute("/P2IYFqtr", 4506); |
| BOOST_REQUIRE(route != nullptr); |
| BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 3_h); |
| BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 2_h); // set by slRenew |
| } |
| |
| BOOST_AUTO_TEST_CASE(RenewProlong_AnnLifetime) |
| { |
| auto pa = makeTrustedAnn("/be01Yiba", 4_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 1589, 2_h), SlAnnounceResult::OK); |
| advanceClocks(1_h); // Route has 1_h remaining lifetime |
| |
| BOOST_CHECK_EQUAL(slRenewSync("/be01Yiba/4324", 1589, 5_h), SlAnnounceResult::OK); |
| |
| Route* route = findAnnRoute("/be01Yiba", 1589); |
| BOOST_REQUIRE(route != nullptr); |
| BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 3_h); |
| BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 3_h); // capped by annExpires |
| } |
| |
| BOOST_AUTO_TEST_CASE(RenewShorten) |
| { |
| auto pa = makeTrustedAnn("/5XCHYCAd", 4_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 3851, 4_h), SlAnnounceResult::OK); |
| advanceClocks(1_h); // Route has 3_h remaining lifetime |
| |
| BOOST_CHECK_EQUAL(slRenewSync("/5XCHYCAd/98934", 3851, 1_h), SlAnnounceResult::OK); |
| |
| Route* route = findAnnRoute("/5XCHYCAd", 3851); |
| BOOST_REQUIRE(route != nullptr); |
| BOOST_CHECK_EQUAL(route->annExpires, time::steady_clock::now() + 3_h); |
| BOOST_CHECK_EQUAL(route->expires.value(), time::steady_clock::now() + 1_h); // set by slRenew |
| } |
| |
| BOOST_AUTO_TEST_CASE(RenewShorten_Zero) |
| { |
| auto pa = makeTrustedAnn("/cdQ7KPNw", 4_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 8031, 4_h), SlAnnounceResult::OK); |
| advanceClocks(1_h); // Route has 3_h remaining lifetime |
| |
| BOOST_CHECK_EQUAL(slRenewSync("/cdQ7KPNw/8023", 8031, 0_s), SlAnnounceResult::EXPIRED); |
| |
| BOOST_CHECK(findAnnRoute("/cdQ7KPNw", 8031) == nullptr); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FindExisting) |
| { |
| auto pa = makeTrustedAnn("/JHugsjjr", 1_h); |
| BOOST_CHECK_EQUAL(slAnnounceSync(pa, 2363, 1_h), SlAnnounceResult::OK); |
| |
| auto found = slFindAnnSync("/JHugsjjr"); |
| BOOST_REQUIRE(found); |
| BOOST_CHECK_EQUAL(found->getAnnouncedName(), "/JHugsjjr"); |
| BOOST_CHECK(found->getData()); |
| |
| auto found2 = slFindAnnSync("/JHugsjjr/StvXhKR5"); |
| BOOST_CHECK(found == found2); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FindNew) |
| { |
| Route route; |
| route.faceId = 1367; |
| route.origin = ndn::nfd::ROUTE_ORIGIN_APP; |
| rib.insert("/dLY1pRhR", route); |
| |
| auto pa = slFindAnnSync("/dLY1pRhR/3qNK9Ngn"); |
| BOOST_REQUIRE(pa); |
| BOOST_CHECK_EQUAL(pa->getAnnouncedName(), "/dLY1pRhR"); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FindNone) |
| { |
| auto pa = slFindAnnSync("/2YNeYuV2"); |
| BOOST_CHECK(!pa); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // SlAnnounce |
| BOOST_AUTO_TEST_SUITE_END() // TestRibManager |
| BOOST_AUTO_TEST_SUITE_END() // Mgmt |
| |
| } // namespace nfd::tests |