blob: 903313e74ce4c40fee6bd1da78ec51b4ac8a2bbe [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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 Pesaventocf7db2f2019-03-24 23:17:28 -040036BOOST_FIXTURE_TEST_SUITE(TestRibEntry, GlobalIoFixture)
Nick Gordon89c4cca2016-11-02 15:42:32 +000037
38BOOST_AUTO_TEST_CASE(Basic)
39{
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040 RibEntry entry;
41 RibEntry::iterator entryIt;
Nick Gordon89c4cca2016-11-02 15:42:32 +000042 bool didInsert = false;
43
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040044 Route route1;
Nick Gordon89c4cca2016-11-02 15:42:32 +000045 route1.faceId = 1;
Davide Pesavento22db5392017-04-14 00:56:43 -040046 route1.origin = ndn::nfd::ROUTE_ORIGIN_APP;
Nick Gordon89c4cca2016-11-02 15:42:32 +000047
Davide Pesavento22db5392017-04-14 00:56:43 -040048 std::tie(entryIt, didInsert) = entry.insertRoute(route1);
Nick Gordon89c4cca2016-11-02 15:42:32 +000049 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
50 BOOST_CHECK(entryIt == entry.findRoute(route1));
51 BOOST_CHECK(didInsert);
52
53 Route route2;
54 route2.faceId = 1;
Davide Pesavento22db5392017-04-14 00:56:43 -040055 route2.origin = ndn::nfd::ROUTE_ORIGIN_NLSR;
Nick Gordon89c4cca2016-11-02 15:42:32 +000056
57 std::tie(entryIt, didInsert) = entry.insertRoute(route2);
58 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
59 BOOST_CHECK(entryIt == entry.findRoute(route2));
60 BOOST_CHECK(didInsert);
61
62 entry.eraseRoute(route1);
63 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
64 BOOST_CHECK(entry.findRoute(route1) == entry.end());
65
66 BOOST_CHECK(entry.findRoute(route1) == entry.getRoutes().end());
67 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
68
69 std::tie(entryIt, didInsert) = entry.insertRoute(route2);
70 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
71 BOOST_CHECK(!didInsert);
72
73 entry.eraseRoute(route1);
74 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
75 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
76}
77
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040078BOOST_FIXTURE_TEST_SUITE(GetAnnouncement, GlobalIoTimeFixture)
Junxiao Shid47cd632018-09-11 03:10:00 +000079
80static Route
81makeSimpleRoute(uint64_t faceId)
82{
83 Route route;
84 route.faceId = faceId;
85 return route;
86}
87
88static Route
Davide Pesavento412c9822021-07-02 00:21:05 -040089makeSimpleRoute(uint64_t faceId, time::nanoseconds expiration)
Junxiao Shid47cd632018-09-11 03:10:00 +000090{
91 Route route = makeSimpleRoute(faceId);
92 route.expires = time::steady_clock::now() + expiration;
93 return route;
94}
95
96BOOST_AUTO_TEST_CASE(Empty)
97{
98 RibEntry entry;
99 // entry has no Route
100
101 advanceClocks(1_s);
102 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
103 BOOST_CHECK_GE(pa.getExpiration(), 15_s);
104 BOOST_CHECK_LE(pa.getExpiration(), 30_s);
105 // A RibEntry without Route does not have a defined expiration time,
106 // so this test only checks that there's no exception and the expiration period is clamped.
107}
108
109BOOST_AUTO_TEST_CASE(ReuseAnnouncement1)
110{
111 RibEntry entry;
112 entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 5858));
113 entry.insertRoute(Route(makePrefixAnn("/A", 555_s), 2454));
114 entry.insertRoute(makeSimpleRoute(8282, 799_s));
115 entry.insertRoute(makeSimpleRoute(1141));
116
117 advanceClocks(1_s);
118 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
119 BOOST_CHECK_EQUAL(pa.getExpiration(), 555_s); // not modified and not clamped
120}
121
122BOOST_AUTO_TEST_CASE(ReuseAnnouncement2)
123{
124 RibEntry entry;
125 entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 7557)); // expires at +212s
126
127 advanceClocks(100_s);
128 entry.insertRoute(Route(makePrefixAnn("/A", 136_s), 1010)); // expires at +236s
129
130 advanceClocks(1_s);
131 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
132 BOOST_CHECK_EQUAL(pa.getExpiration(), 136_s);
133 // Although face 7557's announcement has a longer expiration period,
134 // the route for face 1010 expires last and thus its announcement is chosen.
135}
136
137BOOST_AUTO_TEST_CASE(MakeAnnouncement)
138{
139 RibEntry entry;
140 entry.insertRoute(makeSimpleRoute(6398, 765_s));
141 entry.insertRoute(makeSimpleRoute(2954, 411_s));
142
143 advanceClocks(1_s);
144 auto pa = entry.getPrefixAnnouncement(15_s, 999_s);
145 BOOST_CHECK_EQUAL(pa.getExpiration(), 764_s);
146}
147
148BOOST_AUTO_TEST_CASE(MakeAnnouncementInfinite)
149{
150 RibEntry entry;
151 entry.insertRoute(makeSimpleRoute(4877, 240_s));
152 entry.insertRoute(makeSimpleRoute(5053));
153
154 advanceClocks(1_s);
155 auto pa = entry.getPrefixAnnouncement(15_s, 877_s);
156 BOOST_CHECK_EQUAL(pa.getExpiration(), 877_s); // clamped at maxExpiration
157}
158
159BOOST_AUTO_TEST_CASE(MakeAnnouncementShortExpiration)
160{
161 RibEntry entry;
162 entry.insertRoute(makeSimpleRoute(4877, 8_s));
163 entry.insertRoute(makeSimpleRoute(5053, 9_s));
164
165 advanceClocks(1_s);
166 auto pa = entry.getPrefixAnnouncement(15_s, 666_s);
167 BOOST_CHECK_EQUAL(pa.getExpiration(), 15_s); // clamped at minExpiration
168}
169
170BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement
171
Nick Gordon89c4cca2016-11-02 15:42:32 +0000172BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
173
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400174} // namespace nfd::tests