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" |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 8 | #include "tests/face/dummy-face.hpp" |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 9 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 10 | #include "tests/test-common.hpp" |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 11 | |
| 12 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 13 | namespace tests { |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 14 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 15 | BOOST_FIXTURE_TEST_SUITE(FwForwarder, BaseFixture) |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 16 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 17 | BOOST_AUTO_TEST_CASE(SimpleExchange) |
| 18 | { |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 19 | Forwarder forwarder; |
| 20 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 21 | Name nameA ("ndn:/A"); |
| 22 | Name nameAB ("ndn:/A/B"); |
| 23 | Name nameABC("ndn:/A/B/C"); |
| 24 | Interest interestAB(nameAB); |
| 25 | interestAB.setInterestLifetime(4000); |
| 26 | Data dataABC(nameABC); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | shared_ptr<DummyFace> face1 = make_shared<DummyFace>(); |
| 29 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
| 30 | face1->afterSend += bind(&boost::asio::io_service::stop, &g_io); |
| 31 | face2->afterSend += bind(&boost::asio::io_service::stop, &g_io); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 32 | forwarder.addFace(face1); |
| 33 | forwarder.addFace(face2); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 34 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 35 | Fib& fib = forwarder.getFib(); |
| 36 | std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult = |
| 37 | fib.insert(Name("ndn:/A")); |
| 38 | shared_ptr<fib::Entry> fibEntry = fibInsertResult.first; |
| 39 | fibEntry->addNextHop(face2, 0); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 40 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 41 | face1->receiveInterest(interestAB); |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 42 | g_io.run(); |
| 43 | g_io.reset(); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 44 | BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1); |
| 45 | BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB)); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId()); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 47 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 48 | face2->receiveData(dataABC); |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 49 | g_io.run(); |
| 50 | g_io.reset(); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 51 | BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1); |
| 52 | BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC)); |
Junxiao Shi | 06887ac | 2014-02-13 20:15:42 -0700 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId()); |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 56 | class ScopeLocalhostIncomingTestForwarder : public Forwarder |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 57 | { |
| 58 | public: |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 59 | ScopeLocalhostIncomingTestForwarder() |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 60 | { |
| 61 | } |
| 62 | |
| 63 | virtual void |
| 64 | onDataUnsolicited(Face& inFace, const Data& data) |
| 65 | { |
| 66 | ++m_onDataUnsolicited_count; |
| 67 | } |
| 68 | |
| 69 | protected: |
| 70 | virtual void |
| 71 | dispatchToStrategy(const Face& inFace, |
| 72 | const Interest& interest, |
| 73 | shared_ptr<fib::Entry> fibEntry, |
| 74 | shared_ptr<pit::Entry> pitEntry) |
| 75 | { |
| 76 | ++m_dispatchToStrategy_count; |
| 77 | } |
| 78 | |
| 79 | public: |
| 80 | int m_dispatchToStrategy_count; |
| 81 | int m_onDataUnsolicited_count; |
| 82 | }; |
| 83 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 84 | BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming) |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 85 | { |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 86 | ScopeLocalhostIncomingTestForwarder forwarder; |
| 87 | shared_ptr<Face> face1 = make_shared<DummyLocalFace>(); |
| 88 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 89 | forwarder.addFace(face1); |
| 90 | forwarder.addFace(face2); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 91 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 92 | // local face, /localhost: OK |
| 93 | forwarder.m_dispatchToStrategy_count = 0; |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 94 | forwarder.onIncomingInterest(*face1, Interest("/localhost/A1")); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 97 | // non-local face, /localhost: violate |
| 98 | forwarder.m_dispatchToStrategy_count = 0; |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 99 | forwarder.onIncomingInterest(*face2, Interest("/localhost/A2")); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 102 | // local face, non-/localhost: OK |
| 103 | forwarder.m_dispatchToStrategy_count = 0; |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 104 | forwarder.onIncomingInterest(*face1, Interest("/A3")); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 106 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 107 | // non-local face, non-/localhost: OK |
| 108 | forwarder.m_dispatchToStrategy_count = 0; |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 109 | forwarder.onIncomingInterest(*face2, Interest("/A4")); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 111 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 112 | // local face, /localhost: OK |
| 113 | forwarder.m_onDataUnsolicited_count = 0; |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 114 | forwarder.onIncomingData(*face1, Data("/localhost/B1")); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 116 | |
| 117 | // non-local face, /localhost: OK |
| 118 | forwarder.m_onDataUnsolicited_count = 0; |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 119 | forwarder.onIncomingData(*face2, Data("/localhost/B2")); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0); |
Junxiao Shi | c041ca3 | 2014-02-25 20:01:15 -0700 | [diff] [blame] | 121 | |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 122 | // local face, non-/localhost: OK |
| 123 | forwarder.m_onDataUnsolicited_count = 0; |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 124 | forwarder.onIncomingData(*face1, Data("/B3")); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 125 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 126 | |
| 127 | // non-local face, non-/localhost: OK |
| 128 | forwarder.m_onDataUnsolicited_count = 0; |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 129 | forwarder.onIncomingData(*face2, Data("/B4")); |
Junxiao Shi | 8888449 | 2014-02-15 15:57:43 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1); |
| 131 | } |
| 132 | |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 133 | BOOST_AUTO_TEST_CASE(ScopeLocalhostOutgoing) |
| 134 | { |
Junxiao Shi | 9b27bd2 | 2014-02-26 20:29:58 -0700 | [diff] [blame] | 135 | Forwarder forwarder; |
| 136 | shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>(); |
| 137 | shared_ptr<DummyFace> face2 = make_shared<DummyFace>(); |
| 138 | shared_ptr<Face> face3 = make_shared<DummyLocalFace>(); |
| 139 | forwarder.addFace(face1); |
| 140 | forwarder.addFace(face2); |
| 141 | forwarder.addFace(face3); |
| 142 | Pit& pit = forwarder.getPit(); |
| 143 | |
| 144 | // local face, /localhost: OK |
| 145 | Interest interestA1 = Interest("/localhost/A1"); |
| 146 | shared_ptr<pit::Entry> pitA1 = pit.insert(interestA1).first; |
| 147 | pitA1->insertOrUpdateInRecord(face3, interestA1); |
| 148 | face1->m_sentInterests.clear(); |
| 149 | forwarder.onOutgoingInterest(pitA1, *face1); |
| 150 | BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1); |
| 151 | |
| 152 | // non-local face, /localhost: violate |
| 153 | Interest interestA2 = Interest("/localhost/A2"); |
| 154 | shared_ptr<pit::Entry> pitA2 = pit.insert(interestA2).first; |
| 155 | pitA2->insertOrUpdateInRecord(face3, interestA1); |
| 156 | face2->m_sentInterests.clear(); |
| 157 | forwarder.onOutgoingInterest(pitA2, *face2); |
| 158 | BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 0); |
| 159 | |
| 160 | // local face, non-/localhost: OK |
| 161 | Interest interestA3 = Interest("/A3"); |
| 162 | shared_ptr<pit::Entry> pitA3 = pit.insert(interestA3).first; |
| 163 | pitA3->insertOrUpdateInRecord(face3, interestA3); |
| 164 | face1->m_sentInterests.clear(); |
| 165 | forwarder.onOutgoingInterest(pitA3, *face1); |
| 166 | BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1); |
| 167 | |
| 168 | // non-local face, non-/localhost: OK |
| 169 | Interest interestA4 = Interest("/A4"); |
| 170 | shared_ptr<pit::Entry> pitA4 = pit.insert(interestA4).first; |
| 171 | pitA4->insertOrUpdateInRecord(face3, interestA4); |
| 172 | face2->m_sentInterests.clear(); |
| 173 | forwarder.onOutgoingInterest(pitA4, *face2); |
| 174 | BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 1); |
| 175 | |
| 176 | // local face, /localhost: OK |
| 177 | face1->m_sentDatas.clear(); |
| 178 | forwarder.onOutgoingData(Data("/localhost/B1"), *face1); |
| 179 | BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1); |
| 180 | |
| 181 | // non-local face, /localhost: OK |
| 182 | face2->m_sentDatas.clear(); |
| 183 | forwarder.onOutgoingData(Data("/localhost/B2"), *face2); |
| 184 | BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 0); |
| 185 | |
| 186 | // local face, non-/localhost: OK |
| 187 | face1->m_sentDatas.clear(); |
| 188 | forwarder.onOutgoingData(Data("/B3"), *face1); |
| 189 | BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1); |
| 190 | |
| 191 | // non-local face, non-/localhost: OK |
| 192 | face2->m_sentDatas.clear(); |
| 193 | forwarder.onOutgoingData(Data("/B4"), *face2); |
| 194 | BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 1); |
| 195 | } |
| 196 | |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 197 | BOOST_AUTO_TEST_SUITE_END() |
| 198 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 199 | } // namespace tests |
Junxiao Shi | 8c8d218 | 2014-01-30 22:33:00 -0700 | [diff] [blame] | 200 | } // namespace nfd |