blob: 0c87774935f2f6a09e1664fdfb090d62cd4ceeb5 [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
Davide Pesaventocb065f12019-12-27 01:03:34 -050024#include "tests/boost-test.hpp"
25
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060026#include <boost/mpl/vector.hpp>
27#include <boost/lexical_cast.hpp>
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080028
29namespace nlsr {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060030namespace tlv {
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080031namespace test {
32
33BOOST_AUTO_TEST_SUITE(TlvTestLsdbStatus)
34
35const uint8_t LsdbStatusData1[] =
36{
37 // Header
Tianxing Ma9ea36392018-10-05 14:32:55 -050038 0x8a, 0x7b,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080039 // AdjacencyLsa
40 0x83, 0x32,
41 // LsaInfo
42 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
43 0x80, 0x8b, 0x02, 0x27, 0x10,
44 // Adjacency
45 0x84, 0x1d, 0x07, 0x0c, 0x08, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63,
46 0x79, 0x31, 0x8d, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63, 0x79, 0x31,
47 0x8c, 0x01, 0x80,
Tianxing Ma9ea36392018-10-05 14:32:55 -050048 // CoordinateLsa
49 0x85, 0x27,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080050 // LsaInfo
51 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
52 0x80, 0x8b, 0x02, 0x27, 0x10,
53 // HyperbolicRadius
Tianxing Ma9ea36392018-10-05 14:32:55 -050054 0x87, 0x08, 0x3f, 0xfa, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080055 // HyperbolicAngle
Tianxing Ma9ea36392018-10-05 14:32:55 -050056 0x88, 0x08, 0x3f, 0xfc, 0x7a, 0xe1, 0x47, 0xae, 0x14, 0x7b,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080057 // NameLsa
58 0x89, 0x1c,
59 // LsaInfo
60 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
61 0x80, 0x8b, 0x02, 0x27, 0x10,
62 // Name
63 0x07, 0x07, 0x08, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x31
64};
65
66const uint8_t LsdbStatusData2[] =
67{
68 // Header
69 0x8a, 0x00
70};
71
72const uint8_t LsdbStatusData3[] =
73{
74 // Header
Tianxing Ma9ea36392018-10-05 14:32:55 -050075 0x8a, 0x7b,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080076 // CoordianteLsa
Tianxing Ma9ea36392018-10-05 14:32:55 -050077 0x85, 0x27,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080078 // LsaInfo
79 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
Tianxing Ma9ea36392018-10-05 14:32:55 -050080 0x80, 0x8B, 0x02, 0x27, 0x10,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080081 // HyperbolicRadius
Tianxing Ma9ea36392018-10-05 14:32:55 -050082 0x87, 0x08, 0x3F, 0xFA, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080083 // HyperbolicAngle
Tianxing Ma9ea36392018-10-05 14:32:55 -050084 0x88, 0x08, 0x3F, 0xFC, 0x7A, 0xE1, 0x47, 0xAE, 0x14, 0x7B,
Jiewen Tan7a56d1c2015-01-26 23:26:51 -080085 // NameLsa
86 0x89, 0x1c,
87 // LsaInfo
88 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
89 0x80, 0x8b, 0x02, 0x27, 0x10,
90 // Name
91 0x07, 0x07, 0x08, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x31,
92 // AdjacencyLsa
Tianxing Ma9ea36392018-10-05 14:32:55 -050093 0x83, 0x32,
94 // LsaInfo
95 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
96 0x80, 0x8B, 0x02, 0x27, 0x10,
97 // Adjacency
98 0x84, 0x1d, 0x07, 0x0c, 0x08, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63,
99 0x79, 0x31, 0x8d, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63, 0x79, 0x31,
100 0x8c, 0x01, 0x80
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800101};
102
103BOOST_AUTO_TEST_CASE(LsdbStatusEncode1)
104{
105 LsdbStatus lsdbStatus;
106
107 LsaInfo lsaInfo;
108 lsaInfo.setOriginRouter("test");
109 lsaInfo.setSequenceNumber(128);
110 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
111
112 // AdjacencyLsa
113 AdjacencyLsa adjacencyLsa;
114 adjacencyLsa.setLsaInfo(lsaInfo);
115
116 Adjacency adjacency1;
117 adjacency1.setName("adjacency1");
118 adjacency1.setUri("adjacency1");
119 adjacency1.setCost(128);
120 adjacencyLsa.addAdjacency(adjacency1);
121
122 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
123
124 // CoordinateLsa
125 CoordinateLsa coordinateLsa;
126 coordinateLsa.setLsaInfo(lsaInfo);
127
128 coordinateLsa.setHyperbolicRadius(1.65);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600129 std::vector<double> angles;
130 angles.push_back(1.78);
131 coordinateLsa.setHyperbolicAngle(angles);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800132
133 lsdbStatus.addCoordinateLsa(coordinateLsa);
134
135 // NameLsa
136 NameLsa nameLsa;
137 nameLsa.setLsaInfo(lsaInfo);
138 nameLsa.addName("name1");
139
140 lsdbStatus.addNameLsa(nameLsa);
141
142 const ndn::Block& wire = lsdbStatus.wireEncode();
143
144 BOOST_REQUIRE_EQUAL_COLLECTIONS(LsdbStatusData1,
145 LsdbStatusData1 + sizeof(LsdbStatusData1),
146 wire.begin(), wire.end());
147}
148
149BOOST_AUTO_TEST_CASE(LsdbStatusEncode2)
150{
151 LsdbStatus lsdbStatus;
152
153 const ndn::Block& wire = lsdbStatus.wireEncode();
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800154 BOOST_REQUIRE_EQUAL_COLLECTIONS(LsdbStatusData2,
155 LsdbStatusData2 + sizeof(LsdbStatusData2),
156 wire.begin(), wire.end());
157}
158
159BOOST_AUTO_TEST_CASE(LsdbStatusDecode1)
160{
161 LsdbStatus lsdbStatus;
162
163 lsdbStatus.wireDecode(ndn::Block(LsdbStatusData1, sizeof(LsdbStatusData1)));
164
165 std::list<AdjacencyLsa> adjacencyLsas = lsdbStatus.getAdjacencyLsas();
166 std::list<AdjacencyLsa>::const_iterator it1 = adjacencyLsas.begin();
167
168 LsaInfo lsaInfo = it1->getLsaInfo();
169 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
170 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
171 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
172
173 std::list<Adjacency> adjacencies = it1->getAdjacencies();
174 std::list<Adjacency>::const_iterator it2 = adjacencies.begin();
175 BOOST_CHECK_EQUAL(it2->getName(), "adjacency1");
176 BOOST_CHECK_EQUAL(it2->getUri(), "adjacency1");
177 BOOST_CHECK_EQUAL(it2->getCost(), 128);
178
179 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), true);
180
181 std::list<CoordinateLsa> coordinateLsas = lsdbStatus.getCoordinateLsas();
182 std::list<CoordinateLsa>::const_iterator it3 = coordinateLsas.begin();
183
184 lsaInfo = it3->getLsaInfo();
185 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
186 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
187 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
188
189 BOOST_REQUIRE_EQUAL(it3->getHyperbolicRadius(), 1.65);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600190 std::vector<double> angles;
191 angles.push_back(1.78);
192 BOOST_REQUIRE(it3->getHyperbolicAngle() == angles);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800193
194 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), true);
195
196 std::list<NameLsa> nameLsas = lsdbStatus.getNameLsas();
197 std::list<NameLsa>::const_iterator it4 = nameLsas.begin();
198
199 lsaInfo = it4->getLsaInfo();
200 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
201 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
202 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
203
204 std::list<ndn::Name> names = it4->getNames();
205 std::list<ndn::Name>::const_iterator it5 = names.begin();
206 BOOST_CHECK_EQUAL(*it5, "name1");
207
208 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), true);
209}
210
211BOOST_AUTO_TEST_CASE(LsdbStatusDecode2)
212{
213 LsdbStatus lsdbStatus;
214
215 lsdbStatus.wireDecode(ndn::Block(LsdbStatusData2, sizeof(LsdbStatusData2)));
216
217 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), false);
218 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), false);
219 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), false);
220}
221
222BOOST_AUTO_TEST_CASE(LsdbStatusDecode3)
223{
224 LsdbStatus lsdbStatus;
225
226 BOOST_CHECK_THROW(lsdbStatus.wireDecode(ndn::Block(LsdbStatusData3, sizeof(LsdbStatusData3))),
227 LsdbStatus::Error);
228}
229
230BOOST_AUTO_TEST_CASE(LsdbStatusClear)
231{
232 LsdbStatus lsdbStatus;
233
234 LsaInfo lsaInfo;
235 lsaInfo.setOriginRouter("test");
236 lsaInfo.setSequenceNumber(128);
237 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
238
239 // AdjacencyLsa
240 AdjacencyLsa adjacencyLsa;
241 adjacencyLsa.setLsaInfo(lsaInfo);
242
243 Adjacency adjacency1;
244 adjacency1.setName("adjacency1");
245 adjacency1.setUri("adjacency1");
246 adjacency1.setCost(128);
247 adjacencyLsa.addAdjacency(adjacency1);
248
249 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
250 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), true);
251 lsdbStatus.clearAdjacencyLsas();
252 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), false);
253
254 // CoordinateLsa
255 CoordinateLsa coordinateLsa;
256 coordinateLsa.setLsaInfo(lsaInfo);
257
258 coordinateLsa.setHyperbolicRadius(1.65);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600259 std::vector<double> angles;
260 angles.push_back(1.78);
261 coordinateLsa.setHyperbolicAngle(angles);
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800262
263 lsdbStatus.addCoordinateLsa(coordinateLsa);
264 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), true);
265 lsdbStatus.clearCoordinateLsas();
266 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), false);
267
268 // NameLsa
269 NameLsa nameLsa;
270 nameLsa.setLsaInfo(lsaInfo);
271 nameLsa.addName("name1");
272
273 lsdbStatus.addNameLsa(nameLsa);
274 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), true);
275 lsdbStatus.clearNameLsas();
276 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), false);
277}
278
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600279class Theta
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800280{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600281public:
282 std::vector<double> angles = {1.78};
283};
284
285class ThetaAndPhi
286{
287public:
288 std::vector<double> angles = {1.78, 3.21};
289};
290
291typedef boost::mpl::vector<Theta, ThetaAndPhi> HyperbolicAngleVectorFixture;
292
293BOOST_AUTO_TEST_CASE_TEMPLATE(LsdbStatusOutputStream, HRAngleVector, HyperbolicAngleVectorFixture)
294{
295 HRAngleVector angleVector;
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800296 LsdbStatus lsdbStatus;
297
298 LsaInfo lsaInfo;
299 lsaInfo.setOriginRouter("test");
300 lsaInfo.setSequenceNumber(128);
301 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
302
303 // AdjacencyLsa
304 AdjacencyLsa adjacencyLsa;
305 adjacencyLsa.setLsaInfo(lsaInfo);
306
307 Adjacency adjacency1;
308 adjacency1.setName("adjacency1");
309 adjacency1.setUri("adjacency1");
310 adjacency1.setCost(128);
311 adjacencyLsa.addAdjacency(adjacency1);
312
313 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
314
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600315 // NameLsa
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800316 NameLsa nameLsa;
317 nameLsa.setLsaInfo(lsaInfo);
318 nameLsa.addName("name1");
319
320 lsdbStatus.addNameLsa(nameLsa);
321
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600322 // CoordinateLsa
323 CoordinateLsa coordinateLsa;
324 coordinateLsa.setLsaInfo(lsaInfo);
325
326 coordinateLsa.setHyperbolicRadius(1.65);
327 coordinateLsa.setHyperbolicAngle(angleVector.angles);
328
329 std::string outputAngles = "HyperbolicAngles: ";
330 for (uint i = 0; i < angleVector.angles.size(); i++) {
331 std::string angle = boost::lexical_cast<std::string>(angleVector.angles[i]);
332 if (i == angleVector.angles.size()-1) {
333 outputAngles += angle;
334 }
335 else {
336 outputAngles += angle + ", ";
337 }
338 }
339 outputAngles += "), ";
340
341 lsdbStatus.addCoordinateLsa(coordinateLsa);
342
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800343 std::ostringstream os;
344 os << lsdbStatus;
345
346 BOOST_CHECK_EQUAL(os.str(), "LsdbStatus("
347 "AdjacencyLsa("
348 "LsaInfo("
349 "OriginRouter: /test, "
350 "SequenceNumber: 128, "
351 "ExpirationPeriod: 10000 milliseconds), "
352 "Adjacency(Name: /adjacency1, Uri: adjacency1, Cost: 128)), "
353 "CoordinateLsa("
354 "LsaInfo("
355 "OriginRouter: /test, "
356 "SequenceNumber: 128, "
357 "ExpirationPeriod: 10000 milliseconds), "
358 "HyperbolicRadius: 1.65, "
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600359 + outputAngles +
Jiewen Tan7a56d1c2015-01-26 23:26:51 -0800360 "NameLsa("
361 "LsaInfo("
362 "OriginRouter: /test, "
363 "SequenceNumber: 128, "
364 "ExpirationPeriod: 10000 milliseconds), "
365 "Name: /name1))");
366}
367
368BOOST_AUTO_TEST_SUITE_END()
369
370} // namespace test
371} // namespace tlv
372} // namespace nlsr