blob: e4e795420d98e45c0f2075957380235555c2841f [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"
9#include "face/face.hpp"
10#include "../face/dummy-face.hpp"
11#include "fw/face-table.hpp"
12#include "fw/forwarder.hpp"
13
14#include "common.hpp"
15#include "tests/test-common.hpp"
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070016#include "validation-common.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070017
18namespace nfd {
19namespace tests {
20
21NFD_LOG_INIT("FaceManagerTest");
22
23class TestDummyFace : public DummyFace
24{
25public:
26
27 TestDummyFace()
28 : m_closeFired(false)
29 {
30
31 }
32
33 virtual
34 ~TestDummyFace()
35 {
36
37 }
38
39 virtual void
40 close()
41 {
42 m_closeFired = true;
43 }
44
45 bool
46 didCloseFire() const
47 {
48 return m_closeFired;
49 }
50
51private:
52 bool m_closeFired;
53};
54
55class TestFaceTable : public FaceTable
56{
57public:
58 TestFaceTable(Forwarder& forwarder)
59 : FaceTable(forwarder),
60 m_addFired(false),
61 m_removeFired(false),
62 m_getFired(false),
63 m_dummy(make_shared<TestDummyFace>())
64 {
65
66 }
67
68 virtual
69 ~TestFaceTable()
70 {
71
72 }
73
74 virtual void
75 add(shared_ptr<Face> face)
76 {
77 m_addFired = true;
78 }
79
80 virtual void
81 remove(shared_ptr<Face> face)
82 {
83 m_removeFired = true;
84 }
85
86 virtual shared_ptr<Face>
87 get(FaceId id) const
88 {
89 m_getFired = true;
90 return m_dummy;
91 }
92
93 bool
94 didAddFire() const
95 {
96 return m_addFired;
97 }
98
99 bool
100 didRemoveFire() const
101 {
102 return m_removeFired;
103 }
104
105 bool
106 didGetFire() const
107 {
108 return m_getFired;
109 }
110
111 void
112 reset()
113 {
114 m_addFired = false;
115 m_removeFired = false;
116 m_getFired = false;
117 }
118
119 shared_ptr<TestDummyFace>&
120 getDummyFace()
121 {
122 return m_dummy;
123 }
124
125private:
126 bool m_addFired;
127 bool m_removeFired;
128 mutable bool m_getFired;
129 shared_ptr<TestDummyFace> m_dummy;
130};
131
132
133class TestFaceTableFixture : public BaseFixture
134{
135public:
136 TestFaceTableFixture()
137 : m_faceTable(m_forwarder)
138 {
139
140 }
141
142 virtual
143 ~TestFaceTableFixture()
144 {
145
146 }
147
148protected:
149 Forwarder m_forwarder;
150 TestFaceTable m_faceTable;
151};
152
153class TestFaceManagerCommon
154{
155public:
156 TestFaceManagerCommon()
157 : m_face(make_shared<InternalFace>()),
158 m_callbackFired(false)
159 {
160
161 }
162
163 virtual
164 ~TestFaceManagerCommon()
165 {
166
167 }
168
169 shared_ptr<InternalFace>&
170 getFace()
171 {
172 return m_face;
173 }
174
175 void
176 validateControlResponseCommon(const Data& response,
177 const Name& expectedName,
178 uint32_t expectedCode,
179 const std::string& expectedText,
180 ControlResponse& control)
181 {
182 m_callbackFired = true;
183 Block controlRaw = response.getContent().blockFromValue();
184
185 control.wireDecode(controlRaw);
186
187 // NFD_LOG_DEBUG("received control response"
188 // << " Name: " << response.getName()
189 // << " code: " << control.getCode()
190 // << " text: " << control.getText());
191
192 BOOST_CHECK_EQUAL(response.getName(), expectedName);
193 BOOST_CHECK_EQUAL(control.getCode(), expectedCode);
194 BOOST_CHECK_EQUAL(control.getText(), expectedText);
195 }
196
197 void
198 validateControlResponse(const Data& response,
199 const Name& expectedName,
200 uint32_t expectedCode,
201 const std::string& expectedText)
202 {
203 ControlResponse control;
204 validateControlResponseCommon(response, expectedName,
205 expectedCode, expectedText, control);
206
207 if (!control.getBody().empty())
208 {
209 BOOST_FAIL("found unexpected control response body");
210 }
211 }
212
213 void
214 validateControlResponse(const Data& response,
215 const Name& expectedName,
216 uint32_t expectedCode,
217 const std::string& expectedText,
218 const Block& expectedBody)
219 {
220 ControlResponse control;
221 validateControlResponseCommon(response, expectedName,
222 expectedCode, expectedText, control);
223
224 BOOST_REQUIRE(!control.getBody().empty());
225 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
226
227 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
228 expectedBody.value_size()) == 0);
229
230 }
231
232 bool
233 didCallbackFire() const
234 {
235 return m_callbackFired;
236 }
237
238 void
239 resetCallbackFired()
240 {
241 m_callbackFired = false;
242 }
243
244protected:
245 shared_ptr<InternalFace> m_face;
246
247private:
248 bool m_callbackFired;
249};
250
251class FaceManagerFixture : public TestFaceTableFixture, public TestFaceManagerCommon
252{
253public:
254 FaceManagerFixture()
255 : m_manager(m_faceTable, m_face)
256 {
257 m_manager.setConfigFile(m_config);
258 }
259
260 void
261 parseConfig(const std::string configuration, bool isDryRun)
262 {
263 m_config.parse(configuration, isDryRun);
264 }
265
266 virtual
267 ~FaceManagerFixture()
268 {
269
270 }
271
272 FaceManager&
273 getManager()
274 {
275 return m_manager;
276 }
277
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700278 void
279 addInterestRule(const std::string& regex,
280 ndn::IdentityCertificate& certificate)
281 {
282 m_manager.addInterestRule(regex, certificate);
283 }
284
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700285 bool
286 didFaceTableAddFire() const
287 {
288 return m_faceTable.didAddFire();
289 }
290
291 bool
292 didFaceTableRemoveFire() const
293 {
294 return m_faceTable.didRemoveFire();
295 }
296
297 bool
298 didFaceTableGetFire() const
299 {
300 return m_faceTable.didGetFire();
301 }
302
303 void
304 resetFaceTable()
305 {
306 m_faceTable.reset();
307 }
308
309private:
310 FaceManager m_manager;
311 ConfigFile m_config;
312};
313
314BOOST_FIXTURE_TEST_SUITE(MgmtFaceManager, FaceManagerFixture)
315
316bool
317isExpectedException(const ConfigFile::Error& error, const std::string& expectedMessage)
318{
319 if (error.what() != expectedMessage)
320 {
321 NFD_LOG_ERROR("expected: " << expectedMessage << "\tgot: " << error.what());
322 }
323 return error.what() == expectedMessage;
324}
325
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600326#ifdef HAVE_UNIX_SOCKETS
327
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700328BOOST_AUTO_TEST_CASE(TestProcessSectionUnix)
329{
330 const std::string CONFIG =
331 "face_system\n"
332 "{\n"
333 " unix\n"
334 " {\n"
335 " listen yes\n"
336 " path /tmp/nfd.sock\n"
337 " }\n"
338 "}\n";
339 BOOST_TEST_CHECKPOINT("Calling parse");
340 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
341}
342
343BOOST_AUTO_TEST_CASE(TestProcessSectionUnixDryRun)
344{
345 const std::string CONFIG =
346 "face_system\n"
347 "{\n"
348 " unix\n"
349 " {\n"
350 " listen yes\n"
351 " path /var/run/nfd.sock\n"
352 " }\n"
353 "}\n";
354
355 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
356}
357
358BOOST_AUTO_TEST_CASE(TestProcessSectionUnixBadListen)
359{
360 const std::string CONFIG =
361 "face_system\n"
362 "{\n"
363 " unix\n"
364 " {\n"
365 " listen hello\n"
366 " }\n"
367 "}\n";
368 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
369 bind(&isExpectedException, _1,
370 "Invalid value for option \"listen\" in \"unix\" section"));
371}
372
373BOOST_AUTO_TEST_CASE(TestProcessSectionUnixUnknownOption)
374{
375 const std::string CONFIG =
376 "face_system\n"
377 "{\n"
378 " unix\n"
379 " {\n"
380 " hello\n"
381 " }\n"
382 "}\n";
383 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
384 bind(&isExpectedException, _1,
385 "Unrecognized option \"hello\" in \"unix\" section"));
386}
387
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600388#endif // HAVE_UNIX_SOCKETS
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700389
390
391BOOST_AUTO_TEST_CASE(TestProcessSectionTcp)
392{
393 const std::string CONFIG =
394 "face_system\n"
395 "{\n"
396 " tcp\n"
397 " {\n"
398 " listen yes\n"
399 " port 6363\n"
400 " }\n"
401 "}\n";
402 try
403 {
404 parseConfig(CONFIG, false);
405 }
406 catch (const std::runtime_error& e)
407 {
408 const std::string reason = e.what();
409 if (reason.find("Address in use") != std::string::npos)
410 {
411 BOOST_FAIL(reason);
412 }
413 }
414}
415
416BOOST_AUTO_TEST_CASE(TestProcessSectionTcpDryRun)
417{
418 const std::string CONFIG =
419 "face_system\n"
420 "{\n"
421 " tcp\n"
422 " {\n"
423 " listen yes\n"
424 " port 6363\n"
425 " }\n"
426 "}\n";
427 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
428}
429
430BOOST_AUTO_TEST_CASE(TestProcessSectionTcpBadListen)
431{
432 const std::string CONFIG =
433 "face_system\n"
434 "{\n"
435 " tcp\n"
436 " {\n"
437 " listen hello\n"
438 " }\n"
439 "}\n";
440 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
441 bind(&isExpectedException, _1,
442 "Invalid value for option \"listen\" in \"tcp\" section"));
443}
444
445BOOST_AUTO_TEST_CASE(TestProcessSectionTcpUnknownOption)
446{
447 const std::string CONFIG =
448 "face_system\n"
449 "{\n"
450 " tcp\n"
451 " {\n"
452 " hello\n"
453 " }\n"
454 "}\n";
455 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
456 bind(&isExpectedException, _1,
457 "Unrecognized option \"hello\" in \"tcp\" section"));
458}
459
460BOOST_AUTO_TEST_CASE(TestProcessSectionUdp)
461{
462 const std::string CONFIG =
463 "face_system\n"
464 "{\n"
465 " udp\n"
466 " {\n"
467 " port 6363\n"
468 " idle_timeout 30\n"
469 " keep_alive_interval 25\n"
470 " mcast yes\n"
471 " mcast_port 56363\n"
472 " mcast_group 224.0.23.170\n"
473 " }\n"
474 "}\n";
475 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
476}
477
478BOOST_AUTO_TEST_CASE(TestProcessSectionUdpDryRun)
479{
480 const std::string CONFIG =
481 "face_system\n"
482 "{\n"
483 " udp\n"
484 " {\n"
485 " port 6363\n"
486 " idle_timeout 30\n"
487 " keep_alive_interval 25\n"
488 " mcast yes\n"
489 " mcast_port 56363\n"
490 " mcast_group 224.0.23.170\n"
491 " }\n"
492 "}\n";
493 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
494}
495
496BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadIdleTimeout)
497{
498 const std::string CONFIG =
499 "face_system\n"
500 "{\n"
501 " udp\n"
502 " {\n"
503 " idle_timeout hello\n"
504 " }\n"
505 "}\n";
506
507 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
508 bind(&isExpectedException, _1,
509 "Invalid value for option \"idle_timeout\" in \"udp\" section"));
510}
511
512BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcast)
513{
514 const std::string CONFIG =
515 "face_system\n"
516 "{\n"
517 " udp\n"
518 " {\n"
519 " mcast hello\n"
520 " }\n"
521 "}\n";
522
523 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
524 bind(&isExpectedException, _1,
525 "Invalid value for option \"mcast\" in \"udp\" section"));
526}
527
528BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcastGroup)
529{
530 const std::string CONFIG =
531 "face_system\n"
532 "{\n"
533 " udp\n"
534 " {\n"
535 " mcast no\n"
536 " mcast_port 50\n"
537 " mcast_group hello\n"
538 " }\n"
539 "}\n";
540
541 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
542 bind(&isExpectedException, _1,
543 "Invalid value for option \"mcast_group\" in \"udp\" section"));
544}
545
546BOOST_AUTO_TEST_CASE(TestProcessSectionUdpBadMcastGroupV6)
547{
548 const std::string CONFIG =
549 "face_system\n"
550 "{\n"
551 " udp\n"
552 " {\n"
553 " mcast no\n"
554 " mcast_port 50\n"
555 " mcast_group ::1\n"
556 " }\n"
557 "}\n";
558
559 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
560 bind(&isExpectedException, _1,
561 "Invalid value for option \"mcast_group\" in \"udp\" section"));
562}
563
564BOOST_AUTO_TEST_CASE(TestProcessSectionUdpUnknownOption)
565{
566 const std::string CONFIG =
567 "face_system\n"
568 "{\n"
569 " udp\n"
570 " {\n"
571 " hello\n"
572 " }\n"
573 "}\n";
574 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
575 bind(&isExpectedException, _1,
576 "Unrecognized option \"hello\" in \"udp\" section"));
577}
578
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600579#ifdef HAVE_PCAP
580
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700581BOOST_AUTO_TEST_CASE(TestProcessSectionEther)
582{
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600583
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700584 const std::string CONFIG =
585 "face_system\n"
586 "{\n"
587 " ether\n"
588 " {\n"
589 " mcast yes\n"
590 " mcast_group 01:00:5E:00:17:AA\n"
591 " }\n"
592 "}\n";
593
594 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, false));
595}
596
597BOOST_AUTO_TEST_CASE(TestProcessSectionEtherDryRun)
598{
599 const std::string CONFIG =
600 "face_system\n"
601 "{\n"
602 " ether\n"
603 " {\n"
604 " mcast yes\n"
605 " mcast_group 01:00:5E:00:17:AA\n"
606 " }\n"
607 "}\n";
608
609 BOOST_CHECK_NO_THROW(parseConfig(CONFIG, true));
610}
611
612BOOST_AUTO_TEST_CASE(TestProcessSectionEtherBadMcast)
613{
614 const std::string CONFIG =
615 "face_system\n"
616 "{\n"
617 " ether\n"
618 " {\n"
619 " mcast hello\n"
620 " }\n"
621 "}\n";
622
623 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
624 bind(&isExpectedException, _1,
625 "Invalid value for option \"mcast\" in \"ether\" section"));
626}
627
628BOOST_AUTO_TEST_CASE(TestProcessSectionEtherBadMcastGroup)
629{
630 const std::string CONFIG =
631 "face_system\n"
632 "{\n"
633 " ether\n"
634 " {\n"
635 " mcast yes\n"
636 " mcast_group\n"
637 " }\n"
638 "}\n";
639
640 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
641 bind(&isExpectedException, _1,
642 "Invalid value for option \"mcast_group\" in \"ether\" section"));
643}
644
645BOOST_AUTO_TEST_CASE(TestProcessSectionEtherUnknownOption)
646{
647 const std::string CONFIG =
648 "face_system\n"
649 "{\n"
650 " ether\n"
651 " {\n"
652 " hello\n"
653 " }\n"
654 "}\n";
655 BOOST_CHECK_EXCEPTION(parseConfig(CONFIG, false), ConfigFile::Error,
656 bind(&isExpectedException, _1,
657 "Unrecognized option \"hello\" in \"ether\" section"));
658}
659
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600660#endif
661
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700662BOOST_AUTO_TEST_CASE(TestFireInterestFilter)
663{
664 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/faces"));
665
666 getFace()->onReceiveData +=
667 bind(&FaceManagerFixture::validateControlResponse, this, _1,
668 command->getName(), 400, "Malformed command");
669
670 getFace()->sendInterest(*command);
671
672 BOOST_REQUIRE(didCallbackFire());
673}
674
675BOOST_AUTO_TEST_CASE(MalformedCommmand)
676{
677 shared_ptr<Interest> command(make_shared<Interest>("/localhost/nfd/faces"));
678
679 getFace()->onReceiveData +=
680 bind(&FaceManagerFixture::validateControlResponse, this, _1,
681 command->getName(), 400, "Malformed command");
682
683 getManager().onFaceRequest(*command);
684
685 BOOST_REQUIRE(didCallbackFire());
686}
687
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700688BOOST_AUTO_TEST_CASE(UnsignedCommand)
689{
690 ndn::nfd::FaceManagementOptions options;
691 options.setUri("tcp://127.0.0.1");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700692
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700693 Block encodedOptions(options.wireEncode());
694
695 Name commandName("/localhost/nfd/faces");
696 commandName.append("create");
697 commandName.append(encodedOptions);
698
699 shared_ptr<Interest> command(make_shared<Interest>(commandName));
700
701 getFace()->onReceiveData +=
702 bind(&FaceManagerFixture::validateControlResponse, this, _1,
703 command->getName(), 401, "Signature required");
704
705 getManager().onFaceRequest(*command);
706
707 BOOST_REQUIRE(didCallbackFire());
708}
709
710BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand, UnauthorizedCommandFixture<FaceManagerFixture>)
711{
712 ndn::nfd::FaceManagementOptions options;
713 options.setUri("tcp://127.0.0.1");
714
715 Block encodedOptions(options.wireEncode());
716
717 Name commandName("/localhost/nfd/faces");
718 commandName.append("create");
719 commandName.append(encodedOptions);
720
721 shared_ptr<Interest> command(make_shared<Interest>(commandName));
722 generateCommand(*command);
723
724 getFace()->onReceiveData +=
725 bind(&FaceManagerFixture::validateControlResponse, this, _1,
726 command->getName(), 403, "Unauthorized command");
727
728 getManager().onFaceRequest(*command);
729
730 BOOST_REQUIRE(didCallbackFire());
731}
732
733template <typename T> class AuthorizedCommandFixture : public CommandFixture<T>
734{
735public:
736 AuthorizedCommandFixture()
737 {
738 const std::string regex = "^<localhost><nfd><faces>";
739 T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
740 }
741
742 virtual
743 ~AuthorizedCommandFixture()
744 {
745
746 }
747};
748
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700749BOOST_FIXTURE_TEST_CASE(UnsupportedCommand, AuthorizedCommandFixture<FaceManagerFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700750{
751 ndn::nfd::FaceManagementOptions options;
752
753 Block encodedOptions(options.wireEncode());
754
755 Name commandName("/localhost/nfd/faces");
756 commandName.append("unsupported");
757 commandName.append(encodedOptions);
758
759 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700760 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700761
762 getFace()->onReceiveData +=
763 bind(&FaceManagerFixture::validateControlResponse, this, _1,
764 command->getName(), 501, "Unsupported command");
765
766 getManager().onFaceRequest(*command);
767
768 BOOST_REQUIRE(didCallbackFire());
769}
770
771class ValidatedFaceRequestFixture : public TestFaceTableFixture,
772 public TestFaceManagerCommon,
773 public FaceManager
774{
775public:
776
777 ValidatedFaceRequestFixture()
778 : FaceManager(TestFaceTableFixture::m_faceTable, TestFaceManagerCommon::m_face),
779 m_createFaceFired(false),
780 m_destroyFaceFired(false)
781 {
782
783 }
784
785 virtual void
786 createFace(const Name& requestName,
787 ndn::nfd::FaceManagementOptions& options)
788 {
789 m_createFaceFired = true;
790 }
791
792 virtual void
793 destroyFace(const Name& requestName,
794 ndn::nfd::FaceManagementOptions& options)
795 {
796 m_destroyFaceFired = true;
797 }
798
799 virtual
800 ~ValidatedFaceRequestFixture()
801 {
802
803 }
804
805 bool
806 didCreateFaceFire() const
807 {
808 return m_createFaceFired;
809 }
810
811 bool
812 didDestroyFaceFire() const
813 {
814 return m_destroyFaceFired;
815 }
816
817private:
818 bool m_createFaceFired;
819 bool m_destroyFaceFired;
820};
821
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700822BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestBadOptionParse,
823 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700824{
825 Name commandName("/localhost/nfd/faces");
826 commandName.append("create");
827 commandName.append("NotReallyOptions");
828
829 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700830 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700831
832 getFace()->onReceiveData +=
833 bind(&ValidatedFaceRequestFixture::validateControlResponse, this, _1,
834 command->getName(), 400, "Malformed command");
835
836 onValidatedFaceRequest(command);
837
838 BOOST_REQUIRE(didCallbackFire());
839}
840
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700841BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestCreateFace,
842 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700843{
844 ndn::nfd::FaceManagementOptions options;
845 options.setUri("tcp://127.0.0.1");
846
847 Block encodedOptions(options.wireEncode());
848
849 Name commandName("/localhost/nfd/faces");
850 commandName.append("create");
851 commandName.append(encodedOptions);
852
853 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700854 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700855
856 onValidatedFaceRequest(command);
857 BOOST_CHECK(didCreateFaceFire());
858}
859
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700860BOOST_FIXTURE_TEST_CASE(ValidatedFaceRequestDestroyFace,
861 AuthorizedCommandFixture<ValidatedFaceRequestFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700862{
863 ndn::nfd::FaceManagementOptions options;
864 options.setUri("tcp://127.0.0.1");
865
866 Block encodedOptions(options.wireEncode());
867
868 Name commandName("/localhost/nfd/faces");
869 commandName.append("destroy");
870 commandName.append(encodedOptions);
871
872 shared_ptr<Interest> command(make_shared<Interest>(commandName));
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700873 generateCommand(*command);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700874
875 onValidatedFaceRequest(command);
876 BOOST_CHECK(didDestroyFaceFire());
877}
878
879class FaceFixture : public TestFaceTableFixture,
880 public TestFaceManagerCommon,
881 public FaceManager
882{
883public:
884 FaceFixture()
885 : FaceManager(TestFaceTableFixture::m_faceTable, TestFaceManagerCommon::m_face)
886 {
887
888 }
889
890 virtual
891 ~FaceFixture()
892 {
893
894 }
895};
896
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700897BOOST_FIXTURE_TEST_CASE(CreateFaceBadUri, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700898{
899 ndn::nfd::FaceManagementOptions options;
900 options.setUri("tcp:/127.0.0.1");
901
902 Block encodedOptions(options.wireEncode());
903
904 Name commandName("/localhost/nfd/faces");
905 commandName.append("create");
906 commandName.append(encodedOptions);
907
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700908 shared_ptr<Interest> command(make_shared<Interest>(commandName));
909 generateCommand(*command);
910
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700911 getFace()->onReceiveData +=
912 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700913 command->getName(), 400, "Malformed command");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700914
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700915 createFace(command->getName(), options);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700916
917 BOOST_REQUIRE(didCallbackFire());
918}
919
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700920BOOST_FIXTURE_TEST_CASE(CreateFaceUnknownScheme, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700921{
922 ndn::nfd::FaceManagementOptions options;
923 // this will be an unsupported protocol because no factories have been
924 // added to the face manager
925 options.setUri("tcp://127.0.0.1");
926
927 Block encodedOptions(options.wireEncode());
928
929 Name commandName("/localhost/nfd/faces");
930 commandName.append("create");
931 commandName.append(encodedOptions);
932
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700933 shared_ptr<Interest> command(make_shared<Interest>(commandName));
934 generateCommand(*command);
935
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700936 getFace()->onReceiveData +=
937 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700938 command->getName(), 501, "Unsupported protocol");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700939
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700940 createFace(command->getName(), options);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700941
942 BOOST_REQUIRE(didCallbackFire());
943}
944
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700945BOOST_FIXTURE_TEST_CASE(OnCreated, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700946{
947 ndn::nfd::FaceManagementOptions options;
948 options.setUri("tcp://127.0.0.1");
949
950 Block encodedOptions(options.wireEncode());
951
952 Name commandName("/localhost/nfd/faces");
953 commandName.append("create");
954 commandName.append(encodedOptions);
955
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700956 shared_ptr<Interest> command(make_shared<Interest>(commandName));
957 generateCommand(*command);
958
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700959 ndn::nfd::FaceManagementOptions resultOptions;
960 resultOptions.setUri("tcp://127.0.0.1");
961 resultOptions.setFaceId(-1);
962
963 Block encodedResultOptions(resultOptions.wireEncode());
964
965 getFace()->onReceiveData +=
966 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700967 command->getName(), 200, "Success", encodedResultOptions);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700968
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700969 onCreated(command->getName(), options, make_shared<DummyFace>());
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700970
971 BOOST_REQUIRE(didCallbackFire());
972 BOOST_CHECK(TestFaceTableFixture::m_faceTable.didAddFire());
973}
974
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700975BOOST_FIXTURE_TEST_CASE(OnConnectFailed, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700976{
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700977 ndn::nfd::FaceManagementOptions options;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700978 options.setUri("tcp://127.0.0.1");
979
980 Block encodedOptions(options.wireEncode());
981
982 Name commandName("/localhost/nfd/faces");
983 commandName.append("create");
984 commandName.append(encodedOptions);
985
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700986 shared_ptr<Interest> command(make_shared<Interest>(commandName));
987 generateCommand(*command);
988
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700989 getFace()->onReceiveData +=
990 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700991 command->getName(), 400, "Failed to create face");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700992
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -0700993 onConnectFailed(command->getName(), "unit-test-reason");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700994
995 BOOST_REQUIRE(didCallbackFire());
996 BOOST_CHECK_EQUAL(TestFaceTableFixture::m_faceTable.didAddFire(), false);
997}
998
999
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001000BOOST_FIXTURE_TEST_CASE(DestroyFace, AuthorizedCommandFixture<FaceFixture>)
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001001{
1002 ndn::nfd::FaceManagementOptions options;
1003 options.setUri("tcp://127.0.0.1");
1004
1005 Block encodedOptions(options.wireEncode());
1006
1007 Name commandName("/localhost/nfd/faces");
1008 commandName.append("destroy");
1009 commandName.append(encodedOptions);
1010
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001011 shared_ptr<Interest> command(make_shared<Interest>(commandName));
1012 generateCommand(*command);
1013
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001014 getFace()->onReceiveData +=
1015 bind(&FaceFixture::validateControlResponse, this, _1,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001016 command->getName(), 200, "Success");
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001017
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -07001018 destroyFace(command->getName(), options);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -07001019
1020 BOOST_REQUIRE(didCallbackFire());
1021 BOOST_CHECK(TestFaceTableFixture::m_faceTable.didRemoveFire());
1022 BOOST_CHECK(TestFaceTableFixture::m_faceTable.getDummyFace()->didCloseFire());
1023}
1024
1025BOOST_AUTO_TEST_SUITE_END()
1026
1027} // namespace tests
1028} // namespace nfd
1029