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 | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame^] | 31 | #include "../fw/install-strategy.hpp" |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 32 | |
| 33 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 34 | namespace tests { |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 35 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 36 | using measurements::Entry; |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | 1c93cae | 2014-12-09 22:52:17 -0700 | [diff] [blame] | 38 | class MeasurementsAccessorTestStrategy : public DummyStrategy |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
Junxiao Shi | 7bb0151 | 2014-03-05 21:34:09 -0700 | [diff] [blame] | 41 | MeasurementsAccessorTestStrategy(Forwarder& forwarder, const Name& name) |
Junxiao Shi | 1c93cae | 2014-12-09 22:52:17 -0700 | [diff] [blame] | 42 | : DummyStrategy(forwarder, name) |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 43 | { |
| 44 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 45 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 46 | virtual |
| 47 | ~MeasurementsAccessorTestStrategy() |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 48 | { |
| 49 | } |
| 50 | |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 51 | public: // accessors |
| 52 | MeasurementsAccessor& |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 53 | getMeasurementsAccessor() |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 54 | { |
| 55 | return this->getMeasurements(); |
| 56 | } |
| 57 | }; |
| 58 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 59 | class MeasurementsAccessorFixture : public BaseFixture |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 60 | { |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 61 | protected: |
| 62 | MeasurementsAccessorFixture() |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame^] | 63 | : strategy1(install<MeasurementsAccessorTestStrategy>(forwarder, "ndn:/strategy1")) |
| 64 | , strategy2(install<MeasurementsAccessorTestStrategy>(forwarder, "ndn:/strategy2")) |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 65 | , measurements(forwarder.getMeasurements()) |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame^] | 66 | , accessor1(strategy1.getMeasurementsAccessor()) |
| 67 | , accessor2(strategy2.getMeasurementsAccessor()) |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 68 | { |
| 69 | StrategyChoice& strategyChoice = forwarder.getStrategyChoice(); |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame^] | 70 | strategyChoice.insert("/" , strategy1.getName()); |
| 71 | strategyChoice.insert("/A" , strategy2.getName()); |
| 72 | strategyChoice.insert("/A/B", strategy1.getName()); |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | protected: |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 76 | Forwarder forwarder; |
Junxiao Shi | a49a1ab | 2016-07-15 18:24:36 +0000 | [diff] [blame^] | 77 | MeasurementsAccessorTestStrategy& strategy1; |
| 78 | MeasurementsAccessorTestStrategy& strategy2; |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 79 | Measurements& measurements; |
| 80 | MeasurementsAccessor& accessor1; |
| 81 | MeasurementsAccessor& accessor2; |
| 82 | }; |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 83 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 84 | BOOST_FIXTURE_TEST_SUITE(TableMeasurementsAccessor, MeasurementsAccessorFixture) |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 85 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 86 | BOOST_AUTO_TEST_CASE(Get) |
| 87 | { |
| 88 | BOOST_CHECK(accessor1.get("/" ) != nullptr); |
| 89 | BOOST_CHECK(accessor1.get("/A" ) == nullptr); |
| 90 | BOOST_CHECK(accessor1.get("/A/B" ) != nullptr); |
| 91 | BOOST_CHECK(accessor1.get("/A/B/C") != nullptr); |
| 92 | BOOST_CHECK(accessor1.get("/A/D" ) == nullptr); |
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_CHECK(accessor2.get("/" ) == nullptr); |
| 95 | BOOST_CHECK(accessor2.get("/A" ) != nullptr); |
| 96 | BOOST_CHECK(accessor2.get("/A/B" ) == nullptr); |
| 97 | BOOST_CHECK(accessor2.get("/A/B/C") == nullptr); |
| 98 | BOOST_CHECK(accessor2.get("/A/D" ) != nullptr); |
| 99 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 100 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 101 | BOOST_AUTO_TEST_CASE(GetParent) |
| 102 | { |
| 103 | shared_ptr<Entry> entryRoot = measurements.get("/"); |
| 104 | BOOST_CHECK(accessor1.getParent(*entryRoot) == nullptr); |
| 105 | BOOST_CHECK(accessor2.getParent(*entryRoot) == nullptr); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 106 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 107 | shared_ptr<Entry> entryABC = measurements.get("/A/B/C"); |
| 108 | BOOST_CHECK(accessor1.getParent(*entryABC) != nullptr); |
| 109 | BOOST_CHECK(accessor2.getParent(*entryABC) == nullptr); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 110 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 111 | shared_ptr<Entry> entryAB = measurements.get("/A/B"); |
| 112 | BOOST_CHECK(accessor1.getParent(*entryAB) == nullptr); |
| 113 | // whether accessor2.getParent(*entryAB) can return an Entry is undefined, |
| 114 | // because strategy2 shouldn't obtain entryAB in the first place |
| 115 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | 3535396 | 2015-01-08 09:13:47 -0700 | [diff] [blame] | 117 | BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch) |
| 118 | { |
| 119 | shared_ptr<Interest> interest = makeInterest("/A/B/C"); |
| 120 | shared_ptr<pit::Entry> pitEntry = forwarder.getPit().insert(*interest).first; |
| 121 | |
| 122 | measurements.get("/"); |
| 123 | BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") != nullptr); |
| 124 | BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) != nullptr); |
| 125 | |
| 126 | measurements.get("/A"); |
| 127 | BOOST_CHECK(accessor1.findLongestPrefixMatch("/A/B") == nullptr); |
| 128 | BOOST_CHECK(accessor1.findLongestPrefixMatch(*pitEntry) == nullptr); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(FindExactMatch) |
| 132 | { |
| 133 | measurements.get("/"); |
| 134 | measurements.get("/A"); |
| 135 | measurements.get("/A/B"); |
| 136 | measurements.get("/A/B/C"); |
| 137 | measurements.get("/A/D"); |
| 138 | |
| 139 | BOOST_CHECK(accessor1.findExactMatch("/" ) != nullptr); |
| 140 | BOOST_CHECK(accessor1.findExactMatch("/A" ) == nullptr); |
| 141 | BOOST_CHECK(accessor1.findExactMatch("/A/B" ) != nullptr); |
| 142 | BOOST_CHECK(accessor1.findExactMatch("/A/B/C") != nullptr); |
| 143 | BOOST_CHECK(accessor1.findExactMatch("/A/D" ) == nullptr); |
| 144 | BOOST_CHECK(accessor1.findExactMatch("/A/E" ) == nullptr); |
| 145 | BOOST_CHECK(accessor1.findExactMatch("/F" ) == nullptr); |
| 146 | |
| 147 | BOOST_CHECK(accessor2.findExactMatch("/" ) == nullptr); |
| 148 | BOOST_CHECK(accessor2.findExactMatch("/A" ) != nullptr); |
| 149 | BOOST_CHECK(accessor2.findExactMatch("/A/B" ) == nullptr); |
| 150 | BOOST_CHECK(accessor2.findExactMatch("/A/B/C") == nullptr); |
| 151 | BOOST_CHECK(accessor2.findExactMatch("/A/D" ) != nullptr); |
| 152 | BOOST_CHECK(accessor2.findExactMatch("/A/E" ) == nullptr); |
| 153 | BOOST_CHECK(accessor2.findExactMatch("/F" ) == nullptr); |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_SUITE_END() |
| 157 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 158 | } // namespace tests |
Junxiao Shi | dbe7173 | 2014-02-21 22:23:28 -0700 | [diff] [blame] | 159 | } // namespace nfd |