Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 2 | /* |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, Regents of the University of California, |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | e368d99 | 2014-12-02 23:44:31 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/measurements-accessor.hpp" |
Junxiao Shi | 2d9bdc8 | 2014-03-02 20:55:42 -0700 | [diff] [blame] | 27 | #include "fw/strategy.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 30 | #include "tests/daemon/global-io-fixture.hpp" |
| 31 | #include "tests/daemon/fw/dummy-strategy.hpp" |
| 32 | #include "tests/daemon/fw/choose-strategy.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 33 | |
| 34 | namespace nfd { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 35 | namespace measurements { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | namespace tests { |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 38 | using namespace nfd::tests; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 1c93cae | 2014-12-09 22:52:17 -0700 | [diff] [blame] | 40 | class MeasurementsAccessorTestStrategy : public DummyStrategy |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 43 | static void |
| 44 | registerAs(const Name& strategyName) |
| 45 | { |
| 46 | registerAsImpl<MeasurementsAccessorTestStrategy>(strategyName); |
| 47 | } |
| 48 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 49 | MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name) |
Junxiao Shi | 1c93cae | 2014-12-09 22:52:17 -0700 | [diff] [blame] | 50 | : DummyStrategy(forwarder, name) |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 51 | { |
| 52 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 53 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 54 | MeasurementsAccessor& |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 55 | getMeasurementsAccessor() |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 56 | { |
| 57 | return this->getMeasurements(); |
| 58 | } |
| 59 | }; |
| 60 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 61 | class MeasurementsAccessorFixture : public GlobalIoFixture |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 62 | { |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 63 | protected: |
| 64 | MeasurementsAccessorFixture() |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 65 | { |
Eric Newberry | 358414d | 2021-03-21 20:56:42 -0700 | [diff] [blame] | 66 | const auto strategyP = Name("/measurements-accessor-test-strategy-P").appendVersion(1); |
| 67 | const auto strategyQ = Name("/measurements-accessor-test-strategy-Q").appendVersion(1); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 68 | MeasurementsAccessorTestStrategy::registerAs(strategyP); |
| 69 | MeasurementsAccessorTestStrategy::registerAs(strategyQ); |
| 70 | |
| 71 | accessor1 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/", strategyP) |
| 72 | .getMeasurementsAccessor(); |
| 73 | accessor2 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/A", strategyQ) |
| 74 | .getMeasurementsAccessor(); |
| 75 | accessor3 = &choose<MeasurementsAccessorTestStrategy>(forwarder, "/A/B", strategyP) |
| 76 | .getMeasurementsAccessor(); |
| 77 | |
| 78 | // Despite accessor1 and accessor3 are associated with the same strategy name, |
| 79 | // they are different strategy instances and thus cannot access each other's measurements. |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | protected: |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 83 | FaceTable faceTable; |
| 84 | Forwarder forwarder{faceTable}; |
| 85 | Measurements& measurements{forwarder.getMeasurements()}; |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 86 | MeasurementsAccessor* accessor1; |
| 87 | MeasurementsAccessor* accessor2; |
| 88 | MeasurementsAccessor* accessor3; |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 89 | }; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 90 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 91 | BOOST_AUTO_TEST_SUITE(Table) |
| 92 | BOOST_FIXTURE_TEST_SUITE(TestMeasurementsAccessor, MeasurementsAccessorFixture) |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 94 | BOOST_AUTO_TEST_CASE(Get) |
| 95 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 96 | BOOST_CHECK(accessor1->get("/" ) != nullptr); |
| 97 | BOOST_CHECK(accessor1->get("/A" ) == nullptr); |
| 98 | BOOST_CHECK(accessor1->get("/A/B" ) == nullptr); |
| 99 | BOOST_CHECK(accessor1->get("/A/B/C") == nullptr); |
| 100 | BOOST_CHECK(accessor1->get("/A/D" ) == nullptr); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 102 | BOOST_CHECK(accessor2->get("/" ) == nullptr); |
| 103 | BOOST_CHECK(accessor2->get("/A" ) != nullptr); |
| 104 | BOOST_CHECK(accessor2->get("/A/B" ) == nullptr); |
| 105 | BOOST_CHECK(accessor2->get("/A/B/C") == nullptr); |
| 106 | BOOST_CHECK(accessor2->get("/A/D" ) != nullptr); |
| 107 | |
| 108 | BOOST_CHECK(accessor3->get("/" ) == nullptr); |
| 109 | BOOST_CHECK(accessor3->get("/A" ) == nullptr); |
| 110 | BOOST_CHECK(accessor3->get("/A/B" ) != nullptr); |
| 111 | BOOST_CHECK(accessor3->get("/A/B/C") != nullptr); |
| 112 | BOOST_CHECK(accessor3->get("/A/D" ) == nullptr); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 113 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 114 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 115 | BOOST_AUTO_TEST_CASE(GetParent) |
| 116 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 117 | Entry& entryRoot = measurements.get("/"); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 118 | BOOST_CHECK(accessor1->getParent(entryRoot) == nullptr); |
| 119 | BOOST_CHECK(accessor2->getParent(entryRoot) == nullptr); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 121 | Entry& entryA = measurements.get("/A"); |
| 122 | BOOST_CHECK(accessor2->getParent(entryA) == nullptr); |
| 123 | Entry& entryAD = measurements.get("/A/D"); |
| 124 | BOOST_CHECK(accessor2->getParent(entryAD) != nullptr); |
| 125 | // whether accessor1 and accessor3 can getParent(entryA) and getParent(entryAD) is undefined, |
| 126 | // because they shouldn't have obtained those entries in the first place |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 127 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 129 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch) |
| 130 | { |
| 131 | shared_ptr<Interest> interest = makeInterest("/A/B/C"); |
| 132 | shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first; |
| 133 | |
| 134 | measurements.get("/"); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 135 | BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") != nullptr); |
| 136 | BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) != nullptr); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 137 | |
| 138 | measurements.get("/A"); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 139 | BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") == nullptr); |
| 140 | BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) == nullptr); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_CASE(FindExactMatch) |
| 144 | { |
| 145 | measurements.get("/"); |
| 146 | measurements.get("/A"); |
| 147 | measurements.get("/A/B"); |
| 148 | measurements.get("/A/B/C"); |
| 149 | measurements.get("/A/D"); |
| 150 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 151 | BOOST_CHECK(accessor1->findExactMatch("/" ) != nullptr); |
| 152 | BOOST_CHECK(accessor1->findExactMatch("/A" ) == nullptr); |
| 153 | BOOST_CHECK(accessor1->findExactMatch("/A/B" ) == nullptr); |
| 154 | BOOST_CHECK(accessor1->findExactMatch("/A/B/C") == nullptr); |
| 155 | BOOST_CHECK(accessor1->findExactMatch("/A/D" ) == nullptr); |
| 156 | BOOST_CHECK(accessor1->findExactMatch("/A/E" ) == nullptr); |
| 157 | BOOST_CHECK(accessor1->findExactMatch("/F" ) == nullptr); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 158 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 159 | BOOST_CHECK(accessor2->findExactMatch("/" ) == nullptr); |
| 160 | BOOST_CHECK(accessor2->findExactMatch("/A" ) != nullptr); |
| 161 | BOOST_CHECK(accessor2->findExactMatch("/A/B" ) == nullptr); |
| 162 | BOOST_CHECK(accessor2->findExactMatch("/A/B/C") == nullptr); |
| 163 | BOOST_CHECK(accessor2->findExactMatch("/A/D" ) != nullptr); |
| 164 | BOOST_CHECK(accessor2->findExactMatch("/A/E" ) == nullptr); |
| 165 | BOOST_CHECK(accessor2->findExactMatch("/F" ) == nullptr); |
| 166 | |
| 167 | BOOST_CHECK(accessor3->findExactMatch("/" ) == nullptr); |
| 168 | BOOST_CHECK(accessor3->findExactMatch("/A" ) == nullptr); |
| 169 | BOOST_CHECK(accessor3->findExactMatch("/A/B" ) != nullptr); |
| 170 | BOOST_CHECK(accessor3->findExactMatch("/A/B/C") != nullptr); |
| 171 | BOOST_CHECK(accessor3->findExactMatch("/A/D" ) == nullptr); |
| 172 | BOOST_CHECK(accessor3->findExactMatch("/A/E" ) == nullptr); |
| 173 | BOOST_CHECK(accessor3->findExactMatch("/F" ) == nullptr); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 176 | BOOST_AUTO_TEST_SUITE_END() // TestMeasurementsAccessor |
| 177 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 178 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 179 | } // namespace tests |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 180 | } // namespace measurements |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 181 | } // namespace nfd |