Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "fw/forwarder.hpp" |
| 8 | #include "../face/dummy-face.hpp" |
| 9 | |
| 10 | #include <boost/test/unit_test.hpp> |
| 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | class ForwarderTestFace : public Face { |
| 15 | public: |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 16 | explicit |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 17 | ForwarderTestFace(boost::asio::io_service& ioService) |
| 18 | : m_ioService(ioService) |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | virtual void |
| 23 | sendInterest(const Interest& interest) |
| 24 | { |
| 25 | m_sentInterests.push_back(interest); |
| 26 | m_ioService.stop(); |
| 27 | } |
| 28 | |
| 29 | virtual void |
| 30 | sendData(const Data& data) |
| 31 | { |
| 32 | m_sentDatas.push_back(data); |
| 33 | m_ioService.stop(); |
| 34 | } |
| 35 | |
Alexander Afanasyev | a0a10fb | 2014-02-13 19:56:15 -0800 | [diff] [blame] | 36 | virtual void |
| 37 | close() |
| 38 | { |
| 39 | } |
| 40 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 41 | void |
| 42 | receiveInterest(const Interest& interest) |
| 43 | { |
| 44 | onReceiveInterest(interest); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | receiveData(const Data& data) |
| 49 | { |
| 50 | onReceiveData(data); |
| 51 | } |
| 52 | |
| 53 | public: |
| 54 | std::vector<Interest> m_sentInterests; |
| 55 | std::vector<Data> m_sentDatas; |
| 56 | |
| 57 | private: |
| 58 | boost::asio::io_service& m_ioService; |
| 59 | }; |
| 60 | |
| 61 | BOOST_AUTO_TEST_SUITE(FwForwarder) |
| 62 | |
| 63 | BOOST_AUTO_TEST_CASE(AddRemoveFace) |
| 64 | { |
| 65 | boost::asio::io_service io; |
| 66 | Forwarder forwarder(io); |
| 67 | |
| 68 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 69 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 70 | |
| 71 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
| 72 | BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); |
| 73 | |
| 74 | forwarder.addFace(face1); |
| 75 | forwarder.addFace(face2); |
| 76 | |
| 77 | BOOST_CHECK_NE(face1->getId(), INVALID_FACEID); |
| 78 | BOOST_CHECK_NE(face2->getId(), INVALID_FACEID); |
| 79 | BOOST_CHECK_NE(face1->getId(), face2->getId()); |
| 80 | |
| 81 | forwarder.removeFace(face1); |
| 82 | forwarder.removeFace(face2); |
| 83 | |
| 84 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
| 85 | BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE(SimpleExchange) |
| 89 | { |
| 90 | Name nameA ("ndn:/A"); |
| 91 | Name nameAB ("ndn:/A/B"); |
| 92 | Name nameABC("ndn:/A/B/C"); |
| 93 | Interest interestAB(nameAB); |
| 94 | interestAB.setInterestLifetime(4000); |
| 95 | Data dataABC(nameABC); |
| 96 | |
| 97 | boost::asio::io_service io; |
| 98 | Forwarder forwarder(io); |
| 99 | |
| 100 | shared_ptr<ForwarderTestFace> face1 = make_shared<ForwarderTestFace>(boost::ref(io)); |
| 101 | shared_ptr<ForwarderTestFace> face2 = make_shared<ForwarderTestFace>(boost::ref(io)); |
| 102 | forwarder.addFace(face1); |
| 103 | forwarder.addFace(face2); |
| 104 | |
| 105 | Fib& fib = forwarder.getFib(); |
| 106 | std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = |
| 107 | fib.insert(Name("ndn:/A")); |
| 108 | shared_ptr<fib::Entry> fibEntry = fibInsertResult.first; |
| 109 | fibEntry->addNextHop(face2, 0); |
| 110 | |
| 111 | face1->receiveInterest(interestAB); |
| 112 | io.run(); |
| 113 | io.reset(); |
| 114 | BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1); |
| 115 | BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB)); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 117 | |
| 118 | face2->receiveData(dataABC); |
| 119 | io.run(); |
| 120 | io.reset(); |
| 121 | BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1); |
| 122 | BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC)); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 126 | class ScopeLocalhostTestForwarder : public Forwarder |
| 127 | { |
| 128 | public: |
| 129 | explicit |
| 130 | ScopeLocalhostTestForwarder(boost::asio::io_service& ioService) |
| 131 | : Forwarder(ioService) |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | virtual void |
| 136 | onDataUnsolicited(Face& inFace, const Data& data) |
| 137 | { |
| 138 | ++m_onDataUnsolicited_count; |
| 139 | } |
| 140 | |
| 141 | protected: |
| 142 | virtual void |
| 143 | dispatchToStrategy(const Face& inFace, |
| 144 | const Interest& interest, |
| 145 | shared_ptr<fib::Entry> fibEntry, |
| 146 | shared_ptr<pit::Entry> pitEntry) |
| 147 | { |
| 148 | ++m_dispatchToStrategy_count; |
| 149 | } |
| 150 | |
| 151 | public: |
| 152 | int m_dispatchToStrategy_count; |
| 153 | int m_onDataUnsolicited_count; |
| 154 | }; |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE(ScopeLocalhost) |
| 157 | { |
| 158 | boost::asio::io_service io; |
| 159 | ScopeLocalhostTestForwarder forwarder(io); |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame^] | 160 | shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>(); |
| 161 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 162 | forwarder.addFace(face1); |
| 163 | forwarder.addFace(face2); |
| 164 | |
| 165 | // local face, /localhost: OK |
| 166 | forwarder.m_dispatchToStrategy_count = 0; |
| 167 | forwarder.onIncomingInterest(*face1, Interest(Name("/localhost/A1"))); |
| 168 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
| 169 | |
| 170 | // non-local face, /localhost: violate |
| 171 | forwarder.m_dispatchToStrategy_count = 0; |
| 172 | forwarder.onIncomingInterest(*face2, Interest(Name("/localhost/A2"))); |
| 173 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0); |
| 174 | |
| 175 | // local face, non-/localhost: OK |
| 176 | forwarder.m_dispatchToStrategy_count = 0; |
| 177 | forwarder.onIncomingInterest(*face1, Interest(Name("/A3"))); |
| 178 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
| 179 | |
| 180 | // non-local face, non-/localhost: OK |
| 181 | forwarder.m_dispatchToStrategy_count = 0; |
| 182 | forwarder.onIncomingInterest(*face2, Interest(Name("/A4"))); |
| 183 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
| 184 | |
| 185 | // local face, /localhost: OK |
| 186 | forwarder.m_onDataUnsolicited_count = 0; |
| 187 | forwarder.onIncomingData(*face1, Data(Name("/localhost/B1"))); |
| 188 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 189 | |
| 190 | // non-local face, /localhost: OK |
| 191 | forwarder.m_onDataUnsolicited_count = 0; |
| 192 | forwarder.onIncomingData(*face2, Data(Name("/localhost/B2"))); |
| 193 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0); |
| 194 | |
| 195 | // local face, non-/localhost: OK |
| 196 | forwarder.m_onDataUnsolicited_count = 0; |
| 197 | forwarder.onIncomingData(*face1, Data(Name("/B3"))); |
| 198 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 199 | |
| 200 | // non-local face, non-/localhost: OK |
| 201 | forwarder.m_onDataUnsolicited_count = 0; |
| 202 | forwarder.onIncomingData(*face2, Data(Name("/B4"))); |
| 203 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 204 | } |
| 205 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 206 | BOOST_AUTO_TEST_SUITE_END() |
| 207 | |
| 208 | } // namespace nfd |