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