blob: 12284e81036294d4c6e3b92b0a740d26dd4137d0 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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
37 0x8a, 0x7f,
38 // 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,
47 // CoordianteLsa
48 0x85, 0x2b,
49 // LsaInfo
50 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
51 0x80, 0x8b, 0x02, 0x27, 0x10,
52 // HyperbolicRadius
53 0x87, 0x0a, 0x86, 0x08, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xfa, 0x3f,
54 // HyperbolicAngle
55 0x88, 0x0a, 0x86, 0x08, 0x7b, 0x14, 0xae, 0x47, 0xe1, 0x7a, 0xfc, 0x3f,
56 // 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
74 0x8a, 0x7f,
75 // CoordianteLsa
76 0x85, 0x2b,
77 // LsaInfo
78 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
79 0x80, 0x8b, 0x02, 0x27, 0x10,
80 // HyperbolicRadius
81 0x87, 0x0a, 0x86, 0x08, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xfa, 0x3f,
82 // HyperbolicAngle
83 0x88, 0x0a, 0x86, 0x08, 0x7b, 0x14, 0xae, 0x47, 0xe1, 0x7a, 0xfc, 0x3f,
84 // 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
92 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
100};
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();
153
154 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