blob: dfb9888984e7da583c556b19ed3b1e330843cd10 [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"
Yanbiao Li4ee73d42015-08-19 16:30:16 -070030
31namespace nfd {
Junxiao Shi6535f1e2015-10-08 13:02:18 -070032namespace face {
Yanbiao Li4ee73d42015-08-19 16:30:16 -070033namespace tests {
34
Junxiao Shi6535f1e2015-10-08 13:02:18 -070035using namespace nfd::tests;
36
37BOOST_AUTO_TEST_SUITE(Face)
38
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040039class InternalFaceFixture : public UnitTestTimeFixture, public KeyChainFixture
Yanbiao Li4ee73d42015-08-19 16:30:16 -070040{
41public:
42 InternalFaceFixture()
43 {
Junxiao Shicde37ad2015-12-24 01:02:05 -070044 std::tie(forwarderFace, clientFace) = makeInternalFace(m_keyChain);;
Junxiao Shi6535f1e2015-10-08 13:02:18 -070045
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070046 forwarderFace->afterReceiveInterest.connect(
Junxiao Shi6535f1e2015-10-08 13:02:18 -070047 [this] (const Interest& interest) { receivedInterests.push_back(interest); } );
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070048 forwarderFace->afterReceiveData.connect(
Junxiao Shi6535f1e2015-10-08 13:02:18 -070049 [this] (const Data& data) { receivedData.push_back(data); } );
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070050 forwarderFace->afterReceiveNack.connect(
Junxiao Shi6535f1e2015-10-08 13:02:18 -070051 [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); } );
Yanbiao Li4ee73d42015-08-19 16:30:16 -070052 }
53
54protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070055 shared_ptr<nfd::Face> forwarderFace;
Junxiao Shi6535f1e2015-10-08 13:02:18 -070056 shared_ptr<ndn::Face> clientFace;
57
58 std::vector<Interest> receivedInterests;
59 std::vector<Data> receivedData;
60 std::vector<lp::Nack> receivedNacks;
Yanbiao Li4ee73d42015-08-19 16:30:16 -070061};
62
Junxiao Shi6535f1e2015-10-08 13:02:18 -070063BOOST_FIXTURE_TEST_SUITE(TestInternalFace, InternalFaceFixture)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070064
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070065BOOST_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 Shi6535f1e2015-10-08 13:02:18 -070078// 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 Shi5b8a2b22015-10-22 17:20:44 -070080// "receive" means transmission from client to forwarder.
Junxiao Shi6535f1e2015-10-08 13:02:18 -070081
82BOOST_AUTO_TEST_CASE(ReceiveInterestTimeout)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070083{
Junxiao Shi6535f1e2015-10-08 13:02:18 -070084 shared_ptr<Interest> interest = makeInterest("/TLETccRv");
85 interest->setInterestLifetime(time::milliseconds(100));
Yanbiao Li4ee73d42015-08-19 16:30:16 -070086
Junxiao Shi6535f1e2015-10-08 13:02:18 -070087 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 Li4ee73d42015-08-19 16:30:16 -070093
Junxiao Shi6535f1e2015-10-08 13:02:18 -070094 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 Li4ee73d42015-08-19 16:30:16 -0700100}
101
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700102BOOST_AUTO_TEST_CASE(ReceiveInterestSendData)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700103{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700104 shared_ptr<Interest> interest = makeInterest("/PQstEJGdL");
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700105
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700106 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 Li4ee73d42015-08-19 16:30:16 -0700115
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700116 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 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{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700128 shared_ptr<Interest> 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"); }));
138 this->advanceClocks(time::milliseconds(1), 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
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 Li4ee73d42015-08-19 16:30:16 -0700148}
149
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700150BOOST_AUTO_TEST_CASE(SendInterestReceiveData)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700151{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700152 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 Li4ee73d42015-08-19 16:30:16 -0700157
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700158 shared_ptr<Data> data = makeData("/Wpc8TnEeoF/f6SzV8hD/3uytUJCuIi");
159 clientFace->put(*data);
160 });
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700161
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700162 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 Li4ee73d42015-08-19 16:30:16 -0700169}
170
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700171BOOST_AUTO_TEST_CASE(SendInterestReceiveNack)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700172{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700173 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 Li4ee73d42015-08-19 16:30:16 -0700178
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700179 lp::Nack nack = makeNack("/4YgJKWcXN/5oaTe05o", 191, lp::NackReason::NO_ROUTE);
180 clientFace->put(nack);
181 });
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700182
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700183 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 Li4ee73d42015-08-19 16:30:16 -0700190}
191
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700192BOOST_AUTO_TEST_CASE(CloseForwarderFace)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700193{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700194 forwarderFace->close();
195 this->advanceClocks(time::milliseconds(1), 10);
Junxiao Shi5b8a2b22015-10-22 17:20:44 -0700196 BOOST_CHECK_EQUAL(forwarderFace->getState(), FaceState::CLOSED);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700197 forwarderFace.reset();
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700198
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700199 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 Li4ee73d42015-08-19 16:30:16 -0700211}
212
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700213BOOST_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
223BOOST_AUTO_TEST_SUITE_END() // TestInternalFace
224BOOST_AUTO_TEST_SUITE_END() // Face
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700225
226} // namespace tests
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700227} // namespace face
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700228} // namespace nfd