blob: 73a120f558f37d953960e7b2c0877b6aa8acf5c9 [file] [log] [blame]
Jiewen Tan7a56d1c2015-01-26 23:26:51 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, The University of Memphis,
4 * 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"
25
26namespace nlsr {
27namespace tlv {
28namespace test {
29
30BOOST_AUTO_TEST_SUITE(TlvTestLsdbStatus)
31
32const uint8_t LsdbStatusData1[] =
33{
34 // Header
35 0x8a, 0x7f,
36 // AdjacencyLsa
37 0x83, 0x32,
38 // LsaInfo
39 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
40 0x80, 0x8b, 0x02, 0x27, 0x10,
41 // Adjacency
42 0x84, 0x1d, 0x07, 0x0c, 0x08, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63,
43 0x79, 0x31, 0x8d, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63, 0x79, 0x31,
44 0x8c, 0x01, 0x80,
45 // CoordianteLsa
46 0x85, 0x2b,
47 // LsaInfo
48 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
49 0x80, 0x8b, 0x02, 0x27, 0x10,
50 // HyperbolicRadius
51 0x87, 0x0a, 0x86, 0x08, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xfa, 0x3f,
52 // HyperbolicAngle
53 0x88, 0x0a, 0x86, 0x08, 0x7b, 0x14, 0xae, 0x47, 0xe1, 0x7a, 0xfc, 0x3f,
54 // NameLsa
55 0x89, 0x1c,
56 // LsaInfo
57 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
58 0x80, 0x8b, 0x02, 0x27, 0x10,
59 // Name
60 0x07, 0x07, 0x08, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x31
61};
62
63const uint8_t LsdbStatusData2[] =
64{
65 // Header
66 0x8a, 0x00
67};
68
69const uint8_t LsdbStatusData3[] =
70{
71 // Header
72 0x8a, 0x7f,
73 // CoordianteLsa
74 0x85, 0x2b,
75 // LsaInfo
76 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
77 0x80, 0x8b, 0x02, 0x27, 0x10,
78 // HyperbolicRadius
79 0x87, 0x0a, 0x86, 0x08, 0x66, 0x66, 0x66, 0x66, 0x66, 0x66, 0xfa, 0x3f,
80 // HyperbolicAngle
81 0x88, 0x0a, 0x86, 0x08, 0x7b, 0x14, 0xae, 0x47, 0xe1, 0x7a, 0xfc, 0x3f,
82 // NameLsa
83 0x89, 0x1c,
84 // LsaInfo
85 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
86 0x80, 0x8b, 0x02, 0x27, 0x10,
87 // Name
88 0x07, 0x07, 0x08, 0x05, 0x6e, 0x61, 0x6d, 0x65, 0x31,
89 // AdjacencyLsa
90 0x83, 0x32,
91 // LsaInfo
92 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
93 0x80, 0x8b, 0x02, 0x27, 0x10,
94 // Adjacency
95 0x84, 0x1d, 0x07, 0x0c, 0x08, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63,
96 0x79, 0x31, 0x8d, 0x0a, 0x61, 0x64, 0x6a, 0x61, 0x63, 0x65, 0x6e, 0x63, 0x79, 0x31,
97 0x8c, 0x01, 0x80
98};
99
100BOOST_AUTO_TEST_CASE(LsdbStatusEncode1)
101{
102 LsdbStatus lsdbStatus;
103
104 LsaInfo lsaInfo;
105 lsaInfo.setOriginRouter("test");
106 lsaInfo.setSequenceNumber(128);
107 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
108
109 // AdjacencyLsa
110 AdjacencyLsa adjacencyLsa;
111 adjacencyLsa.setLsaInfo(lsaInfo);
112
113 Adjacency adjacency1;
114 adjacency1.setName("adjacency1");
115 adjacency1.setUri("adjacency1");
116 adjacency1.setCost(128);
117 adjacencyLsa.addAdjacency(adjacency1);
118
119 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
120
121 // CoordinateLsa
122 CoordinateLsa coordinateLsa;
123 coordinateLsa.setLsaInfo(lsaInfo);
124
125 coordinateLsa.setHyperbolicRadius(1.65);
126 coordinateLsa.setHyperbolicAngle(1.78);
127
128 lsdbStatus.addCoordinateLsa(coordinateLsa);
129
130 // NameLsa
131 NameLsa nameLsa;
132 nameLsa.setLsaInfo(lsaInfo);
133 nameLsa.addName("name1");
134
135 lsdbStatus.addNameLsa(nameLsa);
136
137 const ndn::Block& wire = lsdbStatus.wireEncode();
138
139 BOOST_REQUIRE_EQUAL_COLLECTIONS(LsdbStatusData1,
140 LsdbStatusData1 + sizeof(LsdbStatusData1),
141 wire.begin(), wire.end());
142}
143
144BOOST_AUTO_TEST_CASE(LsdbStatusEncode2)
145{
146 LsdbStatus lsdbStatus;
147
148 const ndn::Block& wire = lsdbStatus.wireEncode();
149
150 BOOST_REQUIRE_EQUAL_COLLECTIONS(LsdbStatusData2,
151 LsdbStatusData2 + sizeof(LsdbStatusData2),
152 wire.begin(), wire.end());
153}
154
155BOOST_AUTO_TEST_CASE(LsdbStatusDecode1)
156{
157 LsdbStatus lsdbStatus;
158
159 lsdbStatus.wireDecode(ndn::Block(LsdbStatusData1, sizeof(LsdbStatusData1)));
160
161 std::list<AdjacencyLsa> adjacencyLsas = lsdbStatus.getAdjacencyLsas();
162 std::list<AdjacencyLsa>::const_iterator it1 = adjacencyLsas.begin();
163
164 LsaInfo lsaInfo = it1->getLsaInfo();
165 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
166 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
167 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
168
169 std::list<Adjacency> adjacencies = it1->getAdjacencies();
170 std::list<Adjacency>::const_iterator it2 = adjacencies.begin();
171 BOOST_CHECK_EQUAL(it2->getName(), "adjacency1");
172 BOOST_CHECK_EQUAL(it2->getUri(), "adjacency1");
173 BOOST_CHECK_EQUAL(it2->getCost(), 128);
174
175 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), true);
176
177 std::list<CoordinateLsa> coordinateLsas = lsdbStatus.getCoordinateLsas();
178 std::list<CoordinateLsa>::const_iterator it3 = coordinateLsas.begin();
179
180 lsaInfo = it3->getLsaInfo();
181 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
182 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
183 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
184
185 BOOST_REQUIRE_EQUAL(it3->getHyperbolicRadius(), 1.65);
186 BOOST_REQUIRE_EQUAL(it3->getHyperbolicAngle(), 1.78);
187
188 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), true);
189
190 std::list<NameLsa> nameLsas = lsdbStatus.getNameLsas();
191 std::list<NameLsa>::const_iterator it4 = nameLsas.begin();
192
193 lsaInfo = it4->getLsaInfo();
194 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
195 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
196 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
197
198 std::list<ndn::Name> names = it4->getNames();
199 std::list<ndn::Name>::const_iterator it5 = names.begin();
200 BOOST_CHECK_EQUAL(*it5, "name1");
201
202 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), true);
203}
204
205BOOST_AUTO_TEST_CASE(LsdbStatusDecode2)
206{
207 LsdbStatus lsdbStatus;
208
209 lsdbStatus.wireDecode(ndn::Block(LsdbStatusData2, sizeof(LsdbStatusData2)));
210
211 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), false);
212 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), false);
213 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), false);
214}
215
216BOOST_AUTO_TEST_CASE(LsdbStatusDecode3)
217{
218 LsdbStatus lsdbStatus;
219
220 BOOST_CHECK_THROW(lsdbStatus.wireDecode(ndn::Block(LsdbStatusData3, sizeof(LsdbStatusData3))),
221 LsdbStatus::Error);
222}
223
224BOOST_AUTO_TEST_CASE(LsdbStatusClear)
225{
226 LsdbStatus lsdbStatus;
227
228 LsaInfo lsaInfo;
229 lsaInfo.setOriginRouter("test");
230 lsaInfo.setSequenceNumber(128);
231 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
232
233 // AdjacencyLsa
234 AdjacencyLsa adjacencyLsa;
235 adjacencyLsa.setLsaInfo(lsaInfo);
236
237 Adjacency adjacency1;
238 adjacency1.setName("adjacency1");
239 adjacency1.setUri("adjacency1");
240 adjacency1.setCost(128);
241 adjacencyLsa.addAdjacency(adjacency1);
242
243 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
244 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), true);
245 lsdbStatus.clearAdjacencyLsas();
246 BOOST_CHECK_EQUAL(lsdbStatus.hasAdjacencyLsas(), false);
247
248 // CoordinateLsa
249 CoordinateLsa coordinateLsa;
250 coordinateLsa.setLsaInfo(lsaInfo);
251
252 coordinateLsa.setHyperbolicRadius(1.65);
253 coordinateLsa.setHyperbolicAngle(1.78);
254
255 lsdbStatus.addCoordinateLsa(coordinateLsa);
256 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), true);
257 lsdbStatus.clearCoordinateLsas();
258 BOOST_CHECK_EQUAL(lsdbStatus.hasCoordinateLsas(), false);
259
260 // NameLsa
261 NameLsa nameLsa;
262 nameLsa.setLsaInfo(lsaInfo);
263 nameLsa.addName("name1");
264
265 lsdbStatus.addNameLsa(nameLsa);
266 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), true);
267 lsdbStatus.clearNameLsas();
268 BOOST_CHECK_EQUAL(lsdbStatus.hasNameLsas(), false);
269}
270
271BOOST_AUTO_TEST_CASE(LsdbStatusOutputStream)
272{
273 LsdbStatus lsdbStatus;
274
275 LsaInfo lsaInfo;
276 lsaInfo.setOriginRouter("test");
277 lsaInfo.setSequenceNumber(128);
278 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
279
280 // AdjacencyLsa
281 AdjacencyLsa adjacencyLsa;
282 adjacencyLsa.setLsaInfo(lsaInfo);
283
284 Adjacency adjacency1;
285 adjacency1.setName("adjacency1");
286 adjacency1.setUri("adjacency1");
287 adjacency1.setCost(128);
288 adjacencyLsa.addAdjacency(adjacency1);
289
290 lsdbStatus.addAdjacencyLsa(adjacencyLsa);
291
292 // CoordinateLsa
293 CoordinateLsa coordinateLsa;
294 coordinateLsa.setLsaInfo(lsaInfo);
295
296 coordinateLsa.setHyperbolicRadius(1.65);
297 coordinateLsa.setHyperbolicAngle(1.78);
298
299 lsdbStatus.addCoordinateLsa(coordinateLsa);
300
301 // NameLsa
302 NameLsa nameLsa;
303 nameLsa.setLsaInfo(lsaInfo);
304 nameLsa.addName("name1");
305
306 lsdbStatus.addNameLsa(nameLsa);
307
308 std::ostringstream os;
309 os << lsdbStatus;
310
311 BOOST_CHECK_EQUAL(os.str(), "LsdbStatus("
312 "AdjacencyLsa("
313 "LsaInfo("
314 "OriginRouter: /test, "
315 "SequenceNumber: 128, "
316 "ExpirationPeriod: 10000 milliseconds), "
317 "Adjacency(Name: /adjacency1, Uri: adjacency1, Cost: 128)), "
318 "CoordinateLsa("
319 "LsaInfo("
320 "OriginRouter: /test, "
321 "SequenceNumber: 128, "
322 "ExpirationPeriod: 10000 milliseconds), "
323 "HyperbolicRadius: 1.65, "
324 "HyperbolicAngle: 1.78), "
325 "NameLsa("
326 "LsaInfo("
327 "OriginRouter: /test, "
328 "SequenceNumber: 128, "
329 "ExpirationPeriod: 10000 milliseconds), "
330 "Name: /name1))");
331}
332
333BOOST_AUTO_TEST_SUITE_END()
334
335} // namespace test
336} // namespace tlv
337} // namespace nlsr