Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, 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 | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 25 | |
| 26 | #include "table/strategy-choice.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 27 | #include "tests/daemon/fw/dummy-strategy.hpp" |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 28 | |
| 29 | #include "tests/test-common.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace tests { |
| 33 | |
| 34 | BOOST_FIXTURE_TEST_SUITE(TableStrategyChoice, BaseFixture) |
| 35 | |
| 36 | using fw::Strategy; |
| 37 | |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 38 | BOOST_AUTO_TEST_CASE(Get) |
| 39 | { |
| 40 | Forwarder forwarder; |
| 41 | Name nameP("ndn:/strategy/P"); |
| 42 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP); |
| 43 | |
| 44 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 45 | |
| 46 | // install |
| 47 | BOOST_CHECK_EQUAL(table.install(strategyP), true); |
| 48 | |
| 49 | BOOST_CHECK(table.insert("ndn:/", nameP)); |
| 50 | // { '/'=>P } |
| 51 | |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 52 | auto getRoot = table.get("ndn:/"); |
| 53 | BOOST_CHECK_EQUAL(getRoot.first, true); |
| 54 | BOOST_CHECK_EQUAL(getRoot.second, nameP); |
| 55 | |
| 56 | auto getA = table.get("ndn:/A"); |
| 57 | BOOST_CHECK_EQUAL(getA.first, false); |
Steve DiBenedetto | 77c8751 | 2014-10-06 14:18:22 -0600 | [diff] [blame] | 58 | } |
| 59 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 60 | BOOST_AUTO_TEST_CASE(Effective) |
| 61 | { |
| 62 | Forwarder forwarder; |
| 63 | Name nameP("ndn:/strategy/P"); |
| 64 | Name nameQ("ndn:/strategy/Q"); |
| 65 | Name nameZ("ndn:/strategy/Z"); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 66 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP); |
| 67 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 68 | |
| 69 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 70 | |
| 71 | // install |
| 72 | BOOST_CHECK_EQUAL(table.install(strategyP), true); |
| 73 | BOOST_CHECK_EQUAL(table.install(strategyQ), true); |
| 74 | BOOST_CHECK_EQUAL(table.install(strategyQ), false); |
| 75 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 76 | BOOST_CHECK(table.insert("ndn:/", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 77 | // { '/'=>P } |
| 78 | |
| 79 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 80 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 81 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 82 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 83 | BOOST_CHECK(table.insert("ndn:/A/B", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 84 | // { '/'=>P, '/A/B'=>P } |
| 85 | |
| 86 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 87 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 88 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 89 | // same instance |
| 90 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"), strategyP.get()); |
| 91 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"), strategyP.get()); |
| 92 | BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get()); |
| 93 | |
| 94 | table.erase("ndn:/A"); // no effect |
| 95 | // { '/'=>P, '/A/B'=>P } |
| 96 | |
| 97 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 98 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 99 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 100 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 101 | BOOST_CHECK(table.insert("ndn:/A", nameQ)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 102 | // { '/'=>P, '/A/B'=>P, '/A'=>Q } |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 105 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ); |
| 106 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 107 | |
| 108 | table.erase("ndn:/A/B"); |
| 109 | // { '/'=>P, '/A'=>Q } |
| 110 | |
| 111 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP); |
| 112 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ); |
| 113 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ); |
| 114 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 115 | BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 116 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 117 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 118 | BOOST_CHECK(table.insert("ndn:/A", nameP)); |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 119 | // { '/'=>Q, '/A'=>P } |
| 120 | |
| 121 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ); |
| 122 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP); |
| 123 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP); |
| 124 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ); |
| 125 | } |
| 126 | |
Junxiao Shi | b5888d2 | 2014-05-26 07:35:22 -0700 | [diff] [blame] | 127 | //XXX BOOST_CONCEPT_ASSERT((ForwardIterator<std::vector<int>::iterator>)) |
| 128 | // is also failing. There might be a problem with ForwardIterator concept checking. |
| 129 | //BOOST_CONCEPT_ASSERT((ForwardIterator<StrategyChoice::const_iterator>)); |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(Enumerate) |
| 132 | { |
| 133 | |
| 134 | Forwarder forwarder; |
| 135 | Name nameP("ndn:/strategy/P"); |
| 136 | Name nameQ("ndn:/strategy/Q"); |
| 137 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP); |
| 138 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ); |
| 139 | |
| 140 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 141 | table.install(strategyP); |
| 142 | table.install(strategyQ); |
| 143 | |
| 144 | table.insert("ndn:/", nameP); |
| 145 | table.insert("ndn:/A/B", nameQ); |
| 146 | table.insert("ndn:/A/B/C", nameP); |
| 147 | table.insert("ndn:/D", nameP); |
| 148 | table.insert("ndn:/E", nameQ); |
| 149 | |
| 150 | BOOST_CHECK_EQUAL(table.size(), 5); |
| 151 | |
| 152 | std::map<Name, Name> map; // namespace=>strategyName |
| 153 | for (StrategyChoice::const_iterator it = table.begin(); it != table.end(); ++it) { |
| 154 | map[it->getPrefix()] = it->getStrategyName(); |
| 155 | } |
| 156 | BOOST_CHECK_EQUAL(map.size(), 5); |
| 157 | BOOST_CHECK_EQUAL(map["ndn:/"], nameP); |
| 158 | BOOST_CHECK_EQUAL(map["ndn:/A/B"], nameQ); |
| 159 | BOOST_CHECK_EQUAL(map["ndn:/A/B/C"], nameP); |
| 160 | BOOST_CHECK_EQUAL(map["ndn:/D"], nameP); |
| 161 | BOOST_CHECK_EQUAL(map["ndn:/E"], nameQ); |
| 162 | BOOST_CHECK_EQUAL(map.size(), 5); |
| 163 | } |
| 164 | |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 165 | class PStrategyInfo : public fw::StrategyInfo |
| 166 | { |
Junxiao Shi | 39ef261 | 2014-11-29 20:35:19 -0700 | [diff] [blame^] | 167 | public: |
| 168 | static constexpr int |
| 169 | getTypeId() |
| 170 | { |
| 171 | return 10; |
| 172 | } |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(ClearStrategyInfo) |
| 176 | { |
| 177 | Forwarder forwarder; |
| 178 | Name nameP("ndn:/strategy/P"); |
| 179 | Name nameQ("ndn:/strategy/Q"); |
Alexander Afanasyev | f698028 | 2014-05-13 18:28:40 -0700 | [diff] [blame] | 180 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP); |
| 181 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ); |
Junxiao Shi | e349ea1 | 2014-03-12 01:32:42 -0700 | [diff] [blame] | 182 | |
| 183 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 184 | Measurements& measurements = forwarder.getMeasurements(); |
| 185 | |
| 186 | // install |
| 187 | table.install(strategyP); |
| 188 | table.install(strategyQ); |
| 189 | |
| 190 | BOOST_CHECK(table.insert("ndn:/", nameP)); |
| 191 | // { '/'=>P } |
| 192 | measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 193 | measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 194 | measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 195 | measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>(); |
| 196 | |
| 197 | BOOST_CHECK(table.insert("ndn:/A/B", nameP)); |
| 198 | // { '/'=>P, '/A/B'=>P } |
| 199 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 200 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 201 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 202 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 203 | |
| 204 | BOOST_CHECK(table.insert("ndn:/A", nameQ)); |
| 205 | // { '/'=>P, '/A/B'=>P, '/A'=>Q } |
| 206 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 207 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 208 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 209 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 210 | |
| 211 | table.erase("ndn:/A/B"); |
| 212 | // { '/'=>P, '/A'=>Q } |
| 213 | BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>())); |
| 214 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>())); |
| 215 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>())); |
| 216 | BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>())); |
| 217 | } |
| 218 | |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 219 | BOOST_AUTO_TEST_CASE(EraseNameTreeEntry) |
| 220 | { |
| 221 | Forwarder forwarder; |
| 222 | NameTree& nameTree = forwarder.getNameTree(); |
| 223 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 224 | |
| 225 | Name nameP("ndn:/strategy/P"); |
| 226 | Name nameQ("ndn:/strategy/Q"); |
| 227 | shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(ref(forwarder), nameP); |
| 228 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ); |
| 229 | table.install(strategyP); |
| 230 | table.install(strategyQ); |
| 231 | |
| 232 | table.insert("ndn:/", nameP); |
| 233 | |
| 234 | size_t nNameTreeEntriesBefore = nameTree.size(); |
| 235 | |
| 236 | table.insert("ndn:/A/B", nameQ); |
| 237 | table.erase("ndn:/A/B"); |
| 238 | BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore); |
| 239 | } |
| 240 | |
Junxiao Shi | e93d6a3 | 2014-09-07 16:13:22 -0700 | [diff] [blame] | 241 | BOOST_AUTO_TEST_CASE(Versioning) |
| 242 | { |
| 243 | Forwarder forwarder; |
| 244 | Name nameP("ndn:/strategy/P"); |
| 245 | Name nameP1("ndn:/strategy/P/%FD%01"); |
| 246 | Name nameP2("ndn:/strategy/P/%FD%02"); |
| 247 | Name name3("ndn:/%FD%03"); |
| 248 | Name name4("ndn:/%FD%04"); |
| 249 | Name nameQ("ndn:/strategy/Q"); |
| 250 | Name nameQ5("ndn:/strategy/Q/%FD%05"); |
| 251 | shared_ptr<Strategy> strategyP1 = make_shared<DummyStrategy>(ref(forwarder), nameP1); |
| 252 | shared_ptr<Strategy> strategyP2 = make_shared<DummyStrategy>(ref(forwarder), nameP2); |
| 253 | shared_ptr<Strategy> strategy3 = make_shared<DummyStrategy>(ref(forwarder), name3); |
| 254 | shared_ptr<Strategy> strategy4 = make_shared<DummyStrategy>(ref(forwarder), name4); |
| 255 | shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(ref(forwarder), nameQ); |
| 256 | shared_ptr<Strategy> strategyQ5 = make_shared<DummyStrategy>(ref(forwarder), nameQ5); |
| 257 | |
| 258 | StrategyChoice& table = forwarder.getStrategyChoice(); |
| 259 | |
| 260 | // install |
| 261 | BOOST_CHECK_EQUAL(table.install(strategyP1), true); |
| 262 | BOOST_CHECK_EQUAL(table.install(strategyP1), false); |
| 263 | BOOST_CHECK_EQUAL(table.hasStrategy(nameP, false), true); |
| 264 | BOOST_CHECK_EQUAL(table.hasStrategy(nameP, true), false); |
| 265 | BOOST_CHECK_EQUAL(table.hasStrategy(nameP1, true), true); |
| 266 | |
| 267 | BOOST_CHECK_EQUAL(table.install(strategyP2), true); |
| 268 | BOOST_CHECK_EQUAL(table.install(strategy3), true); |
| 269 | BOOST_CHECK_EQUAL(table.install(strategy4), true); |
| 270 | BOOST_CHECK_EQUAL(table.install(strategyQ), true); |
| 271 | BOOST_CHECK_EQUAL(table.install(strategyQ5), true); |
| 272 | |
| 273 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 274 | // exact match, { '/'=>Q } |
| 275 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ); |
| 276 | |
| 277 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 278 | BOOST_CHECK(table.insert("ndn:/", nameP)); |
| 279 | // { '/'=>P2 } |
| 280 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2); |
| 281 | |
| 282 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 283 | BOOST_CHECK(table.insert("ndn:/", nameP1)); |
| 284 | // { '/'=>P1 } |
| 285 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP1); |
| 286 | |
| 287 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 288 | BOOST_CHECK(table.insert("ndn:/", nameP2)); |
| 289 | // { '/'=>P2 } |
| 290 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameP2); |
| 291 | |
| 292 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 293 | BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/A")); |
| 294 | // not installed |
| 295 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ); |
| 296 | |
| 297 | BOOST_CHECK(table.insert("ndn:/", nameQ)); |
| 298 | BOOST_CHECK(! table.insert("ndn:/", "ndn:/strategy/Z")); |
| 299 | // not installed |
| 300 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), nameQ); |
| 301 | |
| 302 | BOOST_CHECK(table.insert("ndn:/", nameP1)); |
| 303 | BOOST_CHECK(table.insert("ndn:/", "ndn:/")); |
| 304 | // match one component longer only, { '/'=>4 } |
| 305 | BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/").getName(), name4); |
| 306 | } |
| 307 | |
Junxiao Shi | bb5105f | 2014-03-03 12:06:45 -0700 | [diff] [blame] | 308 | BOOST_AUTO_TEST_SUITE_END() |
| 309 | |
| 310 | } // namespace tests |
| 311 | } // namespace nfd |