blob: f8bf855748cf70df3ca3ae5b6f3d2141214322d4 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -05002/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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/>.
akmhoque3d06e792014-05-27 16:23:20 -050019 **/
Nick Gordonf14ec352017-07-24 16:09:58 -050020
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050021#include "name-prefix-list.hpp"
22#include <boost/test/unit_test.hpp>
23
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
41 npl1.remove(b);
42
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};
56 NamePrefixList list2{name1, name2, name3};
Nick Gordon56d1fae2017-05-26 16:39:25 -050057
58 BOOST_CHECK_EQUAL(list1, list2);
59}
60
Nick Gordonf14ec352017-07-24 16:09:58 -050061/*
62 The NamePrefixList will provide a container of all the names it has,
63 without the sources for those names.
64 */
65BOOST_AUTO_TEST_CASE(GetNames)
66{
Nick Gordonf14ec352017-07-24 16:09:58 -050067 const ndn::Name name1{"/ndn/test/prefix1"};
68 const ndn::Name name2{"/ndn/test/prefix2"};
69 const ndn::Name name3{"/ndn/test/prefix3"};
Nick Gordon96861ca2017-10-17 18:25:21 -050070 NamePrefixList list{name1, name2, name3};
Nick Gordonf14ec352017-07-24 16:09:58 -050071
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 */
90BOOST_AUTO_TEST_CASE(countSources)
91{
Nick Gordonf14ec352017-07-24 16:09:58 -050092 const ndn::Name name1{"/ndn/test/prefix1"};
93 const ndn::Name invalidName{"/not/a/prefix"};
Nick Gordon96861ca2017-10-17 18:25:21 -050094 NamePrefixList list;
Nick Gordonf14ec352017-07-24 16:09:58 -050095 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 */
107BOOST_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 */
136BOOST_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 Gordon96861ca2017-10-17 18:25:21 -0500158BOOST_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 Gawandeeb582eb2014-05-01 14:25:20 -0500192BOOST_AUTO_TEST_SUITE_END()
193
Nick Gordonfad8e252016-08-11 14:21:38 -0500194} // namespace test
195} // namespace nlsr