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 | caa60cc | 2024-02-18 18:18:37 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | caa60cc | 2024-02-18 18:18:37 -0500 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Rib) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_SUITE(TestRibEntry, GlobalIoFixture) |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 38 | |
| 39 | BOOST_AUTO_TEST_CASE(Basic) |
| 40 | { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 41 | RibEntry entry; |
| 42 | RibEntry::iterator entryIt; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 43 | bool didInsert = false; |
| 44 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 45 | Route route1; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 46 | route1.faceId = 1; |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 47 | route1.origin = ndn::nfd::ROUTE_ORIGIN_APP; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 48 | |
Davide Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 49 | std::tie(entryIt, didInsert) = entry.insertRoute(route1); |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 50 | 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 Pesavento | 22db539 | 2017-04-14 00:56:43 -0400 | [diff] [blame] | 56 | route2.origin = ndn::nfd::ROUTE_ORIGIN_NLSR; |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 57 | |
| 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 Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 79 | BOOST_FIXTURE_TEST_SUITE(GetAnnouncement, GlobalIoTimeFixture) |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 80 | |
| 81 | static Route |
Davide Pesavento | caa60cc | 2024-02-18 18:18:37 -0500 | [diff] [blame] | 82 | makeSimpleRoute(uint64_t faceId, std::optional<time::nanoseconds> expiration = std::nullopt) |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 83 | { |
| 84 | Route route; |
| 85 | route.faceId = faceId; |
Davide Pesavento | caa60cc | 2024-02-18 18:18:37 -0500 | [diff] [blame] | 86 | if (expiration) { |
| 87 | route.expires = time::steady_clock::now() + *expiration; |
| 88 | } |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 89 | return route; |
| 90 | } |
| 91 | |
| 92 | BOOST_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 | |
| 105 | BOOST_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 | |
| 118 | BOOST_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 | |
| 133 | BOOST_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 | |
| 144 | BOOST_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 | |
| 155 | BOOST_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 | |
| 166 | BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement |
| 167 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 168 | BOOST_AUTO_TEST_SUITE_END() // TestRibEntry |
Davide Pesavento | caa60cc | 2024-02-18 18:18:37 -0500 | [diff] [blame] | 169 | BOOST_AUTO_TEST_SUITE_END() // Rib |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 170 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 171 | } // namespace nfd::tests |