blob: 80e14d7aede08614b957f7743cedb632db0bd513 [file] [log] [blame]
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001// /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2// /**
3// * Copyright (C) 2014 Named Data Networking Project
4// * See COPYING for copyright and distribution information.
5// */
6
7#include "mgmt/face-manager.hpp"
8#include "mgmt/internal-face.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06009#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070010#include "face/face.hpp"
11#include "../face/dummy-face.hpp"
12#include "fw/face-table.hpp"
13#include "fw/forwarder.hpp"
14
15#include "common.hpp"
16#include "tests/test-common.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070017#include "validation-common.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060018#include "face-status-publisher-common.hpp"
19
20#include <ndn-cpp-dev/encoding/tlv.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070021
22namespace nfd {
23namespace tests {
24
25NFD_LOG_INIT("FaceManagerTest");
26
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060027class FaceManagerTestFace : public DummyFace
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070028{
29public:
30
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060031 FaceManagerTestFace()
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070032 : m_closeFired(false)
33 {
34
35 }
36
37 virtual
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060038 ~FaceManagerTestFace()
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070039 {
40
41 }
42
43 virtual void
44 close()
45 {
46 m_closeFired = true;
47 }
48
49 bool
50 didCloseFire() const
51 {
52 return m_closeFired;
53 }
54
55private:
56 bool m_closeFired;
57};
58
59class TestFaceTable : public FaceTable
60{
61public:
62 TestFaceTable(Forwarder& forwarder)
63 : FaceTable(forwarder),
64 m_addFired(false),
65 m_removeFired(false),
66 m_getFired(false),
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060067 m_dummy(make_shared<FaceManagerTestFace>())
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070068 {
69
70 }
71
72 virtual
73 ~TestFaceTable()
74 {
75
76 }
77
78 virtual void
79 add(shared_ptr<Face> face)
80 {
81 m_addFired = true;
82 }
83
84 virtual void
85 remove(shared_ptr<Face> face)
86 {
87 m_removeFired = true;
88 }
89
90 virtual shared_ptr<Face>
91 get(FaceId id) const
92 {
93 m_getFired = true;
94 return m_dummy;
95 }
96
97 bool
98 didAddFire() const
99 {
100 return m_addFired;
101 }
102
103 bool
104 didRemoveFire() const
105 {
106 return m_removeFired;
107 }
108
109 bool
110 didGetFire() const
111 {
112 return m_getFired;
113 }
114
115 void
116 reset()
117 {
118 m_addFired = false;
119 m_removeFired = false;
120 m_getFired = false;
121 }
122
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600123 shared_ptr<FaceManagerTestFace>&
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700124 getDummyFace()
125 {
126 return m_dummy;
127 }
128
129private:
130 bool m_addFired;
131 bool m_removeFired;
132 mutable bool m_getFired;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600133 shared_ptr<FaceManagerTestFace> m_dummy;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700134};
135
136
137class TestFaceTableFixture : public BaseFixture
138{
139public:
140 TestFaceTableFixture()
141 : m_faceTable(m_forwarder)
142 {
143
144 }
145
146 virtual
147 ~TestFaceTableFixture()
148 {
149
150 }
151
152protected:
153 Forwarder m_forwarder;
154 TestFaceTable m_faceTable;
155};
156
157class TestFaceManagerCommon
158{
159public:
160 TestFaceManagerCommon()
161 : m_face(make_shared<InternalFace>()),
162 m_callbackFired(false)
163 {
164
165 }
166
167 virtual
168 ~TestFaceManagerCommon()
169 {
170
171 }
172
173 shared_ptr<InternalFace>&
174 getFace()
175 {
176 return m_face;
177 }
178
179 void
180 validateControlResponseCommon(const Data& response,
181 const Name& expectedName,
182 uint32_t expectedCode,
183 const std::string& expectedText,
184 ControlResponse& control)
185 {
186 m_callbackFired = true;
187 Block controlRaw = response.getContent().blockFromValue();
188
189 control.wireDecode(controlRaw);
190
191 // NFD_LOG_DEBUG("received control response"
192 // << " Name: " << response.getName()
193 // << " code: " << control.getCode()
194 // << " text: " << control.getText());
195
196 BOOST_CHECK_EQUAL(response.getName(), expectedName);
197 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
198 BOOST_CHECK_EQUAL(control.getText(), expectedText);
199 }
200
201 void
202 validateControlResponse(const Data& response,
203 const Name& expectedName,
204 uint32_t expectedCode,
205 const std::string& expectedText)
206 {
207 ControlResponse control;
208 validateControlResponseCommon(response, expectedName,
209 expectedCode, expectedText, control);
210
211 if (!control.getBody().empty())
212 {
213 BOOST_FAIL("found unexpected control response body");
214 }
215 }
216
217 void
218 validateControlResponse(const Data& response,
219 const Name& expectedName,
220 uint32_t expectedCode,
221 const std::string& expectedText,
222 const Block& expectedBody)
223 {
224 ControlResponse control;
225 validateControlResponseCommon(response, expectedName,
226 expectedCode, expectedText, control);
227
228 BOOST_REQUIRE(!control.getBody().empty());
229 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
230
231 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
232 expectedBody.value_size()) == 0);
233
234 }
235
236 bool
237 didCallbackFire() const
238 {
239 return m_callbackFired;
240 }
241
242 void
243 resetCallbackFired()
244 {
245 m_callbackFired = false;
246 }
247
248protected:
249 shared_ptr<InternalFace> m_face;
250
251private:
252 bool m_callbackFired;
253};
254
255class FaceManagerFixture : public TestFaceTableFixture, public TestFaceManagerCommon
256{
257public:
258 FaceManagerFixture()
259 : m_manager(m_faceTable, m_face)
260 {
261 m_manager.setConfigFile(m_config);
262 }
263
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700264 virtual
265 ~FaceManagerFixture()
266 {
267
268 }
269
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600270 void
271 parseConfig(const std::string configuration, bool isDryRun)
272 {
273 m_config.parse(configuration, isDryRun, "dummy-config");
274 }
275
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700276 FaceManager&
277 getManager()
278 {
279 return m_manager;
280 }
281
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700282 void
283 addInterestRule(const std::string& regex,
284 ndn::IdentityCertificate& certificate)
285 {
286 m_manager.addInterestRule(regex, certificate);
287 }
288
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700289 bool
290 didFaceTableAddFire() const
291 {
292 return m_faceTable.didAddFire();
293 }
294
295 bool
296 didFaceTableRemoveFire() const
297 {
298 return m_faceTable.didRemoveFire();
299 }
300
301 bool
302 didFaceTableGetFire() const
303 {
304 return m_faceTable.didGetFire();
305 }
306
307 void
308 resetFaceTable()
309 {
310 m_faceTable.reset();
311 }
312
313private:
314 FaceManager m_manager;
315 ConfigFile m_config;
316};
317
318BOOST_FIXTURE_TEST_SUITE(MgmtFaceManager, FaceManagerFixture)
319
320bool
321isExpectedException(const ConfigFile::Error& error, const std::string& expectedMessage)
322{
323 if (error.what() != expectedMessage)
324 {
325 NFD_LOG_ERROR("expected: " << expectedMessage << "\tgot: " << error.what());
326 }
327 return error.what() == expectedMessage;
328}
329
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600330#ifdef HAVE_UNIX_SOCKETS
331
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700332BOOST_AUTO_TEST_CASE(TestProcessSectionUnix)
333{
334 const std::string CONFIG =
335 "face_system\n"
336 "{\n"
337 " unix\n"
338 " {\n"
339 " listen yes\n"
340 " path /tmp/nfd.sock\n"
341 " }\n"
342 "}\n";
343 BOOST_TEST_CHECKPOINT("Calling parse");
344 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
345}
346
347BOOST_AUTO_TEST_CASE(TestProcessSectionUnixDryRun)
348{
349 const std::string CONFIG =
350 "face_system\n"
351 "{\n"
352 " unix\n"
353 " {\n"
354 " listen yes\n"
355 " path /var/run/nfd.sock\n"
356 " }\n"
357 "}\n";
358
359 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
360}
361
362BOOST_AUTO_TEST_CASE(TestProcessSectionUnixBadListen)
363{
364 const std::string CONFIG =
365 "face_system\n"
366 "{\n"
367 " unix\n"
368 " {\n"
369 " listen hello\n"
370 " }\n"
371 "}\n";
372 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
373 bind(&isExpectedException, _1,
374 "Invalid value for option \"listen\" in \"unix\" section"));
375}
376
377BOOST_AUTO_TEST_CASE(TestProcessSectionUnixUnknownOption)
378{
379 const std::string CONFIG =
380 "face_system\n"
381 "{\n"
382 " unix\n"
383 " {\n"
384 " hello\n"
385 " }\n"
386 "}\n";
387 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
388 bind(&isExpectedException, _1,
389 "Unrecognized option \"hello\" in \"unix\" section"));
390}
391
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600392#endif // HAVE_UNIX_SOCKETS
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700393
394
395BOOST_AUTO_TEST_CASE(TestProcessSectionTcp)
396{
397 const std::string CONFIG =
398 "face_system\n"
399 "{\n"
400 " tcp\n"
401 " {\n"
402 " listen yes\n"
403 " port 6363\n"
404 " }\n"
405 "}\n";
406 try
407 {
408 parseConfig(CONFIG, false);
409 }
410 catch (const std::runtime_error& e)
411 {
412 const std::string reason = e.what();
413 if (reason.find("Address in use") != std::string::npos)
414 {
415 BOOST_FAIL(reason);
416 }
417 }
418}
419
420BOOST_AUTO_TEST_CASE(TestProcessSectionTcpDryRun)
421{
422 const std::string CONFIG =
423 "face_system\n"
424 "{\n"
425 " tcp\n"
426 " {\n"
427 " listen yes\n"
428 " port 6363\n"
429 " }\n"
430 "}\n";
431 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
432}
433
434BOOST_AUTO_TEST_CASE(TestProcessSectionTcpBadListen)
435{
436 const std::string CONFIG =
437 "face_system\n"
438 "{\n"
439 " tcp\n"
440 " {\n"
441 " listen hello\n"
442 " }\n"
443 "}\n";
444 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
445 bind(&isExpectedException, _1,
446 "Invalid value for option \"listen\" in \"tcp\" section"));
447}
448
449BOOST_AUTO_TEST_CASE(TestProcessSectionTcpUnknownOption)
450{
451 const std::string CONFIG =
452 "face_system\n"
453 "{\n"
454 " tcp\n"
455 " {\n"
456 " hello\n"
457 " }\n"
458 "}\n";
459 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
460 bind(&isExpectedException, _1,
461 "Unrecognized option \"hello\" in \"tcp\" section"));
462}
463
464BOOST_AUTO_TEST_CASE(TestProcessSectionUdp)
465{
466 const std::string CONFIG =
467 "face_system\n"
468 "{\n"
469 " udp\n"
470 " {\n"
471 " port 6363\n"
472 " idle_timeout 30\n"
473 " keep_alive_interval 25\n"
474 " mcast yes\n"
475 " mcast_port 56363\n"
476 " mcast_group 224.0.23.170\n"
477 " }\n"
478 "}\n";
479 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
480}
481
482BOOST_AUTO_TEST_CASE(TestProcessSectionUdpDryRun)
483{
484 const std::string CONFIG =
485 "face_system\n"
486 "{\n"
487 " udp\n"
488 " {\n"
489 " port 6363\n"
490 " idle_timeout 30\n"
491 " keep_alive_interval 25\n"
492 " mcast yes\n"
493 " mcast_port 56363\n"
494 " mcast_group 224.0.23.170\n"
495 " }\n"
496 "}\n";
497 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
498}
499
500BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadIdleTimeout)
501{
502 const std::string CONFIG =
503 "face_system\n"
504 "{\n"
505 " udp\n"
506 " {\n"
507 " idle_timeout hello\n"
508 " }\n"
509 "}\n";
510
511 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
512 bind(&isExpectedException, _1,
513 "Invalid value for option \"idle_timeout\" in \"udp\" section"));
514}
515
516BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcast)
517{
518 const std::string CONFIG =
519 "face_system\n"
520 "{\n"
521 " udp\n"
522 " {\n"
523 " mcast hello\n"
524 " }\n"
525 "}\n";
526
527 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
528 bind(&isExpectedException, _1,
529 "Invalid value for option \"mcast\" in \"udp\" section"));
530}
531
532BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcastGroup)
533{
534 const std::string CONFIG =
535 "face_system\n"
536 "{\n"
537 " udp\n"
538 " {\n"
539 " mcast no\n"
540 " mcast_port 50\n"
541 " mcast_group hello\n"
542 " }\n"
543 "}\n";
544
545 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
546 bind(&isExpectedException, _1,
547 "Invalid value for option \"mcast_group\" in \"udp\" section"));
548}
549
550BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcastGroupV6)
551{
552 const std::string CONFIG =
553 "face_system\n"
554 "{\n"
555 " udp\n"
556 " {\n"
557 " mcast no\n"
558 " mcast_port 50\n"
559 " mcast_group ::1\n"
560 " }\n"
561 "}\n";
562
563 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
564 bind(&isExpectedException, _1,
565 "Invalid value for option \"mcast_group\" in \"udp\" section"));
566}
567
568BOOST_AUTO_TEST_CASE(TestProcessSectionUdpUnknownOption)
569{
570 const std::string CONFIG =
571 "face_system\n"
572 "{\n"
573 " udp\n"
574 " {\n"
575 " hello\n"
576 " }\n"
577 "}\n";
578 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
579 bind(&isExpectedException, _1,
580 "Unrecognized option \"hello\" in \"udp\" section"));
581}
582
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600583#ifdef HAVE_PCAP
584
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700585BOOST_AUTO_TEST_CASE(TestProcessSectionEther)
586{
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600587
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700588 const std::string CONFIG =
589 "face_system\n"
590 "{\n"
591 " ether\n"
592 " {\n"
593 " mcast yes\n"
594 " mcast_group 01:00:5E:00:17:AA\n"
595 " }\n"
596 "}\n";
597
598 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
599}
600
601BOOST_AUTO_TEST_CASE(TestProcessSectionEtherDryRun)
602{
603 const std::string CONFIG =
604 "face_system\n"
605 "{\n"
606 " ether\n"
607 " {\n"
608 " mcast yes\n"
609 " mcast_group 01:00:5E:00:17:AA\n"
610 " }\n"
611 "}\n";
612
613 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
614}
615
616BOOST_AUTO_TEST_CASE(TestProcessSectionEtherBadMcast)
617{
618 const std::string CONFIG =
619 "face_system\n"
620 "{\n"
621 " ether\n"
622 " {\n"
623 " mcast hello\n"
624 " }\n"
625 "}\n";
626
627 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
628 bind(&isExpectedException, _1,
629 "Invalid value for option \"mcast\" in \"ether\" section"));
630}
631
632BOOST_AUTO_TEST_CASE(TestProcessSectionEtherBadMcastGroup)
633{
634 const std::string CONFIG =
635 "face_system\n"
636 "{\n"
637 " ether\n"
638 " {\n"
639 " mcast yes\n"
640 " mcast_group\n"
641 " }\n"
642 "}\n";
643
644 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
645 bind(&isExpectedException, _1,
646 "Invalid value for option \"mcast_group\" in \"ether\" section"));
647}
648
649BOOST_AUTO_TEST_CASE(TestProcessSectionEtherUnknownOption)
650{
651 const std::string CONFIG =
652 "face_system\n"
653 "{\n"
654 " ether\n"
655 " {\n"
656 " hello\n"
657 " }\n"
658 "}\n";
659 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
660 bind(&isExpectedException, _1,
661 "Unrecognized option \"hello\" in \"ether\" section"));
662}
663
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600664#endif
665
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700666BOOST_AUTO_TEST_CASE(TestFireInterestFilter)
667{
668 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/faces"));
669
670 getFace()->onReceiveData +=
671 bind(&FaceManagerFixture::validateControlResponse, this, _1,
672 command->getName(), 400, "Malformed command");
673
674 getFace()->sendInterest(*command);
675
676 BOOST_REQUIRE(didCallbackFire());
677}
678
679BOOST_AUTO_TEST_CASE(MalformedCommmand)
680{
681 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/faces"));
682
683 getFace()->onReceiveData +=
684 bind(&FaceManagerFixture::validateControlResponse, this, _1,
685 command->getName(), 400, "Malformed command");
686
687 getManager().onFaceRequest(*command);
688
689 BOOST_REQUIRE(didCallbackFire());
690}
691
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700692BOOST_AUTO_TEST_CASE(UnsignedCommand)
693{
694 ndn::nfd::FaceManagementOptions options;
695 options.setUri("tcp://127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700696
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700697 Block encodedOptions(options.wireEncode());
698
699 Name commandName("/localhost/nfd/faces");
700 commandName.append("create");
701 commandName.append(encodedOptions);
702
703 shared_ptr<Interest> command(make_shared<Interest>(commandName));
704
705 getFace()->onReceiveData +=
706 bind(&FaceManagerFixture::validateControlResponse, this, _1,
707 command->getName(), 401, "Signature required");
708
709 getManager().onFaceRequest(*command);
710
711 BOOST_REQUIRE(didCallbackFire());
712}
713
714BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedCommandFixture<FaceManagerFixture>)
715{
716 ndn::nfd::FaceManagementOptions options;
717 options.setUri("tcp://127.0.0.1");
718
719 Block encodedOptions(options.wireEncode());
720
721 Name commandName("/localhost/nfd/faces");
722 commandName.append("create");
723 commandName.append(encodedOptions);
724
725 shared_ptr<Interest> command(make_shared<Interest>(commandName));
726 generateCommand(*command);
727
728 getFace()->onReceiveData +=
729 bind(&FaceManagerFixture::validateControlResponse, this, _1,
730 command->getName(), 403, "Unauthorized command");
731
732 getManager().onFaceRequest(*command);
733
734 BOOST_REQUIRE(didCallbackFire());
735}
736
737template <typename T> class AuthorizedCommandFixture : public CommandFixture<T>
738{
739public:
740 AuthorizedCommandFixture()
741 {
742 const std::string regex = "^<localhost><nfd><faces>";
743 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
744 }
745
746 virtual
747 ~AuthorizedCommandFixture()
748 {
749
750 }
751};
752
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700753BOOST_FIXTURE_TEST_CASE(UnsupportedCommand, AuthorizedCommandFixture<FaceManagerFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700754{
755 ndn::nfd::FaceManagementOptions options;
756
757 Block encodedOptions(options.wireEncode());
758
759 Name commandName("/localhost/nfd/faces");
760 commandName.append("unsupported");
761 commandName.append(encodedOptions);
762
763 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700764 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700765
766 getFace()->onReceiveData +=
767 bind(&FaceManagerFixture::validateControlResponse, this, _1,
768 command->getName(), 501, "Unsupported command");
769
770 getManager().onFaceRequest(*command);
771
772 BOOST_REQUIRE(didCallbackFire());
773}
774
775class ValidatedFaceRequestFixture : public TestFaceTableFixture,
776 public TestFaceManagerCommon,
777 public FaceManager
778{
779public:
780
781 ValidatedFaceRequestFixture()
782 : FaceManager(TestFaceTableFixture::m_faceTable, TestFaceManagerCommon::m_face),
783 m_createFaceFired(false),
784 m_destroyFaceFired(false)
785 {
786
787 }
788
789 virtual void
790 createFace(const Name& requestName,
791 ndn::nfd::FaceManagementOptions& options)
792 {
793 m_createFaceFired = true;
794 }
795
796 virtual void
797 destroyFace(const Name& requestName,
798 ndn::nfd::FaceManagementOptions& options)
799 {
800 m_destroyFaceFired = true;
801 }
802
803 virtual
804 ~ValidatedFaceRequestFixture()
805 {
806
807 }
808
809 bool
810 didCreateFaceFire() const
811 {
812 return m_createFaceFired;
813 }
814
815 bool
816 didDestroyFaceFire() const
817 {
818 return m_destroyFaceFired;
819 }
820
821private:
822 bool m_createFaceFired;
823 bool m_destroyFaceFired;
824};
825
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700826BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestBadOptionParse,
827 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700828{
829 Name commandName("/localhost/nfd/faces");
830 commandName.append("create");
831 commandName.append("NotReallyOptions");
832
833 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700834 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700835
836 getFace()->onReceiveData +=
837 bind(&ValidatedFaceRequestFixture::validateControlResponse, this, _1,
838 command->getName(), 400, "Malformed command");
839
840 onValidatedFaceRequest(command);
841
842 BOOST_REQUIRE(didCallbackFire());
843}
844
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700845BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestCreateFace,
846 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700847{
848 ndn::nfd::FaceManagementOptions options;
849 options.setUri("tcp://127.0.0.1");
850
851 Block encodedOptions(options.wireEncode());
852
853 Name commandName("/localhost/nfd/faces");
854 commandName.append("create");
855 commandName.append(encodedOptions);
856
857 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700858 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700859
860 onValidatedFaceRequest(command);
861 BOOST_CHECK(didCreateFaceFire());
862}
863
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700864BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestDestroyFace,
865 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700866{
867 ndn::nfd::FaceManagementOptions options;
868 options.setUri("tcp://127.0.0.1");
869
870 Block encodedOptions(options.wireEncode());
871
872 Name commandName("/localhost/nfd/faces");
873 commandName.append("destroy");
874 commandName.append(encodedOptions);
875
876 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700877 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700878
879 onValidatedFaceRequest(command);
880 BOOST_CHECK(didDestroyFaceFire());
881}
882
883class FaceFixture : public TestFaceTableFixture,
884 public TestFaceManagerCommon,
885 public FaceManager
886{
887public:
888 FaceFixture()
889 : FaceManager(TestFaceTableFixture::m_faceTable, TestFaceManagerCommon::m_face)
890 {
891
892 }
893
894 virtual
895 ~FaceFixture()
896 {
897
898 }
899};
900
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700901BOOST_FIXTURE_TEST_CASE(CreateFaceBadUri, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700902{
903 ndn::nfd::FaceManagementOptions options;
904 options.setUri("tcp:/127.0.0.1");
905
906 Block encodedOptions(options.wireEncode());
907
908 Name commandName("/localhost/nfd/faces");
909 commandName.append("create");
910 commandName.append(encodedOptions);
911
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700912 shared_ptr<Interest> command(make_shared<Interest>(commandName));
913 generateCommand(*command);
914
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700915 getFace()->onReceiveData +=
916 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700917 command->getName(), 400, "Malformed command");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700918
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700919 createFace(command->getName(), options);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700920
921 BOOST_REQUIRE(didCallbackFire());
922}
923
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700924BOOST_FIXTURE_TEST_CASE(CreateFaceUnknownScheme, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700925{
926 ndn::nfd::FaceManagementOptions options;
927 // this will be an unsupported protocol because no factories have been
928 // added to the face manager
929 options.setUri("tcp://127.0.0.1");
930
931 Block encodedOptions(options.wireEncode());
932
933 Name commandName("/localhost/nfd/faces");
934 commandName.append("create");
935 commandName.append(encodedOptions);
936
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700937 shared_ptr<Interest> command(make_shared<Interest>(commandName));
938 generateCommand(*command);
939
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700940 getFace()->onReceiveData +=
941 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700942 command->getName(), 501, "Unsupported protocol");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700943
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700944 createFace(command->getName(), options);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700945
946 BOOST_REQUIRE(didCallbackFire());
947}
948
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700949BOOST_FIXTURE_TEST_CASE(OnCreated, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700950{
951 ndn::nfd::FaceManagementOptions options;
952 options.setUri("tcp://127.0.0.1");
953
954 Block encodedOptions(options.wireEncode());
955
956 Name commandName("/localhost/nfd/faces");
957 commandName.append("create");
958 commandName.append(encodedOptions);
959
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700960 shared_ptr<Interest> command(make_shared<Interest>(commandName));
961 generateCommand(*command);
962
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700963 ndn::nfd::FaceManagementOptions resultOptions;
964 resultOptions.setUri("tcp://127.0.0.1");
965 resultOptions.setFaceId(-1);
966
967 Block encodedResultOptions(resultOptions.wireEncode());
968
969 getFace()->onReceiveData +=
970 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700971 command->getName(), 200, "Success", encodedResultOptions);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700972
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700973 onCreated(command->getName(), options, make_shared<DummyFace>());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700974
975 BOOST_REQUIRE(didCallbackFire());
976 BOOST_CHECK(TestFaceTableFixture::m_faceTable.didAddFire());
977}
978
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700979BOOST_FIXTURE_TEST_CASE(OnConnectFailed, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700980{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700981 ndn::nfd::FaceManagementOptions options;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700982 options.setUri("tcp://127.0.0.1");
983
984 Block encodedOptions(options.wireEncode());
985
986 Name commandName("/localhost/nfd/faces");
987 commandName.append("create");
988 commandName.append(encodedOptions);
989
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700990 shared_ptr<Interest> command(make_shared<Interest>(commandName));
991 generateCommand(*command);
992
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700993 getFace()->onReceiveData +=
994 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700995 command->getName(), 400, "Failed to create face");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700996
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700997 onConnectFailed(command->getName(), "unit-test-reason");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700998
999 BOOST_REQUIRE(didCallbackFire());
1000 BOOST_CHECK_EQUAL(TestFaceTableFixture::m_faceTable.didAddFire(), false);
1001}
1002
1003
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001004BOOST_FIXTURE_TEST_CASE(DestroyFace, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001005{
1006 ndn::nfd::FaceManagementOptions options;
1007 options.setUri("tcp://127.0.0.1");
1008
1009 Block encodedOptions(options.wireEncode());
1010
1011 Name commandName("/localhost/nfd/faces");
1012 commandName.append("destroy");
1013 commandName.append(encodedOptions);
1014
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001015 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1016 generateCommand(*command);
1017
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001018 getFace()->onReceiveData +=
1019 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001020 command->getName(), 200, "Success");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001021
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001022 destroyFace(command->getName(), options);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001023
1024 BOOST_REQUIRE(didCallbackFire());
1025 BOOST_CHECK(TestFaceTableFixture::m_faceTable.didRemoveFire());
1026 BOOST_CHECK(TestFaceTableFixture::m_faceTable.getDummyFace()->didCloseFire());
1027}
1028
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001029class FaceListFixture : public FaceStatusPublisherFixture
1030{
1031public:
1032 FaceListFixture()
1033 : m_manager(m_table, m_face)
1034 {
1035
1036 }
1037
1038 virtual
1039 ~FaceListFixture()
1040 {
1041
1042 }
1043
1044protected:
1045 FaceManager m_manager;
1046};
1047
1048BOOST_FIXTURE_TEST_CASE(TestFaceList, FaceListFixture)
1049
1050{
1051 Name commandName("/localhost/nfd/faces/list");
1052 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1053
1054 // MAX_SEGMENT_SIZE == 4400, FaceStatus size with filler counters is 55
1055 // 55 divides 4400 (== 80), so only use 79 FaceStatuses and then two smaller ones
1056 // to force a FaceStatus to span Data packets
1057 for (int i = 0; i < 79; i++)
1058 {
1059 shared_ptr<TestCountersFace> dummy(make_shared<TestCountersFace>());
1060
1061 uint64_t filler = std::numeric_limits<uint64_t>::max() - 1;
1062 dummy->setCounters(filler, filler, filler, filler);
1063
1064 m_referenceFaces.push_front(dummy);
1065
1066 add(dummy);
1067 }
1068
1069 for (int i = 0; i < 2; i++)
1070 {
1071 shared_ptr<TestCountersFace> dummy(make_shared<TestCountersFace>());
1072 uint64_t filler = std::numeric_limits<uint32_t>::max() - 1;
1073 dummy->setCounters(filler, filler, filler, filler);
1074
1075 m_referenceFaces.push_front(dummy);
1076
1077 add(dummy);
1078 }
1079
1080 ndn::EncodingBuffer buffer;
1081
1082 m_face->onReceiveData +=
1083 bind(&FaceStatusPublisherFixture::decodeFaceStatusBlock, this, _1);
1084
1085 m_manager.listFaces(*command);
1086 BOOST_REQUIRE(m_finished);
1087}
1088
1089
1090
1091
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001092BOOST_AUTO_TEST_SUITE_END()
1093
1094} // namespace tests
1095} // namespace nfd
1096
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001097