blob: 0634dd7de33e735bd41a32f9e5f39fbacc22c4da [file] [log] [blame]
Junxiao Shi20377642023-08-24 23:55:40 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
awlane6d7c37f2025-03-07 11:53:58 -06003 * Copyright (c) 2014-2025, The University of Memphis,
Junxiao Shi20377642023-08-24 23:55:40 +00004 * 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 "lsa/name-lsa.hpp"
awlane6d7c37f2025-03-07 11:53:58 -060023#include "name-prefix-list.hpp"
24
25#include "ndn-cxx/encoding/buffer-stream.hpp"
Junxiao Shi20377642023-08-24 23:55:40 +000026
27#include "tests/boost-test.hpp"
28
Davide Pesavento288141a2024-02-13 17:30:35 -050029namespace nlsr::tests {
Junxiao Shi20377642023-08-24 23:55:40 +000030
31BOOST_AUTO_TEST_SUITE(TestNameLsa)
32
33const uint8_t NAME_LSA1[] = {
awlane6d7c37f2025-03-07 11:53:58 -060034 0x89, 0x4F, //name lsa
35 0x80, 0x23, // lsa
36 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, // router name (router1)
37 0x82, 0x01, 0x0C, // sequence number (12)
38 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34,
39 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, // expiration time
40 0x92, 0x13, // prefix info
41 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x31, // name (name1)
42 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
43 0x92, 0x13, // prefix info
44 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, // name (name2)
45 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
Junxiao Shi20377642023-08-24 23:55:40 +000046};
47
48const uint8_t NAME_LSA_EXTRA_NAME[] = {
awlane6d7c37f2025-03-07 11:53:58 -060049 0x89, 0x64, //name lsa
50 0x80, 0x23, // lsa
51 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, // router name (router1)
52 0x82, 0x01, 0x0C, // sequence number (12)
53 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34,
54 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, // expiration time
55 0x92, 0x13, // prefix info
56 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x31, // name (name1)
57 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
58 0x92, 0x13, // prefix info
59 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, // name (name2)
60 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
61 0x92, 0x13, // prefix info
62 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33, // name (name3)
63 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
Junxiao Shi20377642023-08-24 23:55:40 +000064};
65
66const uint8_t NAME_LSA_DIFF_SEQ[] = {
awlane6d7c37f2025-03-07 11:53:58 -060067 0x89, 0x64, //name lsa
68 0x80, 0x23, // lsa
69 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, // router name (router1)
70 0x82, 0x01, 0x0E, // sequence number (12)
71 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34,
72 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, // expiration time
73 0x92, 0x13, // prefix info
74 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x31, // name (name1)
75 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
76 0x92, 0x13, // prefix info
77 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, // name (name2)
78 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
79 0x92, 0x13, // prefix info
80 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33, // name (name3)
81 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
Junxiao Shi20377642023-08-24 23:55:40 +000082};
83
84const uint8_t NAME_LSA_DIFF_TS[] = {
awlane6d7c37f2025-03-07 11:53:58 -060085 0x89, 0x64, //name lsa
86 0x80, 0x23, // lsa
87 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, // router name (router1)
88 0x82, 0x01, 0x0E, // sequence number (12)
89 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34,
90 0x3A, 0x31, 0x33, 0x3A, 0x34, 0x34, // expiration time (10s higher)
91 0x92, 0x13, // prefix info
92 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x31, // name (name1)
93 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
94 0x92, 0x13, // prefix info
95 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, // name (name2)
96 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
97 0x92, 0x13, // prefix info
98 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33, // name (name3)
99 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
100};
101
102const uint8_t NAME_LSA_DIFF_COST[] = {
103 0x89, 0x64, //name lsa
104 0x80, 0x23, // lsa
105 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, // router name (router1)
106 0x82, 0x01, 0x0E, // sequence number (12)
107 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34,
108 0x3A, 0x31, 0x33, 0x3A, 0x34, 0x34, // expiration time (10s higher)
109 0x92, 0x13, // prefix info
110 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x31, // name (name1)
111 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
112 0x92, 0x13, // prefix info
113 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, // name (name2)
114 0x8C, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (0)
115 0x92, 0x13, // prefix info
116 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33, // name (name3)
117 0x8C, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // cost (10)
Junxiao Shi20377642023-08-24 23:55:40 +0000118};
119
120BOOST_AUTO_TEST_CASE(Basic)
121{
122 ndn::Name s1{"name1"};
123 ndn::Name s2{"name2"};
124 NamePrefixList npl1{s1, s2};
125
126 auto testTimePoint = ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196014943));
127
128 // 3rd argument is seqNo
129 NameLsa nlsa1("router1", 12, testTimePoint, npl1);
130 NameLsa nlsa2("router2", 12, testTimePoint, npl1);
131
132 BOOST_CHECK_EQUAL(nlsa1.getType(), Lsa::Type::NAME);
133 BOOST_CHECK(nlsa1.getExpirationTimePoint() == nlsa2.getExpirationTimePoint());
134
135 auto wire = nlsa1.wireEncode();
136 BOOST_TEST(wire == NAME_LSA1, boost::test_tools::per_element());
137
awlane6d7c37f2025-03-07 11:53:58 -0600138 nlsa1.addName(PrefixInfo(ndn::Name("name3"), 0));
Junxiao Shi20377642023-08-24 23:55:40 +0000139 wire = nlsa1.wireEncode();
140 BOOST_TEST(wire == NAME_LSA_EXTRA_NAME, boost::test_tools::per_element());
141
142 nlsa1.setSeqNo(14);
143 wire = nlsa1.wireEncode();
144 BOOST_TEST(wire == NAME_LSA_DIFF_SEQ, boost::test_tools::per_element());
145
146 testTimePoint =
147 ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196024993));
148 nlsa1.setExpirationTimePoint(testTimePoint);
149 wire = nlsa1.wireEncode();
150 BOOST_TEST(wire == NAME_LSA_DIFF_TS, boost::test_tools::per_element());
151
awlane6d7c37f2025-03-07 11:53:58 -0600152 nlsa1.addName(PrefixInfo(ndn::Name("name3"), 10));
153 wire = nlsa1.wireEncode();
154 BOOST_TEST(wire == NAME_LSA_DIFF_COST, boost::test_tools::per_element());
Junxiao Shi20377642023-08-24 23:55:40 +0000155 // Not testing router name as not sure if that will ever change once set
156}
157
158BOOST_AUTO_TEST_CASE(InitializeFromContent)
159{
160 auto testTimePoint = ndn::time::system_clock::now();
161 ndn::Name s1{"name1"};
162 ndn::Name s2{"name2"};
163 NamePrefixList npl1{s1, s2};
164
165 NameLsa nlsa1("router1", 1, testTimePoint, npl1);
166 NameLsa nlsa2(nlsa1.wireEncode());
167 BOOST_CHECK_EQUAL(nlsa1.wireEncode(), nlsa2.wireEncode());
168}
169
170BOOST_AUTO_TEST_CASE(OperatorEquals)
171{
awlane6d7c37f2025-03-07 11:53:58 -0600172 PrefixInfo name1 = PrefixInfo(ndn::Name("/ndn/test/name1"), 0);
173 PrefixInfo name2 = PrefixInfo(ndn::Name("/ndn/test/name2"), 0);
174 PrefixInfo name3 = PrefixInfo(ndn::Name("/ndn/some/other/name1"), 0);
175 PrefixInfo name4 = PrefixInfo(ndn::Name("/ndn/some/other/name1"), 1);
Junxiao Shi20377642023-08-24 23:55:40 +0000176
Junxiao Shi2ecb0592023-08-14 11:35:05 +0000177 NameLsa lsa1;
Junxiao Shi20377642023-08-24 23:55:40 +0000178 lsa1.addName(name1);
179 lsa1.addName(name2);
180 lsa1.addName(name3);
181
Junxiao Shi2ecb0592023-08-14 11:35:05 +0000182 NameLsa lsa2;
Junxiao Shi20377642023-08-24 23:55:40 +0000183 lsa2.addName(name1);
184 lsa2.addName(name2);
Junxiao Shi2ecb0592023-08-14 11:35:05 +0000185 BOOST_CHECK_NE(lsa1, lsa2);
Junxiao Shi20377642023-08-24 23:55:40 +0000186
Junxiao Shi2ecb0592023-08-14 11:35:05 +0000187 lsa2.addName(name3);
188 BOOST_CHECK_EQUAL(lsa1, lsa2);
awlane6d7c37f2025-03-07 11:53:58 -0600189
190 NameLsa lsa3;
191 lsa3.addName(name1);
192 lsa3.addName(name2);
193 lsa3.addName(name4);
194 BOOST_CHECK_NE(lsa2, lsa3);
195
196 NameLsa lsa4;
197 lsa4.addName(name1);
198 lsa4.addName(name2);
199 lsa4.addName(name4);
200 BOOST_CHECK_EQUAL(lsa3, lsa4);
Junxiao Shi20377642023-08-24 23:55:40 +0000201}
202
203BOOST_AUTO_TEST_CASE(Update)
204{
205 NameLsa knownNameLsa;
206 knownNameLsa.m_originRouter = ndn::Name("/yoursunny/_/%C1.Router/dal");
207 knownNameLsa.m_seqNo = 2683;
208 knownNameLsa.setExpirationTimePoint(ndn::time::system_clock::now() + 3561_ms);
awlane6d7c37f2025-03-07 11:53:58 -0600209 knownNameLsa.addName(PrefixInfo(ndn::Name("/yoursunny/_/dal"), 0));
210 knownNameLsa.addName(PrefixInfo(ndn::Name("/ndn"), 0));
Junxiao Shi20377642023-08-24 23:55:40 +0000211
212 auto rcvdLsa = std::make_shared<NameLsa>();
213 rcvdLsa->m_originRouter = ndn::Name("/yoursunny/_/%C1.Router/dal");
214 rcvdLsa->m_seqNo = 2684;
215 rcvdLsa->setExpirationTimePoint(ndn::time::system_clock::now() + 3600_ms);
216
217 auto nlsa = std::static_pointer_cast<NameLsa>(rcvdLsa);
awlane6d7c37f2025-03-07 11:53:58 -0600218 nlsa->addName(PrefixInfo(ndn::Name("/ndn"), 0));
219 nlsa->addName(PrefixInfo(ndn::Name("/yoursunny/_/dal"), 0));
220 PrefixInfo addedName1 = PrefixInfo(ndn::Name("/yoursunny/video/ndn-dpdk_acmicn20_20200917"), 0);
221 PrefixInfo addedName2 = PrefixInfo(ndn::Name("/yoursunny/pushups"), 0);
Junxiao Shi20377642023-08-24 23:55:40 +0000222 nlsa->addName(addedName1);
223 nlsa->addName(addedName2);
224
225 auto [updated, namesToAdd, namesToRemove] = knownNameLsa.update(rcvdLsa);
226
227 BOOST_CHECK_EQUAL(updated, true);
228 BOOST_CHECK_EQUAL(namesToAdd.size(), 2);
229 BOOST_CHECK_EQUAL(namesToRemove.size(), 0);
230 auto it = std::find(namesToAdd.begin(), namesToAdd.end(), addedName1);
231 BOOST_CHECK(it != namesToAdd.end());
232 it = std::find(namesToAdd.begin(), namesToAdd.end(), addedName2);
233 BOOST_CHECK(it != namesToAdd.end());
234}
235
236BOOST_AUTO_TEST_SUITE_END()
237
Davide Pesavento288141a2024-02-13 17:30:35 -0500238} // namespace nlsr::tests