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