blob: 5cfe9527f9843c17898dec6b2241f4bffddc3e27 [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
30#include <thread>
Yanbiao Li73860e32015-08-19 16:30:16 -070031
Yanbiao Li73860e32015-08-19 16:30:16 -070032namespace nfd {
33namespace tests {
34
Yanbiao Li73860e32015-08-19 16:30:16 -070035BOOST_AUTO_TEST_SUITE(Mgmt)
36BOOST_AUTO_TEST_SUITE(TestFaceManager)
37
38BOOST_FIXTURE_TEST_SUITE(CreateFace, BaseFixture)
39
Yanbiao Li73860e32015-08-19 16:30:16 -070040class TcpFaceOnDemand
41{
42public:
43 ControlParameters
44 getParameters()
45 {
46 return ControlParameters()
47 .setUri("tcp4://127.0.0.1:26363")
48 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
49 }
50};
51
52class TcpFacePersistent
53{
54public:
55 ControlParameters
56 getParameters()
57 {
58 return ControlParameters()
59 .setUri("tcp4://127.0.0.1:26363")
60 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
61 }
62};
63
64class TcpFacePermanent
65{
66public:
67 ControlParameters
68 getParameters()
69 {
70 return ControlParameters()
71 .setUri("tcp4://127.0.0.1:26363")
72 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
73 }
74};
75
76class UdpFaceOnDemand
77{
78public:
79 ControlParameters
80 getParameters()
81 {
82 return ControlParameters()
83 .setUri("udp4://127.0.0.1:26363")
84 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_ON_DEMAND);
85 }
86};
87
Yanbiao Li73860e32015-08-19 16:30:16 -070088class UdpFacePersistent
89{
90public:
91 ControlParameters
92 getParameters()
93 {
94 return ControlParameters()
95 .setUri("udp4://127.0.0.1:26363")
96 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
97 }
98};
99
100class UdpFacePermanent
101{
102public:
103 ControlParameters
104 getParameters()
105 {
106 return ControlParameters()
107 .setUri("udp4://127.0.0.1:26363")
108 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERMANENT);
109 }
110};
111
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700112class UdpFaceConnectToSelf // face that will cause afterCreateFaceFailure to be invoked
113 // fails because remote endpoint is prohibited
Yanbiao Li73860e32015-08-19 16:30:16 -0700114{
115public:
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700116 ControlParameters
117 getParameters()
Yanbiao Li73860e32015-08-19 16:30:16 -0700118 {
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700119 return ControlParameters()
120 .setUri("udp4://0.0.0.0:16363"); // cannot connect to self
Yanbiao Li73860e32015-08-19 16:30:16 -0700121 }
122};
123
Eric Newberryf40551a2016-09-05 15:41:16 -0700124class LocalTcpFaceLocalFieldsEnabled
125{
126public:
127 ControlParameters
128 getParameters()
129 {
130 return ControlParameters()
131 .setUri("tcp4://127.0.0.1:26363")
132 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
133 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true);
134 }
135};
136
137class LocalTcpFaceLocalFieldsDisabled
138{
139public:
140 ControlParameters
141 getParameters()
142 {
143 return ControlParameters()
144 .setUri("tcp4://127.0.0.1:26363")
145 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
146 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, false);
147 }
148};
149
150class NonLocalUdpFaceLocalFieldsEnabled // won't work because non-local scope
151{
152public:
153 ControlParameters
154 getParameters()
155 {
156 return ControlParameters()
157 .setUri("udp4://127.0.0.1:26363")
158 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
159 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, true);
160 }
161};
162
163class NonLocalUdpFaceLocalFieldsDisabled
164{
165public:
166 ControlParameters
167 getParameters()
168 {
169 return ControlParameters()
170 .setUri("udp4://127.0.0.1:26363")
171 .setFacePersistency(ndn::nfd::FACE_PERSISTENCY_PERSISTENT)
172 .setFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED, false);
173 }
174};
175
Yanbiao Li73860e32015-08-19 16:30:16 -0700176namespace mpl = boost::mpl;
177
Eric Newberry42602412016-08-27 09:33:18 -0700178// pairs of CreateCommand and Success/Failure status
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700179typedef mpl::vector<mpl::pair<TcpFaceOnDemand, CommandFailure<406>>,
180 mpl::pair<TcpFacePersistent, CommandSuccess>,
181 mpl::pair<TcpFacePermanent, CommandFailure<406>>,
182 mpl::pair<UdpFaceOnDemand, CommandFailure<406>>,
183 mpl::pair<UdpFacePersistent, CommandSuccess>,
184 mpl::pair<UdpFacePermanent, CommandSuccess>,
Eric Newberryf40551a2016-09-05 15:41:16 -0700185 mpl::pair<UdpFaceConnectToSelf, CommandFailure<406>>,
186 mpl::pair<LocalTcpFaceLocalFieldsEnabled, CommandSuccess>,
187 mpl::pair<LocalTcpFaceLocalFieldsDisabled, CommandSuccess>,
188 mpl::pair<NonLocalUdpFaceLocalFieldsEnabled, CommandFailure<406>>,
189 mpl::pair<NonLocalUdpFaceLocalFieldsDisabled, CommandSuccess>> Faces;
Yanbiao Li73860e32015-08-19 16:30:16 -0700190
Eric Newberry42602412016-08-27 09:33:18 -0700191BOOST_FIXTURE_TEST_CASE_TEMPLATE(NewFace, T, Faces, FaceManagerCommandFixture)
Yanbiao Li73860e32015-08-19 16:30:16 -0700192{
193 typedef typename T::first FaceType;
194 typedef typename T::second CreateResult;
195
196 Name commandName("/localhost/nfd/faces");
197 commandName.append("create");
198 commandName.append(FaceType().getParameters().wireEncode());
Junxiao Shib84e6742016-07-19 13:16:22 +0000199 auto command = makeInterest(commandName);
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000200 m_keyChain.sign(*command);
Yanbiao Li73860e32015-08-19 16:30:16 -0700201
202 bool hasCallbackFired = false;
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000203 this->node1.face.onSendData.connect([this, command, &hasCallbackFired] (const Data& response) {
Eric Newberry42602412016-08-27 09:33:18 -0700204 if (!command->getName().isPrefixOf(response.getName())) {
205 return;
206 }
Yanbiao Li73860e32015-08-19 16:30:16 -0700207
Eric Newberry42602412016-08-27 09:33:18 -0700208 ControlResponse actual(response.getContent().blockFromValue());
209 ControlResponse expected(CreateResult().getExpected());
Eric Newberry42602412016-08-27 09:33:18 -0700210 BOOST_TEST_MESSAGE(actual.getText());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000211 BOOST_CHECK_EQUAL(expected.getCode(), actual.getCode());
Yanbiao Li73860e32015-08-19 16:30:16 -0700212
Eric Newberry42602412016-08-27 09:33:18 -0700213 if (actual.getBody().hasWire()) {
214 ControlParameters expectedParams(FaceType().getParameters());
215 ControlParameters actualParams(actual.getBody());
Yanbiao Li73860e32015-08-19 16:30:16 -0700216
Eric Newberry42602412016-08-27 09:33:18 -0700217 BOOST_CHECK(actualParams.hasFaceId());
218 BOOST_CHECK_EQUAL(expectedParams.getFacePersistency(), actualParams.getFacePersistency());
219
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700220 if (actual.getCode() == 200) {
221 if (expectedParams.hasFlags()) {
Eric Newberryf40551a2016-09-05 15:41:16 -0700222 BOOST_CHECK_EQUAL(expectedParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED),
223 actualParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED));
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700224 }
225 else {
Eric Newberryf40551a2016-09-05 15:41:16 -0700226 // local fields are disabled by default
227 BOOST_CHECK_EQUAL(actualParams.getFlagBit(ndn::nfd::BIT_LOCAL_FIELDS_ENABLED), false);
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700228 }
229 }
230 else {
Yanbiao Li73860e32015-08-19 16:30:16 -0700231 BOOST_CHECK_EQUAL(expectedParams.getUri(), actualParams.getUri());
Yanbiao Li73860e32015-08-19 16:30:16 -0700232 }
Eric Newberry42602412016-08-27 09:33:18 -0700233 }
Eric Newberryb5aa7f52016-09-03 20:36:12 -0700234
235 if (actual.getCode() != 200) {
236 // ensure face not created
237 FaceUri uri(FaceType().getParameters().getUri());
238 auto& faceTable = this->node1.manager.m_faceTable;
239 BOOST_CHECK(std::none_of(faceTable.begin(), faceTable.end(), [uri] (Face& face) {
240 return face.getRemoteUri() == uri;
241 }));
242 }
243
Eric Newberry42602412016-08-27 09:33:18 -0700244 hasCallbackFired = true;
245 });
Yanbiao Li73860e32015-08-19 16:30:16 -0700246
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000247 this->node1.face.receive(*command);
Junxiao Shib84e6742016-07-19 13:16:22 +0000248 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700249
250 BOOST_CHECK(hasCallbackFired);
251}
252
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000253BOOST_FIXTURE_TEST_CASE(ExistingFace, FaceManagerCommandFixture)
Yanbiao Li73860e32015-08-19 16:30:16 -0700254{
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000255 using FaceType = UdpFacePersistent;
Yanbiao Li73860e32015-08-19 16:30:16 -0700256
257 {
258 // create face
259
260 Name commandName("/localhost/nfd/faces");
261 commandName.append("create");
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000262 commandName.append(FaceType().getParameters().wireEncode());
Junxiao Shib84e6742016-07-19 13:16:22 +0000263 auto command = makeInterest(commandName);
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000264 m_keyChain.sign(*command);
Yanbiao Li73860e32015-08-19 16:30:16 -0700265
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000266 this->node1.face.receive(*command);
Junxiao Shib84e6742016-07-19 13:16:22 +0000267 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700268 }
269
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000270 // find the created face
271 auto foundFace = this->node1.findFaceByUri(FaceType().getParameters().getUri());
272 BOOST_REQUIRE(foundFace != nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700273
274 {
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000275 // re-create face
Yanbiao Li73860e32015-08-19 16:30:16 -0700276
277 Name commandName("/localhost/nfd/faces");
278 commandName.append("create");
279 commandName.append(FaceType().getParameters().wireEncode());
Junxiao Shib84e6742016-07-19 13:16:22 +0000280 auto command = makeInterest(commandName);
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000281 m_keyChain.sign(*command);
Yanbiao Li73860e32015-08-19 16:30:16 -0700282
283 bool hasCallbackFired = false;
Junxiao Shib84e6742016-07-19 13:16:22 +0000284 this->node1.face.onSendData.connect(
285 [this, command, &hasCallbackFired, foundFace] (const Data& response) {
Yanbiao Li73860e32015-08-19 16:30:16 -0700286 if (!command->getName().isPrefixOf(response.getName())) {
287 return;
288 }
289
290 ControlResponse actual(response.getContent().blockFromValue());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000291 BOOST_REQUIRE_EQUAL(actual.getCode(), 409);
Yanbiao Li73860e32015-08-19 16:30:16 -0700292
Yanbiao Li73860e32015-08-19 16:30:16 -0700293 ControlParameters actualParams(actual.getBody());
Yanbiao Li58ba3f92017-02-15 14:27:18 +0000294 BOOST_CHECK_EQUAL(foundFace->getId(), actualParams.getFaceId());
295 BOOST_CHECK_EQUAL(foundFace->getRemoteUri().toString(), actualParams.getUri());
296 BOOST_CHECK_EQUAL(foundFace->getPersistency(), actualParams.getFacePersistency());
Yanbiao Li73860e32015-08-19 16:30:16 -0700297
298 hasCallbackFired = true;
299 });
300
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000301 this->node1.face.receive(*command);
Junxiao Shib84e6742016-07-19 13:16:22 +0000302 this->advanceClocks(time::milliseconds(1), 5);
Yanbiao Li73860e32015-08-19 16:30:16 -0700303
304 BOOST_CHECK(hasCallbackFired);
305 }
306}
307
308BOOST_AUTO_TEST_SUITE_END() // CreateFace
309BOOST_AUTO_TEST_SUITE_END() // TestFaceManager
310BOOST_AUTO_TEST_SUITE_END() // Mgmt
311
Weiwei Liuf5aee942016-03-19 07:00:42 +0000312} // namespace tests
313} // namespace nfd