Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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" |
Junxiao Shi | 1c93cae | 2014-12-09 22:52:17 -0700 | [diff] [blame] | 30 | #include "../fw/dummy-strategy.hpp" |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 31 | #include "../fw/choose-strategy.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 34 | namespace measurements { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | namespace tests { |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 37 | using namespace nfd::tests; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | 1c93cae | 2014-12-09 22:52:17 -0700 | [diff] [blame] | 39 | class MeasurementsAccessorTestStrategy : public DummyStrategy |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 40 | { |
| 41 | public: |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 42 | static void |
| 43 | registerAs(const Name& strategyName) |
| 44 | { |
| 45 | registerAsImpl<MeasurementsAccessorTestStrategy>(strategyName); |
| 46 | } |
| 47 | |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 48 | MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name) |
Junxiao Shi | 1c93cae | 2014-12-09 22:52:17 -0700 | [diff] [blame] | 49 | : DummyStrategy(forwarder, name) |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 50 | { |
| 51 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 52 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 53 | MeasurementsAccessor& |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 54 | getMeasurementsAccessor() |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 55 | { |
| 56 | return this->getMeasurements(); |
| 57 | } |
| 58 | }; |
| 59 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 60 | class MeasurementsAccessorFixture : public BaseFixture |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 61 | { |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 62 | protected: |
| 63 | MeasurementsAccessorFixture() |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 64 | : measurements(forwarder.getMeasurements()) |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 65 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 66 | const Name strategyP("/measurements-accessor-test-strategy-P/%FD%01"); |
| 67 | const Name strategyQ("/measurements-accessor-test-strategy-Q/%FD%01"); |
| 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: |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 83 | Forwarder forwarder; |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 84 | Measurements& measurements; |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 85 | MeasurementsAccessor* accessor1; |
| 86 | MeasurementsAccessor* accessor2; |
| 87 | MeasurementsAccessor* accessor3; |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 88 | }; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 90 | BOOST_AUTO_TEST_SUITE(Table) |
| 91 | BOOST_FIXTURE_TEST_SUITE(TestMeasurementsAccessor, MeasurementsAccessorFixture) |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 92 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 93 | BOOST_AUTO_TEST_CASE(Get) |
| 94 | { |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 95 | BOOST_CHECK(accessor1->get("/" ) != nullptr); |
| 96 | BOOST_CHECK(accessor1->get("/A" ) == nullptr); |
| 97 | BOOST_CHECK(accessor1->get("/A/B" ) == nullptr); |
| 98 | BOOST_CHECK(accessor1->get("/A/B/C") == nullptr); |
| 99 | BOOST_CHECK(accessor1->get("/A/D" ) == nullptr); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 100 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 101 | BOOST_CHECK(accessor2->get("/" ) == nullptr); |
| 102 | BOOST_CHECK(accessor2->get("/A" ) != nullptr); |
| 103 | BOOST_CHECK(accessor2->get("/A/B" ) == nullptr); |
| 104 | BOOST_CHECK(accessor2->get("/A/B/C") == nullptr); |
| 105 | BOOST_CHECK(accessor2->get("/A/D" ) != nullptr); |
| 106 | |
| 107 | BOOST_CHECK(accessor3->get("/" ) == nullptr); |
| 108 | BOOST_CHECK(accessor3->get("/A" ) == nullptr); |
| 109 | BOOST_CHECK(accessor3->get("/A/B" ) != nullptr); |
| 110 | BOOST_CHECK(accessor3->get("/A/B/C") != nullptr); |
| 111 | BOOST_CHECK(accessor3->get("/A/D" ) == nullptr); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 112 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 114 | BOOST_AUTO_TEST_CASE(GetParent) |
| 115 | { |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 116 | Entry& entryRoot = measurements.get("/"); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 117 | BOOST_CHECK(accessor1->getParent(entryRoot) == nullptr); |
| 118 | BOOST_CHECK(accessor2->getParent(entryRoot) == nullptr); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 119 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 120 | Entry& entryA = measurements.get("/A"); |
| 121 | BOOST_CHECK(accessor2->getParent(entryA) == nullptr); |
| 122 | Entry& entryAD = measurements.get("/A/D"); |
| 123 | BOOST_CHECK(accessor2->getParent(entryAD) != nullptr); |
| 124 | // whether accessor1 and accessor3 can getParent(entryA) and getParent(entryAD) is undefined, |
| 125 | // because they shouldn't have obtained those entries in the first place |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 126 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 127 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 128 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch) |
| 129 | { |
| 130 | shared_ptr<Interest> interest = makeInterest("/A/B/C"); |
| 131 | shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first; |
| 132 | |
| 133 | measurements.get("/"); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 134 | BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") != nullptr); |
| 135 | BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) != nullptr); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 136 | |
| 137 | measurements.get("/A"); |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 138 | BOOST_CHECK(accessor1->findLongestPrefixMatch("/A/B") == nullptr); |
| 139 | BOOST_CHECK(accessor1->findLongestPrefixMatch(*pitEntry) == nullptr); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE(FindExactMatch) |
| 143 | { |
| 144 | measurements.get("/"); |
| 145 | measurements.get("/A"); |
| 146 | measurements.get("/A/B"); |
| 147 | measurements.get("/A/B/C"); |
| 148 | measurements.get("/A/D"); |
| 149 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 150 | BOOST_CHECK(accessor1->findExactMatch("/" ) != nullptr); |
| 151 | BOOST_CHECK(accessor1->findExactMatch("/A" ) == nullptr); |
| 152 | BOOST_CHECK(accessor1->findExactMatch("/A/B" ) == nullptr); |
| 153 | BOOST_CHECK(accessor1->findExactMatch("/A/B/C") == nullptr); |
| 154 | BOOST_CHECK(accessor1->findExactMatch("/A/D" ) == nullptr); |
| 155 | BOOST_CHECK(accessor1->findExactMatch("/A/E" ) == nullptr); |
| 156 | BOOST_CHECK(accessor1->findExactMatch("/F" ) == nullptr); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame^] | 158 | BOOST_CHECK(accessor2->findExactMatch("/" ) == nullptr); |
| 159 | BOOST_CHECK(accessor2->findExactMatch("/A" ) != nullptr); |
| 160 | BOOST_CHECK(accessor2->findExactMatch("/A/B" ) == nullptr); |
| 161 | BOOST_CHECK(accessor2->findExactMatch("/A/B/C") == nullptr); |
| 162 | BOOST_CHECK(accessor2->findExactMatch("/A/D" ) != nullptr); |
| 163 | BOOST_CHECK(accessor2->findExactMatch("/A/E" ) == nullptr); |
| 164 | BOOST_CHECK(accessor2->findExactMatch("/F" ) == nullptr); |
| 165 | |
| 166 | BOOST_CHECK(accessor3->findExactMatch("/" ) == nullptr); |
| 167 | BOOST_CHECK(accessor3->findExactMatch("/A" ) == nullptr); |
| 168 | BOOST_CHECK(accessor3->findExactMatch("/A/B" ) != nullptr); |
| 169 | BOOST_CHECK(accessor3->findExactMatch("/A/B/C") != nullptr); |
| 170 | BOOST_CHECK(accessor3->findExactMatch("/A/D" ) == nullptr); |
| 171 | BOOST_CHECK(accessor3->findExactMatch("/A/E" ) == nullptr); |
| 172 | BOOST_CHECK(accessor3->findExactMatch("/F" ) == nullptr); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 175 | BOOST_AUTO_TEST_SUITE_END() // TestMeasurementsAccessor |
| 176 | BOOST_AUTO_TEST_SUITE_END() // Table |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 177 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 178 | } // namespace tests |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 179 | } // namespace measurements |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 180 | } // namespace nfd |