akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 2 | /** |
Ashlesh Gawande | 0db4d4d | 2020-02-05 20:30:02 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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/>. |
akmhoque | 3d06e79 | 2014-05-27 16:23:20 -0500 | [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 | |
| 41 | npl1.remove(b); |
| 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}; |
| 56 | NamePrefixList list2{name1, name2, name3}; |
Nick Gordon | 56d1fae | 2017-05-26 16:39:25 -0500 | [diff] [blame] | 57 | |
| 58 | BOOST_CHECK_EQUAL(list1, list2); |
| 59 | } |
| 60 | |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 61 | /* |
| 62 | The NamePrefixList will provide a container of all the names it has, |
| 63 | without the sources for those names. |
| 64 | */ |
| 65 | BOOST_AUTO_TEST_CASE(GetNames) |
| 66 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 67 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 68 | const ndn::Name name2{"/ndn/test/prefix2"}; |
| 69 | const ndn::Name name3{"/ndn/test/prefix3"}; |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 70 | NamePrefixList list{name1, name2, name3}; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 71 | |
| 72 | std::vector<ndn::Name> referenceNames{name1, name2, name3}; |
| 73 | |
| 74 | auto names = list.getNames(); |
| 75 | BOOST_REQUIRE_EQUAL(names.size(), 3); |
| 76 | // Verify that all of the names are in the list. |
| 77 | for (const auto& name : names) { |
| 78 | bool didMatch = false; |
| 79 | for (const auto& referenceName : referenceNames) { |
| 80 | didMatch = didMatch || (name == referenceName); |
| 81 | } |
| 82 | BOOST_CHECK(didMatch); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | The NamePrefixList will count the number of sources for a given |
| 88 | name, with zero for a non-existent name. |
| 89 | */ |
| 90 | BOOST_AUTO_TEST_CASE(countSources) |
| 91 | { |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 92 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 93 | const ndn::Name invalidName{"/not/a/prefix"}; |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 94 | NamePrefixList list; |
Nick Gordon | f14ec35 | 2017-07-24 16:09:58 -0500 | [diff] [blame] | 95 | list.insert(name1, "nlsr.conf"); |
| 96 | list.insert(name1, "readvertise"); |
| 97 | list.insert(name1, "prefix-update"); |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(list.countSources(name1), 3); |
| 100 | BOOST_CHECK_EQUAL(list.countSources(invalidName), 0); |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | The NamePrefixList will return a container with all the sources for |
| 105 | a given name, with an empty container for a non-existent name. |
| 106 | */ |
| 107 | BOOST_AUTO_TEST_CASE(getSources) |
| 108 | { |
| 109 | NamePrefixList list; |
| 110 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 111 | const ndn::Name invalidName{"/not/a/prefix"}; |
| 112 | |
| 113 | list.insert(name1, "nlsr.conf"); |
| 114 | list.insert(name1, "readvertise"); |
| 115 | list.insert(name1, "prefix-update"); |
| 116 | std::vector<std::string> referenceSources{"nlsr.conf", "readvertise", "prefix-update"}; |
| 117 | |
| 118 | const std::vector<std::string> sources = list.getSources(name1); |
| 119 | BOOST_REQUIRE_EQUAL(list.countSources(name1), 3); |
| 120 | for (const auto& source : sources) { |
| 121 | bool didMatch = false; |
| 122 | for (const auto& referenceSource : referenceSources) { |
| 123 | didMatch = didMatch || (source == referenceSource); |
| 124 | } |
| 125 | BOOST_CHECK(didMatch); |
| 126 | } |
| 127 | |
| 128 | std::vector<std::string> noSources = list.getSources(invalidName); |
| 129 | BOOST_REQUIRE_EQUAL(noSources.size(), 0); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | The NamePrefixList will not delete a name as long as it at least one |
| 134 | source. |
| 135 | */ |
| 136 | BOOST_AUTO_TEST_CASE(RemainingSourcesAfterRemoval) |
| 137 | { |
| 138 | NamePrefixList list; |
| 139 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 140 | list.insert(name1, "nlsr.conf"); |
| 141 | list.insert(name1, "readvertise"); |
| 142 | list.insert(name1, "prefix-update"); |
| 143 | |
| 144 | list.remove(name1, "prefix-update"); |
| 145 | |
| 146 | std::vector<std::string> referenceSources{"nlsr.conf", "readvertise", "prefix-update"}; |
| 147 | const std::vector<std::string> sources = list.getSources(name1); |
| 148 | BOOST_REQUIRE_EQUAL(list.countSources(name1), 2); |
| 149 | for (const auto& source : sources) { |
| 150 | bool didMatch = false; |
| 151 | for (const auto& referenceSource : referenceSources) { |
| 152 | didMatch = didMatch || (source == referenceSource); |
| 153 | } |
| 154 | BOOST_CHECK(didMatch); |
| 155 | } |
| 156 | } |
| 157 | |
Nick Gordon | 96861ca | 2017-10-17 18:25:21 -0500 | [diff] [blame] | 158 | BOOST_AUTO_TEST_CASE(BraceInitializerCtors) |
| 159 | { |
| 160 | const ndn::Name name1{"/ndn/test/prefix1"}; |
| 161 | const ndn::Name name2{"/ndn/test/prefix2"}; |
| 162 | const ndn::Name name3{"/ndn/test/prefix3"}; |
| 163 | std::list<ndn::Name> testList{name1, name2, name3}; |
| 164 | |
| 165 | const std::vector<std::string> sources1{"static", "readvertise"}; |
| 166 | const std::vector<std::string> sources2{"static", "nlsrc"}; |
| 167 | const std::vector<std::string> sources3{"static"}; |
| 168 | |
| 169 | NamePrefixList list1{name1, name2, name3}; |
| 170 | auto list = list1.getNames(); |
| 171 | BOOST_CHECK_EQUAL(list1.size(), 3); |
| 172 | BOOST_CHECK(testList == list); |
| 173 | |
| 174 | NamePrefixList list2{ NamePrefixList::NamePair{name1, sources1}, |
| 175 | NamePrefixList::NamePair{name2, sources2}, NamePrefixList::NamePair{name3, sources3} }; |
| 176 | auto name1Sources = list2.getSources(name1); |
| 177 | BOOST_CHECK(sources1 == name1Sources); |
| 178 | auto name2Sources = list2.getSources(name2); |
| 179 | BOOST_CHECK(sources2 == name2Sources); |
| 180 | auto name3Sources = list2.getSources(name3); |
| 181 | BOOST_CHECK(sources3 == name3Sources); |
| 182 | |
| 183 | const std::vector<ndn::Name> namesVector{name1, name2, name3}; |
| 184 | NamePrefixList list3(namesVector); |
| 185 | BOOST_CHECK(list1 == list3); |
| 186 | |
| 187 | const std::list<ndn::Name> namesList{name1, name2, name3}; |
| 188 | NamePrefixList list4(namesList); |
| 189 | BOOST_CHECK(list1 == list4); |
| 190 | } |
| 191 | |
Ashlesh Gawande | eb582eb | 2014-05-01 14:25:20 -0500 | [diff] [blame] | 192 | BOOST_AUTO_TEST_SUITE_END() |
| 193 | |
Nick Gordon | fad8e25 | 2016-08-11 14:21:38 -0500 | [diff] [blame] | 194 | } // namespace test |
| 195 | } // namespace nlsr |