| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2014-2022, Regents of the University of California, |
| * Arizona Board of Regents, |
| * Colorado State University, |
| * University Pierre & Marie Curie, Sorbonne University, |
| * Washington University in St. Louis, |
| * Beijing Institute of Technology, |
| * The University of Memphis. |
| * |
| * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| * See AUTHORS.md for complete list of NFD authors and contributors. |
| * |
| * NFD is free software: you can redistribute it and/or modify it under the terms |
| * of the GNU General Public License as published by the Free Software Foundation, |
| * either version 3 of the License, or (at your option) any later version. |
| * |
| * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| * PURPOSE. See the GNU General Public License for more details. |
| * |
| * You should have received a copy of the GNU General Public License along with |
| * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| */ |
| |
| #include "fw/forwarder.hpp" |
| #include "common/global.hpp" |
| |
| #include "tests/test-common.hpp" |
| #include "tests/daemon/global-io-fixture.hpp" |
| #include "tests/daemon/face/dummy-face.hpp" |
| #include "choose-strategy.hpp" |
| #include "dummy-strategy.hpp" |
| |
| #include <ndn-cxx/lp/tags.hpp> |
| |
| namespace nfd::tests { |
| |
| class ForwarderFixture : public GlobalIoTimeFixture |
| { |
| protected: |
| template<typename ...Args> |
| shared_ptr<DummyFace> |
| addFace(Args&&... args) |
| { |
| auto face = make_shared<DummyFace>(std::forward<Args>(args)...); |
| faceTable.add(face); |
| return face; |
| } |
| |
| protected: |
| FaceTable faceTable; |
| Forwarder forwarder{faceTable}; |
| }; |
| |
| BOOST_AUTO_TEST_SUITE(Fw) |
| BOOST_FIXTURE_TEST_SUITE(TestForwarder, ForwarderFixture) |
| |
| BOOST_AUTO_TEST_CASE(SimpleExchange) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| |
| Fib& fib = forwarder.getFib(); |
| fib::Entry* entry = fib.insert("/A").first; |
| fib.addOrUpdateNextHop(*entry, *face2, 0); |
| |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nInInterests, 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nOutInterests, 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nCsHits, 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nCsMisses, 0); |
| face1->receiveInterest(*makeInterest("/A/B")); |
| this->advanceClocks(100_ms, 1_s); |
| BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 1); |
| BOOST_CHECK_EQUAL(face2->sentInterests[0].getName(), "/A/B"); |
| BOOST_REQUIRE(face2->sentInterests[0].getTag<lp::IncomingFaceIdTag>() != nullptr); |
| BOOST_CHECK_EQUAL(*face2->sentInterests[0].getTag<lp::IncomingFaceIdTag>(), face1->getId()); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nInInterests, 1); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nOutInterests, 1); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nCsHits, 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nCsMisses, 1); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nSatisfiedInterests, 0); |
| |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nInData, 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nOutData, 0); |
| face2->receiveData(*makeData("/A/B")); |
| this->advanceClocks(100_ms, 1_s); |
| BOOST_REQUIRE_EQUAL(face1->sentData.size(), 1); |
| BOOST_CHECK_EQUAL(face1->sentData[0].getName(), "/A/B"); |
| BOOST_REQUIRE(face1->sentData[0].getTag<lp::IncomingFaceIdTag>() != nullptr); |
| BOOST_CHECK_EQUAL(*face1->sentData[0].getTag<lp::IncomingFaceIdTag>(), face2->getId()); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nInData, 1); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nOutData, 1); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nInNacks, 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nOutNacks, 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nSatisfiedInterests, 1); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 0); |
| } |
| |
| BOOST_AUTO_TEST_CASE(CsMatched) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| auto face3 = addFace(); |
| |
| Fib& fib = forwarder.getFib(); |
| fib::Entry* entry = fib.insert("/A").first; |
| fib.addOrUpdateNextHop(*entry, *face2, 0); |
| |
| Pit& pit = forwarder.getPit(); |
| BOOST_CHECK_EQUAL(pit.size(), 0); |
| |
| auto data = makeData("/A/B"); |
| data->setTag(make_shared<lp::IncomingFaceIdTag>(face3->getId())); |
| forwarder.getCs().insert(*data); |
| |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nCsHits, 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nCsMisses, 0); |
| face1->receiveInterest(*makeInterest("/A", true)); |
| this->advanceClocks(1_ms, 5_ms); |
| // Interest matching ContentStore should not be forwarded |
| BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 0); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nCsHits, 1); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nCsMisses, 0); |
| |
| BOOST_REQUIRE_EQUAL(face1->sentData.size(), 1); |
| // IncomingFaceId field should be reset to represent CS |
| BOOST_CHECK_EQUAL(face1->sentData[0].getName(), "/A/B"); |
| BOOST_REQUIRE(face1->sentData[0].getTag<lp::IncomingFaceIdTag>() != nullptr); |
| BOOST_CHECK_EQUAL(*face1->sentData[0].getTag<lp::IncomingFaceIdTag>(), face::FACEID_CONTENT_STORE); |
| |
| this->advanceClocks(100_ms, 500_ms); |
| // PIT entry should not be left behind |
| BOOST_CHECK_EQUAL(pit.size(), 0); |
| } |
| |
| BOOST_AUTO_TEST_CASE(InterestWithoutNonce) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| |
| Fib& fib = forwarder.getFib(); |
| fib::Entry* entry = fib.insert("/A").first; |
| fib.addOrUpdateNextHop(*entry, *face2, 0); |
| |
| auto interest = makeInterest("/A"); |
| BOOST_CHECK_EQUAL(interest->hasNonce(), false); |
| face1->receiveInterest(*interest); |
| |
| // Ensure Nonce added if incoming packet did not have Nonce |
| BOOST_REQUIRE_EQUAL(face2->getCounters().nOutInterests, 1); |
| BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 1); |
| BOOST_CHECK_EQUAL(face2->sentInterests.back().hasNonce(), true); |
| } |
| |
| BOOST_AUTO_TEST_CASE(OutgoingInterest) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| |
| Pit& pit = forwarder.getPit(); |
| auto interestA1 = makeInterest("/A", false, std::nullopt, 8378); |
| auto pitA = pit.insert(*interestA1).first; |
| pitA->insertOrUpdateInRecord(*face1, *interestA1); |
| |
| auto interestA2 = makeInterest("/A", false, std::nullopt, 1698); |
| auto outA2 = forwarder.onOutgoingInterest(*interestA2, *face2, pitA); |
| BOOST_REQUIRE(outA2 != nullptr); |
| BOOST_CHECK_EQUAL(outA2->getLastNonce(), 1698); |
| |
| // This packet will be dropped because HopLimit=0 |
| auto interestA3 = makeInterest("/A", false, std::nullopt, 9876); |
| interestA3->setHopLimit(0); |
| auto outA3 = forwarder.onOutgoingInterest(*interestA3, *face2, pitA); |
| BOOST_CHECK(outA3 == nullptr); |
| |
| BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 1); |
| BOOST_CHECK_EQUAL(face2->sentInterests.back().getNonce(), 1698); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NextHopFaceId) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| auto face3 = addFace(); |
| |
| Fib& fib = forwarder.getFib(); |
| fib::Entry* entry = fib.insert("/A").first; |
| fib.addOrUpdateNextHop(*entry, *face3, 0); |
| |
| auto interest = makeInterest("/A/B"); |
| interest->setTag(make_shared<lp::NextHopFaceIdTag>(face2->getId())); |
| |
| face1->receiveInterest(*interest); |
| this->advanceClocks(100_ms, 1_s); |
| BOOST_CHECK_EQUAL(face3->sentInterests.size(), 0); |
| BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 1); |
| BOOST_CHECK_EQUAL(face2->sentInterests.front().getName(), "/A/B"); |
| } |
| |
| BOOST_AUTO_TEST_CASE(HopLimit) |
| { |
| auto faceIn = addFace(); |
| auto faceRemote = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| auto faceLocal = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL); |
| Fib& fib = forwarder.getFib(); |
| fib::Entry* entryRemote = fib.insert("/remote").first; |
| fib.addOrUpdateNextHop(*entryRemote, *faceRemote, 0); |
| fib::Entry* entryLocal = fib.insert("/local").first; |
| fib.addOrUpdateNextHop(*entryLocal, *faceLocal, 0); |
| |
| // Incoming interest w/o HopLimit will not be dropped on send or receive paths |
| auto interestNoHopLimit = makeInterest("/remote/abcdefgh"); |
| faceIn->receiveInterest(*interestNoHopLimit); |
| this->advanceClocks(100_ms, 1_s); |
| BOOST_CHECK_EQUAL(faceRemote->sentInterests.size(), 1); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nInHopLimitZero, 0); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutHopLimitZero, 0); |
| BOOST_REQUIRE_EQUAL(faceRemote->sentInterests.size(), 1); |
| BOOST_CHECK(!faceRemote->sentInterests.back().getHopLimit()); |
| |
| // Incoming interest w/ HopLimit > 1 will not be dropped on send/receive |
| auto interestHopLimit2 = makeInterest("/remote/ijklmnop"); |
| interestHopLimit2->setHopLimit(2); |
| faceIn->receiveInterest(*interestHopLimit2); |
| this->advanceClocks(100_ms, 1_s); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutInterests, 2); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nInHopLimitZero, 0); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutHopLimitZero, 0); |
| BOOST_REQUIRE_EQUAL(faceRemote->sentInterests.size(), 2); |
| BOOST_REQUIRE(faceRemote->sentInterests.back().getHopLimit()); |
| BOOST_CHECK_EQUAL(*faceRemote->sentInterests.back().getHopLimit(), 1); |
| |
| // Incoming interest w/ HopLimit == 1 will be dropped on send path if going out on remote face |
| auto interestHopLimit1Remote = makeInterest("/remote/qrstuvwx"); |
| interestHopLimit1Remote->setHopLimit(1); |
| faceIn->receiveInterest(*interestHopLimit1Remote); |
| this->advanceClocks(100_ms, 1_s); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutInterests, 2); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nInHopLimitZero, 0); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutHopLimitZero, 1); |
| BOOST_CHECK_EQUAL(faceRemote->sentInterests.size(), 2); |
| |
| // Incoming interest w/ HopLimit == 1 will not be dropped on send path if going out on local face |
| auto interestHopLimit1Local = makeInterest("/local/abcdefgh"); |
| interestHopLimit1Local->setHopLimit(1); |
| faceIn->receiveInterest(*interestHopLimit1Local); |
| this->advanceClocks(100_ms, 1_s); |
| BOOST_CHECK_EQUAL(faceLocal->getCounters().nOutInterests, 1); |
| BOOST_CHECK_EQUAL(faceLocal->getCounters().nInHopLimitZero, 0); |
| BOOST_CHECK_EQUAL(faceLocal->getCounters().nOutHopLimitZero, 0); |
| BOOST_REQUIRE_EQUAL(faceLocal->sentInterests.size(), 1); |
| BOOST_REQUIRE(faceLocal->sentInterests.back().getHopLimit()); |
| BOOST_CHECK_EQUAL(*faceLocal->sentInterests.back().getHopLimit(), 0); |
| |
| // Interest w/ HopLimit == 0 will be dropped on receive path |
| auto interestHopLimit0 = makeInterest("/remote/yzabcdef"); |
| interestHopLimit0->setHopLimit(0); |
| faceIn->receiveInterest(*interestHopLimit0); |
| this->advanceClocks(100_ms, 1_s); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutInterests, 2); |
| BOOST_CHECK_EQUAL(faceIn->getCounters().nInHopLimitZero, 1); |
| BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutHopLimitZero, 1); |
| BOOST_CHECK_EQUAL(faceRemote->sentInterests.size(), 2); |
| } |
| |
| BOOST_AUTO_TEST_CASE(AddDefaultHopLimit) |
| { |
| auto face = addFace(); |
| auto faceEndpoint = FaceEndpoint(*face); |
| Pit& pit = forwarder.getPit(); |
| auto i1 = makeInterest("/A"); |
| auto pitA = pit.insert(*i1).first; |
| |
| // By default, no HopLimit should be added |
| auto i2 = makeInterest("/A"); |
| BOOST_TEST(!i2->getHopLimit().has_value()); |
| forwarder.onContentStoreMiss(*i2, faceEndpoint, pitA); |
| BOOST_TEST(!i2->getHopLimit().has_value()); |
| |
| // Change config value to 10 |
| forwarder.m_config.defaultHopLimit = 10; |
| |
| // HopLimit should be set to 10 now |
| auto i3 = makeInterest("/A"); |
| BOOST_TEST(!i3->getHopLimit().has_value()); |
| forwarder.onContentStoreMiss(*i3, faceEndpoint, pitA); |
| BOOST_REQUIRE(i3->getHopLimit().has_value()); |
| BOOST_TEST(*i3->getHopLimit() == 10); |
| |
| // An existing HopLimit should be preserved |
| auto i4 = makeInterest("/A"); |
| i4->setHopLimit(50); |
| forwarder.onContentStoreMiss(*i4, faceEndpoint, pitA); |
| BOOST_REQUIRE(i4->getHopLimit().has_value()); |
| BOOST_TEST(*i4->getHopLimit() == 50); |
| } |
| |
| BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming) |
| { |
| auto face1 = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL); |
| auto face2 = addFace(); // default is non-local |
| |
| auto& strategy = choose<DummyStrategy>(forwarder, "/", DummyStrategy::getStrategyName()); |
| |
| // local face, /localhost: OK |
| strategy.afterReceiveInterest_count = 0; |
| auto i1 = makeInterest("/localhost/A1"); |
| forwarder.onIncomingInterest(*i1, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(strategy.afterReceiveInterest_count, 1); |
| |
| // non-local face, /localhost: violate |
| strategy.afterReceiveInterest_count = 0; |
| auto i2 = makeInterest("/localhost/A2"); |
| forwarder.onIncomingInterest(*i2, FaceEndpoint(*face2)); |
| BOOST_CHECK_EQUAL(strategy.afterReceiveInterest_count, 0); |
| |
| // local face, non-/localhost: OK |
| strategy.afterReceiveInterest_count = 0; |
| auto i3 = makeInterest("/A3"); |
| forwarder.onIncomingInterest(*i3, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(strategy.afterReceiveInterest_count, 1); |
| |
| // non-local face, non-/localhost: OK |
| strategy.afterReceiveInterest_count = 0; |
| auto i4 = makeInterest("/A4"); |
| forwarder.onIncomingInterest(*i4, FaceEndpoint(*face2)); |
| BOOST_CHECK_EQUAL(strategy.afterReceiveInterest_count, 1); |
| |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 0); |
| |
| // local face, /localhost: OK |
| auto d1 = makeData("/localhost/B1"); |
| forwarder.onIncomingData(*d1, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 1); |
| |
| // non-local face, /localhost: violate |
| auto d2 = makeData("/localhost/B2"); |
| forwarder.onIncomingData(*d2, FaceEndpoint(*face2)); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 1); |
| |
| // local face, non-/localhost: OK |
| auto d3 = makeData("/B3"); |
| forwarder.onIncomingData(*d3, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 2); |
| |
| // non-local face, non-/localhost: OK |
| auto d4 = makeData("/B4"); |
| forwarder.onIncomingData(*d4, FaceEndpoint(*face2)); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 3); |
| } |
| |
| BOOST_AUTO_TEST_CASE(IncomingInterestStrategyDispatch) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| |
| auto& strategyA = choose<DummyStrategy>(forwarder, "/", DummyStrategy::getStrategyName()); |
| auto& strategyB = choose<DummyStrategy>(forwarder, "/B", DummyStrategy::getStrategyName()); |
| |
| auto interest1 = makeInterest("/A/1"); |
| strategyA.afterReceiveInterest_count = 0; |
| strategyA.interestOutFace = face2; |
| forwarder.onIncomingInterest(*interest1, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(strategyA.afterReceiveInterest_count, 1); |
| |
| auto interest2 = makeInterest("/B/2", true); |
| strategyB.afterReceiveInterest_count = 0; |
| strategyB.interestOutFace = face2; |
| forwarder.onIncomingInterest(*interest2, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(strategyB.afterReceiveInterest_count, 1); |
| |
| this->advanceClocks(1_ms, 5_ms); |
| |
| auto data1 = makeData("/A/1"); |
| strategyA.beforeSatisfyInterest_count = 0; |
| forwarder.onIncomingData(*data1, FaceEndpoint(*face2)); |
| BOOST_CHECK_EQUAL(strategyA.beforeSatisfyInterest_count, 1); |
| |
| auto data2 = makeData("/B/2/b"); |
| strategyB.beforeSatisfyInterest_count = 0; |
| forwarder.onIncomingData(*data2, FaceEndpoint(*face2)); |
| BOOST_CHECK_EQUAL(strategyB.beforeSatisfyInterest_count, 1); |
| |
| auto interest3 = makeInterest("/A/3", false, 30_ms); |
| forwarder.onIncomingInterest(*interest3, FaceEndpoint(*face1)); |
| auto interest4 = makeInterest("/B/4", false, 5_s); |
| forwarder.onIncomingInterest(*interest4, FaceEndpoint(*face1)); |
| } |
| |
| BOOST_AUTO_TEST_CASE(IncomingData) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| auto face3 = addFace(); |
| auto face4 = addFace(); |
| |
| Pit& pit = forwarder.getPit(); |
| auto interestD = makeInterest("/A/B/C/D"); |
| auto pitD = pit.insert(*interestD).first; |
| pitD->insertOrUpdateInRecord(*face1, *interestD); |
| auto interestA = makeInterest("/A", true); |
| auto pitA = pit.insert(*interestA).first; |
| pitA->insertOrUpdateInRecord(*face2, *interestA); |
| pitA->insertOrUpdateInRecord(*face3, *interestA); |
| auto interestC = makeInterest("/A/B/C", true); |
| auto pitC = pit.insert(*interestC).first; |
| pitC->insertOrUpdateInRecord(*face3, *interestC); |
| pitC->insertOrUpdateInRecord(*face4, *interestC); |
| |
| auto dataD = makeData("/A/B/C/D"); |
| forwarder.onIncomingData(*dataD, FaceEndpoint(*face3)); |
| this->advanceClocks(1_ms, 5_ms); |
| |
| BOOST_CHECK_EQUAL(face1->sentData.size(), 1); |
| BOOST_CHECK_EQUAL(face2->sentData.size(), 1); |
| BOOST_CHECK_EQUAL(face3->sentData.size(), 0); |
| BOOST_CHECK_EQUAL(face4->sentData.size(), 1); |
| |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nInData, 1); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nOutData, 3); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 0); |
| } |
| |
| BOOST_AUTO_TEST_CASE(OutgoingData) |
| { |
| auto face1 = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL); |
| auto face2 = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| auto face3 = addFace(); |
| face3->setId(face::INVALID_FACEID); |
| |
| auto data = makeData("/QkzAWU6K"); |
| auto localData = makeData("/localhost/YH8bqnbv"); |
| |
| face1->sentData.clear(); |
| BOOST_CHECK(forwarder.onOutgoingData(*data, *face1)); |
| BOOST_REQUIRE_EQUAL(face1->sentData.size(), 1); |
| BOOST_CHECK_EQUAL(face1->sentData.back().getName(), data->getName()); |
| |
| // scope control |
| face1->sentData.clear(); |
| face2->sentData.clear(); |
| BOOST_CHECK(!forwarder.onOutgoingData(*localData, *face2)); |
| BOOST_CHECK_EQUAL(face2->sentData.size(), 0); |
| BOOST_CHECK(forwarder.onOutgoingData(*localData, *face1)); |
| BOOST_REQUIRE_EQUAL(face1->sentData.size(), 1); |
| BOOST_CHECK_EQUAL(face1->sentData.back().getName(), localData->getName()); |
| |
| // face with invalid ID |
| face3->sentData.clear(); |
| BOOST_CHECK(!forwarder.onOutgoingData(*data, *face3)); |
| BOOST_CHECK_EQUAL(face3->sentData.size(), 0); |
| } |
| |
| BOOST_AUTO_TEST_CASE(IncomingNack) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| auto face3 = addFace("dummy://", "dummy://", |
| ndn::nfd::FACE_SCOPE_NON_LOCAL, |
| ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| ndn::nfd::LINK_TYPE_MULTI_ACCESS); |
| |
| auto& strategyA = choose<DummyStrategy>(forwarder, "/", DummyStrategy::getStrategyName()); |
| auto& strategyB = choose<DummyStrategy>(forwarder, "/B", DummyStrategy::getStrategyName()); |
| |
| Pit& pit = forwarder.getPit(); |
| |
| // dispatch to the correct strategy |
| auto interest1 = makeInterest("/A/AYJqayrzF", false, std::nullopt, 562); |
| auto pit1 = pit.insert(*interest1).first; |
| pit1->insertOrUpdateOutRecord(*face1, *interest1); |
| auto interest2 = makeInterest("/B/EVyP73ru", false, std::nullopt, 221); |
| auto pit2 = pit.insert(*interest2).first; |
| pit2->insertOrUpdateOutRecord(*face1, *interest2); |
| |
| auto nack1 = makeNack(*interest1, lp::NackReason::CONGESTION); |
| strategyA.afterReceiveNack_count = 0; |
| strategyB.afterReceiveNack_count = 0; |
| forwarder.onIncomingNack(nack1, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 1); |
| BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0); |
| |
| auto nack2 = makeNack(*interest2, lp::NackReason::CONGESTION); |
| strategyA.afterReceiveNack_count = 0; |
| strategyB.afterReceiveNack_count = 0; |
| forwarder.onIncomingNack(nack2, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0); |
| BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 1); |
| |
| // record Nack on PIT out-record |
| auto outRecord1 = pit1->getOutRecord(*face1); |
| BOOST_REQUIRE(outRecord1 != pit1->out_end()); |
| BOOST_REQUIRE(outRecord1->getIncomingNack() != nullptr); |
| BOOST_CHECK_EQUAL(outRecord1->getIncomingNack()->getReason(), lp::NackReason::CONGESTION); |
| |
| // drop if no PIT entry |
| auto nack3 = makeNack(*makeInterest("/yEcw5HhdM", false, std::nullopt, 243), lp::NackReason::CONGESTION); |
| strategyA.afterReceiveNack_count = 0; |
| strategyB.afterReceiveNack_count = 0; |
| forwarder.onIncomingNack(nack3, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0); |
| BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0); |
| |
| // drop if no out-record |
| auto interest4 = makeInterest("/Etab4KpY", false, std::nullopt, 157); |
| auto pit4 = pit.insert(*interest4).first; |
| pit4->insertOrUpdateOutRecord(*face1, *interest4); |
| |
| auto nack4a = makeNack(*interest4, lp::NackReason::CONGESTION); |
| strategyA.afterReceiveNack_count = 0; |
| strategyB.afterReceiveNack_count = 0; |
| forwarder.onIncomingNack(nack4a, FaceEndpoint(*face2)); |
| BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0); |
| BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0); |
| |
| // drop if Nonce does not match out-record |
| auto nack4b = makeNack(*makeInterest("/Etab4KpY", false, std::nullopt, 294), lp::NackReason::CONGESTION); |
| strategyA.afterReceiveNack_count = 0; |
| strategyB.afterReceiveNack_count = 0; |
| forwarder.onIncomingNack(nack4b, FaceEndpoint(*face1)); |
| BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0); |
| BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0); |
| |
| // drop if inFace is multi-access |
| pit4->insertOrUpdateOutRecord(*face3, *interest4); |
| strategyA.afterReceiveNack_count = 0; |
| strategyB.afterReceiveNack_count = 0; |
| forwarder.onIncomingNack(nack4a, FaceEndpoint(*face3)); |
| BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0); |
| BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0); |
| } |
| |
| BOOST_AUTO_TEST_CASE(OutgoingNack) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| auto face3 = addFace("dummy://", "dummy://", |
| ndn::nfd::FACE_SCOPE_NON_LOCAL, |
| ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| ndn::nfd::LINK_TYPE_MULTI_ACCESS); |
| auto face4 = addFace(); |
| face4->setId(face::INVALID_FACEID); |
| |
| Pit& pit = forwarder.getPit(); |
| |
| lp::NackHeader nackHeader; |
| nackHeader.setReason(lp::NackReason::CONGESTION); |
| |
| // don't send Nack if there's no in-record |
| auto interest1 = makeInterest("/fM5IVEtC", false, std::nullopt, 719); |
| auto pit1 = pit.insert(*interest1).first; |
| pit1->insertOrUpdateInRecord(*face1, *interest1); |
| |
| face2->sentNacks.clear(); |
| BOOST_CHECK(!forwarder.onOutgoingNack(nackHeader, *face2, pit1)); |
| BOOST_CHECK_EQUAL(face2->sentNacks.size(), 0); |
| |
| // send Nack with correct Nonce |
| auto interest2a = makeInterest("/Vi8tRm9MG3", false, std::nullopt, 152); |
| auto pit2 = pit.insert(*interest2a).first; |
| pit2->insertOrUpdateInRecord(*face1, *interest2a); |
| auto interest2b = makeInterest("/Vi8tRm9MG3", false, std::nullopt, 808); |
| pit2->insertOrUpdateInRecord(*face2, *interest2b); |
| face1->sentNacks.clear(); |
| face2->sentNacks.clear(); |
| |
| BOOST_CHECK(forwarder.onOutgoingNack(nackHeader, *face1, pit2)); |
| BOOST_REQUIRE_EQUAL(face1->sentNacks.size(), 1); |
| BOOST_CHECK_EQUAL(face1->sentNacks.back().getReason(), lp::NackReason::CONGESTION); |
| BOOST_CHECK_EQUAL(face1->sentNacks.back().getInterest().getNonce(), 152); |
| BOOST_CHECK_EQUAL(face2->sentNacks.size(), 0); |
| |
| // in-record is erased |
| auto inRecord2a = pit2->getInRecord(*face1); |
| BOOST_CHECK(inRecord2a == pit2->in_end()); |
| |
| // send Nack with correct Nonce |
| BOOST_CHECK(forwarder.onOutgoingNack(nackHeader, *face2, pit2)); |
| BOOST_CHECK_EQUAL(face1->sentNacks.size(), 1); |
| BOOST_REQUIRE_EQUAL(face2->sentNacks.size(), 1); |
| BOOST_CHECK_EQUAL(face2->sentNacks.back().getReason(), lp::NackReason::CONGESTION); |
| BOOST_CHECK_EQUAL(face2->sentNacks.back().getInterest().getNonce(), 808); |
| |
| // in-record is erased |
| auto inRecord2b = pit2->getInRecord(*face2); |
| BOOST_CHECK(inRecord2b == pit2->in_end()); |
| |
| // don't send Nack to multi-access face |
| auto interest2c = makeInterest("/Vi8tRm9MG3", false, std::nullopt, 228); |
| pit2->insertOrUpdateInRecord(*face3, *interest2c); |
| |
| face3->sentNacks.clear(); |
| BOOST_CHECK(!forwarder.onOutgoingNack(nackHeader, *face3, pit2)); |
| BOOST_CHECK_EQUAL(face3->sentNacks.size(), 0); |
| |
| // don't send Nack to face with invalid ID |
| auto interest1b = makeInterest("/fM5IVEtC", false, std::nullopt, 553); |
| pit1->insertOrUpdateInRecord(*face4, *interest1b); |
| |
| face4->sentNacks.clear(); |
| BOOST_CHECK(!forwarder.onOutgoingNack(nackHeader, *face4, pit1)); |
| BOOST_CHECK_EQUAL(face4->sentNacks.size(), 0); |
| } |
| |
| BOOST_AUTO_TEST_CASE(InterestLoopNack) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| auto face3 = addFace("dummy://", "dummy://", |
| ndn::nfd::FACE_SCOPE_NON_LOCAL, |
| ndn::nfd::FACE_PERSISTENCY_PERSISTENT, |
| ndn::nfd::LINK_TYPE_MULTI_ACCESS); |
| auto face4 = addFace(); |
| |
| Fib& fib = forwarder.getFib(); |
| fib::Entry* entry = fib.insert("/zT4XwK0Hnx").first; |
| fib.addOrUpdateNextHop(*entry, *face4, 0); |
| |
| // receive Interest on face1 |
| face1->sentNacks.clear(); |
| auto interest1a = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 732); |
| face1->receiveInterest(*interest1a); |
| BOOST_CHECK(face1->sentNacks.empty()); |
| |
| // receive Interest with duplicate Nonce on face1: legit retransmission |
| face1->sentNacks.clear(); |
| auto interest1b = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 732); |
| face1->receiveInterest(*interest1b); |
| BOOST_CHECK(face1->sentNacks.empty()); |
| |
| // receive Interest with duplicate Nonce on face2 |
| face2->sentNacks.clear(); |
| auto interest2a = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 732); |
| face2->receiveInterest(*interest2a); |
| BOOST_REQUIRE_EQUAL(face2->sentNacks.size(), 1); |
| BOOST_CHECK_EQUAL(face2->sentNacks.back().getInterest().wireEncode(), interest2a->wireEncode()); |
| BOOST_CHECK_EQUAL(face2->sentNacks.back().getReason(), lp::NackReason::DUPLICATE); |
| |
| // receive Interest with new Nonce on face2 |
| face2->sentNacks.clear(); |
| auto interest2b = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 944); |
| face2->receiveInterest(*interest2b); |
| BOOST_CHECK(face2->sentNacks.empty()); |
| |
| // receive Interest with duplicate Nonce on face3, don't send Nack to multi-access face |
| face3->sentNacks.clear(); |
| auto interest3a = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 732); |
| face3->receiveInterest(*interest3a); |
| BOOST_CHECK(face3->sentNacks.empty()); |
| } |
| |
| BOOST_AUTO_TEST_CASE(InterestLoopWithShortLifetime) // Bug 1953 |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| |
| // cause an Interest sent out of face2 to loop back into face1 after a delay |
| face2->afterSend.connect([face1, face2] (uint32_t pktType) { |
| if (pktType == tlv::Interest) { |
| auto interest = make_shared<Interest>(face2->sentInterests.back()); |
| getScheduler().schedule(170_ms, [face1, interest] { face1->receiveInterest(*interest); }); |
| } |
| }); |
| |
| Fib& fib = forwarder.getFib(); |
| fib::Entry* entry = fib.insert("/A").first; |
| fib.addOrUpdateNextHop(*entry, *face2, 0); |
| |
| // receive an Interest |
| auto interest = makeInterest("/A/1", false, 50_ms, 82101183); |
| face1->receiveInterest(*interest); |
| |
| // interest should be forwarded only once, as long as Nonce is in Dead Nonce List |
| BOOST_ASSERT(25_ms * 40 < forwarder.getDeadNonceList().getLifetime()); |
| this->advanceClocks(25_ms, 40); |
| BOOST_CHECK_EQUAL(face2->sentInterests.size(), 1); |
| |
| // It's unnecessary to check that Interest with duplicate Nonce can be forwarded again |
| // after it's gone from Dead Nonce List, because the entry lifetime of Dead Nonce List |
| // is an implementation decision. NDN protocol requires Name+Nonce to be unique, |
| // without specifying when Name+Nonce could repeat. Forwarder is permitted to suppress |
| // an Interest if its Name+Nonce has appeared any point in the past. |
| } |
| |
| BOOST_AUTO_TEST_CASE(PitLeak) // Bug 3484 |
| { |
| auto face1 = addFace(); |
| auto interest = makeInterest("/hcLSAsQ9A", false, 2_s, 61883075); |
| |
| auto& dnl = forwarder.getDeadNonceList(); |
| dnl.add(interest->getName(), interest->getNonce()); |
| auto& pit = forwarder.getPit(); |
| BOOST_CHECK_EQUAL(pit.size(), 0); |
| |
| forwarder.onIncomingInterest(*interest, FaceEndpoint(*face1)); |
| this->advanceClocks(100_ms, 20_s); |
| BOOST_CHECK_EQUAL(pit.size(), 0); |
| } |
| |
| BOOST_AUTO_TEST_CASE(UnsolicitedData) |
| { |
| auto face1 = addFace(); |
| auto data = makeData("/A"); |
| |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 0); |
| forwarder.onIncomingData(*data, FaceEndpoint(*face1)); |
| this->advanceClocks(1_ms, 10_ms); |
| BOOST_CHECK_EQUAL(forwarder.getCounters().nUnsolicitedData, 1); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NewNextHop) |
| { |
| auto face1 = addFace(); |
| auto face2 = addFace(); |
| auto face3 = addFace(); |
| auto face4 = addFace(); |
| |
| auto& strategy = choose<DummyStrategy>(forwarder, "/A", DummyStrategy::getStrategyName()); |
| |
| Fib& fib = forwarder.getFib(); |
| Pit& pit = forwarder.getPit(); |
| |
| // fib: "/", "/A/B", "/A/B/C/D/E" |
| fib::Entry* entry = fib.insert("/").first; |
| fib.addOrUpdateNextHop(*entry, *face1, 100); |
| entry = fib.insert("/A/B").first; |
| fib.addOrUpdateNextHop(*entry, *face2, 0); |
| entry = fib.insert("/A/B/C/D/E").first; |
| fib.addOrUpdateNextHop(*entry, *face3, 0); |
| |
| // pit: "/A", "/A/B/C", "/A/B/Z" |
| auto interest1 = makeInterest("/A"); |
| auto pit1 = pit.insert(*interest1).first; |
| pit1->insertOrUpdateInRecord(*face3, *interest1); |
| auto interest2 = makeInterest("/A/B/C"); |
| auto pit2 = pit.insert(*interest2).first; |
| pit2->insertOrUpdateInRecord(*face3, *interest2); |
| auto interest3 = makeInterest("/A/B/Z"); |
| auto pit3 = pit.insert(*interest3).first; |
| pit3->insertOrUpdateInRecord(*face3, *interest3); |
| |
| // new nexthop for "/" |
| entry = fib.insert("/").first; |
| fib.addOrUpdateNextHop(*entry, *face2, 50); |
| |
| // /A --> triggered |
| // /A/B/C --> not triggered |
| // /A/B/Z --> not triggered |
| BOOST_TEST_REQUIRE(strategy.afterNewNextHopCalls.size() == 1); |
| BOOST_TEST(strategy.afterNewNextHopCalls[0] == "/A"); |
| strategy.afterNewNextHopCalls.clear(); |
| |
| // new nexthop for "/A" |
| entry = fib.insert("/A").first; |
| fib.addOrUpdateNextHop(*entry, *face4, 50); |
| |
| // /A --> triggered |
| // /A/B/C --> not triggered |
| // /A/B/Z --> not triggered |
| BOOST_TEST_REQUIRE(strategy.afterNewNextHopCalls.size() == 1); |
| BOOST_TEST(strategy.afterNewNextHopCalls[0] == "/A"); |
| strategy.afterNewNextHopCalls.clear(); |
| |
| // new nexthop for "/A/B" |
| entry = fib.insert("/A/B").first; |
| fib.addOrUpdateNextHop(*entry, *face4, 0); |
| |
| // /A --> not triggered |
| // /A/B/C --> triggered |
| // /A/B/Z --> triggered |
| BOOST_TEST_REQUIRE(strategy.afterNewNextHopCalls.size() == 2); |
| BOOST_TEST(strategy.afterNewNextHopCalls[0] == "/A/B/C"); |
| BOOST_TEST(strategy.afterNewNextHopCalls[1] == "/A/B/Z"); |
| strategy.afterNewNextHopCalls.clear(); |
| |
| // new nexthop for "/A/B/C/D" |
| entry = fib.insert("/A/B/C/D").first; |
| fib.addOrUpdateNextHop(*entry, *face1, 0); |
| |
| // nothing triggered |
| BOOST_TEST(strategy.afterNewNextHopCalls.size() == 0); |
| |
| // create a second pit entry for /A |
| auto interest4 = makeInterest("/A"); |
| interest4->setMustBeFresh(true); |
| auto pit4 = pit.insert(*interest4).first; |
| pit4->insertOrUpdateInRecord(*face3, *interest4); |
| |
| // new nexthop for "/A" |
| entry = fib.insert("/A").first; |
| fib.addOrUpdateNextHop(*entry, *face1, 0); |
| |
| // /A --> triggered twice |
| // /A/B/C --> not triggered |
| // /A/B/Z --> not triggered |
| BOOST_TEST_REQUIRE(strategy.afterNewNextHopCalls.size() == 2); |
| BOOST_TEST(strategy.afterNewNextHopCalls[0] == "/A"); |
| BOOST_TEST(strategy.afterNewNextHopCalls[1] == "/A"); |
| } |
| |
| BOOST_AUTO_TEST_SUITE(ProcessConfig) |
| |
| BOOST_AUTO_TEST_CASE(DefaultHopLimit) |
| { |
| ConfigFile cf; |
| forwarder.setConfigFile(cf); |
| |
| std::string config = R"CONFIG( |
| forwarder |
| { |
| default_hop_limit 10 |
| } |
| )CONFIG"; |
| |
| // The default value is 0 |
| BOOST_TEST(forwarder.m_config.defaultHopLimit == 0); |
| |
| // Dry run parsing should not change the default config |
| cf.parse(config, true, "dummy-config"); |
| BOOST_TEST(forwarder.m_config.defaultHopLimit == 0); |
| |
| // Check if the actual parsing works |
| cf.parse(config, false, "dummy-config"); |
| BOOST_TEST(forwarder.m_config.defaultHopLimit == 10); |
| |
| // After removing default_hop_limit from the config file, |
| // the default value of zero should be restored |
| config = R"CONFIG( |
| forwarder |
| { |
| } |
| )CONFIG"; |
| |
| cf.parse(config, false, "dummy-config"); |
| BOOST_TEST(forwarder.m_config.defaultHopLimit == 0); |
| } |
| |
| BOOST_AUTO_TEST_CASE(BadDefaultHopLimit) |
| { |
| ConfigFile cf; |
| forwarder.setConfigFile(cf); |
| |
| // not a number |
| std::string config = R"CONFIG( |
| forwarder |
| { |
| default_hop_limit hello |
| } |
| )CONFIG"; |
| |
| BOOST_CHECK_THROW(cf.parse(config, true, "dummy-config"), ConfigFile::Error); |
| BOOST_CHECK_THROW(cf.parse(config, false, "dummy-config"), ConfigFile::Error); |
| |
| // negative number |
| config = R"CONFIG( |
| forwarder |
| { |
| default_hop_limit -1 |
| } |
| )CONFIG"; |
| |
| BOOST_CHECK_THROW(cf.parse(config, true, "dummy-config"), ConfigFile::Error); |
| BOOST_CHECK_THROW(cf.parse(config, false, "dummy-config"), ConfigFile::Error); |
| |
| // out of range |
| config = R"CONFIG( |
| forwarder |
| { |
| default_hop_limit 256 |
| } |
| )CONFIG"; |
| |
| BOOST_CHECK_THROW(cf.parse(config, true, "dummy-config"), ConfigFile::Error); |
| BOOST_CHECK_THROW(cf.parse(config, false, "dummy-config"), ConfigFile::Error); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // ProcessConfig |
| |
| BOOST_AUTO_TEST_SUITE_END() // TestForwarder |
| BOOST_AUTO_TEST_SUITE_END() // Fw |
| |
| } // namespace nfd::tests |