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