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