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