blob: 3c9dd5e6a377ae662be3af38f1aeb839b629f376 [file] [log] [blame]
Yanbiao Lif48d0802018-06-01 03:00:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
jaczhifeb86502025-03-06 19:12:27 -08003 * Copyright (c) 2014-2025, 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;
jaczhifeb86502025-03-06 19:12:27 -080046 route.cost = 200;
Yanbiao Lif48d0802018-06-01 03:00:02 -070047 auto routeIt = entry->insertRoute(route).first;
48 return RibRouteRef{entry, routeIt};
49 }
50
51 shared_ptr<HostToGatewayReadvertisePolicy>
52 makePolicy(const ConfigSection& section = ConfigSection())
53 {
54 return make_shared<HostToGatewayReadvertisePolicy>(m_keyChain, section);
55 }
56};
57
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050058BOOST_AUTO_TEST_SUITE(Rib)
Davide Pesavento0a71dd32019-03-17 20:36:18 -040059BOOST_FIXTURE_TEST_SUITE(TestHostToGatewayReadvertisePolicy, HostToGatewayReadvertisePolicyFixture)
Yanbiao Lif48d0802018-06-01 03:00:02 -070060
61BOOST_AUTO_TEST_CASE(PrefixToAdvertise)
62{
Davide Pesavento21353752020-11-20 00:43:44 -050063 BOOST_REQUIRE(m_keyChain.createIdentity("/A"));
64 BOOST_REQUIRE(m_keyChain.createIdentity("/A/B"));
65 BOOST_REQUIRE(m_keyChain.createIdentity("/C/nrd"));
Yanbiao Lif48d0802018-06-01 03:00:02 -070066
jaczhifeb86502025-03-06 19:12:27 -080067 auto policy = makePolicy();
68 auto test = [&policy] (Name routeName, std::optional<ReadvertiseAction> expectedAction) {
Yanbiao Lif48d0802018-06-01 03:00:02 -070069 auto action = policy->handleNewRoute(makeNewRoute(routeName));
70
71 if (expectedAction) {
72 BOOST_REQUIRE(action);
jaczhifeb86502025-03-06 19:12:27 -080073 BOOST_TEST(action->prefix == expectedAction->prefix);
74 BOOST_TEST(action->cost == expectedAction->cost);
75 BOOST_TEST(action->signer == expectedAction->signer);
Yanbiao Lif48d0802018-06-01 03:00:02 -070076 }
77 else {
jaczhifeb86502025-03-06 19:12:27 -080078 BOOST_TEST(!action);
Yanbiao Lif48d0802018-06-01 03:00:02 -070079 }
80 };
81
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040082 test("/D/app", std::nullopt);
jaczhifeb86502025-03-06 19:12:27 -080083 test("/A/B/app", ReadvertiseAction{"/A", 200, ndn::security::signingByIdentity("/A")});
84 test("/C/nrd", ReadvertiseAction{"/C", 200, ndn::security::signingByIdentity("/C/nrd")});
Yanbiao Lif48d0802018-06-01 03:00:02 -070085}
86
87BOOST_AUTO_TEST_CASE(DontReadvertise)
88{
89 auto policy = makePolicy();
jaczhifeb86502025-03-06 19:12:27 -080090 BOOST_TEST(!policy->handleNewRoute(makeNewRoute("/localhost/test")));
91 BOOST_TEST(!policy->handleNewRoute(makeNewRoute("/localhop/nfd")));
Yanbiao Lif48d0802018-06-01 03:00:02 -070092}
93
94BOOST_AUTO_TEST_CASE(LoadRefreshInterval)
95{
96 auto policy = makePolicy();
jaczhifeb86502025-03-06 19:12:27 -080097 BOOST_TEST(policy->getRefreshInterval() == 25_s); // default setting is 25s
Yanbiao Lif48d0802018-06-01 03:00:02 -070098
99 ConfigSection section;
100 section.put("refresh_interval_wrong", 10);
101 policy = makePolicy(section);
jaczhifeb86502025-03-06 19:12:27 -0800102 BOOST_TEST(policy->getRefreshInterval() == 25_s); // wrong syntax
Yanbiao Lif48d0802018-06-01 03:00:02 -0700103
104 section.put("refresh_interval", 10);
105 policy = makePolicy(section);
jaczhifeb86502025-03-06 19:12:27 -0800106 BOOST_TEST(policy->getRefreshInterval() == 10_s);
Yanbiao Lif48d0802018-06-01 03:00:02 -0700107}
108
109BOOST_AUTO_TEST_SUITE_END() // TestHostToGatewayReadvertisePolicy
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500110BOOST_AUTO_TEST_SUITE_END() // Rib
Yanbiao Lif48d0802018-06-01 03:00:02 -0700111
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400112} // namespace nfd::tests