blob: dd17421fc33addff93868f7a9845f834a30662c4 [file] [log] [blame]
Junxiao Shi20377642023-08-24 23:55:40 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shib8752932024-01-07 15:18:46 +00003 * Copyright (c) 2014-2024, 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/adj-lsa.hpp"
23
24#include "tests/boost-test.hpp"
25
Davide Pesavento288141a2024-02-13 17:30:35 -050026namespace nlsr::tests {
Junxiao Shi20377642023-08-24 23:55:40 +000027
28BOOST_AUTO_TEST_SUITE(TestAdjLsa)
29
30const uint8_t ADJ_LSA1[] = {
31 0x83, 0x58, 0x80, 0x2D, 0x07, 0x13, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x04, 0x73, 0x69,
32 0x74, 0x65, 0x08, 0x06, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x82, 0x01, 0x0C, 0x8B, 0x13,
33 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34, 0x3A, 0x31,
34 0x33, 0x3A, 0x33, 0x34, 0x84, 0x27, 0x07, 0x16, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x04,
35 0x73, 0x69, 0x74, 0x65, 0x08, 0x09, 0x61, 0x64, 0x6A, 0x61, 0x63, 0x65, 0x6E, 0x63, 0x79,
36 0x8D, 0x03, 0x3A, 0x2F, 0x2F, 0x8C, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
37};
38
39const uint8_t ADJ_LSA_EXTRA_NEIGHBOR[] = {
40 0x83, 0x80, 0x80, 0x2D, 0x07, 0x13, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x04, 0x73, 0x69,
41 0x74, 0x65, 0x08, 0x06, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x82, 0x01, 0x0C, 0x8B, 0x13,
42 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34, 0x3A, 0x31,
43 0x33, 0x3A, 0x33, 0x34, 0x84, 0x27, 0x07, 0x16, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x04,
44 0x73, 0x69, 0x74, 0x65, 0x08, 0x09, 0x61, 0x64, 0x6A, 0x61, 0x63, 0x65, 0x6E, 0x63, 0x79,
45 0x8D, 0x03, 0x3A, 0x2F, 0x2F, 0x8C, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x84, 0x26, 0x07, 0x15, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x03, 0x65, 0x64, 0x75, 0x08,
47 0x09, 0x61, 0x64, 0x6A, 0x61, 0x63, 0x65, 0x6E, 0x63, 0x79, 0x8D, 0x03, 0x3A, 0x2F, 0x2F,
48 0x8C, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
49};
50
51const uint8_t ADJ_LSA_DIFF_SEQ[] = {
52 0x83, 0x80, 0x80, 0x2D, 0x07, 0x13, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x04, 0x73, 0x69,
53 0x74, 0x65, 0x08, 0x06, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x82, 0x01, 0x0E, 0x8B, 0x13,
54 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34, 0x3A, 0x31,
55 0x33, 0x3A, 0x33, 0x34, 0x84, 0x27, 0x07, 0x16, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x04,
56 0x73, 0x69, 0x74, 0x65, 0x08, 0x09, 0x61, 0x64, 0x6A, 0x61, 0x63, 0x65, 0x6E, 0x63, 0x79,
57 0x8D, 0x03, 0x3A, 0x2F, 0x2F, 0x8C, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x84, 0x26, 0x07, 0x15, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x03, 0x65, 0x64, 0x75, 0x08,
59 0x09, 0x61, 0x64, 0x6A, 0x61, 0x63, 0x65, 0x6E, 0x63, 0x79, 0x8D, 0x03, 0x3A, 0x2F, 0x2F,
60 0x8C, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
61
62const uint8_t ADJ_LSA_DIFF_TS[] = {
63 0x83, 0x80, 0x80, 0x2D, 0x07, 0x13, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x04, 0x73, 0x69,
64 0x74, 0x65, 0x08, 0x06, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x82, 0x01, 0x0E, 0x8B, 0x13,
65 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, 0x20, 0x30, 0x34, 0x3A, 0x31,
66 0x33, 0x3A, 0x34, 0x34, 0x84, 0x27, 0x07, 0x16, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x04,
67 0x73, 0x69, 0x74, 0x65, 0x08, 0x09, 0x61, 0x64, 0x6A, 0x61, 0x63, 0x65, 0x6E, 0x63, 0x79,
68 0x8D, 0x03, 0x3A, 0x2F, 0x2F, 0x8C, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 0x84, 0x26, 0x07, 0x15, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x03, 0x65, 0x64, 0x75, 0x08,
70 0x09, 0x61, 0x64, 0x6A, 0x61, 0x63, 0x65, 0x6E, 0x63, 0x79, 0x8D, 0x03, 0x3A, 0x2F, 0x2F,
71 0x8C, 0x08, 0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
72};
73
74BOOST_AUTO_TEST_CASE(Basic)
75{
76 ndn::Name routerName("/ndn/site/router");
77 ndn::Name adjacencyName("/ndn/site/adjacency");
78 auto testTimePoint = ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196014943));
79 uint32_t seqNo = 12;
80
81 // An AdjLsa initialized with ACTIVE adjacencies should copy the adjacencies
82 AdjacencyList activeAdjacencies;
83 Adjacent activeAdjacency(adjacencyName);
84 activeAdjacency.setStatus(Adjacent::STATUS_ACTIVE);
85 activeAdjacencies.insert(activeAdjacency);
86
Junxiao Shib8752932024-01-07 15:18:46 +000087 AdjLsa alsa1(routerName, seqNo, testTimePoint, activeAdjacencies);
Junxiao Shi20377642023-08-24 23:55:40 +000088 BOOST_CHECK_EQUAL(alsa1.getAdl().size(), 1);
89 BOOST_CHECK_EQUAL(alsa1.getType(), Lsa::Type::ADJACENCY);
90 BOOST_CHECK_EQUAL(alsa1.getSeqNo(), seqNo);
91 BOOST_CHECK_EQUAL(alsa1.getExpirationTimePoint(), testTimePoint);
Junxiao Shi20377642023-08-24 23:55:40 +000092 BOOST_CHECK(alsa1.getAdl().isNeighbor(activeAdjacency.getName()));
93
94 // An AdjLsa initialized with INACTIVE adjacencies should not copy the adjacencies
95 AdjacencyList inactiveAdjacencies;
96 Adjacent inactiveAdjacency(adjacencyName);
97 inactiveAdjacency.setStatus(Adjacent::STATUS_INACTIVE);
98 inactiveAdjacencies.insert(inactiveAdjacency);
99
Junxiao Shib8752932024-01-07 15:18:46 +0000100 AdjLsa alsa2(routerName, seqNo, testTimePoint, inactiveAdjacencies);
Junxiao Shi20377642023-08-24 23:55:40 +0000101 BOOST_CHECK_EQUAL(alsa2.getAdl().size(), 0);
102
103 // Thus, the two LSAs should not have equal content
Junxiao Shib8752932024-01-07 15:18:46 +0000104 BOOST_CHECK_NE(alsa1, alsa2);
Junxiao Shi20377642023-08-24 23:55:40 +0000105
106 // Create a duplicate of alsa1 which should have equal content
Junxiao Shib8752932024-01-07 15:18:46 +0000107 AdjLsa alsa3(routerName, seqNo, testTimePoint, activeAdjacencies);
108 BOOST_CHECK_EQUAL(alsa1, alsa3);
Junxiao Shi20377642023-08-24 23:55:40 +0000109
110 auto wire = alsa1.wireEncode();
111 BOOST_TEST(wire == ADJ_LSA1, boost::test_tools::per_element());
112
113 Adjacent activeAdjacency2("/ndn/edu/adjacency");
114 activeAdjacency2.setStatus(Adjacent::STATUS_ACTIVE);
115 alsa1.addAdjacent(activeAdjacency2);
116 wire = alsa1.wireEncode();
117 BOOST_TEST(wire == ADJ_LSA_EXTRA_NEIGHBOR, boost::test_tools::per_element());
118
119 alsa1.setSeqNo(14);
120 wire = alsa1.wireEncode();
121 BOOST_TEST(wire == ADJ_LSA_DIFF_SEQ, boost::test_tools::per_element());
122
123 testTimePoint = ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196024993));
124 alsa1.setExpirationTimePoint(testTimePoint);
125 wire = alsa1.wireEncode();
126 BOOST_TEST(wire == ADJ_LSA_DIFF_TS, boost::test_tools::per_element());
127}
128
129BOOST_AUTO_TEST_CASE(IncrementAdjacentNumber)
130{
131 Adjacent adj1("adjacent1");
132 Adjacent adj2("adjacent2");
133
134 adj1.setStatus(Adjacent::STATUS_ACTIVE);
135 adj2.setStatus(Adjacent::STATUS_ACTIVE);
136
137 AdjacencyList adjList;
138 adjList.insert(adj1);
139 adjList.insert(adj2);
140
141 auto testTimePoint = ndn::time::system_clock::now() + ndn::time::seconds(3600);
142
Junxiao Shib8752932024-01-07 15:18:46 +0000143 AdjLsa lsa("router1", 12, testTimePoint, adjList);
Junxiao Shi20377642023-08-24 23:55:40 +0000144
145 std::ostringstream os;
146 os << lsa;
147
148 std::string EXPECTED_OUTPUT =
149 " ADJACENCY LSA:\n"
150 " Origin Router : /router1\n"
151 " Sequence Number : 12\n"
152 " Expires in : 3599999 milliseconds\n"
153 " Adjacent(s):\n"
154 " Adjacent 0: (name=/adjacent1, uri=://, cost=10)\n"
155 " Adjacent 1: (name=/adjacent2, uri=://, cost=10)\n";
156
157 BOOST_CHECK_EQUAL(os.str(), EXPECTED_OUTPUT);
158}
159
160BOOST_AUTO_TEST_CASE(InitializeFromContent)
161{
162 Adjacent adj1("adjacent1");
163 Adjacent adj2("adjacent2");
164
165 adj1.setStatus(Adjacent::STATUS_ACTIVE);
166 adj2.setStatus(Adjacent::STATUS_ACTIVE);
167
168 //If we don't do this the test will fail
169 //Adjacent has default cost of 10 but no default
170 //connecting face URI, so initializeFromContent fails
171 adj1.setFaceUri(ndn::FaceUri("udp://10.0.0.1"));
172 adj2.setFaceUri(ndn::FaceUri("udp://10.0.0.2"));
173
174 AdjacencyList adjList;
175 adjList.insert(adj1);
176 adjList.insert(adj2);
177
178 auto testTimePoint = ndn::time::system_clock::now();
179
Junxiao Shib8752932024-01-07 15:18:46 +0000180 AdjLsa adjlsa1("router1", 1, testTimePoint, adjList);
Junxiao Shi20377642023-08-24 23:55:40 +0000181 AdjLsa adjlsa2(adjlsa1.wireEncode());
Junxiao Shib8752932024-01-07 15:18:46 +0000182 BOOST_CHECK_EQUAL(adjlsa1, adjlsa2);
Junxiao Shi20377642023-08-24 23:55:40 +0000183}
184
185BOOST_AUTO_TEST_SUITE_END()
186
Davide Pesavento288141a2024-02-13 17:30:35 -0500187} // namespace nlsr::tests