blob: 974e4177da77ba75616c8b95ce520c51f43bc3a1 [file] [log] [blame]
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070024
25#include "mgmt/face-manager.hpp"
26#include "mgmt/internal-face.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060027#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060028
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070029#include "face/face.hpp"
30#include "../face/dummy-face.hpp"
31#include "fw/face-table.hpp"
32#include "fw/forwarder.hpp"
33
34#include "common.hpp"
35#include "tests/test-common.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070036#include "validation-common.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060037#include "face-status-publisher-common.hpp"
38
Alexander Afanasyev4a771362014-04-24 21:29:33 -070039#include <ndn-cxx/encoding/tlv.hpp>
40#include <ndn-cxx/management/nfd-face-event-notification.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070041
42namespace nfd {
43namespace tests {
44
45NFD_LOG_INIT("FaceManagerTest");
46
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060047class FaceManagerTestFace : public DummyFace
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070048{
49public:
50
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060051 FaceManagerTestFace()
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070052 : m_closeFired(false)
53 {
54
55 }
56
57 virtual
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060058 ~FaceManagerTestFace()
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070059 {
60
61 }
62
63 virtual void
64 close()
65 {
66 m_closeFired = true;
67 }
68
69 bool
70 didCloseFire() const
71 {
72 return m_closeFired;
73 }
74
75private:
76 bool m_closeFired;
77};
78
79class TestFaceTable : public FaceTable
80{
81public:
82 TestFaceTable(Forwarder& forwarder)
83 : FaceTable(forwarder),
84 m_addFired(false),
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070085 m_getFired(false),
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060086 m_dummy(make_shared<FaceManagerTestFace>())
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070087 {
88
89 }
90
91 virtual
92 ~TestFaceTable()
93 {
94
95 }
96
97 virtual void
98 add(shared_ptr<Face> face)
99 {
100 m_addFired = true;
101 }
102
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700103 virtual shared_ptr<Face>
104 get(FaceId id) const
105 {
106 m_getFired = true;
107 return m_dummy;
108 }
109
110 bool
111 didAddFire() const
112 {
113 return m_addFired;
114 }
115
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700116 bool
117 didGetFire() const
118 {
119 return m_getFired;
120 }
121
122 void
123 reset()
124 {
125 m_addFired = false;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700126 m_getFired = false;
127 }
128
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600129 shared_ptr<FaceManagerTestFace>&
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700130 getDummyFace()
131 {
132 return m_dummy;
133 }
134
135private:
136 bool m_addFired;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700137 mutable bool m_getFired;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600138 shared_ptr<FaceManagerTestFace> m_dummy;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700139};
140
141
142class TestFaceTableFixture : public BaseFixture
143{
144public:
145 TestFaceTableFixture()
146 : m_faceTable(m_forwarder)
147 {
148
149 }
150
151 virtual
152 ~TestFaceTableFixture()
153 {
154
155 }
156
157protected:
158 Forwarder m_forwarder;
159 TestFaceTable m_faceTable;
160};
161
162class TestFaceManagerCommon
163{
164public:
165 TestFaceManagerCommon()
166 : m_face(make_shared<InternalFace>()),
167 m_callbackFired(false)
168 {
169
170 }
171
172 virtual
173 ~TestFaceManagerCommon()
174 {
175
176 }
177
178 shared_ptr<InternalFace>&
179 getFace()
180 {
181 return m_face;
182 }
183
184 void
185 validateControlResponseCommon(const Data& response,
186 const Name& expectedName,
187 uint32_t expectedCode,
188 const std::string& expectedText,
189 ControlResponse& control)
190 {
191 m_callbackFired = true;
192 Block controlRaw = response.getContent().blockFromValue();
193
194 control.wireDecode(controlRaw);
195
196 // NFD_LOG_DEBUG("received control response"
197 // << " Name: " << response.getName()
198 // << " code: " << control.getCode()
199 // << " text: " << control.getText());
200
201 BOOST_CHECK_EQUAL(response.getName(), expectedName);
202 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
203 BOOST_CHECK_EQUAL(control.getText(), expectedText);
204 }
205
206 void
207 validateControlResponse(const Data& response,
208 const Name& expectedName,
209 uint32_t expectedCode,
210 const std::string& expectedText)
211 {
212 ControlResponse control;
213 validateControlResponseCommon(response, expectedName,
214 expectedCode, expectedText, control);
215
216 if (!control.getBody().empty())
217 {
218 BOOST_FAIL("found unexpected control response body");
219 }
220 }
221
222 void
223 validateControlResponse(const Data& response,
224 const Name& expectedName,
225 uint32_t expectedCode,
226 const std::string& expectedText,
227 const Block& expectedBody)
228 {
229 ControlResponse control;
230 validateControlResponseCommon(response, expectedName,
231 expectedCode, expectedText, control);
232
233 BOOST_REQUIRE(!control.getBody().empty());
234 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
235
236 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
237 expectedBody.value_size()) == 0);
238
239 }
240
241 bool
242 didCallbackFire() const
243 {
244 return m_callbackFired;
245 }
246
247 void
248 resetCallbackFired()
249 {
250 m_callbackFired = false;
251 }
252
253protected:
254 shared_ptr<InternalFace> m_face;
255
256private:
257 bool m_callbackFired;
258};
259
260class FaceManagerFixture : public TestFaceTableFixture, public TestFaceManagerCommon
261{
262public:
263 FaceManagerFixture()
264 : m_manager(m_faceTable, m_face)
265 {
266 m_manager.setConfigFile(m_config);
267 }
268
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700269 virtual
270 ~FaceManagerFixture()
271 {
272
273 }
274
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600275 void
276 parseConfig(const std::string configuration, bool isDryRun)
277 {
278 m_config.parse(configuration, isDryRun, "dummy-config");
279 }
280
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700281 FaceManager&
282 getManager()
283 {
284 return m_manager;
285 }
286
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700287 void
288 addInterestRule(const std::string& regex,
289 ndn::IdentityCertificate& certificate)
290 {
291 m_manager.addInterestRule(regex, certificate);
292 }
293
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700294 bool
295 didFaceTableAddFire() const
296 {
297 return m_faceTable.didAddFire();
298 }
299
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700300 bool
301 didFaceTableGetFire() const
302 {
303 return m_faceTable.didGetFire();
304 }
305
306 void
307 resetFaceTable()
308 {
309 m_faceTable.reset();
310 }
311
312private:
313 FaceManager m_manager;
314 ConfigFile m_config;
315};
316
317BOOST_FIXTURE_TEST_SUITE(MgmtFaceManager, FaceManagerFixture)
318
319bool
320isExpectedException(const ConfigFile::Error& error, const std::string& expectedMessage)
321{
322 if (error.what() != expectedMessage)
323 {
324 NFD_LOG_ERROR("expected: " << expectedMessage << "\tgot: " << error.what());
325 }
326 return error.what() == expectedMessage;
327}
328
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600329#ifdef HAVE_UNIX_SOCKETS
330
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700331BOOST_AUTO_TEST_CASE(TestProcessSectionUnix)
332{
333 const std::string CONFIG =
334 "face_system\n"
335 "{\n"
336 " unix\n"
337 " {\n"
338 " listen yes\n"
339 " path /tmp/nfd.sock\n"
340 " }\n"
341 "}\n";
342 BOOST_TEST_CHECKPOINT("Calling parse");
343 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
344}
345
346BOOST_AUTO_TEST_CASE(TestProcessSectionUnixDryRun)
347{
348 const std::string CONFIG =
349 "face_system\n"
350 "{\n"
351 " unix\n"
352 " {\n"
353 " listen yes\n"
354 " path /var/run/nfd.sock\n"
355 " }\n"
356 "}\n";
357
358 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
359}
360
361BOOST_AUTO_TEST_CASE(TestProcessSectionUnixBadListen)
362{
363 const std::string CONFIG =
364 "face_system\n"
365 "{\n"
366 " unix\n"
367 " {\n"
368 " listen hello\n"
369 " }\n"
370 "}\n";
371 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
372 bind(&isExpectedException, _1,
373 "Invalid value for option \"listen\" in \"unix\" section"));
374}
375
376BOOST_AUTO_TEST_CASE(TestProcessSectionUnixUnknownOption)
377{
378 const std::string CONFIG =
379 "face_system\n"
380 "{\n"
381 " unix\n"
382 " {\n"
383 " hello\n"
384 " }\n"
385 "}\n";
386 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
387 bind(&isExpectedException, _1,
388 "Unrecognized option \"hello\" in \"unix\" section"));
389}
390
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600391#endif // HAVE_UNIX_SOCKETS
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700392
393
394BOOST_AUTO_TEST_CASE(TestProcessSectionTcp)
395{
396 const std::string CONFIG =
397 "face_system\n"
398 "{\n"
399 " tcp\n"
400 " {\n"
401 " listen yes\n"
402 " port 6363\n"
Steve DiBenedetto95152872014-04-11 12:40:59 -0600403 " enable_v4 yes\n"
404 " enable_v6 yes\n"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700405 " }\n"
406 "}\n";
407 try
408 {
409 parseConfig(CONFIG, false);
410 }
411 catch (const std::runtime_error& e)
412 {
413 const std::string reason = e.what();
414 if (reason.find("Address in use") != std::string::npos)
415 {
416 BOOST_FAIL(reason);
417 }
418 }
419}
420
421BOOST_AUTO_TEST_CASE(TestProcessSectionTcpDryRun)
422{
423 const std::string CONFIG =
424 "face_system\n"
425 "{\n"
426 " tcp\n"
427 " {\n"
428 " listen yes\n"
429 " port 6363\n"
Steve DiBenedetto95152872014-04-11 12:40:59 -0600430 " enable_v4 yes\n"
431 " enable_v6 yes\n"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700432 " }\n"
433 "}\n";
434 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
435}
436
437BOOST_AUTO_TEST_CASE(TestProcessSectionTcpBadListen)
438{
439 const std::string CONFIG =
440 "face_system\n"
441 "{\n"
442 " tcp\n"
443 " {\n"
444 " listen hello\n"
445 " }\n"
446 "}\n";
447 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
448 bind(&isExpectedException, _1,
449 "Invalid value for option \"listen\" in \"tcp\" section"));
450}
451
Steve DiBenedetto95152872014-04-11 12:40:59 -0600452BOOST_AUTO_TEST_CASE(TestProcessSectionTcpChannelsDisabled)
453{
454 const std::string CONFIG =
455 "face_system\n"
456 "{\n"
457 " tcp\n"
458 " {\n"
459 " port 6363\n"
460 " enable_v4 no\n"
461 " enable_v6 no\n"
462 " }\n"
463 "}\n";
464 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
465 bind(&isExpectedException, _1,
466 "IPv4 and IPv6 channels have been disabled."
467 " Remove \"tcp\" section to disable TCP channels or"
468 " re-enable at least one channel type."));
469}
470
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700471BOOST_AUTO_TEST_CASE(TestProcessSectionTcpUnknownOption)
472{
473 const std::string CONFIG =
474 "face_system\n"
475 "{\n"
476 " tcp\n"
477 " {\n"
478 " hello\n"
479 " }\n"
480 "}\n";
481 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
482 bind(&isExpectedException, _1,
483 "Unrecognized option \"hello\" in \"tcp\" section"));
484}
485
486BOOST_AUTO_TEST_CASE(TestProcessSectionUdp)
487{
488 const std::string CONFIG =
489 "face_system\n"
490 "{\n"
491 " udp\n"
492 " {\n"
493 " port 6363\n"
Steve DiBenedetto95152872014-04-11 12:40:59 -0600494 " enable_v4 yes\n"
495 " enable_v6 yes\n"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700496 " idle_timeout 30\n"
497 " keep_alive_interval 25\n"
498 " mcast yes\n"
499 " mcast_port 56363\n"
500 " mcast_group 224.0.23.170\n"
501 " }\n"
502 "}\n";
503 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
504}
505
506BOOST_AUTO_TEST_CASE(TestProcessSectionUdpDryRun)
507{
508 const std::string CONFIG =
509 "face_system\n"
510 "{\n"
511 " udp\n"
512 " {\n"
513 " port 6363\n"
514 " idle_timeout 30\n"
515 " keep_alive_interval 25\n"
516 " mcast yes\n"
517 " mcast_port 56363\n"
518 " mcast_group 224.0.23.170\n"
519 " }\n"
520 "}\n";
521 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
522}
523
524BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadIdleTimeout)
525{
526 const std::string CONFIG =
527 "face_system\n"
528 "{\n"
529 " udp\n"
530 " {\n"
531 " idle_timeout hello\n"
532 " }\n"
533 "}\n";
534
535 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
536 bind(&isExpectedException, _1,
537 "Invalid value for option \"idle_timeout\" in \"udp\" section"));
538}
539
540BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcast)
541{
542 const std::string CONFIG =
543 "face_system\n"
544 "{\n"
545 " udp\n"
546 " {\n"
547 " mcast hello\n"
548 " }\n"
549 "}\n";
550
551 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
552 bind(&isExpectedException, _1,
553 "Invalid value for option \"mcast\" in \"udp\" section"));
554}
555
556BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcastGroup)
557{
558 const std::string CONFIG =
559 "face_system\n"
560 "{\n"
561 " udp\n"
562 " {\n"
563 " mcast no\n"
564 " mcast_port 50\n"
565 " mcast_group hello\n"
566 " }\n"
567 "}\n";
568
569 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
570 bind(&isExpectedException, _1,
571 "Invalid value for option \"mcast_group\" in \"udp\" section"));
572}
573
574BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcastGroupV6)
575{
576 const std::string CONFIG =
577 "face_system\n"
578 "{\n"
579 " udp\n"
580 " {\n"
581 " mcast no\n"
582 " mcast_port 50\n"
583 " mcast_group ::1\n"
584 " }\n"
585 "}\n";
586
587 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
588 bind(&isExpectedException, _1,
589 "Invalid value for option \"mcast_group\" in \"udp\" section"));
590}
591
Steve DiBenedetto95152872014-04-11 12:40:59 -0600592BOOST_AUTO_TEST_CASE(TestProcessSectionUdpChannelsDisabled)
593{
594 const std::string CONFIG =
595 "face_system\n"
596 "{\n"
597 " udp\n"
598 " {\n"
599 " port 6363\n"
600 " enable_v4 no\n"
601 " enable_v6 no\n"
602 " idle_timeout 30\n"
603 " keep_alive_interval 25\n"
604 " mcast yes\n"
605 " mcast_port 56363\n"
606 " mcast_group 224.0.23.170\n"
607 " }\n"
608 "}\n";
609 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
610 bind(&isExpectedException, _1,
611 "IPv4 and IPv6 channels have been disabled."
612 " Remove \"udp\" section to disable UDP channels or"
613 " re-enable at least one channel type."));
614}
615
616BOOST_AUTO_TEST_CASE(TestProcessSectionUdpConflictingMcast)
617{
618 const std::string CONFIG =
619 "face_system\n"
620 "{\n"
621 " udp\n"
622 " {\n"
623 " port 6363\n"
624 " enable_v4 no\n"
625 " enable_v6 yes\n"
626 " idle_timeout 30\n"
627 " keep_alive_interval 25\n"
628 " mcast yes\n"
629 " mcast_port 56363\n"
630 " mcast_group 224.0.23.170\n"
631 " }\n"
632 "}\n";
633 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
634 bind(&isExpectedException, _1,
635 "IPv4 multicast requested, but IPv4 channels"
636 " have been disabled (conflicting configuration options set)"));
637}
638
639
640
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700641BOOST_AUTO_TEST_CASE(TestProcessSectionUdpUnknownOption)
642{
643 const std::string CONFIG =
644 "face_system\n"
645 "{\n"
646 " udp\n"
647 " {\n"
648 " hello\n"
649 " }\n"
650 "}\n";
651 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
652 bind(&isExpectedException, _1,
653 "Unrecognized option \"hello\" in \"udp\" section"));
654}
655
Alexander Afanasyev885a85b2014-04-12 21:01:13 -0700656#ifdef HAVE_LIBPCAP
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600657
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700658BOOST_AUTO_TEST_CASE(TestProcessSectionEther)
659{
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600660
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700661 const std::string CONFIG =
662 "face_system\n"
663 "{\n"
664 " ether\n"
665 " {\n"
666 " mcast yes\n"
667 " mcast_group 01:00:5E:00:17:AA\n"
668 " }\n"
669 "}\n";
670
671 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
672}
673
674BOOST_AUTO_TEST_CASE(TestProcessSectionEtherDryRun)
675{
676 const std::string CONFIG =
677 "face_system\n"
678 "{\n"
679 " ether\n"
680 " {\n"
681 " mcast yes\n"
682 " mcast_group 01:00:5E:00:17:AA\n"
683 " }\n"
684 "}\n";
685
686 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
687}
688
689BOOST_AUTO_TEST_CASE(TestProcessSectionEtherBadMcast)
690{
691 const std::string CONFIG =
692 "face_system\n"
693 "{\n"
694 " ether\n"
695 " {\n"
696 " mcast hello\n"
697 " }\n"
698 "}\n";
699
700 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
701 bind(&isExpectedException, _1,
702 "Invalid value for option \"mcast\" in \"ether\" section"));
703}
704
705BOOST_AUTO_TEST_CASE(TestProcessSectionEtherBadMcastGroup)
706{
707 const std::string CONFIG =
708 "face_system\n"
709 "{\n"
710 " ether\n"
711 " {\n"
712 " mcast yes\n"
713 " mcast_group\n"
714 " }\n"
715 "}\n";
716
717 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
718 bind(&isExpectedException, _1,
719 "Invalid value for option \"mcast_group\" in \"ether\" section"));
720}
721
722BOOST_AUTO_TEST_CASE(TestProcessSectionEtherUnknownOption)
723{
724 const std::string CONFIG =
725 "face_system\n"
726 "{\n"
727 " ether\n"
728 " {\n"
729 " hello\n"
730 " }\n"
731 "}\n";
732 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
733 bind(&isExpectedException, _1,
734 "Unrecognized option \"hello\" in \"ether\" section"));
735}
736
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600737#endif
738
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700739BOOST_AUTO_TEST_CASE(TestFireInterestFilter)
740{
741 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/faces"));
742
743 getFace()->onReceiveData +=
744 bind(&FaceManagerFixture::validateControlResponse, this, _1,
745 command->getName(), 400, "Malformed command");
746
747 getFace()->sendInterest(*command);
Junxiao Shi16d1b7d2014-03-27 21:29:09 -0700748 g_io.run_one();
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700749
750 BOOST_REQUIRE(didCallbackFire());
751}
752
753BOOST_AUTO_TEST_CASE(MalformedCommmand)
754{
755 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/faces"));
756
757 getFace()->onReceiveData +=
758 bind(&FaceManagerFixture::validateControlResponse, this, _1,
759 command->getName(), 400, "Malformed command");
760
761 getManager().onFaceRequest(*command);
762
763 BOOST_REQUIRE(didCallbackFire());
764}
765
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700766BOOST_AUTO_TEST_CASE(UnsignedCommand)
767{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600768 ControlParameters parameters;
769 parameters.setUri("tcp://127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700770
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600771 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700772
773 Name commandName("/localhost/nfd/faces");
774 commandName.append("create");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600775 commandName.append(encodedParameters);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700776
777 shared_ptr<Interest> command(make_shared<Interest>(commandName));
778
779 getFace()->onReceiveData +=
780 bind(&FaceManagerFixture::validateControlResponse, this, _1,
781 command->getName(), 401, "Signature required");
782
783 getManager().onFaceRequest(*command);
784
785 BOOST_REQUIRE(didCallbackFire());
786}
787
788BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedCommandFixture<FaceManagerFixture>)
789{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600790 ControlParameters parameters;
791 parameters.setUri("tcp://127.0.0.1");
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700792
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600793 Block encodedParameters(parameters.wireEncode());
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700794
795 Name commandName("/localhost/nfd/faces");
796 commandName.append("create");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600797 commandName.append(encodedParameters);
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700798
799 shared_ptr<Interest> command(make_shared<Interest>(commandName));
800 generateCommand(*command);
801
802 getFace()->onReceiveData +=
803 bind(&FaceManagerFixture::validateControlResponse, this, _1,
804 command->getName(), 403, "Unauthorized command");
805
806 getManager().onFaceRequest(*command);
807
808 BOOST_REQUIRE(didCallbackFire());
809}
810
811template <typename T> class AuthorizedCommandFixture : public CommandFixture<T>
812{
813public:
814 AuthorizedCommandFixture()
815 {
816 const std::string regex = "^<localhost><nfd><faces>";
817 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
818 }
819
820 virtual
821 ~AuthorizedCommandFixture()
822 {
823
824 }
825};
826
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700827BOOST_FIXTURE_TEST_CASE(UnsupportedCommand, AuthorizedCommandFixture<FaceManagerFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700828{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600829 ControlParameters parameters;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700830
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600831 Block encodedParameters(parameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700832
833 Name commandName("/localhost/nfd/faces");
834 commandName.append("unsupported");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600835 commandName.append(encodedParameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700836
837 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700838 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700839
840 getFace()->onReceiveData +=
841 bind(&FaceManagerFixture::validateControlResponse, this, _1,
842 command->getName(), 501, "Unsupported command");
843
844 getManager().onFaceRequest(*command);
845
846 BOOST_REQUIRE(didCallbackFire());
847}
848
849class ValidatedFaceRequestFixture : public TestFaceTableFixture,
850 public TestFaceManagerCommon,
851 public FaceManager
852{
853public:
854
855 ValidatedFaceRequestFixture()
856 : FaceManager(TestFaceTableFixture::m_faceTable, TestFaceManagerCommon::m_face),
857 m_createFaceFired(false),
858 m_destroyFaceFired(false)
859 {
860
861 }
862
863 virtual void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600864 createFace(const Interest& request,
865 ControlParameters& parameters)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700866 {
867 m_createFaceFired = true;
868 }
869
870 virtual void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600871 destroyFace(const Interest& request,
872 ControlParameters& parameters)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700873 {
874 m_destroyFaceFired = true;
875 }
876
877 virtual
878 ~ValidatedFaceRequestFixture()
879 {
880
881 }
882
883 bool
884 didCreateFaceFire() const
885 {
886 return m_createFaceFired;
887 }
888
889 bool
890 didDestroyFaceFire() const
891 {
892 return m_destroyFaceFired;
893 }
894
895private:
896 bool m_createFaceFired;
897 bool m_destroyFaceFired;
898};
899
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700900BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestBadOptionParse,
901 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700902{
903 Name commandName("/localhost/nfd/faces");
904 commandName.append("create");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600905 commandName.append("NotReallyParameters");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700906
907 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700908 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700909
910 getFace()->onReceiveData +=
911 bind(&ValidatedFaceRequestFixture::validateControlResponse, this, _1,
912 command->getName(), 400, "Malformed command");
913
914 onValidatedFaceRequest(command);
915
916 BOOST_REQUIRE(didCallbackFire());
917}
918
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700919BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestCreateFace,
920 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700921{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600922 ControlParameters parameters;
923 parameters.setUri("tcp://127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700924
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600925 Block encodedParameters(parameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700926
927 Name commandName("/localhost/nfd/faces");
928 commandName.append("create");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600929 commandName.append(encodedParameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700930
931 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700932 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700933
934 onValidatedFaceRequest(command);
935 BOOST_CHECK(didCreateFaceFire());
936}
937
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700938BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestDestroyFace,
939 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700940{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600941 ControlParameters parameters;
942 parameters.setUri("tcp://127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700943
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600944 Block encodedParameters(parameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700945
946 Name commandName("/localhost/nfd/faces");
947 commandName.append("destroy");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600948 commandName.append(encodedParameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700949
950 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700951 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700952
953 onValidatedFaceRequest(command);
954 BOOST_CHECK(didDestroyFaceFire());
955}
956
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600957class FaceTableFixture
958{
959public:
960 FaceTableFixture()
961 : m_faceTable(m_forwarder)
962 {
963 }
964
965 virtual
966 ~FaceTableFixture()
967 {
968 }
969
970protected:
971 Forwarder m_forwarder;
972 FaceTable m_faceTable;
973};
974
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600975class LocalControlFixture : public FaceTableFixture,
976 public TestFaceManagerCommon,
977 public FaceManager
978{
979public:
980 LocalControlFixture()
981 : FaceManager(FaceTableFixture::m_faceTable, TestFaceManagerCommon::m_face)
982 {
983 }
984};
985
986BOOST_FIXTURE_TEST_CASE(LocalControlInFaceId,
987 AuthorizedCommandFixture<LocalControlFixture>)
988{
989 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
990 BOOST_REQUIRE(dummy->isLocal());
991 FaceTableFixture::m_faceTable.add(dummy);
992
993 ControlParameters parameters;
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600994 parameters.setLocalControlFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
995
996 Block encodedParameters(parameters.wireEncode());
997
998 Name enable("/localhost/nfd/faces/enable-local-control");
999 enable.append(encodedParameters);
1000
1001 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
1002 enableCommand->setIncomingFaceId(dummy->getId());
1003
1004 generateCommand(*enableCommand);
1005
1006 TestFaceManagerCommon::m_face->onReceiveData +=
1007 bind(&LocalControlFixture::validateControlResponse, this, _1,
1008 enableCommand->getName(), 200, "Success", encodedParameters);
1009
1010 onValidatedFaceRequest(enableCommand);
1011
1012 BOOST_REQUIRE(didCallbackFire());
1013 BOOST_REQUIRE(dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1014 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1015
1016 TestFaceManagerCommon::m_face->onReceiveData.clear();
1017 resetCallbackFired();
1018
1019 Name disable("/localhost/nfd/faces/disable-local-control");
1020 disable.append(encodedParameters);
1021
1022 shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
1023 disableCommand->setIncomingFaceId(dummy->getId());
1024
1025 generateCommand(*disableCommand);
1026
1027 TestFaceManagerCommon::m_face->onReceiveData +=
1028 bind(&LocalControlFixture::validateControlResponse, this, _1,
1029 disableCommand->getName(), 200, "Success", encodedParameters);
1030
1031 onValidatedFaceRequest(disableCommand);
1032
1033 BOOST_REQUIRE(didCallbackFire());
1034 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1035 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1036}
1037
1038BOOST_FIXTURE_TEST_CASE(LocalControlInFaceIdFaceNotFound,
1039 AuthorizedCommandFixture<LocalControlFixture>)
1040{
1041 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
1042 BOOST_REQUIRE(dummy->isLocal());
1043 FaceTableFixture::m_faceTable.add(dummy);
1044
1045 ControlParameters parameters;
1046 parameters.setLocalControlFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
1047
1048 Block encodedParameters(parameters.wireEncode());
1049
1050 Name enable("/localhost/nfd/faces/enable-local-control");
1051 enable.append(encodedParameters);
1052
1053 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
1054 enableCommand->setIncomingFaceId(dummy->getId() + 100);
1055
1056 generateCommand(*enableCommand);
1057
1058 TestFaceManagerCommon::m_face->onReceiveData +=
1059 bind(&LocalControlFixture::validateControlResponse, this, _1,
Steve DiBenedetto51d242a2014-03-31 13:46:43 -06001060 enableCommand->getName(), 410, "Face not found");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001061
1062 onValidatedFaceRequest(enableCommand);
1063
1064 BOOST_REQUIRE(didCallbackFire());
1065 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1066 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1067
1068 TestFaceManagerCommon::m_face->onReceiveData.clear();
1069 resetCallbackFired();
1070
1071 Name disable("/localhost/nfd/faces/disable-local-control");
1072 disable.append(encodedParameters);
1073
1074 shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
1075 disableCommand->setIncomingFaceId(dummy->getId() + 100);
1076
1077 generateCommand(*disableCommand);
1078
1079 TestFaceManagerCommon::m_face->onReceiveData +=
1080 bind(&LocalControlFixture::validateControlResponse, this, _1,
Steve DiBenedetto51d242a2014-03-31 13:46:43 -06001081 disableCommand->getName(), 410, "Face not found");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001082
1083 onValidatedFaceRequest(disableCommand);
1084
1085 BOOST_REQUIRE(didCallbackFire());
1086 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1087 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1088}
1089
1090BOOST_FIXTURE_TEST_CASE(LocalControlMissingFeature,
1091 AuthorizedCommandFixture<LocalControlFixture>)
1092{
1093 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
1094 BOOST_REQUIRE(dummy->isLocal());
1095 FaceTableFixture::m_faceTable.add(dummy);
1096
1097 ControlParameters parameters;
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001098
1099 Block encodedParameters(parameters.wireEncode());
1100
1101 Name enable("/localhost/nfd/faces/enable-local-control");
1102 enable.append(encodedParameters);
1103
1104 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
1105 enableCommand->setIncomingFaceId(dummy->getId());
1106
1107 generateCommand(*enableCommand);
1108
1109 TestFaceManagerCommon::m_face->onReceiveData +=
1110 bind(&LocalControlFixture::validateControlResponse, this, _1,
1111 enableCommand->getName(), 400, "Malformed command");
1112
1113 onValidatedFaceRequest(enableCommand);
1114
1115 BOOST_REQUIRE(didCallbackFire());
1116 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1117 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1118
1119 TestFaceManagerCommon::m_face->onReceiveData.clear();
1120 resetCallbackFired();
1121
1122 Name disable("/localhost/nfd/faces/disable-local-control");
1123 disable.append(encodedParameters);
1124
1125 shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
1126 disableCommand->setIncomingFaceId(dummy->getId());
1127
1128 generateCommand(*disableCommand);
1129
1130 TestFaceManagerCommon::m_face->onReceiveData +=
1131 bind(&LocalControlFixture::validateControlResponse, this, _1,
1132 disableCommand->getName(), 400, "Malformed command");
1133
1134 onValidatedFaceRequest(disableCommand);
1135
1136 BOOST_REQUIRE(didCallbackFire());
1137 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1138 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1139}
1140
1141BOOST_FIXTURE_TEST_CASE(LocalControlInFaceIdNonLocal,
1142 AuthorizedCommandFixture<LocalControlFixture>)
1143{
1144 shared_ptr<DummyFace> dummy = make_shared<DummyFace>();
1145 BOOST_REQUIRE(!dummy->isLocal());
1146 FaceTableFixture::m_faceTable.add(dummy);
1147
1148 ControlParameters parameters;
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001149 parameters.setLocalControlFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
1150
1151 Block encodedParameters(parameters.wireEncode());
1152
1153 Name enable("/localhost/nfd/faces/enable-local-control");
1154 enable.append(encodedParameters);
1155
1156 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
1157 enableCommand->setIncomingFaceId(dummy->getId());
1158
1159 generateCommand(*enableCommand);
1160
1161 TestFaceManagerCommon::m_face->onReceiveData +=
1162 bind(&LocalControlFixture::validateControlResponse, this, _1,
Steve DiBenedetto51d242a2014-03-31 13:46:43 -06001163 enableCommand->getName(), 412, "Face is non-local");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001164
1165 onValidatedFaceRequest(enableCommand);
1166
1167 BOOST_REQUIRE(didCallbackFire());
1168
1169 TestFaceManagerCommon::m_face->onReceiveData.clear();
1170 resetCallbackFired();
1171
1172 Name disable("/localhost/nfd/faces/disable-local-control");
1173 enable.append(encodedParameters);
1174
1175 shared_ptr<Interest> disableCommand(make_shared<Interest>(enable));
1176 disableCommand->setIncomingFaceId(dummy->getId());
1177
1178 generateCommand(*disableCommand);
1179
1180 TestFaceManagerCommon::m_face->onReceiveData +=
1181 bind(&LocalControlFixture::validateControlResponse, this, _1,
Steve DiBenedetto51d242a2014-03-31 13:46:43 -06001182 disableCommand->getName(), 412, "Face is non-local");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001183
1184 onValidatedFaceRequest(disableCommand);
1185
1186 BOOST_REQUIRE(didCallbackFire());
1187}
1188
1189BOOST_FIXTURE_TEST_CASE(LocalControlNextHopFaceId,
1190 AuthorizedCommandFixture<LocalControlFixture>)
1191{
1192 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
1193 BOOST_REQUIRE(dummy->isLocal());
1194 FaceTableFixture::m_faceTable.add(dummy);
1195
1196 ControlParameters parameters;
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001197 parameters.setLocalControlFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
1198
1199 Block encodedParameters(parameters.wireEncode());
1200
1201 Name enable("/localhost/nfd/faces/enable-local-control");
1202 enable.append(encodedParameters);
1203
1204 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
1205 enableCommand->setIncomingFaceId(dummy->getId());
1206
1207 generateCommand(*enableCommand);
1208
1209 TestFaceManagerCommon::m_face->onReceiveData +=
1210 bind(&LocalControlFixture::validateControlResponse, this, _1,
1211 enableCommand->getName(), 200, "Success", encodedParameters);
1212
1213 onValidatedFaceRequest(enableCommand);
1214
1215 BOOST_REQUIRE(didCallbackFire());
1216 BOOST_REQUIRE(dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1217 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1218
1219
1220 TestFaceManagerCommon::m_face->onReceiveData.clear();
1221 resetCallbackFired();
1222
1223 Name disable("/localhost/nfd/faces/disable-local-control");
1224 disable.append(encodedParameters);
1225
1226 shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
1227 disableCommand->setIncomingFaceId(dummy->getId());
1228
1229 generateCommand(*disableCommand);
1230
1231 TestFaceManagerCommon::m_face->onReceiveData +=
1232 bind(&LocalControlFixture::validateControlResponse, this, _1,
1233 disableCommand->getName(), 200, "Success", encodedParameters);
1234
1235 onValidatedFaceRequest(disableCommand);
1236
1237 BOOST_REQUIRE(didCallbackFire());
1238 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1239 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1240}
1241
1242BOOST_FIXTURE_TEST_CASE(LocalControlNextHopFaceIdFaceNotFound,
1243 AuthorizedCommandFixture<LocalControlFixture>)
1244{
1245 shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
1246 BOOST_REQUIRE(dummy->isLocal());
1247 FaceTableFixture::m_faceTable.add(dummy);
1248
1249 ControlParameters parameters;
1250 parameters.setLocalControlFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
1251
1252 Block encodedParameters(parameters.wireEncode());
1253
1254 Name enable("/localhost/nfd/faces/enable-local-control");
1255 enable.append(encodedParameters);
1256
1257 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
1258 enableCommand->setIncomingFaceId(dummy->getId() + 100);
1259
1260 generateCommand(*enableCommand);
1261
1262 TestFaceManagerCommon::m_face->onReceiveData +=
1263 bind(&LocalControlFixture::validateControlResponse, this, _1,
Steve DiBenedetto51d242a2014-03-31 13:46:43 -06001264 enableCommand->getName(), 410, "Face not found");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001265
1266 onValidatedFaceRequest(enableCommand);
1267
1268 BOOST_REQUIRE(didCallbackFire());
1269 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1270 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1271
1272
1273 TestFaceManagerCommon::m_face->onReceiveData.clear();
1274 resetCallbackFired();
1275
1276 Name disable("/localhost/nfd/faces/disable-local-control");
1277 disable.append(encodedParameters);
1278
1279 shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
1280 disableCommand->setIncomingFaceId(dummy->getId() + 100);
1281
1282 generateCommand(*disableCommand);
1283
1284 TestFaceManagerCommon::m_face->onReceiveData +=
1285 bind(&LocalControlFixture::validateControlResponse, this, _1,
Steve DiBenedetto51d242a2014-03-31 13:46:43 -06001286 disableCommand->getName(), 410, "Face not found");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001287
1288 onValidatedFaceRequest(disableCommand);
1289
1290 BOOST_REQUIRE(didCallbackFire());
1291 BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID));
1292 BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID));
1293}
1294
1295BOOST_FIXTURE_TEST_CASE(LocalControlNextHopFaceIdNonLocal,
1296 AuthorizedCommandFixture<LocalControlFixture>)
1297{
1298 shared_ptr<DummyFace> dummy = make_shared<DummyFace>();
1299 BOOST_REQUIRE(!dummy->isLocal());
1300 FaceTableFixture::m_faceTable.add(dummy);
1301
1302 ControlParameters parameters;
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001303 parameters.setLocalControlFeature(LOCAL_CONTROL_FEATURE_NEXT_HOP_FACE_ID);
1304
1305 Block encodedParameters(parameters.wireEncode());
1306
1307 Name enable("/localhost/nfd/faces/enable-local-control");
1308 enable.append(encodedParameters);
1309
1310 shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
1311 enableCommand->setIncomingFaceId(dummy->getId());
1312
1313 generateCommand(*enableCommand);
1314
1315 TestFaceManagerCommon::m_face->onReceiveData +=
1316 bind(&LocalControlFixture::validateControlResponse, this, _1,
Steve DiBenedetto51d242a2014-03-31 13:46:43 -06001317 enableCommand->getName(), 412, "Face is non-local");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001318
1319 onValidatedFaceRequest(enableCommand);
1320
1321 BOOST_REQUIRE(didCallbackFire());
1322
1323 TestFaceManagerCommon::m_face->onReceiveData.clear();
1324 resetCallbackFired();
1325
1326 Name disable("/localhost/nfd/faces/disable-local-control");
1327 disable.append(encodedParameters);
1328
1329 shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
1330 disableCommand->setIncomingFaceId(dummy->getId());
1331
1332 generateCommand(*disableCommand);
1333
1334 TestFaceManagerCommon::m_face->onReceiveData +=
1335 bind(&LocalControlFixture::validateControlResponse, this, _1,
Steve DiBenedetto51d242a2014-03-31 13:46:43 -06001336 disableCommand->getName(), 412, "Face is non-local");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001337
1338 onValidatedFaceRequest(disableCommand);
1339
1340 BOOST_REQUIRE(didCallbackFire());
1341}
1342
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001343class FaceFixture : public FaceTableFixture,
1344 public TestFaceManagerCommon,
1345 public FaceManager
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001346{
1347public:
1348 FaceFixture()
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001349 : FaceManager(FaceTableFixture::m_faceTable,
1350 TestFaceManagerCommon::m_face)
1351 , m_receivedNotification(false)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001352 {
1353
1354 }
1355
1356 virtual
1357 ~FaceFixture()
1358 {
1359
1360 }
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001361
1362 void
1363 callbackDispatch(const Data& response,
1364 const Name& expectedName,
1365 uint32_t expectedCode,
1366 const std::string& expectedText,
1367 const Block& expectedBody,
1368 const ndn::nfd::FaceEventNotification& expectedFaceEvent)
1369 {
1370 Block payload = response.getContent().blockFromValue();
1371 if (payload.type() == ndn::tlv::nfd::ControlResponse)
1372 {
1373 validateControlResponse(response, expectedName, expectedCode,
1374 expectedText, expectedBody);
1375 }
1376 else if (payload.type() == ndn::tlv::nfd::FaceEventNotification)
1377 {
1378 validateFaceEvent(payload, expectedFaceEvent);
1379 }
1380 else
1381 {
1382 BOOST_FAIL("Received unknown message type: #" << payload.type());
1383 }
1384 }
1385
1386 void
1387 callbackDispatch(const Data& response,
1388 const Name& expectedName,
1389 uint32_t expectedCode,
1390 const std::string& expectedText,
1391 const ndn::nfd::FaceEventNotification& expectedFaceEvent)
1392 {
1393 Block payload = response.getContent().blockFromValue();
1394 if (payload.type() == ndn::tlv::nfd::ControlResponse)
1395 {
1396 validateControlResponse(response, expectedName,
1397 expectedCode, expectedText);
1398 }
1399 else if (payload.type() == ndn::tlv::nfd::FaceEventNotification)
1400 {
1401 validateFaceEvent(payload, expectedFaceEvent);
1402 }
1403 else
1404 {
1405 BOOST_FAIL("Received unknown message type: #" << payload.type());
1406 }
1407 }
1408
1409 void
1410 validateFaceEvent(const Block& wire,
1411 const ndn::nfd::FaceEventNotification& expectedFaceEvent)
1412 {
1413
1414 m_receivedNotification = true;
1415
1416 ndn::nfd::FaceEventNotification notification(wire);
1417
Junxiao Shi6e694322014-04-03 10:27:13 -07001418 BOOST_CHECK_EQUAL(notification.getKind(), expectedFaceEvent.getKind());
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001419 BOOST_CHECK_EQUAL(notification.getFaceId(), expectedFaceEvent.getFaceId());
Junxiao Shi6e694322014-04-03 10:27:13 -07001420 BOOST_CHECK_EQUAL(notification.getRemoteUri(), expectedFaceEvent.getRemoteUri());
1421 BOOST_CHECK_EQUAL(notification.getLocalUri(), expectedFaceEvent.getLocalUri());
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001422 }
1423
1424 bool
1425 didReceiveNotication() const
1426 {
1427 return m_receivedNotification;
1428 }
1429
1430protected:
1431 bool m_receivedNotification;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001432};
1433
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001434BOOST_FIXTURE_TEST_CASE(CreateFaceBadUri, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001435{
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001436 ControlParameters parameters;
1437 parameters.setUri("tcp:/127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001438
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001439 Block encodedParameters(parameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001440
1441 Name commandName("/localhost/nfd/faces");
1442 commandName.append("create");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001443 commandName.append(encodedParameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001444
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001445 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1446 generateCommand(*command);
1447
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001448 getFace()->onReceiveData +=
1449 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001450 command->getName(), 400, "Malformed command");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001451
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001452 createFace(*command, parameters);
1453
1454 BOOST_REQUIRE(didCallbackFire());
1455}
1456
1457BOOST_FIXTURE_TEST_CASE(CreateFaceMissingUri, AuthorizedCommandFixture<FaceFixture>)
1458{
1459 ControlParameters parameters;
1460
1461 Block encodedParameters(parameters.wireEncode());
1462
1463 Name commandName("/localhost/nfd/faces");
1464 commandName.append("create");
1465 commandName.append(encodedParameters);
1466
1467 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1468 generateCommand(*command);
1469
1470 getFace()->onReceiveData +=
1471 bind(&FaceFixture::validateControlResponse, this, _1,
1472 command->getName(), 400, "Malformed command");
1473
1474 createFace(*command, parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001475
1476 BOOST_REQUIRE(didCallbackFire());
1477}
1478
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001479BOOST_FIXTURE_TEST_CASE(CreateFaceUnknownScheme, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001480{
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001481 ControlParameters parameters;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001482 // this will be an unsupported protocol because no factories have been
1483 // added to the face manager
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001484 parameters.setUri("tcp://127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001485
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001486 Block encodedParameters(parameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001487
1488 Name commandName("/localhost/nfd/faces");
1489 commandName.append("create");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001490 commandName.append(encodedParameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001491
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001492 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1493 generateCommand(*command);
1494
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001495 getFace()->onReceiveData +=
1496 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001497 command->getName(), 501, "Unsupported protocol");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001498
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001499 createFace(*command, parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001500
1501 BOOST_REQUIRE(didCallbackFire());
1502}
1503
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001504BOOST_FIXTURE_TEST_CASE(OnCreated, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001505{
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001506 ControlParameters parameters;
1507 parameters.setUri("tcp://127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001508
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001509 Block encodedParameters(parameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001510
1511 Name commandName("/localhost/nfd/faces");
1512 commandName.append("create");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001513 commandName.append(encodedParameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001514
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001515 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1516 generateCommand(*command);
1517
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001518 ControlParameters resultParameters;
1519 resultParameters.setUri("tcp://127.0.0.1");
1520 resultParameters.setFaceId(1);
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001521
1522 shared_ptr<DummyFace> dummy(make_shared<DummyFace>());
1523
Junxiao Shi6e694322014-04-03 10:27:13 -07001524 ndn::nfd::FaceEventNotification expectedFaceEvent;
1525 expectedFaceEvent.setKind(ndn::nfd::FACE_EVENT_CREATED)
1526 .setFaceId(1)
1527 .setRemoteUri(dummy->getRemoteUri().toString())
1528 .setLocalUri(dummy->getLocalUri().toString())
1529 .setFlags(0);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001530
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001531 Block encodedResultParameters(resultParameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001532
1533 getFace()->onReceiveData +=
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001534 bind(&FaceFixture::callbackDispatch, this, _1,
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001535 command->getName(), 200, "Success",
1536 encodedResultParameters, expectedFaceEvent);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001537
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001538 onCreated(command->getName(), parameters, dummy);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001539
1540 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001541 BOOST_REQUIRE(didReceiveNotication());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001542}
1543
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001544BOOST_FIXTURE_TEST_CASE(OnConnectFailed, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001545{
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001546 ControlParameters parameters;
1547 parameters.setUri("tcp://127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001548
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001549 Block encodedParameters(parameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001550
1551 Name commandName("/localhost/nfd/faces");
1552 commandName.append("create");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001553 commandName.append(encodedParameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001554
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001555 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1556 generateCommand(*command);
1557
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001558 getFace()->onReceiveData +=
1559 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedettoca53ac62014-03-27 19:58:40 -06001560 command->getName(), 408, "unit-test-reason");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001561
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001562 onConnectFailed(command->getName(), "unit-test-reason");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001563
1564 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001565 BOOST_CHECK_EQUAL(didReceiveNotication(), false);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001566}
1567
1568
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001569BOOST_FIXTURE_TEST_CASE(DestroyFace, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001570{
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001571 shared_ptr<DummyFace> dummy(make_shared<DummyFace>());
1572 FaceTableFixture::m_faceTable.add(dummy);
1573
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001574 ControlParameters parameters;
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001575 parameters.setFaceId(dummy->getId());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001576
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001577 Block encodedParameters(parameters.wireEncode());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001578
1579 Name commandName("/localhost/nfd/faces");
1580 commandName.append("destroy");
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001581 commandName.append(encodedParameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001582
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001583 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1584 generateCommand(*command);
1585
Junxiao Shi6e694322014-04-03 10:27:13 -07001586 ndn::nfd::FaceEventNotification expectedFaceEvent;
1587 expectedFaceEvent.setKind(ndn::nfd::FACE_EVENT_DESTROYED)
1588 .setFaceId(dummy->getId())
1589 .setRemoteUri(dummy->getRemoteUri().toString())
1590 .setLocalUri(dummy->getLocalUri().toString())
1591 .setFlags(0);
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001592
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001593 getFace()->onReceiveData +=
Alexander Afanasyevf6980282014-05-13 18:28:40 -07001594 bind(&FaceFixture::callbackDispatch, this, _1, command->getName(),
1595 200, "Success", ref(encodedParameters), expectedFaceEvent);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001596
Steve DiBenedetto7564d972014-03-24 14:28:46 -06001597 destroyFace(*command, parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001598
1599 BOOST_REQUIRE(didCallbackFire());
Steve DiBenedettofbb40a82014-03-11 19:40:15 -06001600 BOOST_REQUIRE(didReceiveNotication());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001601}
1602
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001603class FaceListFixture : public FaceStatusPublisherFixture
1604{
1605public:
1606 FaceListFixture()
1607 : m_manager(m_table, m_face)
1608 {
1609
1610 }
1611
1612 virtual
1613 ~FaceListFixture()
1614 {
1615
1616 }
1617
1618protected:
1619 FaceManager m_manager;
1620};
1621
1622BOOST_FIXTURE_TEST_CASE(TestFaceList, FaceListFixture)
1623
1624{
1625 Name commandName("/localhost/nfd/faces/list");
1626 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1627
1628 // MAX_SEGMENT_SIZE == 4400, FaceStatus size with filler counters is 55
1629 // 55 divides 4400 (== 80), so only use 79 FaceStatuses and then two smaller ones
1630 // to force a FaceStatus to span Data packets
1631 for (int i = 0; i < 79; i++)
1632 {
1633 shared_ptr<TestCountersFace> dummy(make_shared<TestCountersFace>());
1634
1635 uint64_t filler = std::numeric_limits<uint64_t>::max() - 1;
1636 dummy->setCounters(filler, filler, filler, filler);
1637
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -07001638 m_referenceFaces.push_back(dummy);
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001639
1640 add(dummy);
1641 }
1642
1643 for (int i = 0; i < 2; i++)
1644 {
1645 shared_ptr<TestCountersFace> dummy(make_shared<TestCountersFace>());
1646 uint64_t filler = std::numeric_limits<uint32_t>::max() - 1;
1647 dummy->setCounters(filler, filler, filler, filler);
1648
Alexander Afanasyev7b7dfdd2014-03-21 13:57:54 -07001649 m_referenceFaces.push_back(dummy);
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001650
1651 add(dummy);
1652 }
1653
1654 ndn::EncodingBuffer buffer;
1655
1656 m_face->onReceiveData +=
1657 bind(&FaceStatusPublisherFixture::decodeFaceStatusBlock, this, _1);
1658
1659 m_manager.listFaces(*command);
1660 BOOST_REQUIRE(m_finished);
1661}
1662
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001663BOOST_AUTO_TEST_SUITE_END()
1664
1665} // namespace tests
1666} // namespace nfd