akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2014-2023, The University of Memphis, |
Nick Gordon | f8b5bcd | 2016-08-11 15:06:50 -0500 | [diff] [blame] | 4 | * Regents of the University of California |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 5 | * |
| 6 | * This file is part of NLSR (Named-data Link State Routing). |
| 7 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 8 | * |
| 9 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 10 | * of the GNU General Public License as published by the Free Software Foundation, |
| 11 | * either version 3 of the License, or (at your option) any later version. |
| 12 | * |
| 13 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 14 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 19 | */ |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 20 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 21 | #include "name-prefix-list.hpp" |
Davide Pesavento | cb065f1 | 2019-12-27 01:03:34 -0500 | [diff] [blame] | 22 | #include "tests/boost-test.hpp" |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 23 | |
| 24 | namespace nlsr { |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 25 | namespace test { |
| 26 | |
| 27 | BOOST_AUTO_TEST_SUITE(TestNpl) |
| 28 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 29 | /* |
| 30 | The NamePrefixList can have names inserted and removed from it. |
| 31 | */ |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 32 | BOOST_AUTO_TEST_CASE(NplSizeAndRemove) |
| 33 | { |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 34 | ndn::Name a{"testname"}; |
| 35 | ndn::Name b{"name"}; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 36 | |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 37 | NamePrefixList npl1{a, b}; |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 38 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 39 | BOOST_CHECK_EQUAL(npl1.size(), 2); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 40 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 41 | npl1.erase(b); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 42 | |
Nick Gordon | ff9a627 | 2017-10-12 13:38:29 -0500 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(npl1.size(), 1); |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 44 | } |
| 45 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 46 | /* |
| 47 | Two NamePrefixLists will be considered equal if they contain the |
| 48 | same names. Sources for names are ignored. |
| 49 | */ |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 50 | BOOST_AUTO_TEST_CASE(OperatorEquals) |
| 51 | { |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 52 | ndn::Name name1("/ndn/test/name1"); |
| 53 | ndn::Name name2("/ndn/test/name2"); |
| 54 | ndn::Name name3("/ndn/some/other/name1"); |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 55 | NamePrefixList list1{name1, name2, name3}; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 56 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 57 | NamePrefixList list2; |
| 58 | BOOST_CHECK_NE(list1, list2); |
| 59 | |
| 60 | list2.insert(name1); |
| 61 | list2.insert(name1, "A1"); |
| 62 | list2.insert(name2, "B0"); |
| 63 | list2.insert(name2, "B1"); |
| 64 | list2.insert(name3, "C0"); |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(list1, list2); |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 66 | |
| 67 | list2.erase(name3, "C0"); |
| 68 | BOOST_CHECK_NE(list1, list2); |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 71 | /* |
| 72 | The NamePrefixList will provide a container of all the names it has, |
| 73 | without the sources for those names. |
| 74 | */ |
| 75 | BOOST_AUTO_TEST_CASE(GetNames) |
| 76 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 77 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 78 | const ndn::Name name2{"/ndn/test/prefix2"}; |
| 79 | const ndn::Name name3{"/ndn/test/prefix3"}; |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 80 | NamePrefixList list{name1, name2, name3}; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 81 | |
| 82 | std::vector<ndn::Name> referenceNames{name1, name2, name3}; |
| 83 | |
| 84 | auto names = list.getNames(); |
| 85 | BOOST_REQUIRE_EQUAL(names.size(), 3); |
| 86 | // Verify that all of the names are in the list. |
| 87 | for (const auto& name : names) { |
| 88 | bool didMatch = false; |
| 89 | for (const auto& referenceName : referenceNames) { |
| 90 | didMatch = didMatch || (name == referenceName); |
| 91 | } |
| 92 | BOOST_CHECK(didMatch); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | The NamePrefixList will count the number of sources for a given |
| 98 | name, with zero for a non-existent name. |
| 99 | */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 100 | BOOST_AUTO_TEST_CASE(CountSources) |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 101 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 102 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 103 | const ndn::Name invalidName{"/not/a/prefix"}; |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 104 | NamePrefixList list; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 105 | list.insert(name1, "nlsr.conf"); |
| 106 | list.insert(name1, "readvertise"); |
| 107 | list.insert(name1, "prefix-update"); |
| 108 | |
| 109 | BOOST_CHECK_EQUAL(list.countSources(name1), 3); |
| 110 | BOOST_CHECK_EQUAL(list.countSources(invalidName), 0); |
| 111 | } |
| 112 | |
| 113 | /* |
| 114 | The NamePrefixList will return a container with all the sources for |
| 115 | a given name, with an empty container for a non-existent name. |
| 116 | */ |
Davide Pesavento | c1d0e8e | 2022-06-15 14:26:02 -0400 | [diff] [blame] | 117 | BOOST_AUTO_TEST_CASE(GetSources) |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 118 | { |
| 119 | NamePrefixList list; |
| 120 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 121 | const ndn::Name invalidName{"/not/a/prefix"}; |
| 122 | |
| 123 | list.insert(name1, "nlsr.conf"); |
| 124 | list.insert(name1, "readvertise"); |
| 125 | list.insert(name1, "prefix-update"); |
| 126 | std::vector<std::string> referenceSources{"nlsr.conf", "readvertise", "prefix-update"}; |
| 127 | |
| 128 | const std::vector<std::string> sources = list.getSources(name1); |
| 129 | BOOST_REQUIRE_EQUAL(list.countSources(name1), 3); |
| 130 | for (const auto& source : sources) { |
| 131 | bool didMatch = false; |
| 132 | for (const auto& referenceSource : referenceSources) { |
| 133 | didMatch = didMatch || (source == referenceSource); |
| 134 | } |
| 135 | BOOST_CHECK(didMatch); |
| 136 | } |
| 137 | |
| 138 | std::vector<std::string> noSources = list.getSources(invalidName); |
| 139 | BOOST_REQUIRE_EQUAL(noSources.size(), 0); |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | The NamePrefixList will not delete a name as long as it at least one |
| 144 | source. |
| 145 | */ |
| 146 | BOOST_AUTO_TEST_CASE(RemainingSourcesAfterRemoval) |
| 147 | { |
| 148 | NamePrefixList list; |
| 149 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 150 | list.insert(name1, "nlsr.conf"); |
| 151 | list.insert(name1, "readvertise"); |
| 152 | list.insert(name1, "prefix-update"); |
| 153 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 154 | list.erase(name1, "prefix-update"); |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 155 | |
| 156 | std::vector<std::string> referenceSources{"nlsr.conf", "readvertise", "prefix-update"}; |
| 157 | const std::vector<std::string> sources = list.getSources(name1); |
| 158 | BOOST_REQUIRE_EQUAL(list.countSources(name1), 2); |
| 159 | for (const auto& source : sources) { |
| 160 | bool didMatch = false; |
| 161 | for (const auto& referenceSource : referenceSources) { |
| 162 | didMatch = didMatch || (source == referenceSource); |
| 163 | } |
| 164 | BOOST_CHECK(didMatch); |
| 165 | } |
| 166 | } |
| 167 | |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 168 | BOOST_AUTO_TEST_CASE(BraceInitializerCtors) |
| 169 | { |
| 170 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 171 | const ndn::Name name2{"/ndn/test/prefix2"}; |
| 172 | const ndn::Name name3{"/ndn/test/prefix3"}; |
| 173 | std::list<ndn::Name> testList{name1, name2, name3}; |
| 174 | |
Junxiao Shi | 52f1664 | 2023-08-15 00:18:35 +0000 | [diff] [blame^] | 175 | const std::vector<std::string> sources1{"readvertise", "static"}; |
| 176 | const std::vector<std::string> sources2{"nlsrc", "static"}; |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 177 | const std::vector<std::string> sources3{"static"}; |
| 178 | |
| 179 | NamePrefixList list1{name1, name2, name3}; |
| 180 | auto list = list1.getNames(); |
| 181 | BOOST_CHECK_EQUAL(list1.size(), 3); |
| 182 | BOOST_CHECK(testList == list); |
| 183 | |
| 184 | NamePrefixList list2{ NamePrefixList::NamePair{name1, sources1}, |
| 185 | NamePrefixList::NamePair{name2, sources2}, NamePrefixList::NamePair{name3, sources3} }; |
| 186 | auto name1Sources = list2.getSources(name1); |
| 187 | BOOST_CHECK(sources1 == name1Sources); |
| 188 | auto name2Sources = list2.getSources(name2); |
| 189 | BOOST_CHECK(sources2 == name2Sources); |
| 190 | auto name3Sources = list2.getSources(name3); |
| 191 | BOOST_CHECK(sources3 == name3Sources); |
| 192 | |
| 193 | const std::vector<ndn::Name> namesVector{name1, name2, name3}; |
| 194 | NamePrefixList list3(namesVector); |
| 195 | BOOST_CHECK(list1 == list3); |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 198 | BOOST_AUTO_TEST_SUITE_END() |
| 199 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 200 | } // namespace test |
| 201 | } // namespace nlsr |