tools: nfdc face create command

refs #3864

Change-Id: Icca589eceae0b78f68cda61e761dd4721ce54f9c
diff --git a/tests/tools/nfdc/face-module.t.cpp b/tests/tools/nfdc/face-module.t.cpp
index 0c9d074..20d5514 100644
--- a/tests/tools/nfdc/face-module.t.cpp
+++ b/tests/tools/nfdc/face-module.t.cpp
@@ -24,6 +24,7 @@
  */
 
 #include "nfdc/face-module.hpp"
+#include <ndn-cxx/mgmt/nfd/face-query-filter.hpp>
 
 #include "execute-command-fixture.hpp"
 #include "status-fixture.hpp"
@@ -33,13 +34,13 @@
 namespace nfdc {
 namespace tests {
 
+using ndn::nfd::FaceQueryFilter;
+
 BOOST_AUTO_TEST_SUITE(Nfdc)
 BOOST_AUTO_TEST_SUITE(TestFaceModule)
 
 BOOST_FIXTURE_TEST_SUITE(ShowCommand, ExecuteCommandFixture)
 
-using ndn::nfd::FaceQueryFilter;
-
 const std::string NORMAL_OUTPUT = std::string(R"TEXT(
   faceid=256
   remote=udp4://84.67.35.111:6363
@@ -105,6 +106,42 @@
 
 BOOST_AUTO_TEST_SUITE_END() // ShowCommand
 
+BOOST_FIXTURE_TEST_SUITE(CreateCommand, ExecuteCommandFixture)
+
+BOOST_AUTO_TEST_CASE(Normal)
+{
+  this->processInterest = [this] (const Interest& interest) {
+    ControlParameters req = MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS("/localhost/nfd/faces/create");
+    BOOST_REQUIRE(req.hasUri());
+    BOOST_CHECK_EQUAL(req.getUri(), "udp4://159.242.33.78:6363");
+    BOOST_REQUIRE(req.hasFacePersistency());
+    BOOST_CHECK_EQUAL(req.getFacePersistency(), FacePersistency::FACE_PERSISTENCY_PERSISTENT);
+
+    ControlParameters resp;
+    resp.setFaceId(2130)
+        .setUri("udp4://159.242.33.78:6363")
+        .setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
+    this->succeedCommand(resp);
+  };
+
+  this->execute("face create udp://159.242.33.78");
+  BOOST_CHECK_EQUAL(exitCode, 0);
+  BOOST_CHECK(out.is_equal("face-created id=2130 remote=udp4://159.242.33.78:6363 persistency=persistent\n"));
+  BOOST_CHECK(err.is_empty());
+}
+
+BOOST_AUTO_TEST_CASE(Error)
+{
+  this->processInterest = nullptr; // no response
+
+  this->execute("face create udp://159.242.33.78");
+  BOOST_CHECK_EQUAL(exitCode, 1);
+  BOOST_CHECK(out.is_empty());
+  BOOST_CHECK(err.is_equal("Error 10060 when creating face: request timed out\n"));
+}
+
+BOOST_AUTO_TEST_SUITE_END() // CreateCommand
+
 const std::string STATUS_XML = stripXmlSpaces(R"XML(
   <faces>
     <face>
diff --git a/tests/tools/nfdc/mock-nfd-mgmt-fixture.hpp b/tests/tools/nfdc/mock-nfd-mgmt-fixture.hpp
index a1dd694..4d55849 100644
--- a/tests/tools/nfdc/mock-nfd-mgmt-fixture.hpp
+++ b/tests/tools/nfdc/mock-nfd-mgmt-fixture.hpp
@@ -37,6 +37,7 @@
 namespace tests {
 
 using namespace nfd::tests;
+using ndn::nfd::ControlParameters;
 
 /** \brief fixture to emulate NFD management
  */
@@ -56,7 +57,43 @@
     });
   }
 
-protected: // status fetching
+protected: // ControlCommand
+  /** \brief check the last Interest is a command with specified prefix
+   *  \retval nullopt last Interest is not the expected command
+   *  \return command parameters
+   */
+  ndn::optional<ControlParameters>
+  getCommand(const Name& expectedPrefix)
+  {
+    if (face.sentInterests.empty() ||
+        !expectedPrefix.isPrefixOf(face.sentInterests.back().getName())) {
+      return ndn::nullopt;
+    }
+    return ControlParameters(face.sentInterests.back().getName()
+                             .at(expectedPrefix.size()).blockFromValue());
+  }
+
+  /** \brief respond to the last command
+   *  \pre last Interest is a command
+   */
+  void
+  succeedCommand(const ControlParameters& parameters)
+  {
+    ndn::nfd::ControlResponse resp(200, "OK");
+    resp.setBody(parameters.wireEncode());
+    this->sendCommandReply(resp);
+  }
+
+  /** \brief respond to the last command
+   *  \pre last Interest is a command
+   */
+  void
+  failCommand(uint32_t code, const std::string& text)
+  {
+    this->sendCommandReply({code, text});
+  }
+
+protected: // StatusDataset
   /** \brief send an empty dataset in reply to StatusDataset request
    *  \param prefix dataset prefix without version and segment
    *  \pre Interest for dataset has been expressed, sendDataset has not been invoked
@@ -114,6 +151,14 @@
     this->advanceClocks(time::milliseconds(100), timeout);
   }
 
+  void
+  sendCommandReply(const ndn::nfd::ControlResponse& resp)
+  {
+    auto data = makeData(face.sentInterests.back().getName());
+    data->setContent(resp.wireEncode());
+    face.receive(*data);
+  }
+
   /** \brief send a payload in reply to StatusDataset request
    *  \param name dataset prefix without version and segment
    *  \param contentArgs passed to Data::setContent
@@ -158,4 +203,13 @@
 } // namespace tools
 } // namespace nfd
 
+#define MOCK_NFD_MGMT_REQUIRE_LAST_COMMAND_IS(expectedPrefix) \
+  [this] { \
+    BOOST_REQUIRE_MESSAGE(!face.sentInterests.empty(), "no Interest expressed"); \
+    auto params = this->getCommand(expectedPrefix); \
+    BOOST_REQUIRE_MESSAGE(params, "last Interest " << face.sentInterests.back().getName() << \
+                          " does not match command prefix " << expectedPrefix); \
+    return *params; \
+  } ()
+
 #endif // NFD_TESTS_TOOLS_NFDC_MOCK_NFD_MGMT_FIXTURE_HPP