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