management: process Nack in nfd::Controller

refs #3264

Change-Id: Ifaa73ac04e8838a89708fc63a578941d700712c6
diff --git a/src/management/nfd-controller.cpp b/src/management/nfd-controller.cpp
index 733ca36..2ebdb4a 100644
--- a/src/management/nfd-controller.cpp
+++ b/src/management/nfd-controller.cpp
@@ -25,7 +25,8 @@
 namespace ndn {
 namespace nfd {
 
-const uint32_t Controller::ERROR_TIMEOUT = 10060;
+const uint32_t Controller::ERROR_TIMEOUT = 10060; // WinSock ESAETIMEDOUT
+const uint32_t Controller::ERROR_NACK = 10800; // 10000 + TLV-TYPE of Nack header
 const uint32_t Controller::ERROR_SERVER = 500;
 const uint32_t Controller::ERROR_LBOUND = 400;
 
@@ -50,6 +51,7 @@
   m_face.expressInterest(interest,
                          bind(&Controller::processCommandResponse, this, _2,
                               command, onSuccess, onFailure),
+                         bind(onFailure, ERROR_NACK, "network Nack received"),
                          bind(onFailure, ERROR_TIMEOUT, "request timed out"));
 }
 
diff --git a/src/management/nfd-controller.hpp b/src/management/nfd-controller.hpp
index 331810f..4fbfa3e 100644
--- a/src/management/nfd-controller.hpp
+++ b/src/management/nfd-controller.hpp
@@ -84,10 +84,13 @@
 
 public:
   /** \brief error code for timeout
-   *  \note comes from http://msdn.microsoft.com/en-us/library/windows/desktop/ms740668.aspx
    */
   static const uint32_t ERROR_TIMEOUT;
 
+  /** \brief error code for network Nack
+   */
+  static const uint32_t ERROR_NACK;
+
   /** \brief error code for server error
    */
   static const uint32_t ERROR_SERVER;
diff --git a/tests/unit-tests/make-interest-data.hpp b/tests/unit-tests/make-interest-data.hpp
index ca3bff5..d5770af 100644
--- a/tests/unit-tests/make-interest-data.hpp
+++ b/tests/unit-tests/make-interest-data.hpp
@@ -30,35 +30,62 @@
 #include "boost-test.hpp"
 
 #include "security/key-chain.hpp"
+#include "lp/nack.hpp"
 
 namespace ndn {
 namespace util {
 
 inline shared_ptr<Interest>
-makeInterest(const Name& name)
+makeInterest(const Name& name, uint32_t nonce = 0)
 {
-  return make_shared<Interest>(name);
+  auto interest = make_shared<Interest>(name);
+  if (nonce != 0) {
+    interest->setNonce(nonce);
+  }
+  return interest;
 }
 
 inline shared_ptr<Data>
-signData(const shared_ptr<Data>& data)
+signData(shared_ptr<Data> data)
 {
-  ndn::SignatureSha256WithRsa fakeSignature;
-  fakeSignature.setValue(makeEmptyBlock(tlv::SignatureValue));
+  SignatureSha256WithRsa fakeSignature;
+  fakeSignature.setValue(encoding::makeEmptyBlock(tlv::SignatureValue));
   data->setSignature(fakeSignature);
   data->wireEncode();
-
   return data;
 }
 
 inline shared_ptr<Data>
 makeData(const Name& name)
 {
-  shared_ptr<Data> data = make_shared<Data>(name);
-
+  auto data = make_shared<Data>(name);
   return signData(data);
 }
 
+inline shared_ptr<Link>
+makeLink(const Name& name, std::initializer_list<std::pair<uint32_t, Name>> delegations)
+{
+  auto link = make_shared<Link>(name, delegations);
+  signData(link);
+  return link;
+}
+
+inline lp::Nack
+makeNack(const Interest& interest, lp::NackReason reason)
+{
+  lp::Nack nack(interest);
+  nack.setReason(reason);
+  return nack;
+}
+
+inline lp::Nack
+makeNack(const Name& name, uint32_t nonce, lp::NackReason reason)
+{
+  Interest interest(name);
+  interest.setNonce(nonce);
+  return makeNack(interest, reason);
+}
+
 } // namespace util
 } // namespace ndn
 
diff --git a/tests/unit-tests/management/nfd-controller.t.cpp b/tests/unit-tests/management/nfd-controller.t.cpp
index 07c5894..6479cd6 100644
--- a/tests/unit-tests/management/nfd-controller.t.cpp
+++ b/tests/unit-tests/management/nfd-controller.t.cpp
@@ -26,6 +26,7 @@
 
 #include "boost-test.hpp"
 #include "util/dummy-client-face.hpp"
+#include "../make-interest-data.hpp"
 #include "../unit-test-time-fixture.hpp"
 
 namespace ndn {
@@ -35,7 +36,7 @@
 using ndn::util::DummyClientFace;
 using ndn::util::makeDummyClientFace;
 
-BOOST_AUTO_TEST_SUITE(ManagementNfdController)
+BOOST_AUTO_TEST_SUITE(Management)
 
 class CommandFixture : public ndn::tests::UnitTestTimeFixture
 {
@@ -75,7 +76,9 @@
   std::vector<CommandFailArgs> commandFailHistory;
 };
 
-BOOST_FIXTURE_TEST_CASE(CommandSuccess, CommandFixture)
+BOOST_FIXTURE_TEST_SUITE(TestNfdController, CommandFixture)
+
+BOOST_AUTO_TEST_CASE(CommandSuccess)
 {
   ControlParameters parameters;
   parameters.setUri("tcp4://192.0.2.1:6363");
@@ -108,10 +111,9 @@
   ControlResponse responsePayload(201, "created");
   responsePayload.setBody(responseBody.wireEncode());
 
-  Data responseData(requestInterest.getName());
-  responseData.setContent(responsePayload.wireEncode());
-  keyChain.sign(responseData);
-  face->receive(responseData);
+  auto responseData = util::makeData(requestInterest.getName());
+  responseData->setContent(responsePayload.wireEncode());
+  face->receive(*responseData);
 
   advanceClocks(time::milliseconds(1));
 
@@ -122,7 +124,7 @@
   BOOST_CHECK_EQUAL(response.getFaceId(), responseBody.getFaceId());
 }
 
-BOOST_FIXTURE_TEST_CASE(CommandInvalidRequest, CommandFixture)
+BOOST_AUTO_TEST_CASE(CommandInvalidRequest)
 {
   ControlParameters parameters;
   parameters.setName("ndn:/should-not-have-this-field");
@@ -135,7 +137,7 @@
                     ControlCommand::ArgumentError);
 }
 
-BOOST_FIXTURE_TEST_CASE(CommandErrorCode, CommandFixture)
+BOOST_AUTO_TEST_CASE(CommandErrorCode)
 {
   ControlParameters parameters;
   parameters.setUri("tcp4://192.0.2.1:6363");
@@ -151,10 +153,9 @@
 
   ControlResponse responsePayload(401, "Not Authenticated");
 
-  Data responseData(requestInterest.getName());
-  responseData.setContent(responsePayload.wireEncode());
-  keyChain.sign(responseData);
-  face->receive(responseData);
+  auto responseData = util::makeData(requestInterest.getName());
+  responseData->setContent(responsePayload.wireEncode());
+  face->receive(*responseData);
   advanceClocks(time::milliseconds(1));
 
   BOOST_CHECK_EQUAL(commandSucceedHistory.size(), 0);
@@ -162,7 +163,7 @@
   BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), 401);
 }
 
-BOOST_FIXTURE_TEST_CASE(CommandInvalidResponse, CommandFixture)
+BOOST_AUTO_TEST_CASE(CommandInvalidResponse)
 {
   ControlParameters parameters;
   parameters.setUri("tcp4://192.0.2.1:6363");
@@ -183,17 +184,38 @@
   ControlResponse responsePayload(201, "created");
   responsePayload.setBody(responseBody.wireEncode());
 
-  Data responseData(requestInterest.getName());
-  responseData.setContent(responsePayload.wireEncode());
-  keyChain.sign(responseData);
-  face->receive(responseData);
+  auto responseData = util::makeData(requestInterest.getName());
+  responseData->setContent(responsePayload.wireEncode());
+  face->receive(*responseData);
   advanceClocks(time::milliseconds(1));
 
   BOOST_CHECK_EQUAL(commandSucceedHistory.size(), 0);
   BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1);
 }
 
-BOOST_FIXTURE_TEST_CASE(OptionsPrefix, CommandFixture)
+BOOST_AUTO_TEST_CASE(CommandNack)
+{
+  ControlParameters parameters;
+  parameters.setUri("tcp4://192.0.2.1:6363");
+
+  BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
+                       parameters,
+                       commandSucceedCallback,
+                       commandFailCallback));
+  advanceClocks(time::milliseconds(1));
+
+  BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
+  const Interest& requestInterest = face->sentInterests[0];
+
+  auto responseNack = util::makeNack(requestInterest, lp::NackReason::NO_ROUTE);
+  face->receive(responseNack);
+  advanceClocks(time::milliseconds(1));
+
+  BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1);
+  BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), Controller::ERROR_NACK);
+}
+
+BOOST_AUTO_TEST_CASE(OptionsPrefix)
 {
   ControlParameters parameters;
   parameters.setName("/ndn/com/example");
@@ -217,7 +239,7 @@
               requestInterest.getName()));
 }
 
-BOOST_FIXTURE_TEST_CASE(OptionsTimeout, CommandFixture)
+BOOST_AUTO_TEST_CASE(OptionsTimeout)
 {
   ControlParameters parameters;
   parameters.setUri("tcp4://192.0.2.1:6363");
@@ -236,7 +258,8 @@
   BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), Controller::ERROR_TIMEOUT);
 }
 
-BOOST_AUTO_TEST_SUITE_END()
+BOOST_AUTO_TEST_SUITE_END() // TestController
+BOOST_AUTO_TEST_SUITE_END() // Management
 
 } // namespace tests
 } // namespace nfd