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 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 21 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 22 | virtual void |
| 23 | sendInterest(const Interest& interest) |
| 24 | { |
| 25 | m_sentInterests.push_back(interest); |
| 26 | m_ioService.stop(); |
| 27 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 28 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 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 | } |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 46 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 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 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 65 | resetGlobalIoService(); |
| 66 | Forwarder forwarder; |
| 67 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 68 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 69 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 71 | BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID); |
| 72 | BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 73 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 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 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 90 | resetGlobalIoService(); |
| 91 | boost::asio::io_service& io = getGlobalIoService(); |
| 92 | Forwarder forwarder; |
| 93 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 94 | Name nameA ("ndn:/A"); |
| 95 | Name nameAB ("ndn:/A/B"); |
| 96 | Name nameABC("ndn:/A/B/C"); |
| 97 | Interest interestAB(nameAB); |
| 98 | interestAB.setInterestLifetime(4000); |
| 99 | Data dataABC(nameABC); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 100 | |
| 101 | shared_ptr<ForwarderTestFace> face1 = make_shared<ForwarderTestFace>(boost::ref(io)); |
| 102 | shared_ptr<ForwarderTestFace> face2 = make_shared<ForwarderTestFace>(boost::ref(io)); |
| 103 | forwarder.addFace(face1); |
| 104 | forwarder.addFace(face2); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 105 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 106 | Fib& fib = forwarder.getFib(); |
| 107 | std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = |
| 108 | fib.insert(Name("ndn:/A")); |
| 109 | shared_ptr<fib::Entry> fibEntry = fibInsertResult.first; |
| 110 | fibEntry->addNextHop(face2, 0); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 112 | face1->receiveInterest(interestAB); |
| 113 | io.run(); |
| 114 | io.reset(); |
| 115 | BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1); |
| 116 | BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB)); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 119 | face2->receiveData(dataABC); |
| 120 | io.run(); |
| 121 | io.reset(); |
| 122 | BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1); |
| 123 | BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC)); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 127 | class ScopeLocalhostTestForwarder : public Forwarder |
| 128 | { |
| 129 | public: |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 130 | ScopeLocalhostTestForwarder() |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 131 | { |
| 132 | } |
| 133 | |
| 134 | virtual void |
| 135 | onDataUnsolicited(Face& inFace, const Data& data) |
| 136 | { |
| 137 | ++m_onDataUnsolicited_count; |
| 138 | } |
| 139 | |
| 140 | protected: |
| 141 | virtual void |
| 142 | dispatchToStrategy(const Face& inFace, |
| 143 | const Interest& interest, |
| 144 | shared_ptr<fib::Entry> fibEntry, |
| 145 | shared_ptr<pit::Entry> pitEntry) |
| 146 | { |
| 147 | ++m_dispatchToStrategy_count; |
| 148 | } |
| 149 | |
| 150 | public: |
| 151 | int m_dispatchToStrategy_count; |
| 152 | int m_onDataUnsolicited_count; |
| 153 | }; |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(ScopeLocalhost) |
| 156 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 157 | resetGlobalIoService(); |
| 158 | ScopeLocalhostTestForwarder forwarder; |
Alexander Afanasyev | bd220a0 | 2014-02-20 00:29:56 -0800 | [diff] [blame] | 159 | shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>(); |
| 160 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 161 | forwarder.addFace(face1); |
| 162 | forwarder.addFace(face2); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 163 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 164 | // local face, /localhost: OK |
| 165 | forwarder.m_dispatchToStrategy_count = 0; |
| 166 | forwarder.onIncomingInterest(*face1, Interest(Name("/localhost/A1"))); |
| 167 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 168 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 169 | // non-local face, /localhost: violate |
| 170 | forwarder.m_dispatchToStrategy_count = 0; |
| 171 | forwarder.onIncomingInterest(*face2, Interest(Name("/localhost/A2"))); |
| 172 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 174 | // local face, non-/localhost: OK |
| 175 | forwarder.m_dispatchToStrategy_count = 0; |
| 176 | forwarder.onIncomingInterest(*face1, Interest(Name("/A3"))); |
| 177 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 178 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 179 | // non-local face, non-/localhost: OK |
| 180 | forwarder.m_dispatchToStrategy_count = 0; |
| 181 | forwarder.onIncomingInterest(*face2, Interest(Name("/A4"))); |
| 182 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 183 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 184 | // local face, /localhost: OK |
| 185 | forwarder.m_onDataUnsolicited_count = 0; |
| 186 | forwarder.onIncomingData(*face1, Data(Name("/localhost/B1"))); |
| 187 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 188 | |
| 189 | // non-local face, /localhost: OK |
| 190 | forwarder.m_onDataUnsolicited_count = 0; |
| 191 | forwarder.onIncomingData(*face2, Data(Name("/localhost/B2"))); |
| 192 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 193 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 194 | // local face, non-/localhost: OK |
| 195 | forwarder.m_onDataUnsolicited_count = 0; |
| 196 | forwarder.onIncomingData(*face1, Data(Name("/B3"))); |
| 197 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 198 | |
| 199 | // non-local face, non-/localhost: OK |
| 200 | forwarder.m_onDataUnsolicited_count = 0; |
| 201 | forwarder.onIncomingData(*face2, Data(Name("/B4"))); |
| 202 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 203 | } |
| 204 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 205 | BOOST_AUTO_TEST_SUITE_END() |
| 206 | |
| 207 | } // namespace nfd |