blob: e9979d24afa9585dbda144326a840e492f01a4c7 [file] [log] [blame]
Yanbiao Lif48d0802018-06-01 03:00:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventocaa60cc2024-02-18 18:18:37 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Yanbiao Lif48d0802018-06-01 03:00:02 -07004 * 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 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"
Yanbiao Lif48d0802018-06-01 03:00:02 -070031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tests {
Yanbiao Lif48d0802018-06-01 03:00:02 -070033
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040034using namespace nfd::rib;
Yanbiao Lif48d0802018-06-01 03:00:02 -070035
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040036class HostToGatewayReadvertisePolicyFixture : public GlobalIoFixture, public KeyChainFixture
Yanbiao Lif48d0802018-06-01 03:00:02 -070037{
38public:
Davide Pesavento0a71dd32019-03-17 20:36:18 -040039 static RibRouteRef
40 makeNewRoute(const Name& prefix)
41 {
Yanbiao Lif48d0802018-06-01 03:00:02 -070042 auto entry = make_shared<RibEntry>();
43 entry->setName(prefix);
44
45 Route route;
46 auto routeIt = entry->insertRoute(route).first;
47 return RibRouteRef{entry, routeIt};
48 }
49
50 shared_ptr<HostToGatewayReadvertisePolicy>
51 makePolicy(const ConfigSection& section = ConfigSection())
52 {
53 return make_shared<HostToGatewayReadvertisePolicy>(m_keyChain, section);
54 }
55};
56
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050057BOOST_AUTO_TEST_SUITE(Rib)
Davide Pesavento0a71dd32019-03-17 20:36:18 -040058BOOST_FIXTURE_TEST_SUITE(TestHostToGatewayReadvertisePolicy, HostToGatewayReadvertisePolicyFixture)
Yanbiao Lif48d0802018-06-01 03:00:02 -070059
60BOOST_AUTO_TEST_CASE(PrefixToAdvertise)
61{
Davide Pesavento21353752020-11-20 00:43:44 -050062 BOOST_REQUIRE(m_keyChain.createIdentity("/A"));
63 BOOST_REQUIRE(m_keyChain.createIdentity("/A/B"));
64 BOOST_REQUIRE(m_keyChain.createIdentity("/C/nrd"));
Yanbiao Lif48d0802018-06-01 03:00:02 -070065
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040066 auto test = [this] (Name routeName, std::optional<ReadvertiseAction> expectedAction) {
Yanbiao Lif48d0802018-06-01 03:00:02 -070067 auto policy = makePolicy();
68 auto action = policy->handleNewRoute(makeNewRoute(routeName));
69
70 if (expectedAction) {
71 BOOST_REQUIRE(action);
72 BOOST_CHECK_EQUAL(action->prefix, expectedAction->prefix);
73 BOOST_REQUIRE_EQUAL(action->signer, expectedAction->signer);
74 }
75 else {
76 BOOST_REQUIRE(!action);
77 }
78 };
79
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040080 test("/D/app", std::nullopt);
Yanbiao Lif48d0802018-06-01 03:00:02 -070081 test("/A/B/app", ReadvertiseAction{"/A", ndn::security::signingByIdentity("/A")});
82 test("/C/nrd", ReadvertiseAction{"/C", ndn::security::signingByIdentity("/C/nrd")});
83}
84
85BOOST_AUTO_TEST_CASE(DontReadvertise)
86{
87 auto policy = makePolicy();
88 BOOST_REQUIRE(!policy->handleNewRoute(makeNewRoute("/localhost/test")));
89 BOOST_REQUIRE(!policy->handleNewRoute(makeNewRoute("/localhop/nfd")));
90}
91
92BOOST_AUTO_TEST_CASE(LoadRefreshInterval)
93{
94 auto policy = makePolicy();
Davide Pesavento14e71f02019-03-28 17:35:25 -040095 BOOST_CHECK_EQUAL(policy->getRefreshInterval(), 25_s); // default setting is 25
Yanbiao Lif48d0802018-06-01 03:00:02 -070096
97 ConfigSection section;
98 section.put("refresh_interval_wrong", 10);
99 policy = makePolicy(section);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400100 BOOST_CHECK_EQUAL(policy->getRefreshInterval(), 25_s); // wrong formate
Yanbiao Lif48d0802018-06-01 03:00:02 -0700101
102 section.put("refresh_interval", 10);
103 policy = makePolicy(section);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400104 BOOST_CHECK_EQUAL(policy->getRefreshInterval(), 10_s);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700105}
106
107BOOST_AUTO_TEST_SUITE_END() // TestHostToGatewayReadvertisePolicy
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500108BOOST_AUTO_TEST_SUITE_END() // Rib
Yanbiao Lif48d0802018-06-01 03:00:02 -0700109
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400110} // namespace nfd::tests