blob: b382ce37533b49e69728b99e11be8288192724d4 [file] [log] [blame]
Junxiao Shibb5105f2014-03-03 12:06:45 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Junxiao Shibb5105f2014-03-03 12:06:45 -070024
25#include "table/strategy-choice.hpp"
Junxiao Shie349ea12014-03-12 01:32:42 -070026#include "tests/fw/dummy-strategy.hpp"
Junxiao Shibb5105f2014-03-03 12:06:45 -070027
28#include "tests/test-common.hpp"
29
30namespace nfd {
31namespace tests {
32
33BOOST_FIXTURE_TEST_SUITE(TableStrategyChoice, BaseFixture)
34
35using fw::Strategy;
36
Junxiao Shibb5105f2014-03-03 12:06:45 -070037BOOST_AUTO_TEST_CASE(Effective)
38{
39 Forwarder forwarder;
40 Name nameP("ndn:/strategy/P");
41 Name nameQ("ndn:/strategy/Q");
42 Name nameZ("ndn:/strategy/Z");
Junxiao Shie349ea12014-03-12 01:32:42 -070043 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(boost::ref(forwarder), nameP);
44 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(boost::ref(forwarder), nameQ);
Junxiao Shibb5105f2014-03-03 12:06:45 -070045
46 StrategyChoice& table = forwarder.getStrategyChoice();
47
48 // install
49 BOOST_CHECK_EQUAL(table.install(strategyP), true);
50 BOOST_CHECK_EQUAL(table.install(strategyQ), true);
51 BOOST_CHECK_EQUAL(table.install(strategyQ), false);
52
Junxiao Shie349ea12014-03-12 01:32:42 -070053 BOOST_CHECK(table.insert("ndn:/", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070054 // { '/'=>P }
55
56 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
57 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
58 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
59
Junxiao Shie349ea12014-03-12 01:32:42 -070060 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070061 // { '/'=>P, '/A/B'=>P }
62
63 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
64 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
65 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
66 // same instance
67 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/"), strategyP.get());
68 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A"), strategyP.get());
69 BOOST_CHECK_EQUAL(&table.findEffectiveStrategy("ndn:/A/B"), strategyP.get());
70
71 table.erase("ndn:/A"); // no effect
72 // { '/'=>P, '/A/B'=>P }
73
74 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
75 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
76 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
77
Junxiao Shie349ea12014-03-12 01:32:42 -070078 BOOST_CHECK(table.insert("ndn:/A", nameQ));
Junxiao Shibb5105f2014-03-03 12:06:45 -070079 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
80
81 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
82 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
83 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
84
85 table.erase("ndn:/A/B");
86 // { '/'=>P, '/A'=>Q }
87
88 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameP);
89 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameQ);
90 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameQ);
91
Junxiao Shie349ea12014-03-12 01:32:42 -070092 BOOST_CHECK(!table.insert("ndn:/", nameZ)); // non existent strategy
Junxiao Shibb5105f2014-03-03 12:06:45 -070093
Junxiao Shie349ea12014-03-12 01:32:42 -070094 BOOST_CHECK(table.insert("ndn:/", nameQ));
95 BOOST_CHECK(table.insert("ndn:/A", nameP));
Junxiao Shibb5105f2014-03-03 12:06:45 -070096 // { '/'=>Q, '/A'=>P }
97
98 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/") .getName(), nameQ);
99 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A") .getName(), nameP);
100 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/A/B").getName(), nameP);
101 BOOST_CHECK_EQUAL(table.findEffectiveStrategy("ndn:/D") .getName(), nameQ);
102}
103
Junxiao Shie349ea12014-03-12 01:32:42 -0700104class PStrategyInfo : public fw::StrategyInfo
105{
106};
107
108BOOST_AUTO_TEST_CASE(ClearStrategyInfo)
109{
110 Forwarder forwarder;
111 Name nameP("ndn:/strategy/P");
112 Name nameQ("ndn:/strategy/Q");
113 shared_ptr<Strategy> strategyP = make_shared<DummyStrategy>(boost::ref(forwarder), nameP);
114 shared_ptr<Strategy> strategyQ = make_shared<DummyStrategy>(boost::ref(forwarder), nameQ);
115
116 StrategyChoice& table = forwarder.getStrategyChoice();
117 Measurements& measurements = forwarder.getMeasurements();
118
119 // install
120 table.install(strategyP);
121 table.install(strategyQ);
122
123 BOOST_CHECK(table.insert("ndn:/", nameP));
124 // { '/'=>P }
125 measurements.get("ndn:/") ->getOrCreateStrategyInfo<PStrategyInfo>();
126 measurements.get("ndn:/A") ->getOrCreateStrategyInfo<PStrategyInfo>();
127 measurements.get("ndn:/A/B") ->getOrCreateStrategyInfo<PStrategyInfo>();
128 measurements.get("ndn:/A/C") ->getOrCreateStrategyInfo<PStrategyInfo>();
129
130 BOOST_CHECK(table.insert("ndn:/A/B", nameP));
131 // { '/'=>P, '/A/B'=>P }
132 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
133 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
134 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
135 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
136
137 BOOST_CHECK(table.insert("ndn:/A", nameQ));
138 // { '/'=>P, '/A/B'=>P, '/A'=>Q }
139 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
140 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
141 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
142 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
143
144 table.erase("ndn:/A/B");
145 // { '/'=>P, '/A'=>Q }
146 BOOST_CHECK( static_cast<bool>(measurements.get("ndn:/") ->getStrategyInfo<PStrategyInfo>()));
147 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A") ->getStrategyInfo<PStrategyInfo>()));
148 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/B")->getStrategyInfo<PStrategyInfo>()));
149 BOOST_CHECK(!static_cast<bool>(measurements.get("ndn:/A/C")->getStrategyInfo<PStrategyInfo>()));
150}
151
Junxiao Shibb5105f2014-03-03 12:06:45 -0700152BOOST_AUTO_TEST_SUITE_END()
153
154} // namespace tests
155} // namespace nfd