Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 1 | /* -*- 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 Afanasyev | 0970340 | 2017-02-08 20:18:17 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/lp/tags.hpp> |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 24 | |
| 25 | #include "ns3/ndnSIM/helper/ndn-app-helper.hpp" |
Spyridon Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 26 | #include "ns3/error-model.h" |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 27 | |
| 28 | #include "../tests-common.hpp" |
| 29 | |
| 30 | namespace ns3 { |
| 31 | namespace ndn { |
| 32 | |
| 33 | class NndCxxFaceFixture : public ScenarioHelperWithCleanupFixture |
| 34 | { |
| 35 | public: |
| 36 | NndCxxFaceFixture() |
| 37 | : hasFired(false) |
| 38 | { |
| 39 | Config::SetDefault("ns3::PointToPointNetDevice::DataRate", StringValue("10Mbps")); |
| 40 | Config::SetDefault("ns3::PointToPointChannel::Delay", StringValue("10ms")); |
Spyridon Mastorakis | f98a341 | 2017-10-30 11:47:58 -0700 | [diff] [blame] | 41 | Config::SetDefault("ns3::QueueBase::MaxPackets", UintegerValue(20)); |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 42 | |
| 43 | createTopology({{"A", "B"}}); |
| 44 | addRoutes({{"A", "B", "/test", 1}}); |
| 45 | } |
| 46 | |
| 47 | protected: |
| 48 | bool hasFired; |
| 49 | }; |
| 50 | |
| 51 | BOOST_FIXTURE_TEST_SUITE(NdnCxxFace, NndCxxFaceFixture) |
| 52 | |
| 53 | class BaseTesterApp |
| 54 | { |
| 55 | public: |
| 56 | typedef std::function<void(const Name&)> NameCallback; |
| 57 | typedef std::function<void()> VoidCallback; |
| 58 | |
| 59 | protected: |
| 60 | ::ndn::Face m_face; |
| 61 | }; |
| 62 | |
| 63 | ///////////////////////////////////////////////////////////////////// |
| 64 | ///////////////////////////////////////////////////////////////////// |
| 65 | ///////////////////////////////////////////////////////////////////// |
| 66 | |
| 67 | class BasicProducer : public BaseTesterApp |
| 68 | { |
| 69 | public: |
| 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 Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 83 | BOOST_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 | |
| 109 | class SingleInterest : public BaseTesterApp |
| 110 | { |
| 111 | public: |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 112 | SingleInterest(const Name& name, const std::function<void(const Data&)>& onData, |
| 113 | const VoidCallback& onNack, const VoidCallback& onTimeout) |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 114 | { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 115 | m_face.expressInterest(Interest(name), std::bind([onData] (const Data& data) { |
Alexander Afanasyev | 0970340 | 2017-02-08 20:18:17 -0800 | [diff] [blame] | 116 | onData(data); |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 117 | }, _2), |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 118 | std::bind(onNack), |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 119 | std::bind(onTimeout)); |
| 120 | } |
| 121 | }; |
| 122 | |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 123 | BOOST_AUTO_TEST_CASE(ExpressInterestLocalhost) |
| 124 | { |
| 125 | // Retrieve data from remote |
| 126 | FactoryCallbackApp::Install(getNode("A"), [this] () -> shared_ptr<void> { |
Alexander Afanasyev | 0970340 | 2017-02-08 20:18:17 -0800 | [diff] [blame] | 127 | return make_shared<SingleInterest>("/localhost", [this] (const Data& data) { |
| 128 | BOOST_CHECK(Name("/localhost").isPrefixOf(data.getName())); |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 129 | this->hasFired = true; |
| 130 | BOOST_CHECK_LE(Simulator::Now().ToDouble(Time::S), 1.01); |
| 131 | }, |
| 132 | [] { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 133 | BOOST_ERROR("Unexpected NACK"); |
| 134 | }, |
| 135 | [] { |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 136 | 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 | |
| 147 | BOOST_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 Afanasyev | 0970340 | 2017-02-08 20:18:17 -0800 | [diff] [blame] | 153 | 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 Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 157 | this->hasFired = true; |
| 158 | BOOST_CHECK_LE(Simulator::Now().ToDouble(Time::S), 2.0); |
| 159 | }, |
| 160 | [] { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 161 | BOOST_ERROR("Unexpected NACK"); |
| 162 | }, |
| 163 | [] { |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 164 | 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 | |
| 175 | BOOST_AUTO_TEST_CASE(ExpressInterestTimeout) |
| 176 | { |
| 177 | FactoryCallbackApp::Install(getNode("A"), [this] () -> shared_ptr<void> { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 178 | return make_shared<SingleInterest>(Name("/test/prefix"), [] (const Data&) { |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 179 | BOOST_ERROR("Unexpected data"); |
| 180 | }, |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 181 | [] { |
| 182 | BOOST_ERROR("Unexpected NACK"); |
| 183 | }, |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 184 | [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 Mastorakis | 5ea3322 | 2016-12-07 14:33:53 -0800 | [diff] [blame] | 191 | // 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 Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 196 | Simulator::Stop(Seconds(20)); |
| 197 | Simulator::Run(); |
| 198 | |
| 199 | BOOST_CHECK(hasFired); |
| 200 | } |
| 201 | |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 202 | ///////////////////////////////////////////////////////////////////// |
| 203 | ///////////////////////////////////////////////////////////////////// |
| 204 | ///////////////////////////////////////////////////////////////////// |
| 205 | |
| 206 | class MultipleInterest : public BaseTesterApp |
| 207 | { |
| 208 | public: |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 209 | MultipleInterest(const Name& name, const NameCallback& onData, const VoidCallback& onTimeout, |
| 210 | const VoidCallback& onNack) |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 211 | : m_scheduler(m_face.getIoService()) |
| 212 | , m_event(m_scheduler) |
| 213 | { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 214 | expressNextInterest(name, 0, onData, onTimeout, onNack); |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | private: |
| 218 | void |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 219 | expressNextInterest(const Name& name, uint32_t seqNo, const NameCallback& onData, |
| 220 | const VoidCallback& onTimeout, const VoidCallback& onNack) |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 221 | { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 222 | m_face.expressInterest(Interest(Name(name).appendSegment(seqNo)), std::bind([=] (const Data& data) { |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 223 | onData(data.getName()); |
| 224 | |
| 225 | m_event = m_scheduler.scheduleEvent(time::seconds(1), |
| 226 | std::bind(&MultipleInterest::expressNextInterest, this, |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 227 | name, seqNo + 1, onData, onTimeout, onNack)); |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 228 | }, _2), |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 229 | std::bind(onNack), |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 230 | std::bind(onTimeout)); |
| 231 | } |
| 232 | |
| 233 | private: |
| 234 | ::ndn::Scheduler m_scheduler; |
| 235 | ::ndn::util::scheduler::ScopedEventId m_event; |
| 236 | }; |
| 237 | |
| 238 | BOOST_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 |
Alexander Afanasyev | e93e8b0 | 2018-05-08 20:10:01 -0400 | [diff] [blame] | 245 | FactoryCallbackApp::Install(getNode("A"), [&recvCount] () -> shared_ptr<void> { |
| 246 | return make_shared<MultipleInterest>("/test/prefix", [&recvCount] (const Name& data) { |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 247 | BOOST_CHECK_EQUAL(data, Name("/test/prefix").appendSegment(recvCount)); |
| 248 | ++recvCount; |
| 249 | }, |
| 250 | [] { |
| 251 | BOOST_ERROR("Unexpected timeout"); |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 252 | }, |
| 253 | [] { |
| 254 | BOOST_ERROR("Unexpected NACK"); |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 255 | }); |
| 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 | |
| 265 | class SingleInterestWithFaceShutdown : public BaseTesterApp |
| 266 | { |
| 267 | public: |
| 268 | SingleInterestWithFaceShutdown() |
| 269 | { |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 270 | m_face.expressInterest(Interest(Name("/interest/to/timeout")), |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 271 | std::bind([] { |
| 272 | BOOST_ERROR("Unexpected response"); |
| 273 | }), |
| 274 | std::bind([this] { |
| 275 | m_face.shutdown(); |
Spyridon Mastorakis | f6d3285 | 2017-09-27 20:28:52 -0700 | [diff] [blame] | 276 | }), |
| 277 | std::bind([this] { |
| 278 | m_face.shutdown(); |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 279 | })); |
| 280 | } |
| 281 | }; |
| 282 | |
| 283 | BOOST_AUTO_TEST_CASE(FaceShutdownFromTimeoutCallback) |
| 284 | { |
| 285 | // This test case to check if Face.shutdown from an onTimeout callback doesn't cause segfaults |
Alexander Afanasyev | e93e8b0 | 2018-05-08 20:10:01 -0400 | [diff] [blame] | 286 | FactoryCallbackApp::Install(getNode("A"), [] () -> shared_ptr<void> { |
Alexander Afanasyev | 5b4c467 | 2015-08-21 12:23:55 -0700 | [diff] [blame] | 287 | return make_shared<SingleInterestWithFaceShutdown>(); |
| 288 | }) |
| 289 | .Start(Seconds(1.01)); |
| 290 | |
| 291 | Simulator::Stop(Seconds(20)); |
| 292 | Simulator::Run(); |
| 293 | } |
| 294 | |
| 295 | BOOST_AUTO_TEST_SUITE_END() |
| 296 | |
| 297 | } // namespace ndn |
| 298 | } // namespace ns3 |