blob: 6fdfd175b5c778783b6594c1e11660f4fe39825e [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoc1d0e8e2022-06-15 14:26:02 -04002/*
Junxiao Shi52f16642023-08-15 00:18:35 +00003 * Copyright (c) 2014-2023, The University of Memphis,
Nick Gordonf8b5bcd2016-08-11 15:06:50 -05004 * Regents of the University of California
akmhoque3d06e792014-05-27 16:23:20 -05005 *
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 Pesaventoc1d0e8e2022-06-15 14:26:02 -040019 */
Nick Gordonf14ec352017-07-24 16:09:58 -050020
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050021#include "name-prefix-list.hpp"
Davide Pesaventocb065f12019-12-27 01:03:34 -050022#include "tests/boost-test.hpp"
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050023
24namespace nlsr {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025namespace test {
26
27BOOST_AUTO_TEST_SUITE(TestNpl)
28
Nick Gordonf14ec352017-07-24 16:09:58 -050029/*
30 The NamePrefixList can have names inserted and removed from it.
31 */
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050032BOOST_AUTO_TEST_CASE(NplSizeAndRemove)
33{
Nick Gordon96861ca2017-10-17 18:25:21 -050034 ndn::Name a{"testname"};
35 ndn::Name b{"name"};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050036
Nick Gordon96861ca2017-10-17 18:25:21 -050037 NamePrefixList npl1{a, b};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050038
Nick Gordonff9a6272017-10-12 13:38:29 -050039 BOOST_CHECK_EQUAL(npl1.size(), 2);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050040
Junxiao Shi52f16642023-08-15 00:18:35 +000041 npl1.erase(b);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050042
Nick Gordonff9a6272017-10-12 13:38:29 -050043 BOOST_CHECK_EQUAL(npl1.size(), 1);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050044}
45
Nick Gordonf14ec352017-07-24 16:09:58 -050046/*
47 Two NamePrefixLists will be considered equal if they contain the
48 same names. Sources for names are ignored.
49 */
Nick Gordon56d1fae2017-05-26 16:39:25 -050050BOOST_AUTO_TEST_CASE(OperatorEquals)
51{
Nick Gordon56d1fae2017-05-26 16:39:25 -050052 ndn::Name name1("/ndn/test/name1");
53 ndn::Name name2("/ndn/test/name2");
54 ndn::Name name3("/ndn/some/other/name1");
Nick Gordon96861ca2017-10-17 18:25:21 -050055 NamePrefixList list1{name1, name2, name3};
Nick Gordon56d1fae2017-05-26 16:39:25 -050056
Junxiao Shi52f16642023-08-15 00:18:35 +000057 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 Gordon56d1fae2017-05-26 16:39:25 -050065 BOOST_CHECK_EQUAL(list1, list2);
Junxiao Shi52f16642023-08-15 00:18:35 +000066
67 list2.erase(name3, "C0");
68 BOOST_CHECK_NE(list1, list2);
Nick Gordon56d1fae2017-05-26 16:39:25 -050069}
70
Nick Gordonf14ec352017-07-24 16:09:58 -050071/*
72 The NamePrefixList will provide a container of all the names it has,
73 without the sources for those names.
74 */
75BOOST_AUTO_TEST_CASE(GetNames)
76{
Nick Gordonf14ec352017-07-24 16:09:58 -050077 const ndn::Name name1{"/ndn/test/prefix1"};
78 const ndn::Name name2{"/ndn/test/prefix2"};
79 const ndn::Name name3{"/ndn/test/prefix3"};
Nick Gordon96861ca2017-10-17 18:25:21 -050080 NamePrefixList list{name1, name2, name3};
Nick Gordonf14ec352017-07-24 16:09:58 -050081
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 Pesaventoc1d0e8e2022-06-15 14:26:02 -0400100BOOST_AUTO_TEST_CASE(CountSources)
Nick Gordonf14ec352017-07-24 16:09:58 -0500101{
Nick Gordonf14ec352017-07-24 16:09:58 -0500102 const ndn::Name name1{"/ndn/test/prefix1"};
103 const ndn::Name invalidName{"/not/a/prefix"};
Nick Gordon96861ca2017-10-17 18:25:21 -0500104 NamePrefixList list;
Nick Gordonf14ec352017-07-24 16:09:58 -0500105 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 Pesaventoc1d0e8e2022-06-15 14:26:02 -0400117BOOST_AUTO_TEST_CASE(GetSources)
Nick Gordonf14ec352017-07-24 16:09:58 -0500118{
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 */
146BOOST_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 Shi52f16642023-08-15 00:18:35 +0000154 list.erase(name1, "prefix-update");
Nick Gordonf14ec352017-07-24 16:09:58 -0500155
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 Gordon96861ca2017-10-17 18:25:21 -0500168BOOST_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 Shi52f16642023-08-15 00:18:35 +0000175 const std::vector<std::string> sources1{"readvertise", "static"};
176 const std::vector<std::string> sources2{"nlsrc", "static"};
Nick Gordon96861ca2017-10-17 18:25:21 -0500177 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 Gordon96861ca2017-10-17 18:25:21 -0500196}
197
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500198BOOST_AUTO_TEST_SUITE_END()
199
Nick Gordonfad8e252016-08-11 14:21:38 -0500200} // namespace test
201} // namespace nlsr