blob: 89722f8e8579e01f7a0bc7393bb2fb862a78c621 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande0421bc62020-05-08 20:42:19 -07002/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Ashlesh Gawande0421bc62020-05-08 20:42:19 -070019 */
Davide Pesaventocb065f12019-12-27 01:03:34 -050020
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050021#include "route/nexthop.hpp"
Davide Pesavento288141a2024-02-13 17:30:35 -050022
Davide Pesaventocb065f12019-12-27 01:03:34 -050023#include "tests/boost-test.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050024
Davide Pesavento288141a2024-02-13 17:30:35 -050025namespace nlsr::tests {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050026
27BOOST_AUTO_TEST_SUITE(TestNexthop)
28
Junxiao Shi6593a432023-08-21 10:50:28 +000029static const ndn::FaceUri faceUri1("udp4://192.168.3.1:6363");
30static const ndn::FaceUri faceUri2("udp4://192.168.3.2:6363");
31
Davide Pesaventoe28d8752022-03-19 03:55:25 -040032static double
Vince Lehman20fe4a92014-09-09 15:57:59 -050033getHyperbolicAdjustedDecimal(unsigned int i)
34{
Davide Pesavento288141a2024-02-13 17:30:35 -050035 return static_cast<double>(i)/(10 * NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR);
Vince Lehman20fe4a92014-09-09 15:57:59 -050036}
37
Davide Pesaventoe28d8752022-03-19 03:55:25 -040038static uint64_t
Vince Lehman20fe4a92014-09-09 15:57:59 -050039applyHyperbolicFactorAndRound(double d)
40{
Davide Pesavento288141a2024-02-13 17:30:35 -050041 return round(NextHop::HYPERBOLIC_COST_ADJUSTMENT_FACTOR * d);
Vince Lehman20fe4a92014-09-09 15:57:59 -050042}
43
44BOOST_AUTO_TEST_CASE(LinkStateSetAndGet)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050045{
Vince Lehman145064a2014-08-23 11:44:16 -050046 NextHop hop1;
Junxiao Shi6593a432023-08-21 10:50:28 +000047 hop1.setConnectingFaceUri(faceUri1);
Vince Lehman145064a2014-08-23 11:44:16 -050048 hop1.setRouteCost(12.34);
Junxiao Shi6593a432023-08-21 10:50:28 +000049 BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1);
Vince Lehman145064a2014-08-23 11:44:16 -050050 BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34);
Vince Lehman20fe4a92014-09-09 15:57:59 -050051 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), 12);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050052
Junxiao Shi6593a432023-08-21 10:50:28 +000053 NextHop hop2(faceUri2, 12.34);
54 BOOST_CHECK_NE(hop1, hop2);
55 BOOST_CHECK_LT(hop1, hop2);
56 BOOST_CHECK_LE(hop1, hop2);
57 BOOST_CHECK_GT(hop2, hop1);
58 BOOST_CHECK_GE(hop2, hop1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050059
Junxiao Shi6593a432023-08-21 10:50:28 +000060 hop2.setConnectingFaceUri(faceUri1);
61 BOOST_CHECK_EQUAL(hop1, hop2);
62 BOOST_CHECK_LE(hop1, hop2);
63 BOOST_CHECK_GE(hop2, hop1);
Vince Lehman145064a2014-08-23 11:44:16 -050064 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
Vince Lehman20fe4a92014-09-09 15:57:59 -050065}
66
67BOOST_AUTO_TEST_CASE(HyperbolicSetAndGet)
68{
69 NextHop hop1;
70 hop1.setHyperbolic(true);
Junxiao Shi6593a432023-08-21 10:50:28 +000071 hop1.setConnectingFaceUri(faceUri1);
Vince Lehman20fe4a92014-09-09 15:57:59 -050072 hop1.setRouteCost(12.34);
Junxiao Shi6593a432023-08-21 10:50:28 +000073 BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1);
Vince Lehman20fe4a92014-09-09 15:57:59 -050074 BOOST_CHECK_EQUAL(hop1.getRouteCost(), 12.34);
75 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), applyHyperbolicFactorAndRound(12.34));
76
Junxiao Shi6593a432023-08-21 10:50:28 +000077 NextHop hop2(faceUri2, 12.34);
Vince Lehman20fe4a92014-09-09 15:57:59 -050078 hop2.setHyperbolic(true);
Junxiao Shi6593a432023-08-21 10:50:28 +000079 BOOST_CHECK_NE(hop1, hop2);
80 BOOST_CHECK_LT(hop1, hop2);
81 BOOST_CHECK_LE(hop1, hop2);
82 BOOST_CHECK_GT(hop2, hop1);
83 BOOST_CHECK_GE(hop2, hop1);
Vince Lehman20fe4a92014-09-09 15:57:59 -050084
Junxiao Shi6593a432023-08-21 10:50:28 +000085 hop2.setConnectingFaceUri(faceUri1);
86 BOOST_CHECK_EQUAL(hop1, hop2);
Vince Lehman20fe4a92014-09-09 15:57:59 -050087 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
Vince Lehman145064a2014-08-23 11:44:16 -050088
89 hop2.setRouteCost(12.35);
Junxiao Shi6593a432023-08-21 10:50:28 +000090 BOOST_CHECK_LT(hop1, hop2);
91 BOOST_CHECK_LT(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050092}
93
Vince Lehman20fe4a92014-09-09 15:57:59 -050094BOOST_AUTO_TEST_CASE(HyperbolicRound)
95{
96 NextHop hop1;
97 hop1.setHyperbolic(true);
Junxiao Shi6593a432023-08-21 10:50:28 +000098 hop1.setConnectingFaceUri(faceUri1);
Vince Lehman20fe4a92014-09-09 15:57:59 -050099 hop1.setRouteCost(1 + getHyperbolicAdjustedDecimal(6));
100
Junxiao Shi6593a432023-08-21 10:50:28 +0000101 BOOST_CHECK_EQUAL(hop1.getConnectingFaceUri(), faceUri1);
Vince Lehman20fe4a92014-09-09 15:57:59 -0500102 BOOST_CHECK_EQUAL(hop1.getRouteCost(), 1 + getHyperbolicAdjustedDecimal(6));
103 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(),
104 applyHyperbolicFactorAndRound((1 + getHyperbolicAdjustedDecimal(6))));
105
106 NextHop hop2;
107 hop2.setHyperbolic(true);
108
109 hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(6));
110 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
111
112 hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(5));
113 BOOST_CHECK_EQUAL(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
114
115 hop2.setRouteCost(1 + getHyperbolicAdjustedDecimal(4));
Junxiao Shi6593a432023-08-21 10:50:28 +0000116 BOOST_CHECK_GT(hop1.getRouteCostAsAdjustedInteger(), hop2.getRouteCostAsAdjustedInteger());
Vince Lehman20fe4a92014-09-09 15:57:59 -0500117}
118
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700119const uint8_t NexthopData[] =
120{
121 // Header
Junxiao Shi6593a432023-08-21 10:50:28 +0000122 0x8f, 0x23,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700123 // Uri
Junxiao Shi6593a432023-08-21 10:50:28 +0000124 0x8d, 0x17, 0x75, 0x64, 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x31, 0x36, 0x38,
125 0x2e, 0x33, 0x2e, 0x31, 0x3a, 0x36, 0x33, 0x36, 0x33,
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700126 // Cost
127 0x86, 0x08, 0x3f, 0xfa, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66
128};
129
Junxiao Shi6593a432023-08-21 10:50:28 +0000130BOOST_AUTO_TEST_CASE(Encode)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700131{
132 NextHop nexthops1;
Junxiao Shi6593a432023-08-21 10:50:28 +0000133 nexthops1.setConnectingFaceUri(faceUri1);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700134 nexthops1.setRouteCost(1.65);
135
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400136 BOOST_TEST(nexthops1.wireEncode() == NexthopData, boost::test_tools::per_element());
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700137}
138
Junxiao Shi6593a432023-08-21 10:50:28 +0000139BOOST_AUTO_TEST_CASE(Decode)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700140{
141 NextHop nexthops1;
Davide Pesaventoe28d8752022-03-19 03:55:25 -0400142 nexthops1.wireDecode(ndn::Block{NexthopData});
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700143
Junxiao Shi6593a432023-08-21 10:50:28 +0000144 BOOST_REQUIRE_EQUAL(nexthops1.getConnectingFaceUri(), faceUri1);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700145 BOOST_REQUIRE_EQUAL(nexthops1.getRouteCost(), 1.65);
146}
147
Junxiao Shi6593a432023-08-21 10:50:28 +0000148BOOST_AUTO_TEST_CASE(OutputStream)
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700149{
150 NextHop nexthops1;
Junxiao Shi6593a432023-08-21 10:50:28 +0000151 nexthops1.setConnectingFaceUri(faceUri1);
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700152 nexthops1.setRouteCost(99);
153
154 std::ostringstream os;
155 os << nexthops1;
Junxiao Shi6593a432023-08-21 10:50:28 +0000156 BOOST_CHECK_EQUAL(os.str(), "NextHop(Uri: udp4://192.168.3.1:6363, Cost: 99)");
Ashlesh Gawande0421bc62020-05-08 20:42:19 -0700157}
158
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500159BOOST_AUTO_TEST_SUITE_END()
160
Davide Pesavento288141a2024-02-13 17:30:35 -0500161} // namespace nlsr::tests