blob: b84b12fb9d4e547c827efb521e735df126e7d044 [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/name-lsa.hpp"
23
24#include "../boost-test.hpp"
25
26namespace nlsr {
27namespace tlv {
28namespace test {
29
30BOOST_AUTO_TEST_SUITE(TlvTestNameLsa)
31
32const uint8_t NameLsaWithNamesData[] =
33{
34 // Header
35 0x89, 0x25,
36 // LsaInfo
37 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
38 0x80, 0x8b, 0x02, 0x27, 0x10,
39 // Name
40 0x07, 0x07, 0x08, 0x05, 0x74, 0x65, 0x73, 0x74, 0x31,
41 // Name
42 0x07, 0x07, 0x08, 0x05, 0x74, 0x65, 0x73, 0x74, 0x32
43};
44
45const uint8_t NameLsaWithoutNamesData[] =
46{
47 // Header
48 0x89, 0x13,
49 // LsaInfo
50 0x80, 0x11, 0x81, 0x08, 0x07, 0x06, 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, 0x82, 0x01,
51 0x80, 0x8b, 0x02, 0x27, 0x10,
52};
53
54BOOST_AUTO_TEST_CASE(NameLsaEncodeWithNames)
55{
56 NameLsa nameLsa;
57
58 LsaInfo lsaInfo;
59 lsaInfo.setOriginRouter("test");
60 lsaInfo.setSequenceNumber(128);
61 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
62 nameLsa.setLsaInfo(lsaInfo);
63
64 nameLsa.addName("test1");
65 nameLsa.addName("test2");
66
67 const ndn::Block& wire = nameLsa.wireEncode();
68
69 BOOST_REQUIRE_EQUAL_COLLECTIONS(NameLsaWithNamesData,
70 NameLsaWithNamesData + sizeof(NameLsaWithNamesData),
71 wire.begin(), wire.end());
72}
73
74BOOST_AUTO_TEST_CASE(NameLsaDecodeWithNames)
75{
76 NameLsa nameLsa;
77
78 nameLsa.wireDecode(ndn::Block(NameLsaWithNamesData, sizeof(NameLsaWithNamesData)));
79
80 LsaInfo lsaInfo = nameLsa.getLsaInfo();
81 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
82 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
83 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
84
85 BOOST_CHECK_EQUAL(nameLsa.hasNames(), true);
86 std::list<ndn::Name> names = nameLsa.getNames();
87 std::list<ndn::Name>::const_iterator it = names.begin();
88 BOOST_CHECK_EQUAL(*it, "test1");
89
90 it++;
91 BOOST_CHECK_EQUAL(*it, "test2");
92}
93
94BOOST_AUTO_TEST_CASE(NameLsaEncodeWithoutNames)
95{
96 NameLsa nameLsa;
97
98 LsaInfo lsaInfo;
99 lsaInfo.setOriginRouter("test");
100 lsaInfo.setSequenceNumber(128);
101 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
102 nameLsa.setLsaInfo(lsaInfo);
103
104 const ndn::Block& wire = nameLsa.wireEncode();
105
106 BOOST_REQUIRE_EQUAL_COLLECTIONS(NameLsaWithoutNamesData,
107 NameLsaWithoutNamesData + sizeof(NameLsaWithoutNamesData),
108 wire.begin(), wire.end());
109}
110
111BOOST_AUTO_TEST_CASE(NameLsaDecodeWithoutNames)
112{
113 NameLsa nameLsa;
114
115 nameLsa.wireDecode(ndn::Block(NameLsaWithoutNamesData, sizeof(NameLsaWithoutNamesData)));
116
117 LsaInfo lsaInfo = nameLsa.getLsaInfo();
118 BOOST_CHECK_EQUAL(lsaInfo.getOriginRouter(), "test");
119 BOOST_CHECK_EQUAL(lsaInfo.getSequenceNumber(), 128);
120 BOOST_CHECK_EQUAL(lsaInfo.getExpirationPeriod(), ndn::time::milliseconds(10000));
121
122 BOOST_CHECK_EQUAL(nameLsa.hasNames(), false);
123}
124
125BOOST_AUTO_TEST_CASE(NameLsaClear)
126{
127 NameLsa nameLsa;
128
129 LsaInfo lsaInfo;
130 lsaInfo.setOriginRouter("test");
131 lsaInfo.setSequenceNumber(128);
132 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
133 nameLsa.setLsaInfo(lsaInfo);
134
135 nameLsa.addName("test1");
136 BOOST_CHECK_EQUAL(nameLsa.getNames().size(), 1);
137
138 std::list<ndn::Name> names = nameLsa.getNames();
139 std::list<ndn::Name>::const_iterator it = names.begin();
140 BOOST_CHECK_EQUAL(*it, "test1");
141
142 nameLsa.clearNames();
143 BOOST_CHECK_EQUAL(nameLsa.getNames().size(), 0);
144
145 nameLsa.addName("test2");
146 BOOST_CHECK_EQUAL(nameLsa.getNames().size(), 1);
147
148 names = nameLsa.getNames();
149 it = names.begin();
150 BOOST_CHECK_EQUAL(*it, "test2");
151}
152
153BOOST_AUTO_TEST_CASE(AdjacencyLsaOutputStream)
154{
155 NameLsa nameLsa;
156
157 LsaInfo lsaInfo;
158 lsaInfo.setOriginRouter("test");
159 lsaInfo.setSequenceNumber(128);
160 lsaInfo.setExpirationPeriod(ndn::time::milliseconds(10000));
161 nameLsa.setLsaInfo(lsaInfo);
162
163 nameLsa.addName("test1");
164 nameLsa.addName("test2");
165
166 std::ostringstream os;
167 os << nameLsa;
168
169 BOOST_CHECK_EQUAL(os.str(), "NameLsa("
170 "LsaInfo("
171 "OriginRouter: /test, "
172 "SequenceNumber: 128, "
173 "ExpirationPeriod: 10000 milliseconds), "
174 "Name: /test1, "
175 "Name: /test2)");
176}
177
178BOOST_AUTO_TEST_SUITE_END()
179
180} // namespace test
181} // namespace tlv
182} // namespace nlsr