blob: f3eff5c6fd78dd3ede0302cd1a88d8af705d3a50 [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/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * 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 Lehmanc2e51f62015-01-20 15:03:11 -060021
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050022#include "lsa.hpp"
Nick Gordonff9a6272017-10-12 13:38:29 -050023#include "test-common.hpp"
24#include "adjacent.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025#include "name-prefix-list.hpp"
alvydce3f182015-04-09 11:23:30 -050026
akmhoquec7a79b22014-05-26 08:06:19 -050027#include <ndn-cxx/util/time.hpp>
alvydce3f182015-04-09 11:23:30 -050028#include <sstream>
29
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050030namespace nlsr {
31namespace test {
alvydce3f182015-04-09 11:23:30 -050032
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050033BOOST_AUTO_TEST_SUITE(TestLsa)
34
35BOOST_AUTO_TEST_CASE(NameLsaBasic)
36{
Nick Gordon96861ca2017-10-17 18:25:21 -050037 ndn::Name s1{"name1"};
38 ndn::Name s2{"name2"};
39 NamePrefixList npl1{s1, s2};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050040
akmhoquec7a79b22014-05-26 08:06:19 -050041 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060042
43 //3rd arg is seqNo. which will be a random number I just put in 12.
44 NameLsa nlsa1("router1", 12, testTimePoint, npl1);
45 NameLsa nlsa2("router2", 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050046
Nick Gordon727d4832017-10-13 18:04:25 -050047 BOOST_CHECK_EQUAL(nlsa1.getType(), Lsa::Type::NAME);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050048
akmhoquec7a79b22014-05-26 08:06:19 -050049 BOOST_CHECK(nlsa1.getExpirationTimePoint() == nlsa2.getExpirationTimePoint());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050050
51 BOOST_CHECK(nlsa1.getKey() != nlsa2.getKey());
52}
53
54BOOST_AUTO_TEST_CASE(AdjacentLsaConstructorAndGetters)
55{
alvyc69c9af2015-04-09 12:38:54 -050056 ndn::Name routerName("/ndn/site/router");
57 ndn::Name adjacencyName("/ndn/site/adjacency");
akmhoquec7a79b22014-05-26 08:06:19 -050058 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
alvyc69c9af2015-04-09 12:38:54 -050059 uint32_t seqNo = 12;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050060
alvyc69c9af2015-04-09 12:38:54 -050061 // An AdjLsa initialized with ACTIVE adjacencies should copy the adjacencies
62 AdjacencyList activeAdjacencies;
63 Adjacent activeAdjacency(adjacencyName);
64 activeAdjacency.setStatus(Adjacent::STATUS_ACTIVE);
65 activeAdjacencies.insert(activeAdjacency);
66
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060067 AdjLsa alsa1(routerName, seqNo, testTimePoint,
Nick Gordonff9a6272017-10-12 13:38:29 -050068 activeAdjacencies.size(), activeAdjacencies);
69 BOOST_CHECK_EQUAL(alsa1.getAdl().size(), 1);
Nick Gordon727d4832017-10-13 18:04:25 -050070 BOOST_CHECK_EQUAL(alsa1.getType(), Lsa::Type::ADJACENCY);
alvyc69c9af2015-04-09 12:38:54 -050071 BOOST_CHECK_EQUAL(alsa1.getLsSeqNo(), seqNo);
akmhoquec7a79b22014-05-26 08:06:19 -050072 BOOST_CHECK_EQUAL(alsa1.getExpirationTimePoint(), testTimePoint);
alvyc69c9af2015-04-09 12:38:54 -050073 BOOST_CHECK_EQUAL(alsa1.getNoLink(), 1);
74 BOOST_CHECK(alsa1.getAdl().isNeighbor(activeAdjacency.getName()));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050075
alvyc69c9af2015-04-09 12:38:54 -050076 // An AdjLsa initialized with INACTIVE adjacencies should not copy the adjacencies
77 AdjacencyList inactiveAdjacencies;
78 Adjacent inactiveAdjacency(adjacencyName);
79 inactiveAdjacency.setStatus(Adjacent::STATUS_INACTIVE);
80 inactiveAdjacencies.insert(inactiveAdjacency);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050081
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060082 AdjLsa alsa2(routerName, seqNo, testTimePoint,
Nick Gordonff9a6272017-10-12 13:38:29 -050083 inactiveAdjacencies.size(), inactiveAdjacencies);
84 BOOST_CHECK_EQUAL(alsa2.getAdl().size(), 0);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050085
alvyc69c9af2015-04-09 12:38:54 -050086 // Thus, the two LSAs should not have equal content
87 BOOST_CHECK_EQUAL(alsa1.isEqualContent(alsa2), false);
88
89 // Create a duplicate of alsa1 which should have equal content
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060090 AdjLsa alsa3(routerName, seqNo, testTimePoint,
Nick Gordonff9a6272017-10-12 13:38:29 -050091 activeAdjacencies.size(), activeAdjacencies);
alvyc69c9af2015-04-09 12:38:54 -050092 BOOST_CHECK(alsa1.isEqualContent(alsa3));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050093}
94
95BOOST_AUTO_TEST_CASE(CoordinateLsaConstructorAndGetters)
96{
akmhoquec7a79b22014-05-26 08:06:19 -050097 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060098 std::vector<double> angles1, angles2;
99 angles1.push_back(30.0);
100 angles2.push_back(30.0);
101 CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles1);
102 CoordinateLsa clsa2("router1", 12, testTimePoint, 2.5, angles2);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500103
104 BOOST_CHECK_CLOSE(clsa1.getCorRadius(), 2.5, 0.0001);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600105 BOOST_CHECK(clsa1.getCorTheta() == angles1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500106
akmhoquefdbddb12014-05-02 18:35:19 -0500107 BOOST_CHECK(clsa1.isEqualContent(clsa2));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500108
Nick Gordonfaf49f42017-10-23 12:36:28 -0500109 BOOST_CHECK_EQUAL(clsa1.serialize(), clsa2.serialize());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500110}
111
alvydce3f182015-04-09 11:23:30 -0500112BOOST_AUTO_TEST_CASE(IncrementAdjacentNumber)
113{
114 Adjacent adj1("adjacent1");
115 Adjacent adj2("adjacent2");
116
117 adj1.setStatus(Adjacent::STATUS_ACTIVE);
118 adj2.setStatus(Adjacent::STATUS_ACTIVE);
119
120 AdjacencyList adjList;
121 adjList.insert(adj1);
122 adjList.insert(adj2);
123
124 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
alvydce3f182015-04-09 11:23:30 -0500125 std::ostringstream ss;
126 ss << testTimePoint;
127
128 const std::string TEST_TIME_POINT_STRING = ss.str();
129
Nick Gordonff9a6272017-10-12 13:38:29 -0500130 AdjLsa lsa("router1", 12, testTimePoint, adjList.size(), adjList);
alvydce3f182015-04-09 11:23:30 -0500131
132 std::string EXPECTED_OUTPUT =
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600133 "Adj Lsa:\n"
134 " Origination Router: /router1\n"
Nick Gordon727d4832017-10-13 18:04:25 -0500135 " Ls Type: ADJACENCY\n"
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600136 " Ls Seq No: 12\n"
137 " Ls Lifetime: " + TEST_TIME_POINT_STRING + "\n"
138 " Adjacents: \n"
139 " Adjacent 1:\n"
140 " Adjacent Name: /adjacent1\n"
141 " Connecting FaceUri: ://\n"
142 " Link Cost: 10\n"
143 " Adjacent 2:\n"
144 " Adjacent Name: /adjacent2\n"
145 " Connecting FaceUri: ://\n"
146 " Link Cost: 10\n"
147 "adj_lsa_end";
alvydce3f182015-04-09 11:23:30 -0500148
149 std::ostringstream os;
150 os << lsa;
151
152 BOOST_CHECK_EQUAL(os.str(), EXPECTED_OUTPUT);
153}
154
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600155BOOST_AUTO_TEST_CASE(TestInitializeFromContent)
156{
157 //Adj LSA
158 Adjacent adj1("adjacent1");
159 Adjacent adj2("adjacent2");
160
161 adj1.setStatus(Adjacent::STATUS_ACTIVE);
162 adj2.setStatus(Adjacent::STATUS_ACTIVE);
163
164 //If we don't do this the test will fail
165 //Adjacent has default cost of 10 but no default
166 //connecting face URI, so initializeFromContent fails
Nick Gordone9733ed2017-04-26 10:48:39 -0500167 adj1.setFaceUri(ndn::util::FaceUri("udp://10.0.0.1"));
168 adj2.setFaceUri(ndn::util::FaceUri("udp://10.0.0.2"));
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600169
170 AdjacencyList adjList;
171 adjList.insert(adj1);
172 adjList.insert(adj2);
173
174 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
175
Nick Gordonff9a6272017-10-12 13:38:29 -0500176 AdjLsa adjlsa1("router1", 1, testTimePoint, adjList.size(), adjList);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600177 AdjLsa adjlsa2;
178
Nick Gordonfaf49f42017-10-23 12:36:28 -0500179 BOOST_CHECK(adjlsa2.initializeFromContent(adjlsa1.serialize()));
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600180
181 BOOST_CHECK(adjlsa1.isEqualContent(adjlsa2));
182
183 //Name LSA
Nick Gordon96861ca2017-10-17 18:25:21 -0500184 ndn::Name s1{"name1"};
185 ndn::Name s2{"name2"};
186 NamePrefixList npl1{s1, s2};
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600187
188 NameLsa nlsa1("router1", 1, testTimePoint, npl1);
189 NameLsa nlsa2;
190
Nick Gordonfaf49f42017-10-23 12:36:28 -0500191 BOOST_CHECK(nlsa2.initializeFromContent(nlsa1.serialize()));
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600192
Nick Gordonfaf49f42017-10-23 12:36:28 -0500193 BOOST_CHECK_EQUAL(nlsa1.serialize(), nlsa2.serialize());
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600194
195 //Coordinate LSA
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600196 std::vector<double> angles = {30, 40.0};
197 CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, angles);
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600198 CoordinateLsa clsa2;
199
Nick Gordonfaf49f42017-10-23 12:36:28 -0500200 BOOST_CHECK(clsa2.initializeFromContent(clsa1.serialize()));
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600201
Nick Gordonfaf49f42017-10-23 12:36:28 -0500202 BOOST_CHECK_EQUAL(clsa1.serialize(), clsa2.serialize());
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600203}
204
Nick Gordon56d1fae2017-05-26 16:39:25 -0500205BOOST_AUTO_TEST_SUITE(TestNameLsa)
206
207BOOST_AUTO_TEST_CASE(OperatorEquals)
208{
209 NameLsa lsa1;
210 NameLsa lsa2;
211 ndn::Name name1("/ndn/test/name1");
212 ndn::Name name2("/ndn/test/name2");
213 ndn::Name name3("/ndn/some/other/name1");
214
215 lsa1.addName(name1);
216 lsa1.addName(name2);
217 lsa1.addName(name3);
218
219 lsa2.addName(name1);
220 lsa2.addName(name2);
221 lsa2.addName(name3);
222
223 BOOST_CHECK(lsa1.isEqualContent(lsa2));
224}
225
226BOOST_AUTO_TEST_SUITE_END() // TestNameLsa
227
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500228BOOST_AUTO_TEST_SUITE_END()
229
alvydce3f182015-04-09 11:23:30 -0500230} // namespace test
231} // namespace nlsr