blob: 642a3173659fc95e69103136d7506c811755fc67 [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
alvydce3f182015-04-09 11:23:30 -050022#include "test-common.hpp"
23
24#include "adjacent.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025#include "lsa.hpp"
26#include "name-prefix-list.hpp"
alvydce3f182015-04-09 11:23:30 -050027
akmhoquec7a79b22014-05-26 08:06:19 -050028#include <ndn-cxx/util/time.hpp>
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050029
alvydce3f182015-04-09 11:23:30 -050030#include <sstream>
31
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050032namespace nlsr {
33namespace test {
alvydce3f182015-04-09 11:23:30 -050034
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050035BOOST_AUTO_TEST_SUITE(TestLsa)
36
37BOOST_AUTO_TEST_CASE(NameLsaBasic)
38{
39 NamePrefixList npl1;
40
41 std::string s1 = "name1";
42 std::string s2 = "name2";
43
44 npl1.insert(s1);
45 npl1.insert(s2);
akmhoquec7a79b22014-05-26 08:06:19 -050046 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060047
48 //3rd arg is seqNo. which will be a random number I just put in 12.
49 NameLsa nlsa1("router1", 12, testTimePoint, npl1);
50 NameLsa nlsa2("router2", 12, testTimePoint, npl1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050051
alvy49b1c0c2014-12-19 13:57:46 -060052 BOOST_CHECK_EQUAL(nlsa1.getLsType(), NameLsa::TYPE_STRING);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050053
akmhoquec7a79b22014-05-26 08:06:19 -050054 BOOST_CHECK(nlsa1.getExpirationTimePoint() == nlsa2.getExpirationTimePoint());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050055
56 BOOST_CHECK(nlsa1.getKey() != nlsa2.getKey());
57}
58
59BOOST_AUTO_TEST_CASE(AdjacentLsaConstructorAndGetters)
60{
alvyc69c9af2015-04-09 12:38:54 -050061 ndn::Name routerName("/ndn/site/router");
62 ndn::Name adjacencyName("/ndn/site/adjacency");
akmhoquec7a79b22014-05-26 08:06:19 -050063 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
alvyc69c9af2015-04-09 12:38:54 -050064 uint32_t seqNo = 12;
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050065
alvyc69c9af2015-04-09 12:38:54 -050066 // An AdjLsa initialized with ACTIVE adjacencies should copy the adjacencies
67 AdjacencyList activeAdjacencies;
68 Adjacent activeAdjacency(adjacencyName);
69 activeAdjacency.setStatus(Adjacent::STATUS_ACTIVE);
70 activeAdjacencies.insert(activeAdjacency);
71
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060072 AdjLsa alsa1(routerName, seqNo, testTimePoint,
alvyc69c9af2015-04-09 12:38:54 -050073 activeAdjacencies.getSize(), activeAdjacencies);
74 BOOST_CHECK_EQUAL(alsa1.getAdl().getSize(), 1);
alvy49b1c0c2014-12-19 13:57:46 -060075 BOOST_CHECK_EQUAL(alsa1.getLsType(), AdjLsa::TYPE_STRING);
alvyc69c9af2015-04-09 12:38:54 -050076 BOOST_CHECK_EQUAL(alsa1.getLsSeqNo(), seqNo);
akmhoquec7a79b22014-05-26 08:06:19 -050077 BOOST_CHECK_EQUAL(alsa1.getExpirationTimePoint(), testTimePoint);
alvyc69c9af2015-04-09 12:38:54 -050078 BOOST_CHECK_EQUAL(alsa1.getNoLink(), 1);
79 BOOST_CHECK(alsa1.getAdl().isNeighbor(activeAdjacency.getName()));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050080
alvyc69c9af2015-04-09 12:38:54 -050081 // An AdjLsa initialized with INACTIVE adjacencies should not copy the adjacencies
82 AdjacencyList inactiveAdjacencies;
83 Adjacent inactiveAdjacency(adjacencyName);
84 inactiveAdjacency.setStatus(Adjacent::STATUS_INACTIVE);
85 inactiveAdjacencies.insert(inactiveAdjacency);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050086
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060087 AdjLsa alsa2(routerName, seqNo, testTimePoint,
alvyc69c9af2015-04-09 12:38:54 -050088 inactiveAdjacencies.getSize(), inactiveAdjacencies);
89 BOOST_CHECK_EQUAL(alsa2.getAdl().getSize(), 0);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050090
alvyc69c9af2015-04-09 12:38:54 -050091 // Thus, the two LSAs should not have equal content
92 BOOST_CHECK_EQUAL(alsa1.isEqualContent(alsa2), false);
93
94 // Create a duplicate of alsa1 which should have equal content
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060095 AdjLsa alsa3(routerName, seqNo, testTimePoint,
alvyc69c9af2015-04-09 12:38:54 -050096 activeAdjacencies.getSize(), activeAdjacencies);
97 BOOST_CHECK(alsa1.isEqualContent(alsa3));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050098}
99
100BOOST_AUTO_TEST_CASE(CoordinateLsaConstructorAndGetters)
101{
akmhoquec7a79b22014-05-26 08:06:19 -0500102 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600103 CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, 30.0);
104 CoordinateLsa clsa2("router1", 12, testTimePoint, 2.5, 30.0);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500105
106 BOOST_CHECK_CLOSE(clsa1.getCorRadius(), 2.5, 0.0001);
107 BOOST_CHECK_CLOSE(clsa1.getCorTheta(), 30.0, 0.0001);
108
akmhoquefdbddb12014-05-02 18:35:19 -0500109 BOOST_CHECK(clsa1.isEqualContent(clsa2));
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500110
111 BOOST_CHECK_EQUAL(clsa1.getData(), clsa2.getData());
112}
113
alvydce3f182015-04-09 11:23:30 -0500114BOOST_AUTO_TEST_CASE(IncrementAdjacentNumber)
115{
116 Adjacent adj1("adjacent1");
117 Adjacent adj2("adjacent2");
118
119 adj1.setStatus(Adjacent::STATUS_ACTIVE);
120 adj2.setStatus(Adjacent::STATUS_ACTIVE);
121
122 AdjacencyList adjList;
123 adjList.insert(adj1);
124 adjList.insert(adj2);
125
126 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
127
128 std::ostringstream ss;
129 ss << testTimePoint;
130
131 const std::string TEST_TIME_POINT_STRING = ss.str();
132
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600133 AdjLsa lsa("router1", 12, testTimePoint, adjList.getSize(), adjList);
alvydce3f182015-04-09 11:23:30 -0500134
135 std::string EXPECTED_OUTPUT =
136 "Adj Lsa:\n"
137 " Origination Router: /router1\n"
138 " Ls Type: adjacency\n"
139 " Ls Seq No: 12\n"
140 " Ls Lifetime: " + TEST_TIME_POINT_STRING + "\n"
141 " Adjacents: \n"
142 " Adjacent 1:\n"
143 " Adjacent Name: /adjacent1\n"
144 " Connecting FaceUri: \n"
145 " Link Cost: 10\n"
146 " Adjacent 2:\n"
147 " Adjacent Name: /adjacent2\n"
148 " Connecting FaceUri: \n"
149 " Link Cost: 10\n"
150 "adj_lsa_end";
151
152 std::ostringstream os;
153 os << lsa;
154
155 BOOST_CHECK_EQUAL(os.str(), EXPECTED_OUTPUT);
156}
157
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600158BOOST_AUTO_TEST_CASE(TestInitializeFromContent)
159{
160 //Adj LSA
161 Adjacent adj1("adjacent1");
162 Adjacent adj2("adjacent2");
163
164 adj1.setStatus(Adjacent::STATUS_ACTIVE);
165 adj2.setStatus(Adjacent::STATUS_ACTIVE);
166
167 //If we don't do this the test will fail
168 //Adjacent has default cost of 10 but no default
169 //connecting face URI, so initializeFromContent fails
170 adj1.setConnectingFaceUri("10.0.0.1");
171 adj2.setConnectingFaceUri("10.0.0.2");
172
173 AdjacencyList adjList;
174 adjList.insert(adj1);
175 adjList.insert(adj2);
176
177 ndn::time::system_clock::TimePoint testTimePoint = ndn::time::system_clock::now();
178
179 AdjLsa adjlsa1("router1", 1, testTimePoint, adjList.getSize(), adjList);
180 AdjLsa adjlsa2;
181
182 BOOST_CHECK(adjlsa2.initializeFromContent(adjlsa1.getData()));
183
184 BOOST_CHECK(adjlsa1.isEqualContent(adjlsa2));
185
186 //Name LSA
187 NamePrefixList npl1;
188
189 std::string s1 = "name1";
190 std::string s2 = "name2";
191
192 npl1.insert(s1);
193 npl1.insert(s2);
194
195 NameLsa nlsa1("router1", 1, testTimePoint, npl1);
196 NameLsa nlsa2;
197
198 BOOST_CHECK(nlsa2.initializeFromContent(nlsa1.getData()));
199
200 BOOST_CHECK_EQUAL(nlsa1.getData(), nlsa2.getData());
201
202 //Coordinate LSA
203 CoordinateLsa clsa1("router1", 12, testTimePoint, 2.5, 30.0);
204 CoordinateLsa clsa2;
205
206 BOOST_CHECK(clsa2.initializeFromContent(clsa1.getData()));
207
208 BOOST_CHECK_EQUAL(clsa1.getData(), clsa2.getData());
209}
210
Nick Gordon56d1fae2017-05-26 16:39:25 -0500211BOOST_AUTO_TEST_SUITE(TestNameLsa)
212
213BOOST_AUTO_TEST_CASE(OperatorEquals)
214{
215 NameLsa lsa1;
216 NameLsa lsa2;
217 ndn::Name name1("/ndn/test/name1");
218 ndn::Name name2("/ndn/test/name2");
219 ndn::Name name3("/ndn/some/other/name1");
220
221 lsa1.addName(name1);
222 lsa1.addName(name2);
223 lsa1.addName(name3);
224
225 lsa2.addName(name1);
226 lsa2.addName(name2);
227 lsa2.addName(name3);
228
229 BOOST_CHECK(lsa1.isEqualContent(lsa2));
230}
231
232BOOST_AUTO_TEST_SUITE_END() // TestNameLsa
233
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500234BOOST_AUTO_TEST_SUITE_END()
235
alvydce3f182015-04-09 11:23:30 -0500236} // namespace test
237} // namespace nlsr