blob: a5c9f3c84db4fe91e706976d0e279e32f750b416 [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/*
3 * Copyright (c) 2014-2018, 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"
27#include "tests/test-common.hpp"
28
29namespace nfd {
30namespace rib {
31namespace tests {
32
Junxiao Shid47cd632018-09-11 03:10:00 +000033using namespace nfd::tests;
34
35BOOST_FIXTURE_TEST_SUITE(TestRibEntry, BaseFixture)
Nick Gordon89c4cca2016-11-02 15:42:32 +000036
37BOOST_AUTO_TEST_CASE(Basic)
38{
39 rib::RibEntry entry;
40 rib::RibEntry::iterator entryIt;
41 bool didInsert = false;
42
43 rib::Route route1;
44 route1.faceId = 1;
Davide Pesavento22db5392017-04-14 00:56:43 -040045 route1.origin = ndn::nfd::ROUTE_ORIGIN_APP;
Nick Gordon89c4cca2016-11-02 15:42:32 +000046
Davide Pesavento22db5392017-04-14 00:56:43 -040047 std::tie(entryIt, didInsert) = entry.insertRoute(route1);
Nick Gordon89c4cca2016-11-02 15:42:32 +000048 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
49 BOOST_CHECK(entryIt == entry.findRoute(route1));
50 BOOST_CHECK(didInsert);
51
52 Route route2;
53 route2.faceId = 1;
Davide Pesavento22db5392017-04-14 00:56:43 -040054 route2.origin = ndn::nfd::ROUTE_ORIGIN_NLSR;
Nick Gordon89c4cca2016-11-02 15:42:32 +000055
56 std::tie(entryIt, didInsert) = entry.insertRoute(route2);
57 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
58 BOOST_CHECK(entryIt == entry.findRoute(route2));
59 BOOST_CHECK(didInsert);
60
61 entry.eraseRoute(route1);
62 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
63 BOOST_CHECK(entry.findRoute(route1) == entry.end());
64
65 BOOST_CHECK(entry.findRoute(route1) == entry.getRoutes().end());
66 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
67
68 std::tie(entryIt, didInsert) = entry.insertRoute(route2);
69 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
70 BOOST_CHECK(!didInsert);
71
72 entry.eraseRoute(route1);
73 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
74 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
75}
76
Junxiao Shid47cd632018-09-11 03:10:00 +000077BOOST_FIXTURE_TEST_SUITE(GetAnnouncement, UnitTestTimeFixture)
78
79static Route
80makeSimpleRoute(uint64_t faceId)
81{
82 Route route;
83 route.faceId = faceId;
84 return route;
85}
86
87static Route
88makeSimpleRoute(uint64_t faceId, time::steady_clock::Duration expiration)
89{
90 Route route = makeSimpleRoute(faceId);
91 route.expires = time::steady_clock::now() + expiration;
92 return route;
93}
94
95BOOST_AUTO_TEST_CASE(Empty)
96{
97 RibEntry entry;
98 // entry has no Route
99
100 advanceClocks(1_s);
101 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
102 BOOST_CHECK_GE(pa.getExpiration(), 15_s);
103 BOOST_CHECK_LE(pa.getExpiration(), 30_s);
104 // A RibEntry without Route does not have a defined expiration time,
105 // so this test only checks that there's no exception and the expiration period is clamped.
106}
107
108BOOST_AUTO_TEST_CASE(ReuseAnnouncement1)
109{
110 RibEntry entry;
111 entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 5858));
112 entry.insertRoute(Route(makePrefixAnn("/A", 555_s), 2454));
113 entry.insertRoute(makeSimpleRoute(8282, 799_s));
114 entry.insertRoute(makeSimpleRoute(1141));
115
116 advanceClocks(1_s);
117 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
118 BOOST_CHECK_EQUAL(pa.getExpiration(), 555_s); // not modified and not clamped
119}
120
121BOOST_AUTO_TEST_CASE(ReuseAnnouncement2)
122{
123 RibEntry entry;
124 entry.insertRoute(Route(makePrefixAnn("/A", 212_s), 7557)); // expires at +212s
125
126 advanceClocks(100_s);
127 entry.insertRoute(Route(makePrefixAnn("/A", 136_s), 1010)); // expires at +236s
128
129 advanceClocks(1_s);
130 auto pa = entry.getPrefixAnnouncement(15_s, 30_s);
131 BOOST_CHECK_EQUAL(pa.getExpiration(), 136_s);
132 // Although face 7557's announcement has a longer expiration period,
133 // the route for face 1010 expires last and thus its announcement is chosen.
134}
135
136BOOST_AUTO_TEST_CASE(MakeAnnouncement)
137{
138 RibEntry entry;
139 entry.insertRoute(makeSimpleRoute(6398, 765_s));
140 entry.insertRoute(makeSimpleRoute(2954, 411_s));
141
142 advanceClocks(1_s);
143 auto pa = entry.getPrefixAnnouncement(15_s, 999_s);
144 BOOST_CHECK_EQUAL(pa.getExpiration(), 764_s);
145}
146
147BOOST_AUTO_TEST_CASE(MakeAnnouncementInfinite)
148{
149 RibEntry entry;
150 entry.insertRoute(makeSimpleRoute(4877, 240_s));
151 entry.insertRoute(makeSimpleRoute(5053));
152
153 advanceClocks(1_s);
154 auto pa = entry.getPrefixAnnouncement(15_s, 877_s);
155 BOOST_CHECK_EQUAL(pa.getExpiration(), 877_s); // clamped at maxExpiration
156}
157
158BOOST_AUTO_TEST_CASE(MakeAnnouncementShortExpiration)
159{
160 RibEntry entry;
161 entry.insertRoute(makeSimpleRoute(4877, 8_s));
162 entry.insertRoute(makeSimpleRoute(5053, 9_s));
163
164 advanceClocks(1_s);
165 auto pa = entry.getPrefixAnnouncement(15_s, 666_s);
166 BOOST_CHECK_EQUAL(pa.getExpiration(), 15_s); // clamped at minExpiration
167}
168
169BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement
170
Nick Gordon89c4cca2016-11-02 15:42:32 +0000171BOOST_AUTO_TEST_SUITE_END() // TestRibEntry
172
173} // namespace tests
174} // namespace rib
175} // namespace nfd