blob: c058fb1854238e4662b9677c273d8fd9456be02a [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Tianxing Ma9ea36392018-10-05 14:32:55 -05003 * Copyright (c) 2014-2019, The University of Memphis,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
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/>.
20 **/
21
22#include "tlv/lsdb-status.hpp"
23
24#include "../boost-test.hpp"
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060025#include <boost/mpl/vector.hpp>
26#include <boost/lexical_cast.hpp>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080027
28namespace nlsr {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060029namespace tlv {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080030namespace test {
31
32BOOST_AUTO_TEST_SUITE(TlvTestLsdbStatus)
33
34const uint8_t LsdbStatusData1[] =
35{
36 // Header
Tianxing Ma9ea36392018-10-05 14:32:55 -050037 0x8a, 0x7b,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080038 // AdjacencyLsa
39 0x83, 0x32,
40 // LsaInfo
41 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
42 0x80, 0x8b, 0x02, 0x27, 0x10,
43 // Adjacency
44 0x84, 0x1d, 0x07, 0x0c, 0x08, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63,
45 0x79, 0x31, 0x8d, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63, 0x79, 0x31,
46 0x8c, 0x01, 0x80,
Tianxing Ma9ea36392018-10-05 14:32:55 -050047 // CoordinateLsa
48 0x85, 0x27,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080049 // LsaInfo
50 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
51 0x80, 0x8b, 0x02, 0x27, 0x10,
52 // HyperbolicRadius
Tianxing Ma9ea36392018-10-05 14:32:55 -050053 0x87, 0x08, 0x3f, 0xfa, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080054 // HyperbolicAngle
Tianxing Ma9ea36392018-10-05 14:32:55 -050055 0x88, 0x08, 0x3f, 0xfc, 0x7a, 0xe1, 0x47, 0xae, 0x14, 0x7b,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080056 // NameLsa
57 0x89, 0x1c,
58 // LsaInfo
59 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
60 0x80, 0x8b, 0x02, 0x27, 0x10,
61 // Name
62 0x07, 0x07, 0x08, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x31
63};
64
65const uint8_t LsdbStatusData2[] =
66{
67 // Header
68 0x8a, 0x00
69};
70
71const uint8_t LsdbStatusData3[] =
72{
73 // Header
Tianxing Ma9ea36392018-10-05 14:32:55 -050074 0x8a, 0x7b,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080075 // CoordianteLsa
Tianxing Ma9ea36392018-10-05 14:32:55 -050076 0x85, 0x27,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080077 // LsaInfo
78 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
Tianxing Ma9ea36392018-10-05 14:32:55 -050079 0x80, 0x8B, 0x02, 0x27, 0x10,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080080 // HyperbolicRadius
Tianxing Ma9ea36392018-10-05 14:32:55 -050081 0x87, 0x08, 0x3F, 0xFA, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080082 // HyperbolicAngle
Tianxing Ma9ea36392018-10-05 14:32:55 -050083 0x88, 0x08, 0x3F, 0xFC, 0x7A, 0xE1, 0x47, 0xAE, 0x14, 0x7B,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080084 // NameLsa
85 0x89, 0x1c,
86 // LsaInfo
87 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
88 0x80, 0x8b, 0x02, 0x27, 0x10,
89 // Name
90 0x07, 0x07, 0x08, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x31,
91 // AdjacencyLsa
Tianxing Ma9ea36392018-10-05 14:32:55 -050092 0x83, 0x32,
93 // LsaInfo
94 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
95 0x80, 0x8B, 0x02, 0x27, 0x10,
96 // Adjacency
97 0x84, 0x1d, 0x07, 0x0c, 0x08, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63,
98 0x79, 0x31, 0x8d, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63, 0x79, 0x31,
99 0x8c, 0x01, 0x80
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800100};
101
102BOOST_AUTO_TEST_CASE(LsdbStatusEncode1)
103{
104 LsdbStatus lsdbStatus;
105
106 LsaInfo lsaInfo;
107 lsaInfo.setOriginRouter("test");
108 lsaInfo.setSequenceNumber(128);
109 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
110
111 // AdjacencyLsa
112 AdjacencyLsa adjacencyLsa;
113 adjacencyLsa.setLsaInfo(lsaInfo);
114
115 Adjacency adjacency1;
116 adjacency1.setName("adjacency1");
117 adjacency1.setUri("adjacency1");
118 adjacency1.setCost(128);
119 adjacencyLsa.addAdjacency(adjacency1);
120
121 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
122
123 // CoordinateLsa
124 CoordinateLsa coordinateLsa;
125 coordinateLsa.setLsaInfo(lsaInfo);
126
127 coordinateLsa.setHyperbolicRadius(1.65);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600128 std::vector<double> angles;
129 angles.push_back(1.78);
130 coordinateLsa.setHyperbolicAngle(angles);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800131
132 lsdbStatus.addCoordinateLsa(coordinateLsa);
133
134 // NameLsa
135 NameLsa nameLsa;
136 nameLsa.setLsaInfo(lsaInfo);
137 nameLsa.addName("name1");
138
139 lsdbStatus.addNameLsa(nameLsa);
140
141 const ndn::Block& wire = lsdbStatus.wireEncode();
142
143 BOOST_REQUIRE_EQUAL_COLLECTIONS(LsdbStatusData1,
144 LsdbStatusData1 + sizeof(LsdbStatusData1),
145 wire.begin(), wire.end());
146}
147
148BOOST_AUTO_TEST_CASE(LsdbStatusEncode2)
149{
150 LsdbStatus lsdbStatus;
151
152 const ndn::Block& wire = lsdbStatus.wireEncode();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800153 BOOST_REQUIRE_EQUAL_COLLECTIONS(LsdbStatusData2,
154 LsdbStatusData2 + sizeof(LsdbStatusData2),
155 wire.begin(), wire.end());
156}
157
158BOOST_AUTO_TEST_CASE(LsdbStatusDecode1)
159{
160 LsdbStatus lsdbStatus;
161
162 lsdbStatus.wireDecode(ndn::Block(LsdbStatusData1, sizeof(LsdbStatusData1)));
163
164 std::list<AdjacencyLsa> adjacencyLsas = lsdbStatus.getAdjacencyLsas();
165 std::list<AdjacencyLsa>::const_iterator it1 = adjacencyLsas.begin();
166
167 LsaInfo lsaInfo = it1->getLsaInfo();
168 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
169 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
170 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
171
172 std::list<Adjacency> adjacencies = it1->getAdjacencies();
173 std::list<Adjacency>::const_iterator it2 = adjacencies.begin();
174 BOOST_CHECK_EQUAL(it2->getName(), "adjacency1");
175 BOOST_CHECK_EQUAL(it2->getUri(), "adjacency1");
176 BOOST_CHECK_EQUAL(it2->getCost(), 128);
177
178 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), true);
179
180 std::list<CoordinateLsa> coordinateLsas = lsdbStatus.getCoordinateLsas();
181 std::list<CoordinateLsa>::const_iterator it3 = coordinateLsas.begin();
182
183 lsaInfo = it3->getLsaInfo();
184 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
185 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
186 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
187
188 BOOST_REQUIRE_EQUAL(it3->getHyperbolicRadius(), 1.65);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600189 std::vector<double> angles;
190 angles.push_back(1.78);
191 BOOST_REQUIRE(it3->getHyperbolicAngle() == angles);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800192
193 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), true);
194
195 std::list<NameLsa> nameLsas = lsdbStatus.getNameLsas();
196 std::list<NameLsa>::const_iterator it4 = nameLsas.begin();
197
198 lsaInfo = it4->getLsaInfo();
199 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
200 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
201 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
202
203 std::list<ndn::Name> names = it4->getNames();
204 std::list<ndn::Name>::const_iterator it5 = names.begin();
205 BOOST_CHECK_EQUAL(*it5, "name1");
206
207 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), true);
208}
209
210BOOST_AUTO_TEST_CASE(LsdbStatusDecode2)
211{
212 LsdbStatus lsdbStatus;
213
214 lsdbStatus.wireDecode(ndn::Block(LsdbStatusData2, sizeof(LsdbStatusData2)));
215
216 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), false);
217 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), false);
218 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), false);
219}
220
221BOOST_AUTO_TEST_CASE(LsdbStatusDecode3)
222{
223 LsdbStatus lsdbStatus;
224
225 BOOST_CHECK_THROW(lsdbStatus.wireDecode(ndn::Block(LsdbStatusData3, sizeof(LsdbStatusData3))),
226 LsdbStatus::Error);
227}
228
229BOOST_AUTO_TEST_CASE(LsdbStatusClear)
230{
231 LsdbStatus lsdbStatus;
232
233 LsaInfo lsaInfo;
234 lsaInfo.setOriginRouter("test");
235 lsaInfo.setSequenceNumber(128);
236 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
237
238 // AdjacencyLsa
239 AdjacencyLsa adjacencyLsa;
240 adjacencyLsa.setLsaInfo(lsaInfo);
241
242 Adjacency adjacency1;
243 adjacency1.setName("adjacency1");
244 adjacency1.setUri("adjacency1");
245 adjacency1.setCost(128);
246 adjacencyLsa.addAdjacency(adjacency1);
247
248 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
249 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), true);
250 lsdbStatus.clearAdjacencyLsas();
251 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), false);
252
253 // CoordinateLsa
254 CoordinateLsa coordinateLsa;
255 coordinateLsa.setLsaInfo(lsaInfo);
256
257 coordinateLsa.setHyperbolicRadius(1.65);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600258 std::vector<double> angles;
259 angles.push_back(1.78);
260 coordinateLsa.setHyperbolicAngle(angles);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800261
262 lsdbStatus.addCoordinateLsa(coordinateLsa);
263 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), true);
264 lsdbStatus.clearCoordinateLsas();
265 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), false);
266
267 // NameLsa
268 NameLsa nameLsa;
269 nameLsa.setLsaInfo(lsaInfo);
270 nameLsa.addName("name1");
271
272 lsdbStatus.addNameLsa(nameLsa);
273 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), true);
274 lsdbStatus.clearNameLsas();
275 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), false);
276}
277
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600278class Theta
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800279{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600280public:
281 std::vector<double> angles = {1.78};
282};
283
284class ThetaAndPhi
285{
286public:
287 std::vector<double> angles = {1.78, 3.21};
288};
289
290typedef boost::mpl::vector<Theta, ThetaAndPhi> HyperbolicAngleVectorFixture;
291
292BOOST_AUTO_TEST_CASE_TEMPLATE(LsdbStatusOutputStream, HRAngleVector, HyperbolicAngleVectorFixture)
293{
294 HRAngleVector angleVector;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800295 LsdbStatus lsdbStatus;
296
297 LsaInfo lsaInfo;
298 lsaInfo.setOriginRouter("test");
299 lsaInfo.setSequenceNumber(128);
300 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
301
302 // AdjacencyLsa
303 AdjacencyLsa adjacencyLsa;
304 adjacencyLsa.setLsaInfo(lsaInfo);
305
306 Adjacency adjacency1;
307 adjacency1.setName("adjacency1");
308 adjacency1.setUri("adjacency1");
309 adjacency1.setCost(128);
310 adjacencyLsa.addAdjacency(adjacency1);
311
312 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
313
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600314 // NameLsa
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800315 NameLsa nameLsa;
316 nameLsa.setLsaInfo(lsaInfo);
317 nameLsa.addName("name1");
318
319 lsdbStatus.addNameLsa(nameLsa);
320
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600321 // CoordinateLsa
322 CoordinateLsa coordinateLsa;
323 coordinateLsa.setLsaInfo(lsaInfo);
324
325 coordinateLsa.setHyperbolicRadius(1.65);
326 coordinateLsa.setHyperbolicAngle(angleVector.angles);
327
328 std::string outputAngles = "HyperbolicAngles: ";
329 for (uint i = 0; i < angleVector.angles.size(); i++) {
330 std::string angle = boost::lexical_cast<std::string>(angleVector.angles[i]);
331 if (i == angleVector.angles.size()-1) {
332 outputAngles += angle;
333 }
334 else {
335 outputAngles += angle + ", ";
336 }
337 }
338 outputAngles += "), ";
339
340 lsdbStatus.addCoordinateLsa(coordinateLsa);
341
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800342 std::ostringstream os;
343 os << lsdbStatus;
344
345 BOOST_CHECK_EQUAL(os.str(), "LsdbStatus("
346 "AdjacencyLsa("
347 "LsaInfo("
348 "OriginRouter: /test, "
349 "SequenceNumber: 128, "
350 "ExpirationPeriod: 10000 milliseconds), "
351 "Adjacency(Name: /adjacency1, Uri: adjacency1, Cost: 128)), "
352 "CoordinateLsa("
353 "LsaInfo("
354 "OriginRouter: /test, "
355 "SequenceNumber: 128, "
356 "ExpirationPeriod: 10000 milliseconds), "
357 "HyperbolicRadius: 1.65, "
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600358 + outputAngles +
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800359 "NameLsa("
360 "LsaInfo("
361 "OriginRouter: /test, "
362 "SequenceNumber: 128, "
363 "ExpirationPeriod: 10000 milliseconds), "
364 "Name: /name1))");
365}
366
367BOOST_AUTO_TEST_SUITE_END()
368
369} // namespace test
370} // namespace tlv
371} // namespace nlsr