blob: 7fd06cae3a6ed5aa0f7aaaf375464910faaf3758 [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/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento288141a2024-02-13 17:30:35 -050024namespace nlsr::tests {
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050025
26BOOST_AUTO_TEST_SUITE(TestNpl)
27
Nick Gordonf14ec352017-07-24 16:09:58 -050028/*
Junxiao Shi931ca9f2023-08-15 02:59:46 +000029 The NamePrefixList will provide a container of all the names it has,
30 without the sources for those names.
31
32 The NamePrefixList will return a container with all the sources for
33 a given name, with an empty container for a non-existent name.
Nick Gordonf14ec352017-07-24 16:09:58 -050034 */
Junxiao Shi931ca9f2023-08-15 02:59:46 +000035BOOST_AUTO_TEST_CASE(Ctor_Insert_Size_GetNames_GetSources)
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050036{
Junxiao Shi931ca9f2023-08-15 02:59:46 +000037 ndn::Name name1{"/ndn/test/prefix1"};
38 ndn::Name name2{"/ndn/test/prefix2"};
39 ndn::Name name3{"/ndn/test/prefix3"};
40 std::list<ndn::Name> expectedNames{name1, name2, name3};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050041
Junxiao Shi931ca9f2023-08-15 02:59:46 +000042 NamePrefixList list1{name1, name2, name3};
43 BOOST_CHECK_EQUAL(list1.size(), 3);
44 BOOST_TEST(list1.getNames() == expectedNames, boost::test_tools::per_element());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050045
Junxiao Shi931ca9f2023-08-15 02:59:46 +000046 std::vector<std::string> sources1{"static", "readvertise"};
47 std::vector<std::string> sources2{"static", "nlsrc"};
48 std::vector<std::string> sources3{"static"};
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050049
Junxiao Shi931ca9f2023-08-15 02:59:46 +000050 NamePrefixList list2;
51 auto list2InsertNameSources = [&] (const ndn::Name& name, ndn::span<const std::string> sources) {
52 for (const auto& source : sources) {
53 list2.insert(name, source);
54 }
55 };
56 list2InsertNameSources(name1, sources1);
57 list2InsertNameSources(name2, sources2);
58 list2InsertNameSources(name3, sources3);
59 BOOST_CHECK_EQUAL(list2.size(), 3);
60 BOOST_TEST(list2.getNames() == expectedNames, boost::test_tools::per_element());
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -050061
Junxiao Shi931ca9f2023-08-15 02:59:46 +000062 std::sort(sources1.begin(), sources1.end());
63 std::sort(sources2.begin(), sources2.end());
64 std::sort(sources3.begin(), sources3.end());
65 BOOST_TEST(list2.getSources(name1) == sources1, boost::test_tools::per_element());
66 BOOST_TEST(list2.getSources(name2) == sources2, boost::test_tools::per_element());
67 BOOST_TEST(list2.getSources(name3) == sources3, boost::test_tools::per_element());
68
69 auto noSources = list2.getSources("/not/a/prefix");
70 BOOST_CHECK_EQUAL(noSources.size(), 0);
71}
72
73/*
74 The NamePrefixList can have names inserted and removed from it.
75
76 The NamePrefixList will not delete a name as long as it at least one
77 source.
78 */
79BOOST_AUTO_TEST_CASE(Insert_Erase)
80{
81 ndn::Name name1{"/ndn/test/prefix1"};
82 ndn::Name name2{"/ndn/test/prefix2"};
83
84 NamePrefixList list;
85 BOOST_CHECK_EQUAL(list.insert(name2), true);
86 BOOST_CHECK_EQUAL(list.size(), 1);
87 BOOST_CHECK_EQUAL(list.insert(name2), false);
88
89 list.insert(name1, "nlsr.conf");
90 BOOST_CHECK_EQUAL(list.size(), 2);
91 list.insert(name1, "readvertise");
92 list.insert(name1, "prefix-update");
93 BOOST_CHECK_EQUAL(list.size(), 2);
94 list.erase(name1, "prefix-update");
95 BOOST_CHECK_EQUAL(list.size(), 2);
96
97 BOOST_TEST(list.getSources(name1) == (std::set<std::string>{"nlsr.conf", "readvertise"}),
98 boost::test_tools::per_element());
99
100 BOOST_CHECK_EQUAL(list.erase(name2), true);
101 BOOST_CHECK_EQUAL(list.erase(name2), false);
102
103 list.erase(name1, "nlsr.conf");
104 list.erase(name1, "readvertise");
105 BOOST_CHECK_EQUAL(list.size(), 0);
106 BOOST_CHECK_EQUAL(list.getSources(name1).size(), 0);
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500107}
108
Nick Gordonf14ec352017-07-24 16:09:58 -0500109/*
110 Two NamePrefixLists will be considered equal if they contain the
111 same names. Sources for names are ignored.
112 */
Nick Gordon56d1fae2017-05-26 16:39:25 -0500113BOOST_AUTO_TEST_CASE(OperatorEquals)
114{
Nick Gordon56d1fae2017-05-26 16:39:25 -0500115 ndn::Name name1("/ndn/test/name1");
116 ndn::Name name2("/ndn/test/name2");
117 ndn::Name name3("/ndn/some/other/name1");
Nick Gordon96861ca2017-10-17 18:25:21 -0500118 NamePrefixList list1{name1, name2, name3};
Nick Gordon56d1fae2017-05-26 16:39:25 -0500119
Junxiao Shi52f16642023-08-15 00:18:35 +0000120 NamePrefixList list2;
121 BOOST_CHECK_NE(list1, list2);
122
123 list2.insert(name1);
124 list2.insert(name1, "A1");
125 list2.insert(name2, "B0");
126 list2.insert(name2, "B1");
127 list2.insert(name3, "C0");
Nick Gordon56d1fae2017-05-26 16:39:25 -0500128 BOOST_CHECK_EQUAL(list1, list2);
Junxiao Shi52f16642023-08-15 00:18:35 +0000129
130 list2.erase(name3, "C0");
131 BOOST_CHECK_NE(list1, list2);
Nick Gordon56d1fae2017-05-26 16:39:25 -0500132}
133
Ashlesh Gawandeeb582eb2014-05-01 14:25:20 -0500134BOOST_AUTO_TEST_SUITE_END()
135
Davide Pesavento288141a2024-02-13 17:30:35 -0500136} // namespace nlsr::tests