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