blob: 325b344244267fc045a60fb57101531fa64dd8be [file] [log] [blame]
Yanbiao Li73860e32015-08-19 16:30:16 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yanbiao Lidf846e52016-01-30 21:53:47 -08003 * Copyright (c) 2014-2016, 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
26#include "mgmt/face-manager.hpp"
Davide Pesavento5f47aa62016-10-07 22:09:09 +020027#include "core/random.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070028#include "face/tcp-factory.hpp"
29#include "face/udp-factory.hpp"
30
Davide Pesavento97210d52016-10-14 15:45:48 +020031#include "nfd-manager-common-fixture.hpp"
32#include "../face/dummy-face.hpp"
33
Yanbiao Li73860e32015-08-19 16:30:16 -070034#include <ndn-cxx/encoding/tlv.hpp>
Junxiao Shi25c6ce42016-09-09 13:49:59 +000035#include <ndn-cxx/mgmt/nfd/channel-status.hpp>
36#include <ndn-cxx/mgmt/nfd/face-event-notification.hpp>
Yanbiao Li73860e32015-08-19 16:30:16 -070037
38namespace nfd {
39namespace tests {
40
Yanbiao Lidf846e52016-01-30 21:53:47 -080041class FaceManagerFixture : public NfdManagerCommonFixture
Yanbiao Li73860e32015-08-19 16:30:16 -070042{
43public:
44 FaceManagerFixture()
45 : m_faceTable(m_forwarder.getFaceTable())
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000046 , m_manager(m_faceTable, m_dispatcher, *m_authenticator)
Yanbiao Li73860e32015-08-19 16:30:16 -070047 {
Junxiao Shi9ddf1b52016-08-22 03:58:55 +000048 setTopPrefix();
Yanbiao Lidf846e52016-01-30 21:53:47 -080049 setPrivilege("faces");
Yanbiao Li73860e32015-08-19 16:30:16 -070050 }
51
52public:
Junxiao Shicde37ad2015-12-24 01:02:05 -070053 enum AddFaceFlags {
54 REMOVE_LAST_NOTIFICATION = 1 << 0,
55 SET_SCOPE_LOCAL = 1 << 1,
56 SET_URI_TEST = 1 << 2,
57 RANDOMIZE_COUNTERS = 1 << 3
58 };
59
60 /** \brief adds a face to the FaceTable
61 * \param options bitwise OR'ed AddFaceFlags
62 */
Yanbiao Li73860e32015-08-19 16:30:16 -070063 shared_ptr<Face>
Junxiao Shicde37ad2015-12-24 01:02:05 -070064 addFace(int flags = 0)
Yanbiao Li73860e32015-08-19 16:30:16 -070065 {
Junxiao Shicde37ad2015-12-24 01:02:05 -070066 std::string uri = "dummy://";
67 ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_NON_LOCAL;
68
69 if ((flags & SET_SCOPE_LOCAL) != 0) {
70 scope = ndn::nfd::FACE_SCOPE_LOCAL;
71 }
72 if ((flags & SET_URI_TEST) != 0) {
73 uri = "test://";
74 }
75
76 auto face = make_shared<DummyFace>(uri, uri, scope);
Yanbiao Li73860e32015-08-19 16:30:16 -070077 m_faceTable.add(face);
Junxiao Shicde37ad2015-12-24 01:02:05 -070078
79 if ((flags & RANDOMIZE_COUNTERS) != 0) {
80 const face::FaceCounters& counters = face->getCounters();
81 randomizeCounter(counters.nInInterests);
82 randomizeCounter(counters.nOutInterests);
83 randomizeCounter(counters.nInData);
84 randomizeCounter(counters.nOutData);
85 randomizeCounter(counters.nInNacks);
86 randomizeCounter(counters.nOutNacks);
87 randomizeCounter(counters.nInPackets);
88 randomizeCounter(counters.nOutPackets);
89 randomizeCounter(counters.nInBytes);
90 randomizeCounter(counters.nOutBytes);
91 }
92
Yanbiao Li73860e32015-08-19 16:30:16 -070093 advanceClocks(time::milliseconds(1), 10); // wait for notification posted
Junxiao Shicde37ad2015-12-24 01:02:05 -070094 if ((flags & REMOVE_LAST_NOTIFICATION) != 0) {
Yanbiao Li73860e32015-08-19 16:30:16 -070095 m_responses.pop_back();
96 }
Junxiao Shicde37ad2015-12-24 01:02:05 -070097
Yanbiao Li73860e32015-08-19 16:30:16 -070098 return face;
99 }
100
Junxiao Shicde37ad2015-12-24 01:02:05 -0700101private:
102 template<typename T>
103 static typename std::enable_if<std::is_base_of<SimpleCounter, T>::value>::type
104 randomizeCounter(const T& counter)
105 {
Davide Pesavento5f47aa62016-10-07 22:09:09 +0200106 static std::uniform_int_distribution<typename T::rep> dist;
107 const_cast<T&>(counter).set(dist(getGlobalRng()));
Junxiao Shicde37ad2015-12-24 01:02:05 -0700108 }
109
Yanbiao Li73860e32015-08-19 16:30:16 -0700110protected:
111 FaceTable& m_faceTable;
112 FaceManager m_manager;
113};
114
Davide Pesavento97210d52016-10-14 15:45:48 +0200115BOOST_AUTO_TEST_SUITE(Mgmt)
Junxiao Shicde37ad2015-12-24 01:02:05 -0700116BOOST_FIXTURE_TEST_SUITE(TestFaceManager, FaceManagerFixture)
Yanbiao Li73860e32015-08-19 16:30:16 -0700117
118BOOST_AUTO_TEST_SUITE(DestroyFace)
119
120BOOST_AUTO_TEST_CASE(Existing)
121{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700122 auto addedFace = addFace(REMOVE_LAST_NOTIFICATION | SET_SCOPE_LOCAL); // clear notification for creation
Yanbiao Li73860e32015-08-19 16:30:16 -0700123
124 auto parameters = ControlParameters().setFaceId(addedFace->getId());
125 auto command = makeControlCommandRequest("/localhost/nfd/faces/destroy", parameters);
126
127 receiveInterest(command);
128
129 BOOST_REQUIRE_EQUAL(m_responses.size(), 2); // one response and one notification
130 // notification is already tested, so ignore it
131
132 BOOST_CHECK_EQUAL(checkResponse(1, command->getName(), makeResponse(200, "OK", parameters)),
133 CheckResponseResult::OK);
134
Junxiao Shicde37ad2015-12-24 01:02:05 -0700135 BOOST_CHECK_EQUAL(addedFace->getId(), face::INVALID_FACEID);
Yanbiao Li73860e32015-08-19 16:30:16 -0700136}
137
138BOOST_AUTO_TEST_CASE(NonExisting)
139{
140 auto parameters = ControlParameters().setFaceId(65535);
141 auto command = makeControlCommandRequest("/localhost/nfd/faces/destroy", parameters);
142
143 receiveInterest(command);
144
145 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
146
147 BOOST_CHECK_EQUAL(checkResponse(0, command->getName(), makeResponse(200, "OK", parameters)),
148 CheckResponseResult::OK);
149}
150
151BOOST_AUTO_TEST_SUITE_END() // DestroyFace
152
Davide Pesavento97210d52016-10-14 15:45:48 +0200153BOOST_AUTO_TEST_SUITE(Datasets)
Yanbiao Li73860e32015-08-19 16:30:16 -0700154
155BOOST_AUTO_TEST_CASE(FaceDataset)
156{
157 size_t nEntries = 303;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700158 for (size_t i = 0; i < nEntries; ++i) {
159 addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST | RANDOMIZE_COUNTERS);
Yanbiao Li73860e32015-08-19 16:30:16 -0700160 }
161
162 receiveInterest(makeInterest("/localhost/nfd/faces/list"));
163
164 Block content;
165 BOOST_CHECK_NO_THROW(content = concatenateResponses());
166 BOOST_CHECK_NO_THROW(content.parse());
167 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
168
Yanbiao Li73860e32015-08-19 16:30:16 -0700169 std::set<FaceId> faceIds;
170 for (size_t idx = 0; idx < nEntries; ++idx) {
171 BOOST_TEST_MESSAGE("processing element: " << idx);
172
173 ndn::nfd::FaceStatus decodedStatus;
174 BOOST_REQUIRE_NO_THROW(decodedStatus.wireDecode(content.elements()[idx]));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700175 BOOST_CHECK(m_faceTable.get(decodedStatus.getFaceId()) != nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700176 faceIds.insert(decodedStatus.getFaceId());
Yanbiao Li73860e32015-08-19 16:30:16 -0700177 }
178
179 BOOST_CHECK_EQUAL(faceIds.size(), nEntries);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700180 // TODO#3325 check dataset contents including counter values
Yanbiao Li73860e32015-08-19 16:30:16 -0700181}
182
183BOOST_AUTO_TEST_CASE(FaceQuery)
184{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700185 auto face1 = addFace(REMOVE_LAST_NOTIFICATION); // dummy://
186 auto face2 = addFace(REMOVE_LAST_NOTIFICATION | SET_SCOPE_LOCAL); // dummy://, local
187 auto face3 = addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST); // test://
Yanbiao Li73860e32015-08-19 16:30:16 -0700188
189 auto generateQueryName = [] (const ndn::nfd::FaceQueryFilter& filter) {
190 return Name("/localhost/nfd/faces/query").append(filter.wireEncode());
191 };
192
193 auto querySchemeName =
194 generateQueryName(ndn::nfd::FaceQueryFilter().setUriScheme("dummy"));
195 auto queryIdName =
196 generateQueryName(ndn::nfd::FaceQueryFilter().setFaceId(face1->getId()));
197 auto queryScopeName =
198 generateQueryName(ndn::nfd::FaceQueryFilter().setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL));
199 auto invalidQueryName =
200 Name("/localhost/nfd/faces/query").append(ndn::makeStringBlock(tlv::Content, "invalid"));
201
202 receiveInterest(makeInterest(querySchemeName)); // face1 and face2 expected
203 receiveInterest(makeInterest(queryIdName)); // face1 expected
204 receiveInterest(makeInterest(queryScopeName)); // face1 and face3 expected
205 receiveInterest(makeInterest(invalidQueryName)); // nack expected
206
207 BOOST_REQUIRE_EQUAL(m_responses.size(), 4);
208
209 Block content;
210 ndn::nfd::FaceStatus status;
211
212 content = m_responses[0].getContent();
213 BOOST_CHECK_NO_THROW(content.parse());
214 BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face2
215 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0]));
216 BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId());
217 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[1]));
218 BOOST_CHECK_EQUAL(face2->getId(), status.getFaceId());
219
220 content = m_responses[1].getContent();
221 BOOST_CHECK_NO_THROW(content.parse());
222 BOOST_CHECK_EQUAL(content.elements().size(), 1); // face1
223 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0]));
224 BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId());
225
226 content = m_responses[2].getContent();
227 BOOST_CHECK_NO_THROW(content.parse());
228 BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face3
229 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0]));
230 BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId());
231 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[1]));
232 BOOST_CHECK_EQUAL(face3->getId(), status.getFaceId());
233
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200234 ControlResponse expectedResponse(400, "Malformed filter"); // nack, 400, malformed filter
Yanbiao Li73860e32015-08-19 16:30:16 -0700235 BOOST_CHECK_EQUAL(checkResponse(3, invalidQueryName, expectedResponse, tlv::ContentType_Nack),
236 CheckResponseResult::OK);
237}
238
239class TestChannel : public Channel
240{
241public:
Davide Pesavento97210d52016-10-14 15:45:48 +0200242 explicit
Yanbiao Li73860e32015-08-19 16:30:16 -0700243 TestChannel(const std::string& uri)
244 {
245 setUri(FaceUri(uri));
246 }
247};
248
249class TestProtocolFactory : public ProtocolFactory
250{
251public:
252 virtual void
253 createFace(const FaceUri& uri,
254 ndn::nfd::FacePersistency persistency,
Eric Newberryf40551a2016-09-05 15:41:16 -0700255 bool wantLocalFieldsEnabled,
Yanbiao Li73860e32015-08-19 16:30:16 -0700256 const FaceCreatedCallback& onCreated,
Davide Pesavento97210d52016-10-14 15:45:48 +0200257 const FaceCreationFailedCallback& onConnectFailed) final
Yanbiao Li73860e32015-08-19 16:30:16 -0700258 {
259 }
260
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200261 virtual std::vector<shared_ptr<const Channel>>
Davide Pesavento97210d52016-10-14 15:45:48 +0200262 getChannels() const final
Yanbiao Li73860e32015-08-19 16:30:16 -0700263 {
264 return m_channels;
265 }
266
267public:
268 shared_ptr<TestChannel>
269 addChannel(const std::string& channelUri)
270 {
271 auto channel = make_shared<TestChannel>(channelUri);
272 m_channels.push_back(channel);
273 return channel;
274 }
275
276private:
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200277 std::vector<shared_ptr<const Channel>> m_channels;
Yanbiao Li73860e32015-08-19 16:30:16 -0700278};
279
280BOOST_AUTO_TEST_CASE(ChannelDataset)
281{
282 auto factory = make_shared<TestProtocolFactory>();
283 m_manager.m_factories["test"] = factory;
284
285 std::map<std::string, shared_ptr<TestChannel>> addedChannels;
286 size_t nEntries = 404;
287 for (size_t i = 0 ; i < nEntries ; i ++) {
288 auto channel = factory->addChannel("test" + boost::lexical_cast<std::string>(i) + "://");
289 addedChannels[channel->getUri().toString()] = channel;
290 }
291
292 receiveInterest(makeInterest("/localhost/nfd/faces/channels"));
293
294 Block content;
295 BOOST_CHECK_NO_THROW(content = concatenateResponses());
296 BOOST_CHECK_NO_THROW(content.parse());
297 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
298
299 for (size_t idx = 0; idx < nEntries; ++idx) {
300 BOOST_TEST_MESSAGE("processing element: " << idx);
301
302 ndn::nfd::ChannelStatus decodedStatus;
303 BOOST_CHECK_NO_THROW(decodedStatus.wireDecode(content.elements()[idx]));
304 BOOST_CHECK(addedChannels.find(decodedStatus.getLocalUri()) != addedChannels.end());
305 }
306}
307
Davide Pesavento97210d52016-10-14 15:45:48 +0200308BOOST_AUTO_TEST_SUITE_END() // Datasets
309
310BOOST_AUTO_TEST_SUITE(Notifications)
311
312BOOST_AUTO_TEST_CASE(FaceEventNotification)
313{
314 auto addedFace = addFace(); // trigger FACE_EVENT_CREATED notification
315 BOOST_CHECK_NE(addedFace->getId(), -1);
316 int64_t faceId = addedFace->getId();
317
318 // check notification
319 {
320 Block payload;
321 ndn::nfd::FaceEventNotification notification;
322 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
323 BOOST_CHECK_NO_THROW(payload = m_responses[0].getContent().blockFromValue());
324 BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification);
325 BOOST_CHECK_NO_THROW(notification.wireDecode(payload));
326 BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_CREATED);
327 BOOST_CHECK_EQUAL(notification.getFaceId(), faceId);
328 BOOST_CHECK_EQUAL(notification.getRemoteUri(), addedFace->getRemoteUri().toString());
329 BOOST_CHECK_EQUAL(notification.getLocalUri(), addedFace->getLocalUri().toString());
330 BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
331 BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
332 BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT);
333 }
334
335 addedFace->close(); // trigger FaceDestroy FACE_EVENT_DESTROYED
336 advanceClocks(time::milliseconds(1), 10);
337
338 // check notification
339 {
340 Block payload;
341 ndn::nfd::FaceEventNotification notification;
342 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
343 BOOST_CHECK_NO_THROW(payload = m_responses[1].getContent().blockFromValue());
344 BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification);
345 BOOST_CHECK_NO_THROW(notification.wireDecode(payload));
346 BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_DESTROYED);
347 BOOST_CHECK_EQUAL(notification.getFaceId(), faceId);
348 BOOST_CHECK_EQUAL(notification.getRemoteUri(), addedFace->getRemoteUri().toString());
349 BOOST_CHECK_EQUAL(notification.getLocalUri(), addedFace->getLocalUri().toString());
350 BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
351 BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
352 BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT);
353 }
354 BOOST_CHECK_EQUAL(addedFace->getId(), face::INVALID_FACEID);
355}
356
357BOOST_AUTO_TEST_SUITE_END() // Notifications
358
Yanbiao Li73860e32015-08-19 16:30:16 -0700359BOOST_AUTO_TEST_SUITE_END() // TestFaceManager
360BOOST_AUTO_TEST_SUITE_END() // Mgmt
361
362} // namespace tests
363} // namespace nfd