blob: 67f3333d3e63c1a0bbd1fbf7442f1fd98b6fd373 [file] [log] [blame]
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -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 <ndn-cxx/face.hpp>
21#include <ndn-cxx/util/scheduler.hpp>
22#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
Alexander Afanasyev09703402017-02-08 20:18:17 -080023#include <ndn-cxx/lp/tags.hpp>
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -070024
25#include "ns3/ndnSIM/helper/ndn-app-helper.hpp"
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -080026#include "ns3/error-model.h"
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -070027
28#include "../tests-common.hpp"
29
30namespace ns3 {
31namespace ndn {
32
33class NndCxxFaceFixture : public ScenarioHelperWithCleanupFixture
34{
35public:
36 NndCxxFaceFixture()
37 : hasFired(false)
38 {
39 Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("10Mbps"));
40 Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms"));
Spyridon Mastorakisf98a3412017-10-30 11:47:58 -070041 Config::SetDefault("ns3::QueueBase::MaxPackets", UintegerValue(20));
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -070042
43 createTopology({{"A", "B"}});
44 addRoutes({{"A", "B", "/test", 1}});
45 }
46
47protected:
48 bool hasFired;
49};
50
51BOOST_FIXTURE_TEST_SUITE(NdnCxxFace, NndCxxFaceFixture)
52
53class BaseTesterApp
54{
55public:
56 typedef std::function<void(const Name&)> NameCallback;
57 typedef std::function<void()> VoidCallback;
58
59protected:
60 ::ndn::Face m_face;
61};
62
63/////////////////////////////////////////////////////////////////////
64/////////////////////////////////////////////////////////////////////
65/////////////////////////////////////////////////////////////////////
66
67class BasicProducer : public BaseTesterApp
68{
69public:
70 BasicProducer(const Name& name, const NameCallback& onInterest, const VoidCallback& onFail)
71 {
72 m_face.setInterestFilter(name,
73 [this, onInterest] (const ::ndn::InterestFilter& filter, const Interest& interest) {
74 auto data = make_shared<Data>(Name(interest.getName()));
75 StackHelper::getKeyChain().sign(*data);
76 m_face.put(*data);
77 onInterest(interest.getName());
78 },
79 std::bind(onFail));
80 }
81};
82
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -070083BOOST_AUTO_TEST_CASE(SetInterestFilter)
84{
85 FactoryCallbackApp::Install(getNode("B"), [this] () -> shared_ptr<void> {
86 return make_shared<BasicProducer>("/test", [this] (const Name& interest) {
87 BOOST_CHECK_EQUAL(interest, "/test/prefix/%FE%00");
88 this->hasFired = true;
89 },
90 [] {
91 BOOST_ERROR("Unexpected failure to set interest filter");
92 });
93 })
94 .Start(Seconds(0.01));
95
96 addApps({{"A", "ns3::ndn::ConsumerBatches",
97 {{"Prefix", "/test/prefix"}, {"Batches", "0s 1"}}, "1s", "5.1s"}});
98
99 Simulator::Stop(Seconds(20));
100 Simulator::Run();
101
102 BOOST_CHECK(hasFired);
103}
104
105/////////////////////////////////////////////////////////////////////
106/////////////////////////////////////////////////////////////////////
107/////////////////////////////////////////////////////////////////////
108
109class SingleInterest : public BaseTesterApp
110{
111public:
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700112 SingleInterest(const Name& name, const std::function<void(const Data&)>& onData,
113 const VoidCallback& onNack, const VoidCallback& onTimeout)
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700114 {
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700115 m_face.expressInterest(Interest(name), std::bind([onData] (const Data& data) {
Alexander Afanasyev09703402017-02-08 20:18:17 -0800116 onData(data);
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700117 }, _2),
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700118 std::bind(onNack),
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700119 std::bind(onTimeout));
120 }
121};
122
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700123BOOST_AUTO_TEST_CASE(ExpressInterestLocalhost)
124{
125 // Retrieve data from remote
126 FactoryCallbackApp::Install(getNode("A"), [this] () -> shared_ptr<void> {
Alexander Afanasyev09703402017-02-08 20:18:17 -0800127 return make_shared<SingleInterest>("/localhost", [this] (const Data& data) {
128 BOOST_CHECK(Name("/localhost").isPrefixOf(data.getName()));
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700129 this->hasFired = true;
130 BOOST_CHECK_LE(Simulator::Now().ToDouble(Time::S), 1.01);
131 },
132 [] {
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700133 BOOST_ERROR("Unexpected NACK");
134 },
135 [] {
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700136 BOOST_ERROR("Unexpected timeout");
137 });
138 })
139 .Start(Seconds(1.01));
140
141 Simulator::Stop(Seconds(20));
142 Simulator::Run();
143
144 BOOST_CHECK(hasFired);
145}
146
147BOOST_AUTO_TEST_CASE(ExpressInterestRemote)
148{
149 addApps({{"B", "ns3::ndn::Producer", {{"Prefix", "/test"}}, "0s", "100s"}});
150
151 // Retrieve data from remote
152 FactoryCallbackApp::Install(getNode("A"), [this] () -> shared_ptr<void> {
Alexander Afanasyev09703402017-02-08 20:18:17 -0800153 return make_shared<SingleInterest>("/test/prefix", [this] (const Data& data) {
154 BOOST_CHECK_EQUAL(data.getName(), "/test/prefix");
155 BOOST_REQUIRE(data.getTag<lp::HopCountTag>() != nullptr);
156 BOOST_CHECK_EQUAL(*data.getTag<lp::HopCountTag>(), 2);
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700157 this->hasFired = true;
158 BOOST_CHECK_LE(Simulator::Now().ToDouble(Time::S), 2.0);
159 },
160 [] {
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700161 BOOST_ERROR("Unexpected NACK");
162 },
163 [] {
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700164 BOOST_ERROR("Unexpected timeout");
165 });
166 })
167 .Start(Seconds(1.01));
168
169 Simulator::Stop(Seconds(20));
170 Simulator::Run();
171
172 BOOST_CHECK(hasFired);
173}
174
175BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
176{
177 FactoryCallbackApp::Install(getNode("A"), [this] () -> shared_ptr<void> {
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700178 return make_shared<SingleInterest>(Name("/test/prefix"), [] (const Data&) {
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700179 BOOST_ERROR("Unexpected data");
180 },
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700181 [] {
182 BOOST_ERROR("Unexpected NACK");
183 },
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700184 [this] {
185 BOOST_CHECK_GT(Simulator::Now().ToDouble(Time::S), 6.0);
186 this->hasFired = true;
187 });
188 })
189 .Start(Seconds(2.01));
190
Spyridon Mastorakis5ea33222016-12-07 14:33:53 -0800191 // Make sure NACKs are never received
192 Ptr<ns3::RateErrorModel> model = CreateObject<ns3::RateErrorModel>();
193 model->SetRate(std::numeric_limits<double>::max());
194 Config::Set("/NodeList/*/DeviceList/*/$ns3::PointToPointNetDevice/ReceiveErrorModel", PointerValue(model));
195
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700196 Simulator::Stop(Seconds(20));
197 Simulator::Run();
198
199 BOOST_CHECK(hasFired);
200}
201
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700202/////////////////////////////////////////////////////////////////////
203/////////////////////////////////////////////////////////////////////
204/////////////////////////////////////////////////////////////////////
205
206class MultipleInterest : public BaseTesterApp
207{
208public:
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700209 MultipleInterest(const Name& name, const NameCallback& onData, const VoidCallback& onTimeout,
210 const VoidCallback& onNack)
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700211 : m_scheduler(m_face.getIoService())
212 , m_event(m_scheduler)
213 {
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700214 expressNextInterest(name, 0, onData, onTimeout, onNack);
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700215 }
216
217private:
218 void
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700219 expressNextInterest(const Name& name, uint32_t seqNo, const NameCallback& onData,
220 const VoidCallback& onTimeout, const VoidCallback& onNack)
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700221 {
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700222 m_face.expressInterest(Interest(Name(name).appendSegment(seqNo)), std::bind([=] (const Data& data) {
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700223 onData(data.getName());
224
225 m_event = m_scheduler.scheduleEvent(time::seconds(1),
226 std::bind(&MultipleInterest::expressNextInterest, this,
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700227 name, seqNo + 1, onData, onTimeout, onNack));
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700228 }, _2),
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700229 std::bind(onNack),
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700230 std::bind(onTimeout));
231 }
232
233private:
234 ::ndn::Scheduler m_scheduler;
235 ::ndn::util::scheduler::ScopedEventId m_event;
236};
237
238BOOST_AUTO_TEST_CASE(ExpressMultipleInterests)
239{
240 addApps({{"B", "ns3::ndn::Producer", {{"Prefix", "/test"}}, "0s", "100s"}});
241
242 size_t recvCount = 0;
243
244 // Retrieve data from remote
245 FactoryCallbackApp::Install(getNode("A"), [this, &recvCount] () -> shared_ptr<void> {
246 return make_shared<MultipleInterest>("/test/prefix", [this, &recvCount] (const Name& data) {
247 BOOST_CHECK_EQUAL(data, Name("/test/prefix").appendSegment(recvCount));
248 ++recvCount;
249 },
250 [] {
251 BOOST_ERROR("Unexpected timeout");
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700252 },
253 [] {
254 BOOST_ERROR("Unexpected NACK");
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700255 });
256 })
257 .Start(Seconds(1.01));
258
259 Simulator::Stop(Seconds(10.9)); // this test case also checks that apps stops properly
260 Simulator::Run();
261
262 BOOST_CHECK_EQUAL(recvCount, 10);
263}
264
265class SingleInterestWithFaceShutdown : public BaseTesterApp
266{
267public:
268 SingleInterestWithFaceShutdown()
269 {
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700270 m_face.expressInterest(Interest(Name("/interest/to/timeout")),
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700271 std::bind([] {
272 BOOST_ERROR("Unexpected response");
273 }),
274 std::bind([this] {
275 m_face.shutdown();
Spyridon Mastorakisf6d32852017-09-27 20:28:52 -0700276 }),
277 std::bind([this] {
278 m_face.shutdown();
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700279 }));
280 }
281};
282
283BOOST_AUTO_TEST_CASE(FaceShutdownFromTimeoutCallback)
284{
285 // This test case to check if Face.shutdown from an onTimeout callback doesn't cause segfaults
Alexander Afanasyev5b4c4672015-08-21 12:23:55 -0700286 FactoryCallbackApp::Install(getNode("A"), [this] () -> shared_ptr<void> {
287 return make_shared<SingleInterestWithFaceShutdown>();
288 })
289 .Start(Seconds(1.01));
290
291 Simulator::Stop(Seconds(20));
292 Simulator::Run();
293}
294
295BOOST_AUTO_TEST_SUITE_END()
296
297} // namespace ndn
298} // namespace ns3