Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, Regents of the University of California, |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 4 | * 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 | |
| 26 | #include "rib/readvertise/host-to-gateway-readvertise-policy.hpp" |
| 27 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 29 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 30 | #include "tests/daemon/global-io-fixture.hpp" |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 31 | |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 32 | namespace nfd { |
| 33 | namespace rib { |
| 34 | namespace tests { |
| 35 | |
| 36 | using namespace nfd::tests; |
| 37 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 38 | class HostToGatewayReadvertisePolicyFixture : public GlobalIoFixture, public KeyChainFixture |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 41 | static RibRouteRef |
| 42 | makeNewRoute(const Name& prefix) |
| 43 | { |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 44 | auto entry = make_shared<RibEntry>(); |
| 45 | entry->setName(prefix); |
| 46 | |
| 47 | Route route; |
| 48 | auto routeIt = entry->insertRoute(route).first; |
| 49 | return RibRouteRef{entry, routeIt}; |
| 50 | } |
| 51 | |
| 52 | shared_ptr<HostToGatewayReadvertisePolicy> |
| 53 | makePolicy(const ConfigSection& section = ConfigSection()) |
| 54 | { |
| 55 | return make_shared<HostToGatewayReadvertisePolicy>(m_keyChain, section); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | BOOST_AUTO_TEST_SUITE(Readvertise) |
Davide Pesavento | 0a71dd3 | 2019-03-17 20:36:18 -0400 | [diff] [blame] | 60 | BOOST_FIXTURE_TEST_SUITE(TestHostToGatewayReadvertisePolicy, HostToGatewayReadvertisePolicyFixture) |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 61 | |
| 62 | BOOST_AUTO_TEST_CASE(PrefixToAdvertise) |
| 63 | { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 64 | BOOST_REQUIRE(m_keyChain.createIdentity("/A")); |
| 65 | BOOST_REQUIRE(m_keyChain.createIdentity("/A/B")); |
| 66 | BOOST_REQUIRE(m_keyChain.createIdentity("/C/nrd")); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 67 | |
| 68 | auto test = [this] (Name routeName, optional<ReadvertiseAction> expectedAction) { |
| 69 | auto policy = makePolicy(); |
| 70 | auto action = policy->handleNewRoute(makeNewRoute(routeName)); |
| 71 | |
| 72 | if (expectedAction) { |
| 73 | BOOST_REQUIRE(action); |
| 74 | BOOST_CHECK_EQUAL(action->prefix, expectedAction->prefix); |
| 75 | BOOST_REQUIRE_EQUAL(action->signer, expectedAction->signer); |
| 76 | } |
| 77 | else { |
| 78 | BOOST_REQUIRE(!action); |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | test("/D/app", nullopt); |
| 83 | test("/A/B/app", ReadvertiseAction{"/A", ndn::security::signingByIdentity("/A")}); |
| 84 | test("/C/nrd", ReadvertiseAction{"/C", ndn::security::signingByIdentity("/C/nrd")}); |
| 85 | } |
| 86 | |
| 87 | BOOST_AUTO_TEST_CASE(DontReadvertise) |
| 88 | { |
| 89 | auto policy = makePolicy(); |
| 90 | BOOST_REQUIRE(!policy->handleNewRoute(makeNewRoute("/localhost/test"))); |
| 91 | BOOST_REQUIRE(!policy->handleNewRoute(makeNewRoute("/localhop/nfd"))); |
| 92 | } |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(LoadRefreshInterval) |
| 95 | { |
| 96 | auto policy = makePolicy(); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 97 | BOOST_CHECK_EQUAL(policy->getRefreshInterval(), 25_s); // default setting is 25 |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 98 | |
| 99 | ConfigSection section; |
| 100 | section.put("refresh_interval_wrong", 10); |
| 101 | policy = makePolicy(section); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 102 | BOOST_CHECK_EQUAL(policy->getRefreshInterval(), 25_s); // wrong formate |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 103 | |
| 104 | section.put("refresh_interval", 10); |
| 105 | policy = makePolicy(section); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 106 | BOOST_CHECK_EQUAL(policy->getRefreshInterval(), 10_s); |
Yanbiao Li | f48d080 | 2018-06-01 03:00:02 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_SUITE_END() // TestHostToGatewayReadvertisePolicy |
| 110 | BOOST_AUTO_TEST_SUITE_END() // Readvertise |
| 111 | |
| 112 | } // namespace tests |
| 113 | } // namespace rib |
| 114 | } // namespace nfd |