diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp index d438f106..a605ef0d 100644 --- a/daemon/fw/forwarder.cpp +++ b/daemon/fw/forwarder.cpp @@ -117,8 +117,7 @@ Forwarder::onIncomingInterest(Face& inFace, const Interest& interest) } // insert InRecord - pit::InRecordCollection::iterator inRecordIt = - pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest); + pitEntry->insertOrUpdateInRecord(inFace.shared_from_this(), interest); // app chosen nexthops bool isAppChosenNexthops = false; // TODO get from local control header @@ -150,13 +149,21 @@ Forwarder::onOutgoingInterest(shared_ptr pitEntry, Face& outFace) { NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() << " interest=" << pitEntry->getName()); + // /localhost scope control + bool violatesLocalhost = !outFace.isLocal() && + s_localhostName.isPrefixOf(pitEntry->getName()); + if (violatesLocalhost) { + NFD_LOG_DEBUG("onOutgoingInterest face=" << outFace.getId() + << " interest=" << pitEntry->getName() << " violates /localhost"); + return; + } + // pick Interest const Interest& interest = pitEntry->getInterest(); // TODO pick the last incoming Interest // insert OutRecord - pit::OutRecordCollection::iterator outRecordIt = - pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest); + pitEntry->insertOrUpdateOutRecord(outFace.shared_from_this(), interest); // set PIT unsatisfy timer this->setUnsatisfyTimer(pitEntry); @@ -198,7 +205,7 @@ Forwarder::onIncomingData(Face& inFace, const Data& data) s_localhostName.isPrefixOf(data.getName()); if (violatesLocalhost) { NFD_LOG_DEBUG("onIncomingData face=" << inFace.getId() - << " interest=" << data.getName() << " violates /localhost"); + << " data=" << data.getName() << " violates /localhost"); return; } @@ -269,6 +276,15 @@ Forwarder::onOutgoingData(const Data& data, Face& outFace) { NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() << " data=" << data.getName()); + // /localhost scope control + bool violatesLocalhost = !outFace.isLocal() && + s_localhostName.isPrefixOf(data.getName()); + if (violatesLocalhost) { + NFD_LOG_DEBUG("onOutgoingData face=" << outFace.getId() + << " data=" << data.getName() << " violates /localhost"); + return; + } + // traffic manager // pass through diff --git a/tests/face/dummy-face.hpp b/tests/face/dummy-face.hpp index 92100b6a..45a3d43d 100644 --- a/tests/face/dummy-face.hpp +++ b/tests/face/dummy-face.hpp @@ -13,8 +13,7 @@ namespace nfd { /** \class DummyFace - * \brief provides a Face that cannot communicate - * for unit testing only + * \brief a Face for unit testing */ template class DummyFaceImpl : public FaceBase @@ -23,11 +22,15 @@ public: virtual void sendInterest(const Interest& interest) { + m_sentInterests.push_back(interest); + this->afterSend(); } - + virtual void sendData(const Data& data) { + m_sentDatas.push_back(data); + this->afterSend(); } virtual void @@ -35,7 +38,27 @@ public: { } -private: + void + receiveInterest(const Interest& interest) + { + this->onReceiveInterest(interest); + } + + void + receiveData(const Data& data) + { + this->onReceiveData(data); + } + +protected: + virtual void + afterSend() + { + } + +public: + std::vector m_sentInterests; + std::vector m_sentDatas; }; typedef DummyFaceImpl DummyFace; diff --git a/tests/fw/forwarder.cpp b/tests/fw/forwarder.cpp index daa80c7b..66077660 100644 --- a/tests/fw/forwarder.cpp +++ b/tests/fw/forwarder.cpp @@ -11,53 +11,6 @@ namespace nfd { -class ForwarderTestFace : public Face { -public: - explicit - ForwarderTestFace(boost::asio::io_service& ioService) - : m_ioService(ioService) - { - } - - virtual void - sendInterest(const Interest& interest) - { - m_sentInterests.push_back(interest); - m_ioService.stop(); - } - - virtual void - sendData(const Data& data) - { - m_sentDatas.push_back(data); - m_ioService.stop(); - } - - virtual void - close() - { - } - - void - receiveInterest(const Interest& interest) - { - onReceiveInterest(interest); - } - - void - receiveData(const Data& data) - { - onReceiveData(data); - } - -public: - std::vector m_sentInterests; - std::vector m_sentDatas; - -private: - boost::asio::io_service& m_ioService; -}; - BOOST_AUTO_TEST_SUITE(FwForwarder) BOOST_AUTO_TEST_CASE(AddRemoveFace) @@ -85,10 +38,18 @@ BOOST_AUTO_TEST_CASE(AddRemoveFace) BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); } +class ForwarderTestFace : public DummyFace { +public: + virtual void + afterSend() + { + getGlobalIoService().stop(); + } +}; + BOOST_AUTO_TEST_CASE(SimpleExchange) { resetGlobalIoService(); - boost::asio::io_service& io = getGlobalIoService(); Forwarder forwarder; Name nameA ("ndn:/A"); @@ -98,8 +59,8 @@ BOOST_AUTO_TEST_CASE(SimpleExchange) interestAB.setInterestLifetime(4000); Data dataABC(nameABC); - shared_ptr face1 = make_shared(boost::ref(io)); - shared_ptr face2 = make_shared(boost::ref(io)); + shared_ptr face1 = make_shared(); + shared_ptr face2 = make_shared(); forwarder.addFace(face1); forwarder.addFace(face2); @@ -110,24 +71,24 @@ BOOST_AUTO_TEST_CASE(SimpleExchange) fibEntry->addNextHop(face2, 0); face1->receiveInterest(interestAB); - io.run(); - io.reset(); + getGlobalIoService().run(); + getGlobalIoService().reset(); BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1); BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB)); BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId()); face2->receiveData(dataABC); - io.run(); - io.reset(); + getGlobalIoService().run(); + getGlobalIoService().reset(); BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1); BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC)); BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId()); } -class ScopeLocalhostTestForwarder : public Forwarder +class ScopeLocalhostIncomingTestForwarder : public Forwarder { public: - ScopeLocalhostTestForwarder() + ScopeLocalhostIncomingTestForwarder() { } @@ -152,56 +113,121 @@ public: int m_onDataUnsolicited_count; }; -BOOST_AUTO_TEST_CASE(ScopeLocalhost) +BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming) { resetGlobalIoService(); - ScopeLocalhostTestForwarder forwarder; - shared_ptr face1 = make_shared(); - shared_ptr face2 = make_shared(); + ScopeLocalhostIncomingTestForwarder forwarder; + shared_ptr face1 = make_shared(); + shared_ptr face2 = make_shared(); forwarder.addFace(face1); forwarder.addFace(face2); // local face, /localhost: OK forwarder.m_dispatchToStrategy_count = 0; - forwarder.onIncomingInterest(*face1, Interest(Name("/localhost/A1"))); + forwarder.onIncomingInterest(*face1, Interest("/localhost/A1")); BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); // non-local face, /localhost: violate forwarder.m_dispatchToStrategy_count = 0; - forwarder.onIncomingInterest(*face2, Interest(Name("/localhost/A2"))); + forwarder.onIncomingInterest(*face2, Interest("/localhost/A2")); BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0); // local face, non-/localhost: OK forwarder.m_dispatchToStrategy_count = 0; - forwarder.onIncomingInterest(*face1, Interest(Name("/A3"))); + forwarder.onIncomingInterest(*face1, Interest("/A3")); BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); // non-local face, non-/localhost: OK forwarder.m_dispatchToStrategy_count = 0; - forwarder.onIncomingInterest(*face2, Interest(Name("/A4"))); + forwarder.onIncomingInterest(*face2, Interest("/A4")); BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); // local face, /localhost: OK forwarder.m_onDataUnsolicited_count = 0; - forwarder.onIncomingData(*face1, Data(Name("/localhost/B1"))); + forwarder.onIncomingData(*face1, Data("/localhost/B1")); BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); // non-local face, /localhost: OK forwarder.m_onDataUnsolicited_count = 0; - forwarder.onIncomingData(*face2, Data(Name("/localhost/B2"))); + forwarder.onIncomingData(*face2, Data("/localhost/B2")); BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0); // local face, non-/localhost: OK forwarder.m_onDataUnsolicited_count = 0; - forwarder.onIncomingData(*face1, Data(Name("/B3"))); + forwarder.onIncomingData(*face1, Data("/B3")); BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); // non-local face, non-/localhost: OK forwarder.m_onDataUnsolicited_count = 0; - forwarder.onIncomingData(*face2, Data(Name("/B4"))); + forwarder.onIncomingData(*face2, Data("/B4")); BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); } +BOOST_AUTO_TEST_CASE(ScopeLocalhostOutgoing) +{ + resetGlobalIoService(); + Forwarder forwarder; + shared_ptr face1 = make_shared(); + shared_ptr face2 = make_shared(); + shared_ptr face3 = make_shared(); + forwarder.addFace(face1); + forwarder.addFace(face2); + forwarder.addFace(face3); + Pit& pit = forwarder.getPit(); + + // local face, /localhost: OK + Interest interestA1 = Interest("/localhost/A1"); + shared_ptr pitA1 = pit.insert(interestA1).first; + pitA1->insertOrUpdateInRecord(face3, interestA1); + face1->m_sentInterests.clear(); + forwarder.onOutgoingInterest(pitA1, *face1); + BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1); + + // non-local face, /localhost: violate + Interest interestA2 = Interest("/localhost/A2"); + shared_ptr pitA2 = pit.insert(interestA2).first; + pitA2->insertOrUpdateInRecord(face3, interestA1); + face2->m_sentInterests.clear(); + forwarder.onOutgoingInterest(pitA2, *face2); + BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 0); + + // local face, non-/localhost: OK + Interest interestA3 = Interest("/A3"); + shared_ptr pitA3 = pit.insert(interestA3).first; + pitA3->insertOrUpdateInRecord(face3, interestA3); + face1->m_sentInterests.clear(); + forwarder.onOutgoingInterest(pitA3, *face1); + BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1); + + // non-local face, non-/localhost: OK + Interest interestA4 = Interest("/A4"); + shared_ptr pitA4 = pit.insert(interestA4).first; + pitA4->insertOrUpdateInRecord(face3, interestA4); + face2->m_sentInterests.clear(); + forwarder.onOutgoingInterest(pitA4, *face2); + BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 1); + + // local face, /localhost: OK + face1->m_sentDatas.clear(); + forwarder.onOutgoingData(Data("/localhost/B1"), *face1); + BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1); + + // non-local face, /localhost: OK + face2->m_sentDatas.clear(); + forwarder.onOutgoingData(Data("/localhost/B2"), *face2); + BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 0); + + // local face, non-/localhost: OK + face1->m_sentDatas.clear(); + forwarder.onOutgoingData(Data("/B3"), *face1); + BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1); + + // non-local face, non-/localhost: OK + face2->m_sentDatas.clear(); + forwarder.onOutgoingData(Data("/B4"), *face2); + BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 1); +} + BOOST_AUTO_TEST_SUITE_END() } // namespace nfd