Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "face/internal-face.hpp" |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 27 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 28 | #include "transport-test-common.hpp" |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame^] | 29 | #include "tests/key-chain-fixture.hpp" |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 32 | namespace face { |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 33 | namespace tests { |
| 34 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 35 | using namespace nfd::tests; |
| 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Face) |
| 38 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame^] | 39 | class InternalFaceFixture : public UnitTestTimeFixture, public KeyChainFixture |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 40 | { |
| 41 | public: |
| 42 | InternalFaceFixture() |
| 43 | { |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 44 | std::tie(forwarderFace, clientFace) = makeInternalFace(m_keyChain);; |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 45 | |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 46 | forwarderFace->afterReceiveInterest.connect( |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 47 | [this] (const Interest& interest) { receivedInterests.push_back(interest); } ); |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 48 | forwarderFace->afterReceiveData.connect( |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 49 | [this] (const Data& data) { receivedData.push_back(data); } ); |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 50 | forwarderFace->afterReceiveNack.connect( |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 51 | [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); } ); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | protected: |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 55 | shared_ptr<nfd::Face> forwarderFace; |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 56 | shared_ptr<ndn::Face> clientFace; |
| 57 | |
| 58 | std::vector<Interest> receivedInterests; |
| 59 | std::vector<Data> receivedData; |
| 60 | std::vector<lp::Nack> receivedNacks; |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 63 | BOOST_FIXTURE_TEST_SUITE(TestInternalFace, InternalFaceFixture) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 65 | BOOST_AUTO_TEST_CASE(TransportStaticProperties) |
| 66 | { |
| 67 | Transport* transport = forwarderFace->getTransport(); |
| 68 | checkStaticPropertiesInitialized(*transport); |
| 69 | |
| 70 | BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("internal://")); |
| 71 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("internal://")); |
| 72 | BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_LOCAL); |
| 73 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 74 | BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 75 | BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED); |
| 76 | } |
| 77 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 78 | // note: "send" and "receive" in test case names refer to the direction seen on forwarderFace. |
| 79 | // i.e. "send" means transmission from forwarder to client, |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 80 | // "receive" means transmission from client to forwarder. |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 81 | |
| 82 | BOOST_AUTO_TEST_CASE(ReceiveInterestTimeout) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 83 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 84 | shared_ptr<Interest> interest = makeInterest("/TLETccRv"); |
| 85 | interest->setInterestLifetime(time::milliseconds(100)); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 86 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 87 | bool hasTimeout = false; |
| 88 | clientFace->expressInterest(*interest, |
| 89 | bind([] { BOOST_ERROR("unexpected Data"); }), |
| 90 | bind([] { BOOST_ERROR("unexpected Nack"); }), |
| 91 | bind([&hasTimeout] { hasTimeout = true; })); |
| 92 | this->advanceClocks(time::milliseconds(1), 10); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 94 | BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1); |
| 95 | BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/TLETccRv"); |
| 96 | |
| 97 | this->advanceClocks(time::milliseconds(1), 100); |
| 98 | |
| 99 | BOOST_CHECK(hasTimeout); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 102 | BOOST_AUTO_TEST_CASE(ReceiveInterestSendData) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 103 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 104 | shared_ptr<Interest> interest = makeInterest("/PQstEJGdL"); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 105 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 106 | bool hasReceivedData = false; |
| 107 | clientFace->expressInterest(*interest, |
| 108 | [&hasReceivedData] (const Interest&, const Data& data) { |
| 109 | hasReceivedData = true; |
| 110 | BOOST_CHECK_EQUAL(data.getName(), "/PQstEJGdL/aI7oCrDXNX"); |
| 111 | }, |
| 112 | bind([] { BOOST_ERROR("unexpected Nack"); }), |
| 113 | bind([] { BOOST_ERROR("unexpected timeout"); })); |
| 114 | this->advanceClocks(time::milliseconds(1), 10); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 115 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 116 | BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1); |
| 117 | BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/PQstEJGdL"); |
| 118 | |
| 119 | shared_ptr<Data> data = makeData("/PQstEJGdL/aI7oCrDXNX"); |
| 120 | forwarderFace->sendData(*data); |
| 121 | this->advanceClocks(time::milliseconds(1), 10); |
| 122 | |
| 123 | BOOST_CHECK(hasReceivedData); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(ReceiveInterestSendNack) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 127 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 128 | shared_ptr<Interest> interest = makeInterest("/1HrsRM1X", 152); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 129 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 130 | bool hasReceivedNack = false; |
| 131 | clientFace->expressInterest(*interest, |
| 132 | bind([] { BOOST_ERROR("unexpected Data"); }), |
| 133 | [&hasReceivedNack] (const Interest&, const lp::Nack& nack) { |
| 134 | hasReceivedNack = true; |
| 135 | BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::NO_ROUTE); |
| 136 | }, |
| 137 | bind([] { BOOST_ERROR("unexpected timeout"); })); |
| 138 | this->advanceClocks(time::milliseconds(1), 10); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 139 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 140 | BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1); |
| 141 | BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/1HrsRM1X"); |
| 142 | |
| 143 | lp::Nack nack = makeNack("/1HrsRM1X", 152, lp::NackReason::NO_ROUTE); |
| 144 | forwarderFace->sendNack(nack); |
| 145 | this->advanceClocks(time::milliseconds(1), 10); |
| 146 | |
| 147 | BOOST_CHECK(hasReceivedNack); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(SendInterestReceiveData) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 151 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 152 | bool hasDeliveredInterest = false; |
| 153 | clientFace->setInterestFilter("/Wpc8TnEeoF", |
| 154 | [this, &hasDeliveredInterest] (const ndn::InterestFilter&, const Interest& interest) { |
| 155 | hasDeliveredInterest = true; |
| 156 | BOOST_CHECK_EQUAL(interest.getName(), "/Wpc8TnEeoF/f6SzV8hD"); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 158 | shared_ptr<Data> data = makeData("/Wpc8TnEeoF/f6SzV8hD/3uytUJCuIi"); |
| 159 | clientFace->put(*data); |
| 160 | }); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 162 | shared_ptr<Interest> interest = makeInterest("/Wpc8TnEeoF/f6SzV8hD"); |
| 163 | forwarderFace->sendInterest(*interest); |
| 164 | this->advanceClocks(time::milliseconds(1), 10); |
| 165 | |
| 166 | BOOST_CHECK(hasDeliveredInterest); |
| 167 | BOOST_REQUIRE_EQUAL(receivedData.size(), 1); |
| 168 | BOOST_CHECK_EQUAL(receivedData.back().getName(), "/Wpc8TnEeoF/f6SzV8hD/3uytUJCuIi"); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 171 | BOOST_AUTO_TEST_CASE(SendInterestReceiveNack) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 172 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 173 | bool hasDeliveredInterest = false; |
| 174 | clientFace->setInterestFilter("/4YgJKWcXN", |
| 175 | [this, &hasDeliveredInterest] (const ndn::InterestFilter&, const Interest& interest) { |
| 176 | hasDeliveredInterest = true; |
| 177 | BOOST_CHECK_EQUAL(interest.getName(), "/4YgJKWcXN/5oaTe05o"); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 178 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 179 | lp::Nack nack = makeNack("/4YgJKWcXN/5oaTe05o", 191, lp::NackReason::NO_ROUTE); |
| 180 | clientFace->put(nack); |
| 181 | }); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 182 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 183 | shared_ptr<Interest> interest = makeInterest("/4YgJKWcXN/5oaTe05o", 191); |
| 184 | forwarderFace->sendInterest(*interest); |
| 185 | this->advanceClocks(time::milliseconds(1), 10); |
| 186 | |
| 187 | BOOST_CHECK(hasDeliveredInterest); |
| 188 | BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1); |
| 189 | BOOST_CHECK_EQUAL(receivedNacks.back().getReason(), lp::NackReason::NO_ROUTE); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 192 | BOOST_AUTO_TEST_CASE(CloseForwarderFace) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 193 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 194 | forwarderFace->close(); |
| 195 | this->advanceClocks(time::milliseconds(1), 10); |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 196 | BOOST_CHECK_EQUAL(forwarderFace->getState(), FaceState::CLOSED); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 197 | forwarderFace.reset(); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 198 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 199 | shared_ptr<Interest> interest = makeInterest("/zpHsVesu0B"); |
| 200 | interest->setInterestLifetime(time::milliseconds(100)); |
| 201 | |
| 202 | bool hasTimeout = false; |
| 203 | clientFace->expressInterest(*interest, |
| 204 | bind([] { BOOST_ERROR("unexpected Data"); }), |
| 205 | bind([] { BOOST_ERROR("unexpected Nack"); }), |
| 206 | bind([&hasTimeout] { hasTimeout = true; })); |
| 207 | BOOST_CHECK_NO_THROW(this->advanceClocks(time::milliseconds(1), 200)); |
| 208 | |
| 209 | BOOST_CHECK_EQUAL(receivedInterests.size(), 0); |
| 210 | BOOST_CHECK(hasTimeout); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 213 | BOOST_AUTO_TEST_CASE(CloseClientFace) |
| 214 | { |
| 215 | g_io.poll(); // #3248 workaround |
| 216 | clientFace.reset(); |
| 217 | |
| 218 | shared_ptr<Interest> interest = makeInterest("/aau42XQqb"); |
| 219 | forwarderFace->sendInterest(*interest); |
| 220 | BOOST_CHECK_NO_THROW(this->advanceClocks(time::milliseconds(1), 10)); |
| 221 | } |
| 222 | |
| 223 | BOOST_AUTO_TEST_SUITE_END() // TestInternalFace |
| 224 | BOOST_AUTO_TEST_SUITE_END() // Face |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 225 | |
| 226 | } // namespace tests |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 227 | } // namespace face |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 228 | } // namespace nfd |