blob: f96e532822d0a74b7d41aa4ca25bc8e5491dfcfe [file] [log] [blame]
Yanbiao Li73860e32015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Li58ba3f92017-02-15 14:27:18 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
Yanbiao Li73860e32015-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
Yanbiao Li58ba3f92017-02-15 14:27:18 +000026#include "mgmt/face-manager.hpp"
Eric Newberry42602412016-08-27 09:33:18 -070027#include "face-manager-command-fixture.hpp"
28#include "nfd-manager-common-fixture.hpp"
Junxiao Shib84e6742016-07-19 13:16:22 +000029
Yanbiao Li73860e32015-08-19 16:30:16 -070030namespace nfd {
31namespace tests {
32
Yanbiao Li73860e32015-08-19 16:30:16 -070033BOOST_AUTO_TEST_SUITE(Mgmt)
34BOOST_AUTO_TEST_SUITE(TestFaceManager)
35
36BOOST_FIXTURE_TEST_SUITE(CreateFace, BaseFixture)
37
Yanbiao Li73860e32015-08-19 16:30:16 -070038class TcpFaceOnDemand
39{
40public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -040041 static ControlParameters
Yanbiao Li73860e32015-08-19 16:30:16 -070042 getParameters()
43 {
44 return ControlParameters()
45 .setUri("tcp4://127.0.0.1:26363")
46 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
47 }
48};
49
50class TcpFacePersistent
51{
52public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -040053 static ControlParameters
Yanbiao Li73860e32015-08-19 16:30:16 -070054 getParameters()
55 {
56 return ControlParameters()
57 .setUri("tcp4://127.0.0.1:26363")
58 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
59 }
60};
61
62class TcpFacePermanent
63{
64public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -040065 static ControlParameters
Yanbiao Li73860e32015-08-19 16:30:16 -070066 getParameters()
67 {
68 return ControlParameters()
69 .setUri("tcp4://127.0.0.1:26363")
70 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
71 }
72};
73
74class UdpFaceOnDemand
75{
76public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -040077 static ControlParameters
Yanbiao Li73860e32015-08-19 16:30:16 -070078 getParameters()
79 {
80 return ControlParameters()
81 .setUri("udp4://127.0.0.1:26363")
82 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
83 }
84};
85
Yanbiao Li73860e32015-08-19 16:30:16 -070086class UdpFacePersistent
87{
88public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -040089 static ControlParameters
Yanbiao Li73860e32015-08-19 16:30:16 -070090 getParameters()
91 {
92 return ControlParameters()
93 .setUri("udp4://127.0.0.1:26363")
94 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
95 }
96};
97
98class UdpFacePermanent
99{
100public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400101 static ControlParameters
Yanbiao Li73860e32015-08-19 16:30:16 -0700102 getParameters()
103 {
104 return ControlParameters()
105 .setUri("udp4://127.0.0.1:26363")
106 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
107 }
108};
109
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700110class UdpFaceConnectToSelf // face that will cause afterCreateFaceFailure to be invoked
111 // fails because remote endpoint is prohibited
Yanbiao Li73860e32015-08-19 16:30:16 -0700112{
113public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400114 static ControlParameters
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700115 getParameters()
Yanbiao Li73860e32015-08-19 16:30:16 -0700116 {
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700117 return ControlParameters()
118 .setUri("udp4://0.0.0.0:16363"); // cannot connect to self
Yanbiao Li73860e32015-08-19 16:30:16 -0700119 }
120};
121
Eric Newberryf40551a2016-09-05 15:41:16 -0700122class LocalTcpFaceLocalFieldsEnabled
123{
124public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400125 static ControlParameters
Eric Newberryf40551a2016-09-05 15:41:16 -0700126 getParameters()
127 {
128 return ControlParameters()
129 .setUri("tcp4://127.0.0.1:26363")
130 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
131 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true);
132 }
133};
134
135class LocalTcpFaceLocalFieldsDisabled
136{
137public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400138 static ControlParameters
Eric Newberryf40551a2016-09-05 15:41:16 -0700139 getParameters()
140 {
141 return ControlParameters()
142 .setUri("tcp4://127.0.0.1:26363")
143 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
144 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, false);
145 }
146};
147
148class NonLocalUdpFaceLocalFieldsEnabled // won't work because non-local scope
149{
150public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400151 static ControlParameters
Eric Newberryf40551a2016-09-05 15:41:16 -0700152 getParameters()
153 {
154 return ControlParameters()
155 .setUri("udp4://127.0.0.1:26363")
156 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
157 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true);
158 }
159};
160
161class NonLocalUdpFaceLocalFieldsDisabled
162{
163public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400164 static ControlParameters
Eric Newberryf40551a2016-09-05 15:41:16 -0700165 getParameters()
166 {
167 return ControlParameters()
168 .setUri("udp4://127.0.0.1:26363")
169 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
170 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, false);
171 }
172};
173
Yanbiao Li73860e32015-08-19 16:30:16 -0700174namespace mpl = boost::mpl;
175
Eric Newberry42602412016-08-27 09:33:18 -0700176// pairs of CreateCommand and Success/Failure status
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400177using TestCases = mpl::vector<
178 mpl::pair<TcpFaceOnDemand, CommandFailure<406>>,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700179 mpl::pair<TcpFacePersistent, CommandSuccess>,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400180 mpl::pair<TcpFacePermanent, CommandSuccess>,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700181 mpl::pair<UdpFaceOnDemand, CommandFailure<406>>,
182 mpl::pair<UdpFacePersistent, CommandSuccess>,
183 mpl::pair<UdpFacePermanent, CommandSuccess>,
Eric Newberryf40551a2016-09-05 15:41:16 -0700184 mpl::pair<UdpFaceConnectToSelf, CommandFailure<406>>,
185 mpl::pair<LocalTcpFaceLocalFieldsEnabled, CommandSuccess>,
186 mpl::pair<LocalTcpFaceLocalFieldsDisabled, CommandSuccess>,
187 mpl::pair<NonLocalUdpFaceLocalFieldsEnabled, CommandFailure<406>>,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400188 mpl::pair<NonLocalUdpFaceLocalFieldsDisabled, CommandSuccess>>;
Yanbiao Li73860e32015-08-19 16:30:16 -0700189
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400190BOOST_FIXTURE_TEST_CASE_TEMPLATE(NewFace, T, TestCases, FaceManagerCommandFixture)
Yanbiao Li73860e32015-08-19 16:30:16 -0700191{
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400192 using FaceType = typename T::first;
193 using CreateResult = typename T::second;
Yanbiao Li73860e32015-08-19 16:30:16 -0700194
195 Name commandName("/localhost/nfd/faces");
196 commandName.append("create");
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400197 commandName.append(FaceType::getParameters().wireEncode());
Junxiao Shib84e6742016-07-19 13:16:22 +0000198 auto command = makeInterest(commandName);
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000199 m_keyChain.sign(*command);
Yanbiao Li73860e32015-08-19 16:30:16 -0700200
201 bool hasCallbackFired = false;
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000202 this->node1.face.onSendData.connect([this, command, &hasCallbackFired] (const Data& response) {
Eric Newberry42602412016-08-27 09:33:18 -0700203 if (!command->getName().isPrefixOf(response.getName())) {
204 return;
205 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700206
Eric Newberry42602412016-08-27 09:33:18 -0700207 ControlResponse actual(response.getContent().blockFromValue());
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400208 ControlResponse expected(CreateResult::getExpected());
Eric Newberry42602412016-08-27 09:33:18 -0700209 BOOST_TEST_MESSAGE(actual.getText());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000210 BOOST_CHECK_EQUAL(expected.getCode(), actual.getCode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700211
Eric Newberry42602412016-08-27 09:33:18 -0700212 if (actual.getBody().hasWire()) {
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400213 ControlParameters expectedParams(FaceType::getParameters());
Eric Newberry42602412016-08-27 09:33:18 -0700214 ControlParameters actualParams(actual.getBody());
Yanbiao Li73860e32015-08-19 16:30:16 -0700215
Eric Newberry42602412016-08-27 09:33:18 -0700216 BOOST_CHECK(actualParams.hasFaceId());
217 BOOST_CHECK_EQUAL(expectedParams.getFacePersistency(), actualParams.getFacePersistency());
218
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700219 if (actual.getCode() == 200) {
220 if (expectedParams.hasFlags()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700221 BOOST_CHECK_EQUAL(expectedParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED),
222 actualParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700223 }
224 else {
Eric Newberryf40551a2016-09-05 15:41:16 -0700225 // local fields are disabled by default
226 BOOST_CHECK_EQUAL(actualParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED), false);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700227 }
228 }
229 else {
Yanbiao Li73860e32015-08-19 16:30:16 -0700230 BOOST_CHECK_EQUAL(expectedParams.getUri(), actualParams.getUri());
Yanbiao Li73860e32015-08-19 16:30:16 -0700231 }
Eric Newberry42602412016-08-27 09:33:18 -0700232 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700233
234 if (actual.getCode() != 200) {
235 // ensure face not created
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400236 FaceUri uri(FaceType::getParameters().getUri());
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700237 auto& faceTable = this->node1.manager.m_faceTable;
238 BOOST_CHECK(std::none_of(faceTable.begin(), faceTable.end(), [uri] (Face& face) {
239 return face.getRemoteUri() == uri;
240 }));
241 }
242
Eric Newberry42602412016-08-27 09:33:18 -0700243 hasCallbackFired = true;
244 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700245
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000246 this->node1.face.receive(*command);
Junxiao Shib84e6742016-07-19 13:16:22 +0000247 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700248
249 BOOST_CHECK(hasCallbackFired);
250}
251
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000252BOOST_FIXTURE_TEST_CASE(ExistingFace, FaceManagerCommandFixture)
Yanbiao Li73860e32015-08-19 16:30:16 -0700253{
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000254 using FaceType = UdpFacePersistent;
Yanbiao Li73860e32015-08-19 16:30:16 -0700255
256 {
257 // create face
258
259 Name commandName("/localhost/nfd/faces");
260 commandName.append("create");
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400261 commandName.append(FaceType::getParameters().wireEncode());
Junxiao Shib84e6742016-07-19 13:16:22 +0000262 auto command = makeInterest(commandName);
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000263 m_keyChain.sign(*command);
Yanbiao Li73860e32015-08-19 16:30:16 -0700264
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000265 this->node1.face.receive(*command);
Junxiao Shib84e6742016-07-19 13:16:22 +0000266 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700267 }
268
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000269 // find the created face
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400270 auto foundFace = this->node1.findFaceByUri(FaceType::getParameters().getUri());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000271 BOOST_REQUIRE(foundFace != nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700272
273 {
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000274 // re-create face
Yanbiao Li73860e32015-08-19 16:30:16 -0700275
276 Name commandName("/localhost/nfd/faces");
277 commandName.append("create");
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400278 commandName.append(FaceType::getParameters().wireEncode());
Junxiao Shib84e6742016-07-19 13:16:22 +0000279 auto command = makeInterest(commandName);
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000280 m_keyChain.sign(*command);
Yanbiao Li73860e32015-08-19 16:30:16 -0700281
282 bool hasCallbackFired = false;
Junxiao Shib84e6742016-07-19 13:16:22 +0000283 this->node1.face.onSendData.connect(
284 [this, command, &hasCallbackFired, foundFace] (const Data& response) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700285 if (!command->getName().isPrefixOf(response.getName())) {
286 return;
287 }
288
289 ControlResponse actual(response.getContent().blockFromValue());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000290 BOOST_REQUIRE_EQUAL(actual.getCode(), 409);
Yanbiao Li73860e32015-08-19 16:30:16 -0700291
Yanbiao Li73860e32015-08-19 16:30:16 -0700292 ControlParameters actualParams(actual.getBody());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000293 BOOST_CHECK_EQUAL(foundFace->getId(), actualParams.getFaceId());
294 BOOST_CHECK_EQUAL(foundFace->getRemoteUri().toString(), actualParams.getUri());
295 BOOST_CHECK_EQUAL(foundFace->getPersistency(), actualParams.getFacePersistency());
Yanbiao Li73860e32015-08-19 16:30:16 -0700296
297 hasCallbackFired = true;
298 });
299
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000300 this->node1.face.receive(*command);
Junxiao Shib84e6742016-07-19 13:16:22 +0000301 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700302
303 BOOST_CHECK(hasCallbackFired);
304 }
305}
306
307BOOST_AUTO_TEST_SUITE_END() // CreateFace
308BOOST_AUTO_TEST_SUITE_END() // TestFaceManager
309BOOST_AUTO_TEST_SUITE_END() // Mgmt
310
Weiwei Liuf5aee942016-03-19 07:00:42 +0000311} // namespace tests
312} // namespace nfd