blob: d3e0b346dfe08ea92895d476231aeeaf5bc24643 [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"
Yanbiao Lidf846e52016-01-30 21:53:47 -080027#include "nfd-manager-common-fixture.hpp"
Yanbiao Li73860e32015-08-19 16:30:16 -070028#include "../face/dummy-face.hpp"
29#include "face/tcp-factory.hpp"
30#include "face/udp-factory.hpp"
31
32#include <ndn-cxx/util/random.hpp>
33#include <ndn-cxx/encoding/tlv.hpp>
34#include <ndn-cxx/management/nfd-channel-status.hpp>
35#include <ndn-cxx/management/nfd-face-event-notification.hpp>
36
37namespace nfd {
38namespace tests {
39
Junxiao Shicde37ad2015-12-24 01:02:05 -070040BOOST_AUTO_TEST_SUITE(Mgmt)
41
Yanbiao Lidf846e52016-01-30 21:53:47 -080042class FaceManagerFixture : public NfdManagerCommonFixture
Yanbiao Li73860e32015-08-19 16:30:16 -070043{
44public:
45 FaceManagerFixture()
46 : m_faceTable(m_forwarder.getFaceTable())
47 , m_manager(m_faceTable, m_dispatcher, m_validator)
48 {
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 {
106 const_cast<T&>(counter).set(ndn::random::generateWord64());
107 }
108
Yanbiao Li73860e32015-08-19 16:30:16 -0700109protected:
110 FaceTable& m_faceTable;
111 FaceManager m_manager;
112};
113
Junxiao Shicde37ad2015-12-24 01:02:05 -0700114BOOST_FIXTURE_TEST_SUITE(TestFaceManager, FaceManagerFixture)
Yanbiao Li73860e32015-08-19 16:30:16 -0700115
116BOOST_AUTO_TEST_SUITE(DestroyFace)
117
118BOOST_AUTO_TEST_CASE(Existing)
119{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700120 auto addedFace = addFace(REMOVE_LAST_NOTIFICATION | SET_SCOPE_LOCAL); // clear notification for creation
Yanbiao Li73860e32015-08-19 16:30:16 -0700121
122 auto parameters = ControlParameters().setFaceId(addedFace->getId());
123 auto command = makeControlCommandRequest("/localhost/nfd/faces/destroy", parameters);
124
125 receiveInterest(command);
126
127 BOOST_REQUIRE_EQUAL(m_responses.size(), 2); // one response and one notification
128 // notification is already tested, so ignore it
129
130 BOOST_CHECK_EQUAL(checkResponse(1, command->getName(), makeResponse(200, "OK", parameters)),
131 CheckResponseResult::OK);
132
Junxiao Shicde37ad2015-12-24 01:02:05 -0700133 BOOST_CHECK_EQUAL(addedFace->getId(), face::INVALID_FACEID);
Yanbiao Li73860e32015-08-19 16:30:16 -0700134}
135
136BOOST_AUTO_TEST_CASE(NonExisting)
137{
138 auto parameters = ControlParameters().setFaceId(65535);
139 auto command = makeControlCommandRequest("/localhost/nfd/faces/destroy", parameters);
140
141 receiveInterest(command);
142
143 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
144
145 BOOST_CHECK_EQUAL(checkResponse(0, command->getName(), makeResponse(200, "OK", parameters)),
146 CheckResponseResult::OK);
147}
148
149BOOST_AUTO_TEST_SUITE_END() // DestroyFace
150
151BOOST_AUTO_TEST_CASE(FaceEvents)
152{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700153 auto addedFace = addFace(); // trigger FACE_EVENT_CREATED notification
Yanbiao Li73860e32015-08-19 16:30:16 -0700154 BOOST_CHECK_NE(addedFace->getId(), -1);
155 int64_t faceId = addedFace->getId();
156
157 // check notification
158 {
159 Block payload;
160 ndn::nfd::FaceEventNotification notification;
161 BOOST_REQUIRE_EQUAL(m_responses.size(), 1);
162 BOOST_CHECK_NO_THROW(payload = m_responses[0].getContent().blockFromValue());
163 BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification);
164 BOOST_CHECK_NO_THROW(notification.wireDecode(payload));
165 BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_CREATED);
166 BOOST_CHECK_EQUAL(notification.getFaceId(), faceId);
167 BOOST_CHECK_EQUAL(notification.getRemoteUri(), addedFace->getRemoteUri().toString());
168 BOOST_CHECK_EQUAL(notification.getLocalUri(), addedFace->getLocalUri().toString());
169 BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
170 BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
171 BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT);
172 }
173
174 addedFace->close(); // trigger FaceDestroy FACE_EVENT_DESTROYED
175 advanceClocks(time::milliseconds(1), 10);
176
177 // check notification
178 {
179 Block payload;
180 ndn::nfd::FaceEventNotification notification;
181 BOOST_REQUIRE_EQUAL(m_responses.size(), 2);
182 BOOST_CHECK_NO_THROW(payload = m_responses[1].getContent().blockFromValue());
183 BOOST_CHECK_EQUAL(payload.type(), ndn::tlv::nfd::FaceEventNotification);
184 BOOST_CHECK_NO_THROW(notification.wireDecode(payload));
185 BOOST_CHECK_EQUAL(notification.getKind(), ndn::nfd::FACE_EVENT_DESTROYED);
186 BOOST_CHECK_EQUAL(notification.getFaceId(), faceId);
187 BOOST_CHECK_EQUAL(notification.getRemoteUri(), addedFace->getRemoteUri().toString());
188 BOOST_CHECK_EQUAL(notification.getLocalUri(), addedFace->getLocalUri().toString());
189 BOOST_CHECK_EQUAL(notification.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL);
190 BOOST_CHECK_EQUAL(notification.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT);
191 BOOST_CHECK_EQUAL(notification.getLinkType(), ndn::nfd::LinkType::LINK_TYPE_POINT_TO_POINT);
192 }
Junxiao Shicde37ad2015-12-24 01:02:05 -0700193 BOOST_CHECK_EQUAL(addedFace->getId(), face::INVALID_FACEID);
Yanbiao Li73860e32015-08-19 16:30:16 -0700194}
195
Yanbiao Li73860e32015-08-19 16:30:16 -0700196// @todo Refactor when ndn::nfd::FaceStatus implementes operator!= and operator<<
197class FaceStatus : public ndn::nfd::FaceStatus
198{
199public:
200 FaceStatus(const ndn::nfd::FaceStatus& s)
201 : ndn::nfd::FaceStatus(s)
202 {
203 }
204};
205
206bool
207operator!=(const FaceStatus& left, const FaceStatus& right)
208{
209 return left.getRemoteUri() != right.getRemoteUri() ||
Junxiao Shicde37ad2015-12-24 01:02:05 -0700210 left.getLocalUri() != right.getLocalUri() ||
211 left.getFaceScope() != right.getFaceScope() ||
212 left.getFacePersistency() != right.getFacePersistency() ||
213 left.getLinkType() != right.getLinkType() ||
214 left.getNInInterests() != right.getNInInterests() ||
215 left.getNInDatas() != right.getNInDatas() ||
216 left.getNOutInterests() != right.getNOutInterests() ||
217 left.getNOutDatas() != right.getNOutDatas() ||
218 left.getNInBytes() != right.getNInBytes() ||
219 left.getNOutBytes() != right.getNOutBytes();
Yanbiao Li73860e32015-08-19 16:30:16 -0700220}
221
222std::ostream&
223operator<<(std::ostream &os, const FaceStatus& status)
224{
225 os << "[" << status.getRemoteUri() << ", "
226 << status.getLocalUri() << ", "
227 << status.getFacePersistency() << ", "
228 << status.getLinkType() << ", "
229 << status.getNInInterests() << ", "
230 << status.getNInDatas() << ", "
231 << status.getNOutInterests() << ", "
232 << status.getNOutDatas() << ", "
233 << status.getNInBytes() << ", "
234 << status.getNOutBytes() << "]";
235 return os;
236}
237
238BOOST_AUTO_TEST_CASE(FaceDataset)
239{
240 size_t nEntries = 303;
Junxiao Shicde37ad2015-12-24 01:02:05 -0700241 for (size_t i = 0; i < nEntries; ++i) {
242 addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST | RANDOMIZE_COUNTERS);
Yanbiao Li73860e32015-08-19 16:30:16 -0700243 }
244
245 receiveInterest(makeInterest("/localhost/nfd/faces/list"));
246
247 Block content;
248 BOOST_CHECK_NO_THROW(content = concatenateResponses());
249 BOOST_CHECK_NO_THROW(content.parse());
250 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
251
Yanbiao Li73860e32015-08-19 16:30:16 -0700252 std::set<FaceId> faceIds;
253 for (size_t idx = 0; idx < nEntries; ++idx) {
254 BOOST_TEST_MESSAGE("processing element: " << idx);
255
256 ndn::nfd::FaceStatus decodedStatus;
257 BOOST_REQUIRE_NO_THROW(decodedStatus.wireDecode(content.elements()[idx]));
Junxiao Shida93f1f2015-11-11 06:13:16 -0700258 BOOST_CHECK(m_faceTable.get(decodedStatus.getFaceId()) != nullptr);
Yanbiao Li73860e32015-08-19 16:30:16 -0700259 faceIds.insert(decodedStatus.getFaceId());
Yanbiao Li73860e32015-08-19 16:30:16 -0700260 }
261
262 BOOST_CHECK_EQUAL(faceIds.size(), nEntries);
Junxiao Shida93f1f2015-11-11 06:13:16 -0700263 // TODO#3325 check dataset contents including counter values
Yanbiao Li73860e32015-08-19 16:30:16 -0700264}
265
266BOOST_AUTO_TEST_CASE(FaceQuery)
267{
Junxiao Shicde37ad2015-12-24 01:02:05 -0700268 auto face1 = addFace(REMOVE_LAST_NOTIFICATION); // dummy://
269 auto face2 = addFace(REMOVE_LAST_NOTIFICATION | SET_SCOPE_LOCAL); // dummy://, local
270 auto face3 = addFace(REMOVE_LAST_NOTIFICATION | SET_URI_TEST); // test://
Yanbiao Li73860e32015-08-19 16:30:16 -0700271
272 auto generateQueryName = [] (const ndn::nfd::FaceQueryFilter& filter) {
273 return Name("/localhost/nfd/faces/query").append(filter.wireEncode());
274 };
275
276 auto querySchemeName =
277 generateQueryName(ndn::nfd::FaceQueryFilter().setUriScheme("dummy"));
278 auto queryIdName =
279 generateQueryName(ndn::nfd::FaceQueryFilter().setFaceId(face1->getId()));
280 auto queryScopeName =
281 generateQueryName(ndn::nfd::FaceQueryFilter().setFaceScope(ndn::nfd::FACE_SCOPE_NON_LOCAL));
282 auto invalidQueryName =
283 Name("/localhost/nfd/faces/query").append(ndn::makeStringBlock(tlv::Content, "invalid"));
284
285 receiveInterest(makeInterest(querySchemeName)); // face1 and face2 expected
286 receiveInterest(makeInterest(queryIdName)); // face1 expected
287 receiveInterest(makeInterest(queryScopeName)); // face1 and face3 expected
288 receiveInterest(makeInterest(invalidQueryName)); // nack expected
289
290 BOOST_REQUIRE_EQUAL(m_responses.size(), 4);
291
292 Block content;
293 ndn::nfd::FaceStatus status;
294
295 content = m_responses[0].getContent();
296 BOOST_CHECK_NO_THROW(content.parse());
297 BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face2
298 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0]));
299 BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId());
300 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[1]));
301 BOOST_CHECK_EQUAL(face2->getId(), status.getFaceId());
302
303 content = m_responses[1].getContent();
304 BOOST_CHECK_NO_THROW(content.parse());
305 BOOST_CHECK_EQUAL(content.elements().size(), 1); // face1
306 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0]));
307 BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId());
308
309 content = m_responses[2].getContent();
310 BOOST_CHECK_NO_THROW(content.parse());
311 BOOST_CHECK_EQUAL(content.elements().size(), 2); // face1 and face3
312 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[0]));
313 BOOST_CHECK_EQUAL(face1->getId(), status.getFaceId());
314 BOOST_CHECK_NO_THROW(status.wireDecode(content.elements()[1]));
315 BOOST_CHECK_EQUAL(face3->getId(), status.getFaceId());
316
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200317 ControlResponse expectedResponse(400, "Malformed filter"); // nack, 400, malformed filter
Yanbiao Li73860e32015-08-19 16:30:16 -0700318 BOOST_CHECK_EQUAL(checkResponse(3, invalidQueryName, expectedResponse, tlv::ContentType_Nack),
319 CheckResponseResult::OK);
320}
321
322class TestChannel : public Channel
323{
324public:
325 TestChannel(const std::string& uri)
326 {
327 setUri(FaceUri(uri));
328 }
329};
330
331class TestProtocolFactory : public ProtocolFactory
332{
333public:
334 virtual void
335 createFace(const FaceUri& uri,
336 ndn::nfd::FacePersistency persistency,
337 const FaceCreatedCallback& onCreated,
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200338 const FaceCreationFailedCallback& onConnectFailed) DECL_OVERRIDE
Yanbiao Li73860e32015-08-19 16:30:16 -0700339 {
340 }
341
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200342 virtual std::vector<shared_ptr<const Channel>>
Yanbiao Li73860e32015-08-19 16:30:16 -0700343 getChannels() const DECL_OVERRIDE
344 {
345 return m_channels;
346 }
347
348public:
349 shared_ptr<TestChannel>
350 addChannel(const std::string& channelUri)
351 {
352 auto channel = make_shared<TestChannel>(channelUri);
353 m_channels.push_back(channel);
354 return channel;
355 }
356
357private:
Davide Pesavento1d7e7af2015-10-10 23:54:08 +0200358 std::vector<shared_ptr<const Channel>> m_channels;
Yanbiao Li73860e32015-08-19 16:30:16 -0700359};
360
361BOOST_AUTO_TEST_CASE(ChannelDataset)
362{
363 auto factory = make_shared<TestProtocolFactory>();
364 m_manager.m_factories["test"] = factory;
365
366 std::map<std::string, shared_ptr<TestChannel>> addedChannels;
367 size_t nEntries = 404;
368 for (size_t i = 0 ; i < nEntries ; i ++) {
369 auto channel = factory->addChannel("test" + boost::lexical_cast<std::string>(i) + "://");
370 addedChannels[channel->getUri().toString()] = channel;
371 }
372
373 receiveInterest(makeInterest("/localhost/nfd/faces/channels"));
374
375 Block content;
376 BOOST_CHECK_NO_THROW(content = concatenateResponses());
377 BOOST_CHECK_NO_THROW(content.parse());
378 BOOST_REQUIRE_EQUAL(content.elements().size(), nEntries);
379
380 for (size_t idx = 0; idx < nEntries; ++idx) {
381 BOOST_TEST_MESSAGE("processing element: " << idx);
382
383 ndn::nfd::ChannelStatus decodedStatus;
384 BOOST_CHECK_NO_THROW(decodedStatus.wireDecode(content.elements()[idx]));
385 BOOST_CHECK(addedChannels.find(decodedStatus.getLocalUri()) != addedChannels.end());
386 }
387}
388
389BOOST_AUTO_TEST_SUITE_END() // TestFaceManager
390BOOST_AUTO_TEST_SUITE_END() // Mgmt
391
392} // namespace tests
393} // namespace nfd