blob: 05e133fd520a8f1c7c9791b0f31f17ba4d96d8aa [file] [log] [blame]
Yanbiao Li4ee73d42015-08-19 16:30:16 -07001/* -*- 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 Shi6535f1e2015-10-08 13:02:18 -070027#include "face/lp-face-wrapper.hpp"
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070028#include "transport-properties.hpp"
29
Yanbiao Li4ee73d42015-08-19 16:30:16 -070030#include "tests/test-common.hpp"
Junxiao Shi6535f1e2015-10-08 13:02:18 -070031#include "tests/identity-management-fixture.hpp"
Yanbiao Li4ee73d42015-08-19 16:30:16 -070032
33namespace nfd {
Junxiao Shi6535f1e2015-10-08 13:02:18 -070034namespace face {
Yanbiao Li4ee73d42015-08-19 16:30:16 -070035namespace tests {
36
Junxiao Shi6535f1e2015-10-08 13:02:18 -070037using namespace nfd::tests;
38
39BOOST_AUTO_TEST_SUITE(Face)
40
41class InternalFaceFixture : public UnitTestTimeFixture
42 , public IdentityManagementFixture
Yanbiao Li4ee73d42015-08-19 16:30:16 -070043{
44public:
45 InternalFaceFixture()
46 {
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070047 std::tie(forwarderFaceW, clientFace) = makeInternalFace(m_keyChain);;
48 forwarderFace = static_pointer_cast<LpFaceWrapper>(forwarderFaceW)->getLpFace();
49 // TODO#3172 eliminate wrapper
Junxiao Shi6535f1e2015-10-08 13:02:18 -070050
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070051 forwarderFace->afterReceiveInterest.connect(
Junxiao Shi6535f1e2015-10-08 13:02:18 -070052 [this] (const Interest& interest) { receivedInterests.push_back(interest); } );
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070053 forwarderFace->afterReceiveData.connect(
Junxiao Shi6535f1e2015-10-08 13:02:18 -070054 [this] (const Data& data) { receivedData.push_back(data); } );
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070055 forwarderFace->afterReceiveNack.connect(
Junxiao Shi6535f1e2015-10-08 13:02:18 -070056 [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); } );
Yanbiao Li4ee73d42015-08-19 16:30:16 -070057 }
58
59protected:
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070060 shared_ptr<nfd::Face> forwarderFaceW;
61 LpFace* forwarderFace;
Junxiao Shi6535f1e2015-10-08 13:02:18 -070062 shared_ptr<ndn::Face> clientFace;
63
64 std::vector<Interest> receivedInterests;
65 std::vector<Data> receivedData;
66 std::vector<lp::Nack> receivedNacks;
Yanbiao Li4ee73d42015-08-19 16:30:16 -070067};
68
Junxiao Shi6535f1e2015-10-08 13:02:18 -070069BOOST_FIXTURE_TEST_SUITE(TestInternalFace, InternalFaceFixture)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070070
Junxiao Shi5b8a2b22015-10-22 17:20:44 -070071BOOST_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 Shi6535f1e2015-10-08 13:02:18 -070084// 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 Shi5b8a2b22015-10-22 17:20:44 -070086// "receive" means transmission from client to forwarder.
Junxiao Shi6535f1e2015-10-08 13:02:18 -070087
88BOOST_AUTO_TEST_CASE(ReceiveInterestTimeout)
Yanbiao Li4ee73d42015-08-19 16:30:16 -070089{
Junxiao Shi6535f1e2015-10-08 13:02:18 -070090 shared_ptr<Interest> interest = makeInterest("/TLETccRv");
91 interest->setInterestLifetime(time::milliseconds(100));
Yanbiao Li4ee73d42015-08-19 16:30:16 -070092
Junxiao Shi6535f1e2015-10-08 13:02:18 -070093 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 Li4ee73d42015-08-19 16:30:16 -070099
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700100 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 Li4ee73d42015-08-19 16:30:16 -0700106}
107
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700108BOOST_AUTO_TEST_CASE(ReceiveInterestSendData)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700109{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700110 shared_ptr<Interest> interest = makeInterest("/PQstEJGdL");
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700111
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700112 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 Li4ee73d42015-08-19 16:30:16 -0700121
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700122 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 Li4ee73d42015-08-19 16:30:16 -0700130}
131
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700132BOOST_AUTO_TEST_CASE(ReceiveInterestSendNack)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700133{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700134 shared_ptr<Interest> interest = makeInterest("/1HrsRM1X", 152);
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700135
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700136 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 Li4ee73d42015-08-19 16:30:16 -0700145
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700146 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 Li4ee73d42015-08-19 16:30:16 -0700154}
155
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700156BOOST_AUTO_TEST_CASE(SendInterestReceiveData)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700157{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700158 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 Li4ee73d42015-08-19 16:30:16 -0700163
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700164 shared_ptr<Data> data = makeData("/Wpc8TnEeoF/f6SzV8hD/3uytUJCuIi");
165 clientFace->put(*data);
166 });
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700167
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700168 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 Li4ee73d42015-08-19 16:30:16 -0700175}
176
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700177BOOST_AUTO_TEST_CASE(SendInterestReceiveNack)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700178{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700179 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 Li4ee73d42015-08-19 16:30:16 -0700184
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700185 lp::Nack nack = makeNack("/4YgJKWcXN/5oaTe05o", 191, lp::NackReason::NO_ROUTE);
186 clientFace->put(nack);
187 });
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700188
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700189 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 Li4ee73d42015-08-19 16:30:16 -0700196}
197
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700198BOOST_AUTO_TEST_CASE(CloseForwarderFace)
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700199{
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700200 forwarderFace->close();
201 this->advanceClocks(time::milliseconds(1), 10);
Junxiao Shi5b8a2b22015-10-22 17:20:44 -0700202 BOOST_CHECK_EQUAL(forwarderFace->getState(), FaceState::CLOSED);
203 forwarderFaceW.reset();
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700204
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700205 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 Li4ee73d42015-08-19 16:30:16 -0700217}
218
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700219BOOST_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
229BOOST_AUTO_TEST_SUITE_END() // TestInternalFace
230BOOST_AUTO_TEST_SUITE_END() // Face
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700231
232} // namespace tests
Junxiao Shi6535f1e2015-10-08 13:02:18 -0700233} // namespace face
Yanbiao Li4ee73d42015-08-19 16:30:16 -0700234} // namespace nfd