blob: e06a93d88f08cddb8396db2110215a7d40c0b5d4 [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:
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700146 NullStrategy(nfd::Forwarder& forwarder, const Name& name = getStrategyName())
147 : Strategy(forwarder)
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700148 {
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700149 this->setInstanceName(name);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700150 }
151
152 virtual void
153 afterReceiveInterest(const Face& inFace, const Interest& interest,
Spyridon Mastorakisb0b22412016-12-07 14:33:46 -0800154 const shared_ptr<nfd::pit::Entry>& pitEntry)
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700155 {
156 // this strategy doesn't forward interests
157 }
158
159public:
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700160 static const Name& getStrategyName()
161 {
162 static Name strategyName("ndn:/localhost/nfd/strategy/unit-tests/null-strategy/%FD%00");
163 return strategyName;
164 }
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700165};
166
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700167// template<class Strategy>
168// static void
169// Install(Ptr<Node> node, const Name& namePrefix);
170BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNode)
171{
172 StrategyChoiceHelper::Install<NullStrategy>(getNode("A2"), "/prefix");
173
174 Simulator::Stop(Seconds(5.0));
175 Simulator::Run();
176
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700177 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0);
178 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 5);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700179
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700180 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0);
181 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700182}
183
184// template<class Strategy>
185// static void
186// Install(const NodeContainer& c, const Name& namePrefix);
187BOOST_AUTO_TEST_CASE(InstallCustomStrategyOnNodeContainer)
188{
189 NodeContainer nodes;
190 nodes.Add(getNode("A1"));
191 nodes.Add(getNode("A2"));
192
193 StrategyChoiceHelper::Install<NullStrategy>(nodes, "/prefix");
194
195 Simulator::Stop(Seconds(5.0));
196 Simulator::Run();
197
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700198 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0);
199 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700200
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700201 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0);
202 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700203}
204
205// template<class Strategy>
206// static void
207// InstallAll(const Name& namePrefix);
208BOOST_AUTO_TEST_CASE(InstallAllCustomStrategy)
209{
210 StrategyChoiceHelper::InstallAll<NullStrategy>("/prefix");
211
212 Simulator::Stop(Seconds(5.0));
213 Simulator::Run();
214
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700215 BOOST_CHECK_EQUAL(getFace("A1", "B1")->getCounters().nOutInterests, 0);
216 BOOST_CHECK_EQUAL(getFace("A1", "C1")->getCounters().nOutInterests, 0);
Alexander Afanasyevc512dba2015-08-13 16:51:26 -0700217
Alexander Afanasyevca3c67e2016-09-08 15:48:23 -0700218 BOOST_CHECK_EQUAL(getFace("A2", "B2")->getCounters().nOutInterests, 0);
219 BOOST_CHECK_EQUAL(getFace("A2", "C2")->getCounters().nOutInterests, 0);
Yuanzhi Gaodd516fe2015-04-23 04:18:24 -0700220}
221
222BOOST_AUTO_TEST_SUITE_END()
223
224} // namespace ndn
225} // namespace ns3