blob: 5bdef4d1c59fb419e81b825439a1f72f9709ea3d [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -05002/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, The University of Memphis,
Vince Lehmancec38852015-03-31 13:21:38 -05004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmancec38852015-03-31 13:21:38 -050021
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050022#include "route/nexthop-list.hpp"
23#include "route/nexthop.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050024#include "tests/boost-test.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025
26namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050027namespace test {
28
29BOOST_AUTO_TEST_SUITE(TestNhl)
30
31BOOST_AUTO_TEST_CASE(NhlAddNextHop)
32{
33 NextHop np1;
34
35 NexthopList nhl1;
36
37 nhl1.addNextHop(np1);
Nick Gordonff9a6272017-10-12 13:38:29 -050038 BOOST_CHECK_EQUAL(nhl1.size(), (uint32_t)1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050039
40 nhl1.removeNextHop(np1);
Nick Gordonff9a6272017-10-12 13:38:29 -050041 BOOST_CHECK_EQUAL(nhl1.size(), (uint32_t)0);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050042}
43
Vince Lehman20fe4a92014-09-09 15:57:59 -050044BOOST_AUTO_TEST_CASE(LinkStateRemoveNextHop)
Vince Lehman145064a2014-08-23 11:44:16 -050045{
46 NextHop hop1;
47 hop1.setRouteCost(12.34);
48
49 NexthopList hopList;
50 hopList.addNextHop(hop1);
51
52 NextHop hop2;
Vince Lehman20fe4a92014-09-09 15:57:59 -050053 hop2.setRouteCost(13.01);
54
Nick Gordonff9a6272017-10-12 13:38:29 -050055 BOOST_REQUIRE_EQUAL(hopList.size(), 1);
Vince Lehman20fe4a92014-09-09 15:57:59 -050056
57 hopList.removeNextHop(hop2);
Nick Gordonff9a6272017-10-12 13:38:29 -050058 BOOST_CHECK_EQUAL(hopList.size(), 1);
Vince Lehman20fe4a92014-09-09 15:57:59 -050059
60 hopList.removeNextHop(hop1);
Nick Gordonff9a6272017-10-12 13:38:29 -050061 BOOST_CHECK_EQUAL(hopList.size(), 0);
Vince Lehman20fe4a92014-09-09 15:57:59 -050062}
63
64BOOST_AUTO_TEST_CASE(HyperbolicRemoveNextHop)
65{
66 NextHop hop1;
67 hop1.setHyperbolic(true);
68 hop1.setRouteCost(12.34);
69
70 NexthopList hopList;
71 hopList.addNextHop(hop1);
72
73 NextHop hop2;
74 hop2.setHyperbolic(true);
Vince Lehman145064a2014-08-23 11:44:16 -050075 hop2.setRouteCost(12.35);
76
Nick Gordonff9a6272017-10-12 13:38:29 -050077 BOOST_REQUIRE_EQUAL(hopList.size(), 1);
Vince Lehman145064a2014-08-23 11:44:16 -050078
79 hopList.removeNextHop(hop2);
Nick Gordonff9a6272017-10-12 13:38:29 -050080 BOOST_CHECK_EQUAL(hopList.size(), 1);
Vince Lehman145064a2014-08-23 11:44:16 -050081
82 hopList.removeNextHop(hop1);
Nick Gordonff9a6272017-10-12 13:38:29 -050083 BOOST_CHECK_EQUAL(hopList.size(), 0);
Vince Lehman145064a2014-08-23 11:44:16 -050084}
85
Vince Lehmancec38852015-03-31 13:21:38 -050086BOOST_AUTO_TEST_CASE(TieBreaker)
87{
Vince Lehmanef21d8e2015-04-01 15:59:39 -050088 // equal-cost hops are sorted lexicographically
Vince Lehmancec38852015-03-31 13:21:38 -050089 NextHop hopA;
90 hopA.setRouteCost(25);
Vince Lehmanef21d8e2015-04-01 15:59:39 -050091 hopA.setConnectingFaceUri("AAAZZ");
Vince Lehmancec38852015-03-31 13:21:38 -050092
93 NextHop hopZ;
94 hopZ.setRouteCost(25);
Vince Lehmanef21d8e2015-04-01 15:59:39 -050095 hopZ.setConnectingFaceUri("ZZA");
Vince Lehmancec38852015-03-31 13:21:38 -050096
97 NexthopList list;
98 list.addNextHop(hopA);
99 list.addNextHop(hopZ);
100
Vince Lehmancec38852015-03-31 13:21:38 -0500101 NexthopList::iterator it = list.begin();
102 BOOST_CHECK_EQUAL(it->getConnectingFaceUri(), hopA.getConnectingFaceUri());
103
104 list.reset();
Vince Lehmancec38852015-03-31 13:21:38 -0500105 list.addNextHop(hopZ);
106 list.addNextHop(hopA);
107
Vince Lehmancec38852015-03-31 13:21:38 -0500108 it = list.begin();
109 BOOST_CHECK_EQUAL(it->getConnectingFaceUri(), hopA.getConnectingFaceUri());
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500110
111
112 // equal-cost and lexicographically equal hops are sorted by the length of their face uris
113 NextHop longUriHop;
114 longUriHop.setRouteCost(25);
115 longUriHop.setConnectingFaceUri("AAAAAA");
116
117 NextHop shortUriHop;
118 shortUriHop.setRouteCost(25);
119 shortUriHop.setConnectingFaceUri("AAA");
120
121 list.reset();
122 list.addNextHop(longUriHop);
123 list.addNextHop(shortUriHop);
124
125 it = list.begin();
126 BOOST_CHECK_EQUAL(it->getConnectingFaceUri(), shortUriHop.getConnectingFaceUri());
127}
128
129BOOST_AUTO_TEST_CASE(SortOnAddAndRemove)
130{
131 NexthopList list;
132
133 NextHop hopA("A", 10);
134 NextHop hopB("B", 5);
135 NextHop hopC("C", 25);
136
137 list.addNextHop(hopA);
138 list.addNextHop(hopB);
139 list.addNextHop(hopC);
140
Nick Gordonff9a6272017-10-12 13:38:29 -0500141 BOOST_REQUIRE_EQUAL(list.size(), 3);
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500142
143 double lastCost = 0;
144 for (const auto& hop : list) {
145 BOOST_CHECK(hop.getRouteCost() > lastCost);
146 lastCost = hop.getRouteCost();
147 }
148
149 // removing a hop keep the list sorted
150 list.removeNextHop(hopA);
151
Nick Gordonff9a6272017-10-12 13:38:29 -0500152 BOOST_REQUIRE_EQUAL(list.size(), 2);
Vince Lehmanef21d8e2015-04-01 15:59:39 -0500153
154 lastCost = 0;
155 for (const auto& hop : list) {
156 BOOST_CHECK(hop.getRouteCost() > lastCost);
157 lastCost = hop.getRouteCost();
158 }
Vince Lehmancec38852015-03-31 13:21:38 -0500159}
160
Nick Gordon608061b2017-07-06 12:45:04 -0500161/* If there are two NextHops going to the same neighbor, then the list
162 should always select the one with the cheaper cost. This would be
163 caused by a Name being advertised by two different routers, which
164 are reachable through the same neighbor.
165 */
166BOOST_AUTO_TEST_CASE(UseCheaperNextHop)
167{
168 NexthopList list;
169
170 NextHop hopA("udp4://10.0.0.1:6363", 10);
171 NextHop hopB("udp4://10.0.0.1:6363", 5);
172
173 list.addNextHop(hopA);
174 list.addNextHop(hopB);
175
Nick Gordonff9a6272017-10-12 13:38:29 -0500176 BOOST_REQUIRE_EQUAL(list.size(), 1);
Nick Gordon608061b2017-07-06 12:45:04 -0500177
178 for (const auto& hop : list) {
179 BOOST_CHECK_EQUAL(hop, hopB);
180 }
181}
182
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500183BOOST_AUTO_TEST_SUITE_END()
184
Nick Gordonfad8e252016-08-11 14:21:38 -0500185} // namespace test
186} // namespace nlsr