Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 4 | * 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 Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 27 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/daemon/global-io-fixture.hpp" |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 30 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 31 | namespace nfd::tests { |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | using rib::RibEntry; |
| 34 | using rib::Route; |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 35 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 36 | BOOST_FIXTURE_TEST_SUITE(TestRibEntry, GlobalIoFixture) |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 37 | |
| 38 | BOOST_AUTO_TEST_CASE(Basic) |
| 39 | { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 40 | RibEntry entry; |
| 41 | RibEntry::iterator entryIt; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 42 | bool didInsert = false; |
| 43 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 44 | Route route1; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 45 | route1.faceId = 1; |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 46 | route1.origin = ndn::nfd::ROUTE_ORIGIN_APP; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 47 | |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 48 | std::tie(entryIt, didInsert) = entry.insertRoute(route1); |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 49 | 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 Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 55 | route2.origin = ndn::nfd::ROUTE_ORIGIN_NLSR; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 56 | |
| 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 Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 78 | BOOST_FIXTURE_TEST_SUITE(GetAnnouncement, GlobalIoTimeFixture) |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 79 | |
| 80 | static Route |
| 81 | makeSimpleRoute(uint64_t faceId) |
| 82 | { |
| 83 | Route route; |
| 84 | route.faceId = faceId; |
| 85 | return route; |
| 86 | } |
| 87 | |
| 88 | static Route |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 89 | makeSimpleRoute(uint64_t faceId, time::nanoseconds expiration) |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 90 | { |
| 91 | Route route = makeSimpleRoute(faceId); |
| 92 | route.expires = time::steady_clock::now() + expiration; |
| 93 | return route; |
| 94 | } |
| 95 | |
| 96 | BOOST_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 | |
| 109 | BOOST_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 | |
| 122 | BOOST_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 | |
| 137 | BOOST_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 | |
| 148 | BOOST_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 | |
| 159 | BOOST_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 | |
| 170 | BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement |
| 171 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 172 | BOOST_AUTO_TEST_SUITE_END() // TestRibEntry |
| 173 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 174 | } // namespace nfd::tests |