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 | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
| 31 | namespace nfd { |
| 32 | namespace rib { |
| 33 | namespace tests { |
| 34 | |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 35 | using namespace nfd::tests; |
| 36 | |
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 | { |
| 41 | rib::RibEntry entry; |
| 42 | rib::RibEntry::iterator entryIt; |
| 43 | bool didInsert = false; |
| 44 | |
| 45 | rib::Route route1; |
| 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 |
| 82 | makeSimpleRoute(uint64_t faceId) |
| 83 | { |
| 84 | Route route; |
| 85 | route.faceId = faceId; |
| 86 | return route; |
| 87 | } |
| 88 | |
| 89 | static Route |
| 90 | makeSimpleRoute(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 | |
| 97 | BOOST_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 | |
| 110 | BOOST_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 | |
| 123 | BOOST_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 | |
| 138 | BOOST_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 | |
| 149 | BOOST_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 | |
| 160 | BOOST_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 | |
| 171 | BOOST_AUTO_TEST_SUITE_END() // GetAnnouncement |
| 172 | |
Nick Gordon | 89c4cca | 2016-11-02 15:42:32 +0000 | [diff] [blame] | 173 | BOOST_AUTO_TEST_SUITE_END() // TestRibEntry |
| 174 | |
| 175 | } // namespace tests |
| 176 | } // namespace rib |
| 177 | } // namespace nfd |