blob: f6e61ea1ce3a8e7fb79b57271fbc8b69a8da12e3 [file] [log] [blame]
Nick Gordon89c4cca2016-11-02 15:42:32 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shid47cd632018-09-11 03:10:00 +00002/*
Davide Pesaventocaa60cc2024-02-18 18:18:37 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Nick Gordon89c4cca2016-11-02 15:42:32 +00004 * 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/rib-entry.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040027
Nick Gordon89c4cca2016-11-02 15:42:32 +000028#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
Nick Gordon89c4cca2016-11-02 15:42:32 +000030
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040031namespace nfd::tests {
Nick Gordon89c4cca2016-11-02 15:42:32 +000032
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033using rib::RibEntry;
34using rib::Route;
Junxiao Shid47cd632018-09-11 03:10:00 +000035
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050036BOOST_AUTO_TEST_SUITE(Rib)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040037BOOST_FIXTURE_TEST_SUITE(TestRibEntry, GlobalIoFixture)
Nick Gordon89c4cca2016-11-02 15:42:32 +000038
39BOOST_AUTO_TEST_CASE(Basic)
40{
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040041 RibEntry entry;
42 RibEntry::iterator entryIt;
Nick Gordon89c4cca2016-11-02 15:42:32 +000043 bool didInsert = false;
44
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040045 Route route1;
Nick Gordon89c4cca2016-11-02 15:42:32 +000046 route1.faceId = 1;
Davide Pesavento22db5392017-04-14 00:56:43 -040047 route1.origin = ndn::nfd::ROUTE_ORIGIN_APP;
Nick Gordon89c4cca2016-11-02 15:42:32 +000048
Davide Pesavento22db5392017-04-14 00:56:43 -040049 std::tie(entryIt, didInsert) = entry.insertRoute(route1);
Nick Gordon89c4cca2016-11-02 15:42:32 +000050 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
51 BOOST_CHECK(entryIt == entry.findRoute(route1));
52 BOOST_CHECK(didInsert);
53
54 Route route2;
55 route2.faceId = 1;
Davide Pesavento22db5392017-04-14 00:56:43 -040056 route2.origin = ndn::nfd::ROUTE_ORIGIN_NLSR;
Nick Gordon89c4cca2016-11-02 15:42:32 +000057
58 std::tie(entryIt, didInsert) = entry.insertRoute(route2);
59 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
60 BOOST_CHECK(entryIt == entry.findRoute(route2));
61 BOOST_CHECK(didInsert);
62
63 entry.eraseRoute(route1);
64 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
65 BOOST_CHECK(entry.findRoute(route1) == entry.end());
66
67 BOOST_CHECK(entry.findRoute(route1) == entry.getRoutes().end());
68 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
69
70 std::tie(entryIt, didInsert) = entry.insertRoute(route2);
71 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
72 BOOST_CHECK(!didInsert);
73
74 entry.eraseRoute(route1);
75 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
76 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
77}
78
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040079BOOST_FIXTURE_TEST_SUITE(GetAnnouncement, GlobalIoTimeFixture)
Junxiao Shid47cd632018-09-11 03:10:00 +000080
81static Route
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050082makeSimpleRoute(uint64_t faceId, std::optional<time::nanoseconds> expiration = std::nullopt)
Junxiao Shid47cd632018-09-11 03:10:00 +000083{
84 Route route;
85 route.faceId = faceId;
Davide Pesaventocaa60cc2024-02-18 18:18:37 -050086 if (expiration) {
87 route.expires = time::steady_clock::now() + *expiration;
88 }
Junxiao Shid47cd632018-09-11 03:10:00 +000089 return route;
90}
91
92BOOST_AUTO_TEST_CASE(Empty)
93{
94 RibEntry entry;
95 // entry has no Route
96
97 advanceClocks(1_s);
98 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
99 BOOST_CHECK_GE(pa.getExpiration(), 15_s);
100 BOOST_CHECK_LE(pa.getExpiration(), 30_s);
101 // A RibEntry without Route does not have a defined expiration time,
102 // so this test only checks that there's no exception and the expiration period is clamped.
103}
104
105BOOST_AUTO_TEST_CASE(ReuseAnnouncement1)
106{
107 RibEntry entry;
108 entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 5858));
109 entry.insertRoute(Route(makePrefixAnn("/A", 555_s), 2454));
110 entry.insertRoute(makeSimpleRoute(8282, 799_s));
111 entry.insertRoute(makeSimpleRoute(1141));
112
113 advanceClocks(1_s);
114 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
115 BOOST_CHECK_EQUAL(pa.getExpiration(), 555_s); // not modified and not clamped
116}
117
118BOOST_AUTO_TEST_CASE(ReuseAnnouncement2)
119{
120 RibEntry entry;
121 entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 7557)); // expires at +212s
122
123 advanceClocks(100_s);
124 entry.insertRoute(Route(makePrefixAnn("/A", 136_s), 1010)); // expires at +236s
125
126 advanceClocks(1_s);
127 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
128 BOOST_CHECK_EQUAL(pa.getExpiration(), 136_s);
129 // Although face 7557's announcement has a longer expiration period,
130 // the route for face 1010 expires last and thus its announcement is chosen.
131}
132
133BOOST_AUTO_TEST_CASE(MakeAnnouncement)
134{
135 RibEntry entry;
136 entry.insertRoute(makeSimpleRoute(6398, 765_s));
137 entry.insertRoute(makeSimpleRoute(2954, 411_s));
138
139 advanceClocks(1_s);
140 auto pa = entry.getPrefixAnnouncement(15_s, 999_s);
141 BOOST_CHECK_EQUAL(pa.getExpiration(), 764_s);
142}
143
144BOOST_AUTO_TEST_CASE(MakeAnnouncementInfinite)
145{
146 RibEntry entry;
147 entry.insertRoute(makeSimpleRoute(4877, 240_s));
148 entry.insertRoute(makeSimpleRoute(5053));
149
150 advanceClocks(1_s);
151 auto pa = entry.getPrefixAnnouncement(15_s, 877_s);
152 BOOST_CHECK_EQUAL(pa.getExpiration(), 877_s); // clamped at maxExpiration
153}
154
155BOOST_AUTO_TEST_CASE(MakeAnnouncementShortExpiration)
156{
157 RibEntry entry;
158 entry.insertRoute(makeSimpleRoute(4877, 8_s));
159 entry.insertRoute(makeSimpleRoute(5053, 9_s));
160
161 advanceClocks(1_s);
162 auto pa = entry.getPrefixAnnouncement(15_s, 666_s);
163 BOOST_CHECK_EQUAL(pa.getExpiration(), 15_s); // clamped at minExpiration
164}
165
166BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement
167
Nick Gordon89c4cca2016-11-02 15:42:32 +0000168BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
Davide Pesaventocaa60cc2024-02-18 18:18:37 -0500169BOOST_AUTO_TEST_SUITE_END() // Rib
Nick Gordon89c4cca2016-11-02 15:42:32 +0000170
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400171} // namespace nfd::tests