Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [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, |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26: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/route.hpp" |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 27 | |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26: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" |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 30 | |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 31 | #include <boost/lexical_cast.hpp> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 34 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | using rib::Route; |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 36 | |
Davide Pesavento | caa60cc | 2024-02-18 18:18:37 -0500 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Rib) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 38 | BOOST_FIXTURE_TEST_SUITE(TestRoute, GlobalIoTimeFixture) |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 39 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 40 | BOOST_AUTO_TEST_SUITE(CreateFromAnnouncement) |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 41 | |
| 42 | BOOST_AUTO_TEST_CASE(NoValidity) |
| 43 | { |
| 44 | auto pa = makePrefixAnn("/A", 212_s); |
| 45 | Route route(pa, 1815); |
| 46 | BOOST_CHECK_EQUAL(route.faceId, 1815); |
| 47 | BOOST_CHECK_EQUAL(route.origin, ndn::nfd::ROUTE_ORIGIN_PREFIXANN); |
| 48 | BOOST_CHECK_EQUAL(route.cost, 2048); |
| 49 | BOOST_CHECK_EQUAL(route.flags, ndn::nfd::ROUTE_FLAG_CHILD_INHERIT); |
| 50 | BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s); |
| 51 | BOOST_REQUIRE(route.expires); |
| 52 | BOOST_CHECK_EQUAL(*route.expires, route.annExpires); |
| 53 | } |
| 54 | |
| 55 | BOOST_AUTO_TEST_CASE(BeforeNotBefore) |
| 56 | { |
Davide Pesavento | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 57 | auto pa = makePrefixAnn("/A", 212_s, ndn::security::ValidityPeriod::makeRelative(10_s, 80_s)); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 58 | Route route(pa, 1053); |
| 59 | BOOST_CHECK_LE(route.annExpires, time::steady_clock::now()); |
| 60 | BOOST_REQUIRE(route.expires); |
| 61 | BOOST_CHECK_LE(*route.expires, time::steady_clock::now()); |
| 62 | } |
| 63 | |
| 64 | BOOST_AUTO_TEST_CASE(AfterNotAfter) |
| 65 | { |
Davide Pesavento | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 66 | auto pa = makePrefixAnn("/A", 212_s, ndn::security::ValidityPeriod::makeRelative(-80_s, -10_s)); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 67 | Route route(pa, 2972); |
| 68 | BOOST_CHECK_LE(route.annExpires, time::steady_clock::now()); |
| 69 | BOOST_REQUIRE(route.expires); |
| 70 | BOOST_CHECK_LE(*route.expires, time::steady_clock::now()); |
| 71 | } |
| 72 | |
| 73 | BOOST_AUTO_TEST_CASE(ExpirationLtValidity) |
| 74 | { |
Davide Pesavento | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 75 | auto pa = makePrefixAnn("/A", 212_s, ndn::security::ValidityPeriod::makeRelative(-100_s, 300_s)); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 76 | Route route(pa, 7804); |
| 77 | BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 212_s); |
| 78 | BOOST_REQUIRE(route.expires); |
| 79 | BOOST_CHECK_EQUAL(*route.expires, route.annExpires); |
| 80 | } |
| 81 | |
| 82 | BOOST_AUTO_TEST_CASE(ValidityLtExpiration) |
| 83 | { |
Davide Pesavento | d6ea0b1 | 2023-03-13 21:35:03 -0400 | [diff] [blame] | 84 | auto pa = makePrefixAnn("/A", 212_s, ndn::security::ValidityPeriod::makeRelative(-100_s, 200_s)); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 85 | Route route(pa, 7804); |
| 86 | BOOST_CHECK_EQUAL(route.annExpires, time::steady_clock::now() + 200_s); |
| 87 | BOOST_REQUIRE(route.expires); |
| 88 | BOOST_CHECK_EQUAL(*route.expires, route.annExpires); |
| 89 | } |
| 90 | |
| 91 | BOOST_AUTO_TEST_SUITE_END() // CreateFromAnnouncement |
| 92 | |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 93 | BOOST_AUTO_TEST_CASE(Equality) |
| 94 | { |
| 95 | Route a; |
| 96 | Route b; |
| 97 | BOOST_CHECK_EQUAL(a, b); |
| 98 | |
| 99 | a.faceId = b.faceId = 15404; |
| 100 | a.origin = b.origin = ndn::nfd::ROUTE_ORIGIN_NLSR; |
| 101 | a.flags = b.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT; |
| 102 | a.cost = b.cost = 28826; |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 103 | a.expires = b.expires = time::steady_clock::now() + 26782232_ms; |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(a, b); |
| 105 | |
| 106 | b.faceId = 18559; |
| 107 | BOOST_CHECK_NE(a, b); |
| 108 | a.faceId = 18559; |
| 109 | |
| 110 | b.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT; |
| 111 | BOOST_CHECK_NE(a, b); |
| 112 | a.origin = ndn::nfd::ROUTE_ORIGIN_CLIENT; |
| 113 | |
| 114 | b.flags = ndn::nfd::ROUTE_FLAG_CAPTURE; |
| 115 | BOOST_CHECK_NE(a, b); |
| 116 | a.flags = ndn::nfd::ROUTE_FLAG_CAPTURE; |
| 117 | |
| 118 | b.cost = 103; |
| 119 | BOOST_CHECK_NE(a, b); |
| 120 | a.cost = 103; |
| 121 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 122 | b.expires = std::nullopt; |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 123 | BOOST_CHECK_NE(a, b); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 124 | a.expires = std::nullopt; |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 125 | |
| 126 | BOOST_CHECK_EQUAL(a, b); |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 127 | |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 128 | auto ann1 = makePrefixAnn("/ann", 1_h); |
| 129 | auto ann2 = makePrefixAnn("/ann", 2_h); |
| 130 | BOOST_CHECK_EQUAL(Route(ann1, 7001), Route(ann1, 7001)); |
| 131 | BOOST_CHECK_NE(Route(ann1, 7001), Route(ann1, 7002)); |
| 132 | BOOST_CHECK_NE(Route(ann1, 7001), Route(ann2, 7001)); |
| 133 | } |
| 134 | |
Davide Pesavento | caa60cc | 2024-02-18 18:18:37 -0500 | [diff] [blame] | 135 | BOOST_AUTO_TEST_CASE(Print) |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 136 | { |
| 137 | Route r; |
| 138 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r), |
| 139 | "Route(faceid: 0, origin: app, cost: 0, flags: 0x0, never expires)"); |
| 140 | |
| 141 | r.faceId = 4980; |
| 142 | r.origin = ndn::nfd::ROUTE_ORIGIN_STATIC; |
| 143 | r.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT; |
| 144 | r.cost = 2312; |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 145 | r.expires = time::steady_clock::now() + 791214234_ms; |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r), |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 147 | "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, expires in: " |
| 148 | "791214234 milliseconds)"); |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 149 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 150 | r.expires = std::nullopt; |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 151 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r), |
| 152 | "Route(faceid: 4980, origin: static, cost: 2312, flags: 0x1, never expires)"); |
Junxiao Shi | d47cd63 | 2018-09-11 03:10:00 +0000 | [diff] [blame] | 153 | |
| 154 | r = Route(makePrefixAnn("/ann", 1_h), 3247); |
| 155 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(r), |
| 156 | "Route(faceid: 3247, origin: prefixann, cost: 2048, flags: 0x1, expires in: " |
| 157 | "3600000 milliseconds, announcement: (/ann expires=3600000 milliseconds))"); |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | BOOST_AUTO_TEST_SUITE_END() // TestRoute |
Davide Pesavento | caa60cc | 2024-02-18 18:18:37 -0500 | [diff] [blame] | 161 | BOOST_AUTO_TEST_SUITE_END() // Rib |
Junxiao Shi | 3f21e58 | 2017-05-29 15:26:32 +0000 | [diff] [blame] | 162 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 163 | } // namespace nfd::tests |