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