blob: 5a39ddbe99058cc0b57f28f5bda8802a64f331b8 [file] [log] [blame]
Yanbiao Li73860e32015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi60286192017-07-26 01:07:48 +00002/*
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 Newberryf40551a2016-09-05 15:41:16 -0700110class LocalTcpFaceLocalFieldsEnabled
111{
112public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400113 static ControlParameters
Eric Newberryf40551a2016-09-05 15:41:16 -0700114 getParameters()
115 {
116 return ControlParameters()
117 .setUri("tcp4://127.0.0.1:26363")
118 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
119 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true);
120 }
121};
122
123class LocalTcpFaceLocalFieldsDisabled
124{
125public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400126 static ControlParameters
Eric Newberryf40551a2016-09-05 15:41:16 -0700127 getParameters()
128 {
129 return ControlParameters()
130 .setUri("tcp4://127.0.0.1:26363")
131 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
132 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, false);
133 }
134};
135
136class NonLocalUdpFaceLocalFieldsEnabled // won't work because non-local scope
137{
138public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400139 static ControlParameters
Eric Newberryf40551a2016-09-05 15:41:16 -0700140 getParameters()
141 {
142 return ControlParameters()
143 .setUri("udp4://127.0.0.1:26363")
144 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
145 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true);
146 }
147};
148
149class NonLocalUdpFaceLocalFieldsDisabled
150{
151public:
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400152 static ControlParameters
Eric Newberryf40551a2016-09-05 15:41:16 -0700153 getParameters()
154 {
155 return ControlParameters()
156 .setUri("udp4://127.0.0.1:26363")
157 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
158 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, false);
159 }
160};
161
Junxiao Shi38c7d9e2017-04-13 14:22:50 +0000162class FaceUriMalformed
163{
164public:
165 static ControlParameters
166 getParameters()
167 {
168 return ControlParameters()
169 .setUri("tcp4://127.0.0.1:not-a-port");
170 }
171};
172
173class FaceUriNonCanonical
174{
175public:
176 static ControlParameters
177 getParameters()
178 {
179 return ControlParameters()
180 .setUri("udp://localhost");
181 }
182};
183
184class FaceUriUnsupportedScheme
185{
186public:
187 static ControlParameters
188 getParameters()
189 {
190 return ControlParameters()
191 .setUri("dev://eth0");
192 }
193};
194
Yanbiao Li73860e32015-08-19 16:30:16 -0700195namespace mpl = boost::mpl;
196
Eric Newberry42602412016-08-27 09:33:18 -0700197// pairs of CreateCommand and Success/Failure status
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400198using TestCases = mpl::vector<
199 mpl::pair<TcpFaceOnDemand, CommandFailure<406>>,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700200 mpl::pair<TcpFacePersistent, CommandSuccess>,
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400201 mpl::pair<TcpFacePermanent, CommandSuccess>,
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700202 mpl::pair<UdpFaceOnDemand, CommandFailure<406>>,
203 mpl::pair<UdpFacePersistent, CommandSuccess>,
204 mpl::pair<UdpFacePermanent, CommandSuccess>,
Eric Newberryf40551a2016-09-05 15:41:16 -0700205 mpl::pair<LocalTcpFaceLocalFieldsEnabled, CommandSuccess>,
206 mpl::pair<LocalTcpFaceLocalFieldsDisabled, CommandSuccess>,
207 mpl::pair<NonLocalUdpFaceLocalFieldsEnabled, CommandFailure<406>>,
Junxiao Shi38c7d9e2017-04-13 14:22:50 +0000208 mpl::pair<NonLocalUdpFaceLocalFieldsDisabled, CommandSuccess>,
209 mpl::pair<FaceUriMalformed, CommandFailure<400>>,
210 mpl::pair<FaceUriNonCanonical, CommandFailure<400>>,
211 mpl::pair<FaceUriUnsupportedScheme, CommandFailure<406>>>;
Yanbiao Li73860e32015-08-19 16:30:16 -0700212
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400213BOOST_FIXTURE_TEST_CASE_TEMPLATE(NewFace, T, TestCases, FaceManagerCommandFixture)
Yanbiao Li73860e32015-08-19 16:30:16 -0700214{
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400215 using FaceType = typename T::first;
216 using CreateResult = typename T::second;
Yanbiao Li73860e32015-08-19 16:30:16 -0700217
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000218 Interest req = makeControlCommandRequest("/localhost/nfd/faces/create", FaceType::getParameters());
Yanbiao Li73860e32015-08-19 16:30:16 -0700219
220 bool hasCallbackFired = false;
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000221 this->node1.face.onSendData.connect([this, req, &hasCallbackFired] (const Data& response) {
222 if (!req.getName().isPrefixOf(response.getName())) {
Eric Newberry42602412016-08-27 09:33:18 -0700223 return;
224 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700225
Eric Newberry42602412016-08-27 09:33:18 -0700226 ControlResponse actual(response.getContent().blockFromValue());
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400227 ControlResponse expected(CreateResult::getExpected());
Eric Newberry42602412016-08-27 09:33:18 -0700228 BOOST_TEST_MESSAGE(actual.getText());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000229 BOOST_CHECK_EQUAL(expected.getCode(), actual.getCode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700230
Eric Newberry42602412016-08-27 09:33:18 -0700231 if (actual.getBody().hasWire()) {
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400232 ControlParameters expectedParams(FaceType::getParameters());
Eric Newberry42602412016-08-27 09:33:18 -0700233 ControlParameters actualParams(actual.getBody());
Yanbiao Li73860e32015-08-19 16:30:16 -0700234
Eric Newberry42602412016-08-27 09:33:18 -0700235 BOOST_CHECK(actualParams.hasFaceId());
236 BOOST_CHECK_EQUAL(expectedParams.getFacePersistency(), actualParams.getFacePersistency());
237
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700238 if (actual.getCode() == 200) {
239 if (expectedParams.hasFlags()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700240 BOOST_CHECK_EQUAL(expectedParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED),
241 actualParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700242 }
243 else {
Eric Newberryf40551a2016-09-05 15:41:16 -0700244 // local fields are disabled by default
245 BOOST_CHECK_EQUAL(actualParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED), false);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700246 }
247 }
248 else {
Yanbiao Li73860e32015-08-19 16:30:16 -0700249 BOOST_CHECK_EQUAL(expectedParams.getUri(), actualParams.getUri());
Yanbiao Li73860e32015-08-19 16:30:16 -0700250 }
Eric Newberry42602412016-08-27 09:33:18 -0700251 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700252
253 if (actual.getCode() != 200) {
Junxiao Shi38c7d9e2017-04-13 14:22:50 +0000254 FaceUri uri;
255 if (uri.parse(FaceType::getParameters().getUri())) {
256 // ensure face not created
257 auto& faceTable = this->node1.manager.m_faceTable;
258 BOOST_CHECK(std::none_of(faceTable.begin(), faceTable.end(), [uri] (Face& face) {
259 return face.getRemoteUri() == uri;
260 }));
261 }
262 else {
263 // do not check malformed FaceUri
264 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700265 }
266
Eric Newberry42602412016-08-27 09:33:18 -0700267 hasCallbackFired = true;
268 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700269
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000270 this->node1.face.receive(req);
Junxiao Shib84e6742016-07-19 13:16:22 +0000271 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700272
273 BOOST_CHECK(hasCallbackFired);
274}
275
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000276BOOST_FIXTURE_TEST_CASE(ExistingFace, FaceManagerCommandFixture)
Yanbiao Li73860e32015-08-19 16:30:16 -0700277{
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000278 using FaceType = UdpFacePersistent;
Yanbiao Li73860e32015-08-19 16:30:16 -0700279
280 {
281 // create face
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000282 Interest req = makeControlCommandRequest("/localhost/nfd/faces/create", FaceType::getParameters());
283 this->node1.face.receive(req);
Junxiao Shib84e6742016-07-19 13:16:22 +0000284 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700285 }
286
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000287 // find the created face
Davide Pesaventoa3c9ddb2017-04-10 22:15:24 -0400288 auto foundFace = this->node1.findFaceByUri(FaceType::getParameters().getUri());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000289 BOOST_REQUIRE(foundFace != nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700290
291 {
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000292 // re-create face
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000293 Interest req = makeControlCommandRequest("/localhost/nfd/faces/create", FaceType::getParameters());
Yanbiao Li73860e32015-08-19 16:30:16 -0700294
295 bool hasCallbackFired = false;
Junxiao Shib84e6742016-07-19 13:16:22 +0000296 this->node1.face.onSendData.connect(
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000297 [this, req, &hasCallbackFired, foundFace] (const Data& response) {
298 if (!req.getName().isPrefixOf(response.getName())) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700299 return;
300 }
301
302 ControlResponse actual(response.getContent().blockFromValue());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000303 BOOST_REQUIRE_EQUAL(actual.getCode(), 409);
Yanbiao Li73860e32015-08-19 16:30:16 -0700304
Yanbiao Li73860e32015-08-19 16:30:16 -0700305 ControlParameters actualParams(actual.getBody());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000306 BOOST_CHECK_EQUAL(foundFace->getId(), actualParams.getFaceId());
307 BOOST_CHECK_EQUAL(foundFace->getRemoteUri().toString(), actualParams.getUri());
308 BOOST_CHECK_EQUAL(foundFace->getPersistency(), actualParams.getFacePersistency());
Yanbiao Li73860e32015-08-19 16:30:16 -0700309
310 hasCallbackFired = true;
311 });
312
Junxiao Shi8a1f1702017-07-03 00:05:08 +0000313 this->node1.face.receive(req);
Junxiao Shib84e6742016-07-19 13:16:22 +0000314 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700315
316 BOOST_CHECK(hasCallbackFired);
317 }
318}
319
320BOOST_AUTO_TEST_SUITE_END() // CreateFace
321BOOST_AUTO_TEST_SUITE_END() // TestFaceManager
322BOOST_AUTO_TEST_SUITE_END() // Mgmt
323
Weiwei Liuf5aee942016-03-19 07:00:42 +0000324} // namespace tests
325} // namespace nfd