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/name-lsa.hpp" |
| 23 | |
| 24 | #include "tests/boost-test.hpp" |
| 25 | |
| 26 | namespace nlsr::test { |
| 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(TestNameLsa) |
| 29 | |
| 30 | const uint8_t NAME_LSA1[] = { |
| 31 | 0x89, 0x37, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, |
| 32 | 0x82, 0x01, 0x0C, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, |
| 33 | 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, |
| 34 | 0x6D, 0x65, 0x31, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32 |
| 35 | }; |
| 36 | |
| 37 | const uint8_t NAME_LSA_EXTRA_NAME[] = { |
| 38 | 0x89, 0x40, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, |
| 39 | 0x82, 0x01, 0x0C, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, |
| 40 | 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, |
| 41 | 0x6D, 0x65, 0x31, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, 0x07, 0x07, 0x08, |
| 42 | 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33 |
| 43 | }; |
| 44 | |
| 45 | const uint8_t NAME_LSA_DIFF_SEQ[] = { |
| 46 | 0x89, 0x40, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, |
| 47 | 0x82, 0x01, 0x0E, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, |
| 48 | 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x33, 0x34, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, |
| 49 | 0x6D, 0x65, 0x31, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, 0x07, 0x07, 0x08, |
| 50 | 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33 |
| 51 | }; |
| 52 | |
| 53 | const uint8_t NAME_LSA_DIFF_TS[] = { |
| 54 | 0x89, 0x40, 0x80, 0x23, 0x07, 0x09, 0x08, 0x07, 0x72, 0x6F, 0x75, 0x74, 0x65, 0x72, 0x31, |
| 55 | 0x82, 0x01, 0x0E, 0x8B, 0x13, 0x32, 0x30, 0x32, 0x30, 0x2D, 0x30, 0x33, 0x2D, 0x32, 0x36, |
| 56 | 0x20, 0x30, 0x34, 0x3A, 0x31, 0x33, 0x3A, 0x34, 0x34, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, |
| 57 | 0x6D, 0x65, 0x31, 0x07, 0x07, 0x08, 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x32, 0x07, 0x07, 0x08, |
| 58 | 0x05, 0x6E, 0x61, 0x6D, 0x65, 0x33 |
| 59 | }; |
| 60 | |
| 61 | BOOST_AUTO_TEST_CASE(Basic) |
| 62 | { |
| 63 | ndn::Name s1{"name1"}; |
| 64 | ndn::Name s2{"name2"}; |
| 65 | NamePrefixList npl1{s1, s2}; |
| 66 | |
| 67 | auto testTimePoint = ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196014943)); |
| 68 | |
| 69 | // 3rd argument is seqNo |
| 70 | NameLsa nlsa1("router1", 12, testTimePoint, npl1); |
| 71 | NameLsa nlsa2("router2", 12, testTimePoint, npl1); |
| 72 | |
| 73 | BOOST_CHECK_EQUAL(nlsa1.getType(), Lsa::Type::NAME); |
| 74 | BOOST_CHECK(nlsa1.getExpirationTimePoint() == nlsa2.getExpirationTimePoint()); |
| 75 | |
| 76 | auto wire = nlsa1.wireEncode(); |
| 77 | BOOST_TEST(wire == NAME_LSA1, boost::test_tools::per_element()); |
| 78 | |
| 79 | nlsa1.addName("name3"); |
| 80 | wire = nlsa1.wireEncode(); |
| 81 | BOOST_TEST(wire == NAME_LSA_EXTRA_NAME, boost::test_tools::per_element()); |
| 82 | |
| 83 | nlsa1.setSeqNo(14); |
| 84 | wire = nlsa1.wireEncode(); |
| 85 | BOOST_TEST(wire == NAME_LSA_DIFF_SEQ, boost::test_tools::per_element()); |
| 86 | |
| 87 | testTimePoint = |
| 88 | ndn::time::fromUnixTimestamp(ndn::time::milliseconds(1585196024993)); |
| 89 | nlsa1.setExpirationTimePoint(testTimePoint); |
| 90 | wire = nlsa1.wireEncode(); |
| 91 | BOOST_TEST(wire == NAME_LSA_DIFF_TS, boost::test_tools::per_element()); |
| 92 | |
| 93 | // Not testing router name as not sure if that will ever change once set |
| 94 | } |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE(InitializeFromContent) |
| 97 | { |
| 98 | auto testTimePoint = ndn::time::system_clock::now(); |
| 99 | ndn::Name s1{"name1"}; |
| 100 | ndn::Name s2{"name2"}; |
| 101 | NamePrefixList npl1{s1, s2}; |
| 102 | |
| 103 | NameLsa nlsa1("router1", 1, testTimePoint, npl1); |
| 104 | NameLsa nlsa2(nlsa1.wireEncode()); |
| 105 | BOOST_CHECK_EQUAL(nlsa1.wireEncode(), nlsa2.wireEncode()); |
| 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_CASE(OperatorEquals) |
| 109 | { |
| 110 | NameLsa lsa1; |
| 111 | NameLsa lsa2; |
| 112 | ndn::Name name1("/ndn/test/name1"); |
| 113 | ndn::Name name2("/ndn/test/name2"); |
| 114 | ndn::Name name3("/ndn/some/other/name1"); |
| 115 | |
| 116 | lsa1.addName(name1); |
| 117 | lsa1.addName(name2); |
| 118 | lsa1.addName(name3); |
| 119 | |
| 120 | lsa2.addName(name1); |
| 121 | lsa2.addName(name2); |
| 122 | lsa2.addName(name3); |
| 123 | |
| 124 | BOOST_CHECK(lsa1.isEqualContent(lsa2)); |
| 125 | } |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(Update) |
| 128 | { |
| 129 | NameLsa knownNameLsa; |
| 130 | knownNameLsa.m_originRouter = ndn::Name("/yoursunny/_/%C1.Router/dal"); |
| 131 | knownNameLsa.m_seqNo = 2683; |
| 132 | knownNameLsa.setExpirationTimePoint(ndn::time::system_clock::now() + 3561_ms); |
| 133 | knownNameLsa.addName("/yoursunny/_/dal"); |
| 134 | knownNameLsa.addName("/ndn"); |
| 135 | |
| 136 | auto rcvdLsa = std::make_shared<NameLsa>(); |
| 137 | rcvdLsa->m_originRouter = ndn::Name("/yoursunny/_/%C1.Router/dal"); |
| 138 | rcvdLsa->m_seqNo = 2684; |
| 139 | rcvdLsa->setExpirationTimePoint(ndn::time::system_clock::now() + 3600_ms); |
| 140 | |
| 141 | auto nlsa = std::static_pointer_cast<NameLsa>(rcvdLsa); |
| 142 | nlsa->addName("/ndn"); |
| 143 | nlsa->addName("/yoursunny/_/dal"); |
| 144 | ndn::Name addedName1("/yoursunny/video/ndn-dpdk_acmicn20_20200917"); |
| 145 | ndn::Name addedName2("/yoursunny/pushups"); |
| 146 | nlsa->addName(addedName1); |
| 147 | nlsa->addName(addedName2); |
| 148 | |
| 149 | auto [updated, namesToAdd, namesToRemove] = knownNameLsa.update(rcvdLsa); |
| 150 | |
| 151 | BOOST_CHECK_EQUAL(updated, true); |
| 152 | BOOST_CHECK_EQUAL(namesToAdd.size(), 2); |
| 153 | BOOST_CHECK_EQUAL(namesToRemove.size(), 0); |
| 154 | auto it = std::find(namesToAdd.begin(), namesToAdd.end(), addedName1); |
| 155 | BOOST_CHECK(it != namesToAdd.end()); |
| 156 | it = std::find(namesToAdd.begin(), namesToAdd.end(), addedName2); |
| 157 | BOOST_CHECK(it != namesToAdd.end()); |
| 158 | } |
| 159 | |
| 160 | BOOST_AUTO_TEST_SUITE_END() |
| 161 | |
| 162 | } // namespace nlsr::test |