Junxiao Shi | 2037764 | 2023-08-24 23:55:40 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2023, 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 "lsa/adj-lsa.hpp" |
| 23 | |
| 24 | #include "tests/boost-test.hpp" |
| 25 | |
| 26 | namespace nlsr::test { |
| 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(TestAdjLsa) |
| 29 | |
| 30 | const 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 | |
| 39 | const 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 | |
| 51 | const 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 | |
| 62 | const 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 | |
| 74 | BOOST_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 | |
| 87 | AdjLsa alsa1(routerName, seqNo, testTimePoint, |
| 88 | activeAdjacencies.size(), activeAdjacencies); |
| 89 | BOOST_CHECK_EQUAL(alsa1.getAdl().size(), 1); |
| 90 | BOOST_CHECK_EQUAL(alsa1.getType(), Lsa::Type::ADJACENCY); |
| 91 | BOOST_CHECK_EQUAL(alsa1.getSeqNo(), seqNo); |
| 92 | BOOST_CHECK_EQUAL(alsa1.getExpirationTimePoint(), testTimePoint); |
| 93 | BOOST_CHECK_EQUAL(alsa1.getNoLink(), 1); |
| 94 | BOOST_CHECK(alsa1.getAdl().isNeighbor(activeAdjacency.getName())); |
| 95 | |
| 96 | // An AdjLsa initialized with INACTIVE adjacencies should not copy the adjacencies |
| 97 | AdjacencyList inactiveAdjacencies; |
| 98 | Adjacent inactiveAdjacency(adjacencyName); |
| 99 | inactiveAdjacency.setStatus(Adjacent::STATUS_INACTIVE); |
| 100 | inactiveAdjacencies.insert(inactiveAdjacency); |
| 101 | |
| 102 | AdjLsa alsa2(routerName, seqNo, testTimePoint, |
| 103 | inactiveAdjacencies.size(), inactiveAdjacencies); |
| 104 | BOOST_CHECK_EQUAL(alsa2.getAdl().size(), 0); |
| 105 | |
| 106 | // Thus, the two LSAs should not have equal content |
| 107 | BOOST_CHECK_EQUAL(alsa1.isEqualContent(alsa2), false); |
| 108 | |
| 109 | // Create a duplicate of alsa1 which should have equal content |
| 110 | AdjLsa alsa3(routerName, seqNo, testTimePoint, |
| 111 | activeAdjacencies.size(), activeAdjacencies); |
| 112 | BOOST_CHECK(alsa1.isEqualContent(alsa3)); |
| 113 | |
| 114 | auto wire = alsa1.wireEncode(); |
| 115 | BOOST_TEST(wire == ADJ_LSA1, boost::test_tools::per_element()); |
| 116 | |
| 117 | Adjacent activeAdjacency2("/ndn/edu/adjacency"); |
| 118 | activeAdjacency2.setStatus(Adjacent::STATUS_ACTIVE); |
| 119 | alsa1.addAdjacent(activeAdjacency2); |
| 120 | wire = alsa1.wireEncode(); |
| 121 | BOOST_TEST(wire == ADJ_LSA_EXTRA_NEIGHBOR, boost::test_tools::per_element()); |
| 122 | |
| 123 | alsa1.setSeqNo(14); |
| 124 | wire = alsa1.wireEncode(); |
| 125 | BOOST_TEST(wire == ADJ_LSA_DIFF_SEQ, boost::test_tools::per_element()); |
| 126 | |
| 127 | testTimePoint = ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196024993)); |
| 128 | alsa1.setExpirationTimePoint(testTimePoint); |
| 129 | wire = alsa1.wireEncode(); |
| 130 | BOOST_TEST(wire == ADJ_LSA_DIFF_TS, boost::test_tools::per_element()); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(IncrementAdjacentNumber) |
| 134 | { |
| 135 | Adjacent adj1("adjacent1"); |
| 136 | Adjacent adj2("adjacent2"); |
| 137 | |
| 138 | adj1.setStatus(Adjacent::STATUS_ACTIVE); |
| 139 | adj2.setStatus(Adjacent::STATUS_ACTIVE); |
| 140 | |
| 141 | AdjacencyList adjList; |
| 142 | adjList.insert(adj1); |
| 143 | adjList.insert(adj2); |
| 144 | |
| 145 | auto testTimePoint = ndn::time::system_clock::now() + ndn::time::seconds(3600); |
| 146 | |
| 147 | AdjLsa lsa("router1", 12, testTimePoint, adjList.size(), adjList); |
| 148 | |
| 149 | std::ostringstream os; |
| 150 | os << lsa; |
| 151 | |
| 152 | std::string EXPECTED_OUTPUT = |
| 153 | " ADJACENCY LSA:\n" |
| 154 | " Origin Router : /router1\n" |
| 155 | " Sequence Number : 12\n" |
| 156 | " Expires in : 3599999 milliseconds\n" |
| 157 | " Adjacent(s):\n" |
| 158 | " Adjacent 0: (name=/adjacent1, uri=://, cost=10)\n" |
| 159 | " Adjacent 1: (name=/adjacent2, uri=://, cost=10)\n"; |
| 160 | |
| 161 | BOOST_CHECK_EQUAL(os.str(), EXPECTED_OUTPUT); |
| 162 | } |
| 163 | |
| 164 | BOOST_AUTO_TEST_CASE(InitializeFromContent) |
| 165 | { |
| 166 | Adjacent adj1("adjacent1"); |
| 167 | Adjacent adj2("adjacent2"); |
| 168 | |
| 169 | adj1.setStatus(Adjacent::STATUS_ACTIVE); |
| 170 | adj2.setStatus(Adjacent::STATUS_ACTIVE); |
| 171 | |
| 172 | //If we don't do this the test will fail |
| 173 | //Adjacent has default cost of 10 but no default |
| 174 | //connecting face URI, so initializeFromContent fails |
| 175 | adj1.setFaceUri(ndn::FaceUri("udp://10.0.0.1")); |
| 176 | adj2.setFaceUri(ndn::FaceUri("udp://10.0.0.2")); |
| 177 | |
| 178 | AdjacencyList adjList; |
| 179 | adjList.insert(adj1); |
| 180 | adjList.insert(adj2); |
| 181 | |
| 182 | auto testTimePoint = ndn::time::system_clock::now(); |
| 183 | |
| 184 | AdjLsa adjlsa1("router1", 1, testTimePoint, adjList.size(), adjList); |
| 185 | AdjLsa adjlsa2(adjlsa1.wireEncode()); |
| 186 | BOOST_CHECK(adjlsa1.isEqualContent(adjlsa2)); |
| 187 | } |
| 188 | |
| 189 | BOOST_AUTO_TEST_SUITE_END() |
| 190 | |
| 191 | } // namespace nlsr::test |