blob: 70b053a9d38e704d17b78e8a77c4aacb76f10d0a [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 Pesaventocf7db2f2019-03-24 23:17:28 -04003 * Copyright (c) 2014-2019, 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
31namespace nfd {
32namespace rib {
33namespace tests {
34
Junxiao Shid47cd632018-09-11 03:10:00 +000035using namespace nfd::tests;
36
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{
41 rib::RibEntry entry;
42 rib::RibEntry::iterator entryIt;
43 bool didInsert = false;
44
45 rib::Route route1;
46 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
82makeSimpleRoute(uint64_t faceId)
83{
84 Route route;
85 route.faceId = faceId;
86 return route;
87}
88
89static Route
90makeSimpleRoute(uint64_t faceId, time::steady_clock::Duration expiration)
91{
92 Route route = makeSimpleRoute(faceId);
93 route.expires = time::steady_clock::now() + expiration;
94 return route;
95}
96
97BOOST_AUTO_TEST_CASE(Empty)
98{
99 RibEntry entry;
100 // entry has no Route
101
102 advanceClocks(1_s);
103 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
104 BOOST_CHECK_GE(pa.getExpiration(), 15_s);
105 BOOST_CHECK_LE(pa.getExpiration(), 30_s);
106 // A RibEntry without Route does not have a defined expiration time,
107 // so this test only checks that there's no exception and the expiration period is clamped.
108}
109
110BOOST_AUTO_TEST_CASE(ReuseAnnouncement1)
111{
112 RibEntry entry;
113 entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 5858));
114 entry.insertRoute(Route(makePrefixAnn("/A", 555_s), 2454));
115 entry.insertRoute(makeSimpleRoute(8282, 799_s));
116 entry.insertRoute(makeSimpleRoute(1141));
117
118 advanceClocks(1_s);
119 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
120 BOOST_CHECK_EQUAL(pa.getExpiration(), 555_s); // not modified and not clamped
121}
122
123BOOST_AUTO_TEST_CASE(ReuseAnnouncement2)
124{
125 RibEntry entry;
126 entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 7557)); // expires at +212s
127
128 advanceClocks(100_s);
129 entry.insertRoute(Route(makePrefixAnn("/A", 136_s), 1010)); // expires at +236s
130
131 advanceClocks(1_s);
132 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
133 BOOST_CHECK_EQUAL(pa.getExpiration(), 136_s);
134 // Although face 7557's announcement has a longer expiration period,
135 // the route for face 1010 expires last and thus its announcement is chosen.
136}
137
138BOOST_AUTO_TEST_CASE(MakeAnnouncement)
139{
140 RibEntry entry;
141 entry.insertRoute(makeSimpleRoute(6398, 765_s));
142 entry.insertRoute(makeSimpleRoute(2954, 411_s));
143
144 advanceClocks(1_s);
145 auto pa = entry.getPrefixAnnouncement(15_s, 999_s);
146 BOOST_CHECK_EQUAL(pa.getExpiration(), 764_s);
147}
148
149BOOST_AUTO_TEST_CASE(MakeAnnouncementInfinite)
150{
151 RibEntry entry;
152 entry.insertRoute(makeSimpleRoute(4877, 240_s));
153 entry.insertRoute(makeSimpleRoute(5053));
154
155 advanceClocks(1_s);
156 auto pa = entry.getPrefixAnnouncement(15_s, 877_s);
157 BOOST_CHECK_EQUAL(pa.getExpiration(), 877_s); // clamped at maxExpiration
158}
159
160BOOST_AUTO_TEST_CASE(MakeAnnouncementShortExpiration)
161{
162 RibEntry entry;
163 entry.insertRoute(makeSimpleRoute(4877, 8_s));
164 entry.insertRoute(makeSimpleRoute(5053, 9_s));
165
166 advanceClocks(1_s);
167 auto pa = entry.getPrefixAnnouncement(15_s, 666_s);
168 BOOST_CHECK_EQUAL(pa.getExpiration(), 15_s); // clamped at minExpiration
169}
170
171BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement
172
Nick Gordon89c4cca2016-11-02 15:42:32 +0000173BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
174
175} // namespace tests
176} // namespace rib
177} // namespace nfd