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