mgmt, main: add support for authorized commands

refs: #1227

Change-Id: I907d1fa6e78775470c5376fcdfe898be4c311001
diff --git a/tests/mgmt/local-control-header-manager.cpp b/tests/mgmt/local-control-header-manager.cpp
index 43a649d..168f43f 100644
--- a/tests/mgmt/local-control-header-manager.cpp
+++ b/tests/mgmt/local-control-header-manager.cpp
@@ -9,6 +9,7 @@
 #include "tests/face/dummy-face.hpp"
 
 #include "tests/test-common.hpp"
+#include "validation-common.hpp"
 
 namespace nfd {
 namespace tests {
@@ -19,12 +20,6 @@
 {
 public:
 
-  LocalControlHeaderManagerFixture()
-    : m_callbackFired(false)
-  {
-
-  }
-
   shared_ptr<Face>
   getFace(FaceId id)
   {
@@ -42,6 +37,25 @@
     m_faces.push_back(face);
   }
 
+  shared_ptr<InternalFace>
+  getInternalFace()
+  {
+    return m_face;
+  }
+
+  LocalControlHeaderManager&
+  getManager()
+  {
+    return m_manager;
+  }
+
+  void
+  addInterestRule(const std::string& regex,
+                  ndn::IdentityCertificate& certificate)
+  {
+    m_manager.addInterestRule(regex, certificate);
+  }
+
   void
   validateControlResponse(const Data& response,
                           const Name& expectedName,
@@ -82,49 +96,76 @@
     m_callbackFired = false;
   }
 
+protected:
+  LocalControlHeaderManagerFixture()
+    : m_face(make_shared<InternalFace>()),
+      m_manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
+                m_face),
+      m_callbackFired(false)
+  {
+  }
+
 private:
+  shared_ptr<InternalFace> m_face;
+  LocalControlHeaderManager m_manager;
   std::vector<shared_ptr<Face> > m_faces;
   bool m_callbackFired;
 };
 
-BOOST_FIXTURE_TEST_SUITE(MgmtLocalControlHeaderManager, LocalControlHeaderManagerFixture)
+template <typename T> class AuthorizedCommandFixture:
+    public CommandFixture<T>
+{
+public:
+  AuthorizedCommandFixture()
+  {
+    const std::string regex = "^<localhost><nfd><control-header>";
+    T::addInterestRule(regex, *CommandFixture<T>::m_certificate);
+  }
+
+  virtual
+  ~AuthorizedCommandFixture()
+  {
+  }
+};
+
+BOOST_FIXTURE_TEST_SUITE(MgmtLocalControlHeaderManager,
+                         AuthorizedCommandFixture<LocalControlHeaderManagerFixture>)
 
 BOOST_AUTO_TEST_CASE(InFaceId)
 {
   shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
   addFace(dummy);
 
-  shared_ptr<InternalFace> face(make_shared<InternalFace>());
-
-  LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
-                                        face);
-
   Name enable("/localhost/nfd/control-header/in-faceid/enable");
+  shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
+  enableCommand->setIncomingFaceId(1);
 
-  face->onReceiveData +=
+  generateCommand(*enableCommand);
+
+  getInternalFace()->onReceiveData +=
     bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
-         enable, 200, "Success");
+         enableCommand->getName(), 200, "Success");
 
-  Interest enableCommand(enable);
-  enableCommand.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(enableCommand);
+  getManager().onLocalControlHeaderRequest(*enableCommand);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_REQUIRE(dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
   BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
 
-  face->onReceiveData.clear();
+  getInternalFace()->onReceiveData.clear();
   resetCallbackFired();
 
   Name disable("/localhost/nfd/control-header/in-faceid/disable");
+  shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
+  disableCommand->setIncomingFaceId(1);
 
-  face->onReceiveData +=
+  generateCommand(*disableCommand);
+
+  getInternalFace()->onReceiveData +=
     bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
-         disable, 200, "Success");
+         disableCommand->getName(), 200, "Success");
 
-  Interest disableCommand(disable);
-  disableCommand.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(disableCommand);
+  getManager().onLocalControlHeaderRequest(*disableCommand);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
@@ -136,38 +177,36 @@
   shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
   addFace(dummy);
 
-  shared_ptr<InternalFace> face(make_shared<InternalFace>());
-
-  LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
-                                        face);
-
   Name enable("/localhost/nfd/control-header/nexthop-faceid/enable");
 
-  face->onReceiveData +=
-    bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
-         enable, 200, "Success");
+  shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
+  enableCommand->setIncomingFaceId(1);
+  generateCommand(*enableCommand);
 
-  Interest enableCommand(enable);
-  enableCommand.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(enableCommand);
+  getInternalFace()->onReceiveData +=
+    bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
+         enableCommand->getName(), 200, "Success");
+
+  getManager().onLocalControlHeaderRequest(*enableCommand);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_REQUIRE(dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
   BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
 
-
-  face->onReceiveData.clear();
+  getInternalFace()->onReceiveData.clear();
   resetCallbackFired();
 
   Name disable("/localhost/nfd/control-header/nexthop-faceid/disable");
+  shared_ptr<Interest> disableCommand(make_shared<Interest>(disable));
+  disableCommand->setIncomingFaceId(1);
 
-  face->onReceiveData +=
+  generateCommand(*disableCommand);
+
+  getInternalFace()->onReceiveData +=
     bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
-         disable, 200, "Success");
+         disableCommand->getName(), 200, "Success");
 
-  Interest disableCommand(disable);
-  disableCommand.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(disableCommand);
+  getManager().onLocalControlHeaderRequest(*disableCommand);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
@@ -179,20 +218,15 @@
   shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
   addFace(dummy);
 
-  shared_ptr<InternalFace> face(make_shared<InternalFace>());
-
-  LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
-                                        face);
-
   Name commandName("/localhost/nfd/control-header");
+  Interest command(commandName);
+  command.setIncomingFaceId(1);
 
-  face->onReceiveData +=
+  getInternalFace()->onReceiveData +=
     bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
          commandName, 400, "Malformed command");
 
-  Interest command(commandName);
-  command.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(command);
+  getManager().onLocalControlHeaderRequest(command);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
@@ -204,20 +238,15 @@
   shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
   addFace(dummy);
 
-  shared_ptr<InternalFace> face(make_shared<InternalFace>());
-
-  LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
-                                        face);
-
   Name commandName("/localhost/nfd/control-header/in-faceid");
 
-  face->onReceiveData +=
+  getInternalFace()->onReceiveData +=
     bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
          commandName, 400, "Malformed command");
 
   Interest command(commandName);
   command.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(command);
+  getManager().onLocalControlHeaderRequest(command);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
@@ -229,20 +258,17 @@
   shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
   addFace(dummy);
 
-  shared_ptr<InternalFace> face(make_shared<InternalFace>());
-
-  LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
-                                        face);
-
   Name commandName("/localhost/nfd/control-header/madeup/moremadeup");
+  shared_ptr<Interest> command(make_shared<Interest>(commandName));
+  command->setIncomingFaceId(1);
 
-  face->onReceiveData +=
+  generateCommand(*command);
+
+  getInternalFace()->onReceiveData +=
     bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
-         commandName, 501, "Unsupported");
+         command->getName(), 501, "Unsupported");
 
-  Interest command(commandName);
-  command.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(command);
+  getManager().onLocalControlHeaderRequest(*command);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
@@ -254,20 +280,18 @@
   shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
   addFace(dummy);
 
-  shared_ptr<InternalFace> face(make_shared<InternalFace>());
-
-  LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
-                                        face);
-
   Name commandName("/localhost/nfd/control-header/in-faceid/madeup");
+  shared_ptr<Interest> command(make_shared<Interest>(commandName));
+  command->setIncomingFaceId(1);
 
-  face->onReceiveData +=
+
+  generateCommand(*command);
+
+  getInternalFace()->onReceiveData +=
     bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
-         commandName, 501, "Unsupported");
+         command->getName(), 501, "Unsupported");
 
-  Interest command(commandName);
-  command.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(command);
+  getManager().onLocalControlHeaderRequest(*command);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
@@ -279,26 +303,45 @@
   shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
   addFace(dummy);
 
-  shared_ptr<InternalFace> face(make_shared<InternalFace>());
-
-  LocalControlHeaderManager manager(bind(&LocalControlHeaderManagerFixture::getFace, this, _1),
-                                        face);
-
   Name commandName("/localhost/nfd/control-header/nexthop-faceid/madeup");
+  shared_ptr<Interest> command(make_shared<Interest>(commandName));
+  command->setIncomingFaceId(1);
 
-  face->onReceiveData +=
+  generateCommand(*command);
+
+  getInternalFace()->onReceiveData +=
     bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
-         commandName, 501, "Unsupported");
+         command->getName(), 501, "Unsupported");
 
-  Interest command(commandName);
-  command.setIncomingFaceId(1);
-  manager.onLocalControlHeaderRequest(command);
+  getManager().onLocalControlHeaderRequest(*command);
 
   BOOST_REQUIRE(didCallbackFire());
   BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
   BOOST_CHECK(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_NEXTHOP_FACEID));
 }
 
+BOOST_FIXTURE_TEST_CASE(UnauthorizedCommand,
+                        UnauthorizedCommandFixture<LocalControlHeaderManagerFixture>)
+{
+  shared_ptr<LocalFace> dummy = make_shared<DummyLocalFace>();
+  addFace(dummy);
+
+  Name enable("/localhost/nfd/control-header/in-faceid/enable");
+  shared_ptr<Interest> enableCommand(make_shared<Interest>(enable));
+  enableCommand->setIncomingFaceId(1);
+
+  generateCommand(*enableCommand);
+
+  getInternalFace()->onReceiveData +=
+    bind(&LocalControlHeaderManagerFixture::validateControlResponse, this, _1,
+         enableCommand->getName(), 403, "Unauthorized command");
+
+  getManager().onLocalControlHeaderRequest(*enableCommand);
+
+  BOOST_REQUIRE(didCallbackFire());
+  BOOST_REQUIRE(!dummy->isLocalControlHeaderEnabled(LOCAL_CONTROL_HEADER_FEATURE_IN_FACEID));
+}
+
 BOOST_AUTO_TEST_SUITE_END()
 
 } // namespace tests