Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California, |
| 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 | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 27 | #include "face/lp-face-wrapper.hpp" |
Eric Newberry | 8717e87 | 2015-11-23 12:41:50 -0700 | [diff] [blame] | 28 | #include "transport-test-common.hpp" |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 30 | #include "tests/identity-management-fixture.hpp" |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 33 | namespace face { |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 34 | namespace tests { |
| 35 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 36 | using namespace nfd::tests; |
| 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(Face) |
| 39 | |
| 40 | class InternalFaceFixture : public UnitTestTimeFixture |
| 41 | , public IdentityManagementFixture |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 42 | { |
| 43 | public: |
| 44 | InternalFaceFixture() |
| 45 | { |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 46 | std::tie(forwarderFaceW, clientFace) = makeInternalFace(m_keyChain);; |
| 47 | forwarderFace = static_pointer_cast<LpFaceWrapper>(forwarderFaceW)->getLpFace(); |
| 48 | // TODO#3172 eliminate wrapper |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 49 | |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 50 | forwarderFace->afterReceiveInterest.connect( |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 51 | [this] (const Interest& interest) { receivedInterests.push_back(interest); } ); |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 52 | forwarderFace->afterReceiveData.connect( |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 53 | [this] (const Data& data) { receivedData.push_back(data); } ); |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 54 | forwarderFace->afterReceiveNack.connect( |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 55 | [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); } ); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | protected: |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 59 | shared_ptr<nfd::Face> forwarderFaceW; |
| 60 | LpFace* forwarderFace; |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 61 | shared_ptr<ndn::Face> clientFace; |
| 62 | |
| 63 | std::vector<Interest> receivedInterests; |
| 64 | std::vector<Data> receivedData; |
| 65 | std::vector<lp::Nack> receivedNacks; |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 68 | BOOST_FIXTURE_TEST_SUITE(TestInternalFace, InternalFaceFixture) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 69 | |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 70 | BOOST_AUTO_TEST_CASE(TransportStaticProperties) |
| 71 | { |
| 72 | Transport* transport = forwarderFace->getTransport(); |
| 73 | checkStaticPropertiesInitialized(*transport); |
| 74 | |
| 75 | BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("internal://")); |
| 76 | BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("internal://")); |
| 77 | BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_LOCAL); |
| 78 | BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT); |
| 79 | BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 80 | BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED); |
| 81 | } |
| 82 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 83 | // note: "send" and "receive" in test case names refer to the direction seen on forwarderFace. |
| 84 | // i.e. "send" means transmission from forwarder to client, |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 85 | // "receive" means transmission from client to forwarder. |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 86 | |
| 87 | BOOST_AUTO_TEST_CASE(ReceiveInterestTimeout) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 88 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 89 | shared_ptr<Interest> interest = makeInterest("/TLETccRv"); |
| 90 | interest->setInterestLifetime(time::milliseconds(100)); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 91 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 92 | bool hasTimeout = false; |
| 93 | clientFace->expressInterest(*interest, |
| 94 | bind([] { BOOST_ERROR("unexpected Data"); }), |
| 95 | bind([] { BOOST_ERROR("unexpected Nack"); }), |
| 96 | bind([&hasTimeout] { hasTimeout = true; })); |
| 97 | this->advanceClocks(time::milliseconds(1), 10); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 98 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 99 | BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1); |
| 100 | BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/TLETccRv"); |
| 101 | |
| 102 | this->advanceClocks(time::milliseconds(1), 100); |
| 103 | |
| 104 | BOOST_CHECK(hasTimeout); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 107 | BOOST_AUTO_TEST_CASE(ReceiveInterestSendData) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 108 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 109 | shared_ptr<Interest> interest = makeInterest("/PQstEJGdL"); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 110 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 111 | bool hasReceivedData = false; |
| 112 | clientFace->expressInterest(*interest, |
| 113 | [&hasReceivedData] (const Interest&, const Data& data) { |
| 114 | hasReceivedData = true; |
| 115 | BOOST_CHECK_EQUAL(data.getName(), "/PQstEJGdL/aI7oCrDXNX"); |
| 116 | }, |
| 117 | bind([] { BOOST_ERROR("unexpected Nack"); }), |
| 118 | bind([] { BOOST_ERROR("unexpected timeout"); })); |
| 119 | this->advanceClocks(time::milliseconds(1), 10); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 120 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 121 | BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1); |
| 122 | BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/PQstEJGdL"); |
| 123 | |
| 124 | shared_ptr<Data> data = makeData("/PQstEJGdL/aI7oCrDXNX"); |
| 125 | forwarderFace->sendData(*data); |
| 126 | this->advanceClocks(time::milliseconds(1), 10); |
| 127 | |
| 128 | BOOST_CHECK(hasReceivedData); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 131 | BOOST_AUTO_TEST_CASE(ReceiveInterestSendNack) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 132 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 133 | shared_ptr<Interest> interest = makeInterest("/1HrsRM1X", 152); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 134 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 135 | bool hasReceivedNack = false; |
| 136 | clientFace->expressInterest(*interest, |
| 137 | bind([] { BOOST_ERROR("unexpected Data"); }), |
| 138 | [&hasReceivedNack] (const Interest&, const lp::Nack& nack) { |
| 139 | hasReceivedNack = true; |
| 140 | BOOST_CHECK_EQUAL(nack.getReason(), lp::NackReason::NO_ROUTE); |
| 141 | }, |
| 142 | bind([] { BOOST_ERROR("unexpected timeout"); })); |
| 143 | this->advanceClocks(time::milliseconds(1), 10); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 144 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 145 | BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1); |
| 146 | BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/1HrsRM1X"); |
| 147 | |
| 148 | lp::Nack nack = makeNack("/1HrsRM1X", 152, lp::NackReason::NO_ROUTE); |
| 149 | forwarderFace->sendNack(nack); |
| 150 | this->advanceClocks(time::milliseconds(1), 10); |
| 151 | |
| 152 | BOOST_CHECK(hasReceivedNack); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 155 | BOOST_AUTO_TEST_CASE(SendInterestReceiveData) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 156 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 157 | bool hasDeliveredInterest = false; |
| 158 | clientFace->setInterestFilter("/Wpc8TnEeoF", |
| 159 | [this, &hasDeliveredInterest] (const ndn::InterestFilter&, const Interest& interest) { |
| 160 | hasDeliveredInterest = true; |
| 161 | BOOST_CHECK_EQUAL(interest.getName(), "/Wpc8TnEeoF/f6SzV8hD"); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 162 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 163 | shared_ptr<Data> data = makeData("/Wpc8TnEeoF/f6SzV8hD/3uytUJCuIi"); |
| 164 | clientFace->put(*data); |
| 165 | }); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 166 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 167 | shared_ptr<Interest> interest = makeInterest("/Wpc8TnEeoF/f6SzV8hD"); |
| 168 | forwarderFace->sendInterest(*interest); |
| 169 | this->advanceClocks(time::milliseconds(1), 10); |
| 170 | |
| 171 | BOOST_CHECK(hasDeliveredInterest); |
| 172 | BOOST_REQUIRE_EQUAL(receivedData.size(), 1); |
| 173 | BOOST_CHECK_EQUAL(receivedData.back().getName(), "/Wpc8TnEeoF/f6SzV8hD/3uytUJCuIi"); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 174 | } |
| 175 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 176 | BOOST_AUTO_TEST_CASE(SendInterestReceiveNack) |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 177 | { |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 178 | bool hasDeliveredInterest = false; |
| 179 | clientFace->setInterestFilter("/4YgJKWcXN", |
| 180 | [this, &hasDeliveredInterest] (const ndn::InterestFilter&, const Interest& interest) { |
| 181 | hasDeliveredInterest = true; |
| 182 | BOOST_CHECK_EQUAL(interest.getName(), "/4YgJKWcXN/5oaTe05o"); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 183 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 184 | lp::Nack nack = makeNack("/4YgJKWcXN/5oaTe05o", 191, lp::NackReason::NO_ROUTE); |
| 185 | clientFace->put(nack); |
| 186 | }); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 187 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 188 | shared_ptr<Interest> interest = makeInterest("/4YgJKWcXN/5oaTe05o", 191); |
| 189 | forwarderFace->sendInterest(*interest); |
| 190 | this->advanceClocks(time::milliseconds(1), 10); |
| 191 | |
| 192 | BOOST_CHECK(hasDeliveredInterest); |
| 193 | BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1); |
| 194 | BOOST_CHECK_EQUAL(receivedNacks.back().getReason(), lp::NackReason::NO_ROUTE); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 197 | BOOST_AUTO_TEST_CASE(CloseForwarderFace) |
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 | forwarderFace->close(); |
| 200 | this->advanceClocks(time::milliseconds(1), 10); |
Junxiao Shi | 5b8a2b2 | 2015-10-22 17:20:44 -0700 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(forwarderFace->getState(), FaceState::CLOSED); |
| 202 | forwarderFaceW.reset(); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 203 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 204 | shared_ptr<Interest> interest = makeInterest("/zpHsVesu0B"); |
| 205 | interest->setInterestLifetime(time::milliseconds(100)); |
| 206 | |
| 207 | bool hasTimeout = false; |
| 208 | clientFace->expressInterest(*interest, |
| 209 | bind([] { BOOST_ERROR("unexpected Data"); }), |
| 210 | bind([] { BOOST_ERROR("unexpected Nack"); }), |
| 211 | bind([&hasTimeout] { hasTimeout = true; })); |
| 212 | BOOST_CHECK_NO_THROW(this->advanceClocks(time::milliseconds(1), 200)); |
| 213 | |
| 214 | BOOST_CHECK_EQUAL(receivedInterests.size(), 0); |
| 215 | BOOST_CHECK(hasTimeout); |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 218 | BOOST_AUTO_TEST_CASE(CloseClientFace) |
| 219 | { |
| 220 | g_io.poll(); // #3248 workaround |
| 221 | clientFace.reset(); |
| 222 | |
| 223 | shared_ptr<Interest> interest = makeInterest("/aau42XQqb"); |
| 224 | forwarderFace->sendInterest(*interest); |
| 225 | BOOST_CHECK_NO_THROW(this->advanceClocks(time::milliseconds(1), 10)); |
| 226 | } |
| 227 | |
| 228 | BOOST_AUTO_TEST_SUITE_END() // TestInternalFace |
| 229 | BOOST_AUTO_TEST_SUITE_END() // Face |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 230 | |
| 231 | } // namespace tests |
Junxiao Shi | 6535f1e | 2015-10-08 13:02:18 -0700 | [diff] [blame] | 232 | } // namespace face |
Yanbiao Li | 4ee73d4 | 2015-08-19 16:30:16 -0700 | [diff] [blame] | 233 | } // namespace nfd |