blob: d8f9f4ebf9d225b92a84ca24f216fed8201a0b86 [file] [log] [blame]
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
4 *
5 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
7 *
8 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#include "helper/ndn-strategy-choice-helper.hpp"
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070021
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -070022#include "ns3/ndnSIM/NFD/daemon/fw/strategy.hpp"
23
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070024#include "../tests-common.hpp"
25
26namespace ns3 {
27namespace ndn {
28
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070029class StrategyChoiceHelperFixture : public ScenarioHelperWithCleanupFixture
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070030{
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070031public:
32 StrategyChoiceHelperFixture()
33 {
34 Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("10Mbps"));
35 Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("1ms"));
Spyridon Mastorakisf98a3412017-10-30 11:47:58 -070036 Config::SetDefault("ns3::QueueBase::MaxPackets", UintegerValue(500));
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070037
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070038 // Creating two 3 node topologies: //
39 // //
40 // +----+ +----+ //
41 // +- | B1 | +- | B2 | //
42 // / +----+ / +----+ //
43 // +----+ / +----+ / //
44 // | | --+ | | --+ //
45 // | A1 | | A2 | //
46 // | | --+ | | --+ //
47 // +----+ \ +----+ \ //
48 // \ +----+ \ +----+ //
49 // +- | C1 | +- | C2 | //
50 // +----+ +----+ //
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070051
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070052 createTopology({
53 {"A1", "B1"},
54 {"A1", "C1"},
55 {"A2", "B2"},
56 {"A2", "C2"}
57 });
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070058
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070059 addRoutes({
60 {"A1", "B1", "/prefix", 200},
61 {"A1", "C1", "/prefix", 100},
62 {"A2", "B2", "/prefix", 100},
63 {"A2", "C2", "/prefix", 200}
64 });
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070065
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070066 addApps({
67 {"A1", "ns3::ndn::ConsumerCbr",
68 {{"Prefix", "/prefix"}, {"Frequency", "1"}},
69 "0s", "100s"},
70 {"A2", "ns3::ndn::ConsumerCbr",
71 {{"Prefix", "/prefix"}, {"Frequency", "1"}},
72 "0s", "100s"},
73 });
74 }
75};
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070076
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070077BOOST_FIXTURE_TEST_SUITE(TestStrategyChoiceHelper, StrategyChoiceHelperFixture)
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070078
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070079BOOST_AUTO_TEST_CASE(DefaultStrategies)
80{
81 Simulator::Stop(Seconds(5.0));
82 Simulator::Run();
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070083
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -070084 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0);
85 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5);
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070086
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -070087 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 5);
88 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070089}
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070090
Alexander Afanasyevc512dba2015-08-13 16:51:26 -070091// static void
92// Install(Ptr<Node> node, const Name& namePrefix, const Name& strategy);
93BOOST_AUTO_TEST_CASE(InstallBuiltInStrategyOnNode)
94{
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -070095 StrategyChoiceHelper::Install(getNode("A2"), "/prefix", "/localhost/nfd/strategy/multicast");
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -070096
97 Simulator::Stop(Seconds(5.0));
98 Simulator::Run();
99
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700100 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0);
101 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5);
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -0700102
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700103 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 5);
104 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 5);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700105}
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -0700106
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700107// static void
108// Install(Ptr<Node> node, const Name& namePrefix, const Name& strategy);
109BOOST_AUTO_TEST_CASE(InstallBuiltInStrategyOnNodeContainer)
110{
111 NodeContainer nodes;
112 nodes.Add(getNode("A1"));
113 nodes.Add(getNode("A2"));
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -0700114
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700115 StrategyChoiceHelper::Install(nodes, "/prefix", "/localhost/nfd/strategy/multicast");
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700116
117 Simulator::Stop(Seconds(5.0));
118 Simulator::Run();
119
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700120 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 5);
121 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700122
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700123 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 5);
124 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 5);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700125}
126
127// static void
128// InstallAll(const Name& namePrefix, const Name& strategy);
129BOOST_AUTO_TEST_CASE(InstallAllBuiltInStrategy)
130{
Alexander Afanasyevc3c7f042015-08-21 11:38:00 -0700131 StrategyChoiceHelper::InstallAll("/prefix", "/localhost/nfd/strategy/multicast");
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700132
133 Simulator::Stop(Seconds(5.0));
134 Simulator::Run();
135
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700136 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 5);
137 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700138
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700139 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 5);
140 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 5);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700141}
142
143
144class NullStrategy : public nfd::fw::Strategy {
145public:
146 NullStrategy(nfd::Forwarder& forwarder)
147 : Strategy(forwarder, STRATEGY_NAME)
148 {
149 }
150
151 virtual void
152 afterReceiveInterest(const Face& inFace, const Interest& interest,
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800153 const shared_ptr<nfd::pit::Entry>& pitEntry)
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700154 {
155 // this strategy doesn't forward interests
156 }
157
158public:
159 static const Name STRATEGY_NAME;
160};
161
162const Name NullStrategy::STRATEGY_NAME = "ndn:/localhost/nfd/strategy/unit-tests/null-strategy";
163
164// template<class Strategy>
165// static void
166// Install(Ptr<Node> node, const Name& namePrefix);
167BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNode)
168{
169 StrategyChoiceHelper::Install<NullStrategy>(getNode("A2"), "/prefix");
170
171 Simulator::Stop(Seconds(5.0));
172 Simulator::Run();
173
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700174 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0);
175 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700176
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700177 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0);
178 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700179}
180
181// template<class Strategy>
182// static void
183// Install(const NodeContainer& c, const Name& namePrefix);
184BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNodeContainer)
185{
186 NodeContainer nodes;
187 nodes.Add(getNode("A1"));
188 nodes.Add(getNode("A2"));
189
190 StrategyChoiceHelper::Install<NullStrategy>(nodes, "/prefix");
191
192 Simulator::Stop(Seconds(5.0));
193 Simulator::Run();
194
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700195 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0);
196 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700197
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700198 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0);
199 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700200}
201
202// template<class Strategy>
203// static void
204// InstallAll(const Name& namePrefix);
205BOOST_AUTO_TEST_CASE(InstallAllCustomStrategy)
206{
207 StrategyChoiceHelper::InstallAll<NullStrategy>("/prefix");
208
209 Simulator::Stop(Seconds(5.0));
210 Simulator::Run();
211
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700212 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0);
213 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700214
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700215 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0);
216 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0);
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -0700217}
218
219BOOST_AUTO_TEST_SUITE_END()
220
221} // namespace ndn
222} // namespace ns3