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