blob: a830e3e13a6f5a0a974e978432ad6a8bf456f9e1 [file] [log] [blame]
Yanbiao Li4ee73d42015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento1d12d2f2019-03-22 12:44:14 -04002/*
3 * Copyright (c) 2014-2019, Regents of the University of California,
Yanbiao Li4ee73d42015-08-19 16:30:16 -07004 * 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 Shi5b8a2b22015-10-22 17:20:44 -070027
Junxiao Shicde37ad2015-12-24 01:02:05 -070028#include "transport-test-common.hpp"
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040029#include "tests/key-chain-fixture.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Yanbiao Li4ee73d42015-08-19 16:30:16 -070031
32namespace nfd {
Junxiao Shi6535f1e2015-10-08 13:02:18 -070033namespace face {
Yanbiao Li4ee73d42015-08-19 16:30:16 -070034namespace tests {
35
Junxiao Shi6535f1e2015-10-08 13:02:18 -070036using namespace nfd::tests;
37
38BOOST_AUTO_TEST_SUITE(Face)
39
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040040class InternalFaceFixture : public GlobalIoTimeFixture, public KeyChainFixture
Yanbiao Li4ee73d42015-08-19 16:30:16 -070041{
42public:
43 InternalFaceFixture()
44 {
Davide Pesavento284bd622019-03-31 02:10:02 -040045 std::tie(forwarderFace, clientFace) = makeInternalFace(m_keyChain);
Junxiao Shi6535f1e2015-10-08 13:02:18 -070046
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070047 forwarderFace->afterReceiveInterest.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000048 [this] (const Interest& interest, const EndpointId&) { receivedInterests.push_back(interest); } );
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070049 forwarderFace->afterReceiveData.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000050 [this] (const Data& data, const EndpointId&) { receivedData.push_back(data); } );
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070051 forwarderFace->afterReceiveNack.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000052 [this] (const lp::Nack& nack, const EndpointId&) { receivedNacks.push_back(nack); } );
Yanbiao Li4ee73d42015-08-19 16:30:16 -070053 }
54
55protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070056 shared_ptr<nfd::Face> forwarderFace;
Junxiao Shi6535f1e2015-10-08 13:02:18 -070057 shared_ptr<ndn::Face> clientFace;
58
59 std::vector<Interest> receivedInterests;
60 std::vector<Data> receivedData;
61 std::vector<lp::Nack> receivedNacks;
Yanbiao Li4ee73d42015-08-19 16:30:16 -070062};
63
Junxiao Shi6535f1e2015-10-08 13:02:18 -070064BOOST_FIXTURE_TEST_SUITE(TestInternalFace, InternalFaceFixture)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070065
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070066BOOST_AUTO_TEST_CASE(TransportStaticProperties)
67{
68 Transport* transport = forwarderFace->getTransport();
69 checkStaticPropertiesInitialized(*transport);
70
71 BOOST_CHECK_EQUAL(transport->getLocalUri(), FaceUri("internal://"));
72 BOOST_CHECK_EQUAL(transport->getRemoteUri(), FaceUri("internal://"));
73 BOOST_CHECK_EQUAL(transport->getScope(), ndn::nfd::FACE_SCOPE_LOCAL);
74 BOOST_CHECK_EQUAL(transport->getPersistency(), ndn::nfd::FACE_PERSISTENCY_PERMANENT);
75 BOOST_CHECK_EQUAL(transport->getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT);
76 BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED);
77}
78
Junxiao Shi6535f1e2015-10-08 13:02:18 -070079// note: "send" and "receive" in test case names refer to the direction seen on forwarderFace.
80// i.e. "send" means transmission from forwarder to client,
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070081// "receive" means transmission from client to forwarder.
Junxiao Shi6535f1e2015-10-08 13:02:18 -070082
83BOOST_AUTO_TEST_CASE(ReceiveInterestTimeout)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070084{
Davide Pesavento284bd622019-03-31 02:10:02 -040085 auto interest = makeInterest("/TLETccRv");
Davide Pesavento14e71f02019-03-28 17:35:25 -040086 interest->setInterestLifetime(100_ms);
Yanbiao Li4ee73d42015-08-19 16:30:16 -070087
Junxiao Shi6535f1e2015-10-08 13:02:18 -070088 bool hasTimeout = false;
89 clientFace->expressInterest(*interest,
90 bind([] { BOOST_ERROR("unexpected Data"); }),
91 bind([] { BOOST_ERROR("unexpected Nack"); }),
92 bind([&hasTimeout] { hasTimeout = true; }));
Davide Pesavento14e71f02019-03-28 17:35:25 -040093 this->advanceClocks(1_ms, 10);
Yanbiao Li4ee73d42015-08-19 16:30:16 -070094
Junxiao Shi6535f1e2015-10-08 13:02:18 -070095 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
96 BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/TLETccRv");
97
Davide Pesavento14e71f02019-03-28 17:35:25 -040098 this->advanceClocks(1_ms, 100);
Junxiao Shi6535f1e2015-10-08 13:02:18 -070099
100 BOOST_CHECK(hasTimeout);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700101}
102
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700103BOOST_AUTO_TEST_CASE(ReceiveInterestSendData)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700104{
Davide Pesavento284bd622019-03-31 02:10:02 -0400105 auto interest = makeInterest("/PQstEJGdL");
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700106
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700107 bool hasReceivedData = false;
108 clientFace->expressInterest(*interest,
109 [&hasReceivedData] (const Interest&, const Data& data) {
110 hasReceivedData = true;
111 BOOST_CHECK_EQUAL(data.getName(), "/PQstEJGdL/aI7oCrDXNX");
112 },
113 bind([] { BOOST_ERROR("unexpected Nack"); }),
114 bind([] { BOOST_ERROR("unexpected timeout"); }));
Davide Pesavento14e71f02019-03-28 17:35:25 -0400115 this->advanceClocks(1_ms, 10);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700116
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700117 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
118 BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/PQstEJGdL");
119
ashiqopu075bb7d2019-03-10 01:38:21 +0000120 forwarderFace->sendData(*makeData("/PQstEJGdL/aI7oCrDXNX"), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400121 this->advanceClocks(1_ms, 10);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700122
123 BOOST_CHECK(hasReceivedData);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700124}
125
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700126BOOST_AUTO_TEST_CASE(ReceiveInterestSendNack)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700127{
Davide Pesavento284bd622019-03-31 02:10:02 -0400128 auto interest = makeInterest("/1HrsRM1X", 152);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700129
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700130 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"); }));
Davide Pesavento14e71f02019-03-28 17:35:25 -0400138 this->advanceClocks(1_ms, 10);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700139
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700140 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
141 BOOST_CHECK_EQUAL(receivedInterests.back().getName(), "/1HrsRM1X");
142
ashiqopu075bb7d2019-03-10 01:38:21 +0000143 forwarderFace->sendNack(makeNack("/1HrsRM1X", 152, lp::NackReason::NO_ROUTE), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400144 this->advanceClocks(1_ms, 10);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700145
146 BOOST_CHECK(hasReceivedNack);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700147}
148
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700149BOOST_AUTO_TEST_CASE(SendInterestReceiveData)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700150{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700151 bool hasDeliveredInterest = false;
152 clientFace->setInterestFilter("/Wpc8TnEeoF",
153 [this, &hasDeliveredInterest] (const ndn::InterestFilter&, const Interest& interest) {
154 hasDeliveredInterest = true;
155 BOOST_CHECK_EQUAL(interest.getName(), "/Wpc8TnEeoF/f6SzV8hD");
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700156
Davide Pesavento284bd622019-03-31 02:10:02 -0400157 clientFace->put(*makeData("/Wpc8TnEeoF/f6SzV8hD/3uytUJCuIi"));
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700158 });
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700159
ashiqopu075bb7d2019-03-10 01:38:21 +0000160 forwarderFace->sendInterest(*makeInterest("/Wpc8TnEeoF/f6SzV8hD"), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400161 this->advanceClocks(1_ms, 10);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700162
163 BOOST_CHECK(hasDeliveredInterest);
164 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
165 BOOST_CHECK_EQUAL(receivedData.back().getName(), "/Wpc8TnEeoF/f6SzV8hD/3uytUJCuIi");
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700166}
167
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700168BOOST_AUTO_TEST_CASE(SendInterestReceiveNack)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700169{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700170 bool hasDeliveredInterest = false;
171 clientFace->setInterestFilter("/4YgJKWcXN",
172 [this, &hasDeliveredInterest] (const ndn::InterestFilter&, const Interest& interest) {
173 hasDeliveredInterest = true;
174 BOOST_CHECK_EQUAL(interest.getName(), "/4YgJKWcXN/5oaTe05o");
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700175
Davide Pesavento284bd622019-03-31 02:10:02 -0400176 clientFace->put(makeNack("/4YgJKWcXN/5oaTe05o", 191, lp::NackReason::NO_ROUTE));
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700177 });
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700178
ashiqopu075bb7d2019-03-10 01:38:21 +0000179 forwarderFace->sendInterest(*makeInterest("/4YgJKWcXN/5oaTe05o", 191), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400180 this->advanceClocks(1_ms, 10);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700181
182 BOOST_CHECK(hasDeliveredInterest);
183 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
184 BOOST_CHECK_EQUAL(receivedNacks.back().getReason(), lp::NackReason::NO_ROUTE);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700185}
186
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700187BOOST_AUTO_TEST_CASE(CloseForwarderFace)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700188{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700189 forwarderFace->close();
Davide Pesavento14e71f02019-03-28 17:35:25 -0400190 this->advanceClocks(1_ms, 10);
Junxiao Shi5b8a2b22015-10-22 17:20:44 -0700191 BOOST_CHECK_EQUAL(forwarderFace->getState(), FaceState::CLOSED);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700192 forwarderFace.reset();
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700193
Davide Pesavento284bd622019-03-31 02:10:02 -0400194 auto interest = makeInterest("/zpHsVesu0B");
Davide Pesavento14e71f02019-03-28 17:35:25 -0400195 interest->setInterestLifetime(100_ms);
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700196
197 bool hasTimeout = false;
198 clientFace->expressInterest(*interest,
199 bind([] { BOOST_ERROR("unexpected Data"); }),
200 bind([] { BOOST_ERROR("unexpected Nack"); }),
201 bind([&hasTimeout] { hasTimeout = true; }));
Davide Pesavento14e71f02019-03-28 17:35:25 -0400202 BOOST_CHECK_NO_THROW(this->advanceClocks(1_ms, 200));
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700203
204 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
205 BOOST_CHECK(hasTimeout);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700206}
207
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700208BOOST_AUTO_TEST_CASE(CloseClientFace)
209{
210 g_io.poll(); // #3248 workaround
211 clientFace.reset();
212
ashiqopu075bb7d2019-03-10 01:38:21 +0000213 forwarderFace->sendInterest(*makeInterest("/aau42XQqb"), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400214 BOOST_CHECK_NO_THROW(this->advanceClocks(1_ms, 10));
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700215}
216
217BOOST_AUTO_TEST_SUITE_END() // TestInternalFace
218BOOST_AUTO_TEST_SUITE_END() // Face
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700219
220} // namespace tests
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700221} // namespace face
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700222} // namespace nfd