management: StatusDataset client
refs #3329
Change-Id: Icf755ba7ed1fd36f6ea09af97cb77f70757f84c7
diff --git a/src/management/nfd-controller.cpp b/src/management/nfd-controller.cpp
index 2ebdb4a..acd2435 100644
--- a/src/management/nfd-controller.cpp
+++ b/src/management/nfd-controller.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -21,18 +21,23 @@
#include "nfd-controller.hpp"
#include "nfd-control-response.hpp"
+#include "../util/segment-fetcher.hpp"
namespace ndn {
namespace nfd {
+using ndn::util::SegmentFetcher;
+
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;
+ValidatorNull Controller::s_validatorNull;
Controller::Controller(Face& face, KeyChain& keyChain)
: m_face(face)
, m_keyChain(keyChain)
+ , m_validator(s_validatorNull) /// \todo #3653 accept validator as constructor parameter
{
}
@@ -103,5 +108,41 @@
onSuccess(parameters);
}
+void
+Controller::fetchDataset(const Name& prefix,
+ const std::function<void(const ConstBufferPtr&)>& processResponse,
+ const CommandFailCallback& onFailure,
+ const CommandOptions& options)
+{
+ Interest baseInterest(prefix);
+ baseInterest.setInterestLifetime(options.getTimeout());
+
+ SegmentFetcher::fetch(m_face, baseInterest, m_validator, processResponse,
+ bind(&Controller::processDatasetFetchError, this, onFailure, _1, _2));
+}
+
+void
+Controller::processDatasetFetchError(const CommandFailCallback& onFailure,
+ uint32_t code, std::string msg)
+{
+ switch (static_cast<SegmentFetcher::ErrorCode>(code)) {
+ // It's intentional to cast as SegmentFetcher::ErrorCode, and to not have a 'default' clause.
+ // This forces the switch statement to handle every defined SegmentFetcher::ErrorCode,
+ // and breaks compilation if it does not.
+ case SegmentFetcher::ErrorCode::INTEREST_TIMEOUT:
+ onFailure(ERROR_TIMEOUT, msg);
+ break;
+ case SegmentFetcher::ErrorCode::DATA_HAS_NO_SEGMENT:
+ onFailure(ERROR_SERVER, msg);
+ break;
+ case SegmentFetcher::ErrorCode::SEGMENT_VALIDATION_FAIL:
+ BOOST_ASSERT(false); /// \todo #3653 introduce ERROR_VALIDATION
+ break;
+ case SegmentFetcher::ErrorCode::NACK_ERROR:
+ onFailure(ERROR_NACK, msg);
+ break;
+ }
+}
+
} // namespace nfd
} // namespace ndn
diff --git a/src/management/nfd-controller.hpp b/src/management/nfd-controller.hpp
index 4fbfa3e..7ebc5eb 100644
--- a/src/management/nfd-controller.hpp
+++ b/src/management/nfd-controller.hpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -23,9 +23,11 @@
#define NDN_MANAGEMENT_NFD_CONTROLLER_HPP
#include "nfd-control-command.hpp"
+#include "nfd-status-dataset.hpp"
+#include "nfd-command-options.hpp"
#include "../face.hpp"
#include "../security/key-chain.hpp"
-#include "nfd-command-options.hpp"
+#include "../security/validator-null.hpp"
namespace ndn {
namespace nfd {
@@ -48,7 +50,7 @@
/** \brief a callback on command failure
*/
- typedef function<void(uint32_t/*code*/,const std::string&/*reason*/)> CommandFailCallback;
+ typedef function<void(uint32_t code, const std::string& reason)> CommandFailCallback;
/** \brief construct a Controller that uses face for transport,
* and uses the passed KeyChain to sign commands
@@ -68,6 +70,29 @@
this->startCommand(command, parameters, onSuccess, onFailure, options);
}
+ /** \brief start dataset fetching
+ */
+ template<typename Dataset>
+ typename std::enable_if<std::is_default_constructible<Dataset>::value>::type
+ fetch(const std::function<void(typename Dataset::ResultType)>& onSuccess,
+ const CommandFailCallback& onFailure,
+ const CommandOptions& options = CommandOptions())
+ {
+ this->fetchDataset(make_shared<Dataset>(), onSuccess, onFailure, options);
+ }
+
+ /** \brief start dataset fetching
+ */
+ template<typename Dataset, typename ParamType = typename Dataset::ParamType>
+ void
+ fetch(const ParamType& param,
+ const std::function<void(typename Dataset::ResultType)>& onSuccess,
+ const CommandFailCallback& onFailure,
+ const CommandOptions& options = CommandOptions())
+ {
+ this->fetchDataset(make_shared<Dataset>(param), onSuccess, onFailure, options);
+ }
+
private:
void
startCommand(const shared_ptr<ControlCommand>& command,
@@ -82,6 +107,30 @@
const CommandSucceedCallback& onSuccess,
const CommandFailCallback& onFailure);
+ template<typename Dataset>
+ void
+ fetchDataset(shared_ptr<Dataset> dataset,
+ const std::function<void(typename Dataset::ResultType)>& onSuccess,
+ const CommandFailCallback& onFailure,
+ const CommandOptions& options);
+
+ void
+ fetchDataset(const Name& prefix,
+ const std::function<void(const ConstBufferPtr&)>& processResponse,
+ const CommandFailCallback& onFailure,
+ const CommandOptions& options);
+
+ template<typename Dataset>
+ void
+ processDatasetResponse(shared_ptr<Dataset> dataset,
+ const std::function<void(typename Dataset::ResultType)>& onSuccess,
+ const CommandFailCallback& onFailure,
+ ConstBufferPtr payload);
+
+ void
+ processDatasetFetchError(const CommandFailCallback& onFailure, uint32_t code, std::string msg);
+
+
public:
/** \brief error code for timeout
*/
@@ -102,8 +151,44 @@
protected:
Face& m_face;
KeyChain& m_keyChain;
+ Validator& m_validator;
+
+private:
+ static ValidatorNull s_validatorNull;
};
+template<typename Dataset>
+inline void
+Controller::fetchDataset(shared_ptr<Dataset> dataset,
+ const std::function<void(typename Dataset::ResultType)>& onSuccess,
+ const CommandFailCallback& onFailure,
+ const CommandOptions& options)
+{
+ Name prefix = dataset->getDatasetPrefix(options.getPrefix());
+ this->fetchDataset(prefix,
+ bind(&Controller::processDatasetResponse<Dataset>, this, dataset, onSuccess, onFailure, _1),
+ onFailure,
+ options);
+}
+
+template<typename Dataset>
+inline void
+Controller::processDatasetResponse(shared_ptr<Dataset> dataset,
+ const std::function<void(typename Dataset::ResultType)>& onSuccess,
+ const CommandFailCallback& onFailure,
+ ConstBufferPtr payload)
+{
+ typename Dataset::ResultType result;
+ try {
+ result = dataset->parseResult(payload);
+ }
+ catch (const tlv::Error& ex) {
+ onFailure(ERROR_SERVER, ex.what());
+ return;
+ }
+ onSuccess(result);
+}
+
} // namespace nfd
} // namespace ndn
diff --git a/src/management/nfd-status-dataset.cpp b/src/management/nfd-status-dataset.cpp
new file mode 100644
index 0000000..2409254
--- /dev/null
+++ b/src/management/nfd-status-dataset.cpp
@@ -0,0 +1,151 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "nfd-status-dataset.hpp"
+#include "../util/concepts.hpp"
+
+namespace ndn {
+namespace nfd {
+
+StatusDataset::StatusDataset(const PartialName& datasetName)
+ : m_datasetName(datasetName)
+{
+}
+
+Name
+StatusDataset::getDatasetPrefix(const Name& prefix) const
+{
+ Name name;
+ name.append(prefix).append(m_datasetName);
+ this->addParameters(name);
+ return name;
+}
+
+void
+StatusDataset::addParameters(Name& name) const
+{
+}
+
+/**
+ * \brief parses elements into a vector of T
+ * \tparam T element type
+ * \param payload pointer to a buffer of zero or more blocks of decodable by T
+ * \return a vector of T
+ * \throw tlv::Error cannot parse payload
+ */
+template<typename T>
+static std::vector<T>
+parseDatasetVector(ConstBufferPtr payload)
+{
+ BOOST_CONCEPT_ASSERT((WireDecodable<T>));
+
+ std::vector<T> result;
+
+ size_t offset = 0;
+ while (offset < payload->size()) {
+ bool isOk = false;
+ Block block;
+ std::tie(isOk, block) = Block::fromBuffer(payload, offset);
+ if (!isOk) {
+ BOOST_THROW_EXCEPTION(StatusDataset::ParseResultError("cannot decode Block"));
+ }
+
+ offset += block.size();
+ result.emplace_back(block);
+ }
+
+ return result;
+}
+
+ForwarderGeneralStatusDataset::ForwarderGeneralStatusDataset()
+ : StatusDataset("status/general")
+{
+}
+
+ForwarderGeneralStatusDataset::ResultType
+ForwarderGeneralStatusDataset::parseResult(ConstBufferPtr payload) const
+{
+ return ForwarderStatus(Block(tlv::Content, payload));
+}
+
+FaceDatasetBase::FaceDatasetBase(const PartialName& datasetName)
+ : StatusDataset(datasetName)
+{
+}
+
+FaceDatasetBase::ResultType
+FaceDatasetBase::parseResult(ConstBufferPtr payload) const
+{
+ return parseDatasetVector<FaceStatus>(payload);
+}
+
+FaceDataset::FaceDataset()
+ : FaceDatasetBase("faces/list")
+{
+}
+
+FaceQueryDataset::FaceQueryDataset(const FaceQueryFilter& filter)
+ : FaceDatasetBase("faces/query")
+ , m_filter(filter)
+{
+}
+
+void
+FaceQueryDataset::addParameters(Name& name) const
+{
+ name.append(m_filter.wireEncode());
+}
+
+FibDataset::FibDataset()
+ : StatusDataset("fib/list")
+{
+}
+
+FibDataset::ResultType
+FibDataset::parseResult(ConstBufferPtr payload) const
+{
+ return parseDatasetVector<FibEntry>(payload);
+}
+
+StrategyChoiceDataset::StrategyChoiceDataset()
+ : StatusDataset("strategy-choice/list")
+{
+}
+
+StrategyChoiceDataset::ResultType
+StrategyChoiceDataset::parseResult(ConstBufferPtr payload) const
+{
+ return parseDatasetVector<StrategyChoice>(payload);
+}
+
+RibDataset::RibDataset()
+ : StatusDataset("rib/list")
+{
+}
+
+RibDataset::ResultType
+RibDataset::parseResult(ConstBufferPtr payload) const
+{
+ return parseDatasetVector<RibEntry>(payload);
+}
+
+} // namespace nfd
+} // namespace ndn
diff --git a/src/management/nfd-status-dataset.hpp b/src/management/nfd-status-dataset.hpp
new file mode 100644
index 0000000..39537bc
--- /dev/null
+++ b/src/management/nfd-status-dataset.hpp
@@ -0,0 +1,234 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_MANAGEMENT_NFD_STATUS_DATASET_HPP
+#define NDN_MANAGEMENT_NFD_STATUS_DATASET_HPP
+
+#include "../name.hpp"
+#include "nfd-forwarder-status.hpp"
+#include "nfd-face-status.hpp"
+#include "nfd-face-query-filter.hpp"
+#include "nfd-fib-entry.hpp"
+#include "nfd-strategy-choice.hpp"
+#include "nfd-rib-entry.hpp"
+
+namespace ndn {
+namespace nfd {
+
+/**
+ * \ingroup management
+ * \brief base class of NFD StatusDataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
+ */
+class StatusDataset : noncopyable
+{
+public:
+#ifdef DOXYGEN
+ /**
+ * \brief if defined, specifies constructor argument type;
+ * otherwise, constructor has no argument
+ */
+ typedef int ParamType;
+#endif
+
+ /**
+ * \brief constructs a name prefix for the dataset
+ * \param prefix top-level prefix, such as ndn:/localhost/nfd
+ * \return name prefix without version and segment components
+ */
+ Name
+ getDatasetPrefix(const Name& prefix) const;
+
+#ifdef DOXYGEN
+ /**
+ * \brief provides the result type, usually a vector
+ */
+ typedef std::vector<int> ResultType;
+#endif
+
+ /**
+ * \brief indicates reassembled payload cannot be parsed as ResultType
+ */
+ class ParseResultError : public tlv::Error
+ {
+ public:
+ explicit
+ ParseResultError(const std::string& what)
+ : tlv::Error(what)
+ {
+ }
+ };
+
+#ifdef DOXYGEN
+ /**
+ * \brief parses a result from reassembled payload
+ * \param payload reassembled payload
+ * \throw tlv::Error cannot parse payload
+ */
+ ResultType
+ parseResult(ConstBufferPtr payload) const;
+#endif
+
+protected:
+ /**
+ * \brief constructs a StatusDataset instance with given sub-prefix
+ * \param datasetName dataset name after top-level prefix, such as faces/list
+ */
+ explicit
+ StatusDataset(const PartialName& datasetName);
+
+private:
+ /**
+ * \brief appends parameters to the dataset name prefix
+ * \param[in,out] the dataset name prefix onto which parameter components can be appended
+ */
+ virtual void
+ addParameters(Name& name) const;
+
+private:
+ PartialName m_datasetName;
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a status/general dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus#General-Status-Dataset
+ */
+class ForwarderGeneralStatusDataset : public StatusDataset
+{
+public:
+ ForwarderGeneralStatusDataset();
+
+ typedef ForwarderStatus ResultType;
+
+ ResultType
+ parseResult(ConstBufferPtr payload) const;
+};
+
+
+/**
+ * \ingroup management
+ * \brief provides common functionality among FaceDataset and FaceQueryDataset
+ */
+class FaceDatasetBase : public StatusDataset
+{
+public:
+ typedef std::vector<FaceStatus> ResultType;
+
+ ResultType
+ parseResult(ConstBufferPtr payload) const;
+
+protected:
+ explicit
+ FaceDatasetBase(const PartialName& datasetName);
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a faces/list dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Face-Dataset
+ */
+class FaceDataset : public FaceDatasetBase
+{
+public:
+ FaceDataset();
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a faces/query dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Query-Operation
+ */
+class FaceQueryDataset : public FaceDatasetBase
+{
+public:
+ typedef FaceQueryFilter ParamType;
+
+ explicit
+ FaceQueryDataset(const FaceQueryFilter& filter);
+
+private:
+ virtual void
+ addParameters(Name& name) const override;
+
+private:
+ FaceQueryFilter m_filter;
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a fib/list dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset
+ */
+class FibDataset : public StatusDataset
+{
+public:
+ FibDataset();
+
+ typedef std::vector<FibEntry> ResultType;
+
+ ResultType
+ parseResult(ConstBufferPtr payload) const;
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a strategy-choice/list dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice#Strategy-Choice-Dataset
+ */
+class StrategyChoiceDataset : public StatusDataset
+{
+public:
+ StrategyChoiceDataset();
+
+ typedef std::vector<StrategyChoice> ResultType;
+
+ ResultType
+ parseResult(ConstBufferPtr payload) const;
+};
+
+
+/**
+ * \ingroup management
+ * \brief represents a rib/list dataset
+ * \sa http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset
+ */
+class RibDataset : public StatusDataset
+{
+public:
+ RibDataset();
+
+ typedef std::vector<RibEntry> ResultType;
+
+ ResultType
+ parseResult(ConstBufferPtr payload) const;
+};
+
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_STATUS_DATASET_HPP
diff --git a/tests/unit-tests/management/nfd-controller-fixture.hpp b/tests/unit-tests/management/nfd-controller-fixture.hpp
new file mode 100644
index 0000000..e9ffde2
--- /dev/null
+++ b/tests/unit-tests/management/nfd-controller-fixture.hpp
@@ -0,0 +1,70 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#ifndef NDN_TESTS_MANAGEMENT_NFD_CONTROLLER_FIXTURE_HPP
+#define NDN_TESTS_MANAGEMENT_NFD_CONTROLLER_FIXTURE_HPP
+
+#include "boost-test.hpp"
+#include "util/dummy-client-face.hpp"
+#include "../identity-management-time-fixture.hpp"
+
+namespace ndn {
+namespace nfd {
+namespace tests {
+
+using namespace ndn::tests;
+
+class ControllerFixture : public security::IdentityManagementTimeFixture
+{
+protected:
+ ControllerFixture()
+ : face(io)
+ , controller(face, m_keyChain)
+ , failCallback(bind(&ControllerFixture::fail, this, _1, _2))
+ {
+ Name identityName("/localhost/ControllerFixture");
+ if (this->addIdentity(identityName)) {
+ m_keyChain.setDefaultIdentity(identityName);
+ }
+ else {
+ BOOST_FAIL("cannot create identity");
+ }
+ }
+
+private:
+ void
+ fail(uint32_t code, const std::string& reason)
+ {
+ failCodes.push_back(code);
+ }
+
+protected:
+ ndn::util::DummyClientFace face;
+ Controller controller;
+ Controller::CommandFailCallback failCallback;
+ std::vector<uint32_t> failCodes;
+};
+
+} // namespace tests
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_TESTS_MANAGEMENT_NFD_CONTROLLER_FIXTURE_HPP
diff --git a/tests/unit-tests/management/nfd-controller.t.cpp b/tests/unit-tests/management/nfd-controller.t.cpp
index 2379d5c..c2e2b6c 100644
--- a/tests/unit-tests/management/nfd-controller.t.cpp
+++ b/tests/unit-tests/management/nfd-controller.t.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+ * Copyright (c) 2013-2016 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
@@ -24,55 +24,36 @@
#include <boost/tuple/tuple.hpp>
-#include "boost-test.hpp"
-#include "util/dummy-client-face.hpp"
+#include "nfd-controller-fixture.hpp"
#include "../make-interest-data.hpp"
-#include "../unit-test-time-fixture.hpp"
namespace ndn {
namespace nfd {
namespace tests {
-using ndn::util::DummyClientFace;
+using ndn::util::makeData;
+using ndn::util::makeNack;
BOOST_AUTO_TEST_SUITE(Management)
-class CommandFixture : public ndn::tests::UnitTestTimeFixture
+class CommandFixture : public ControllerFixture
{
protected:
CommandFixture()
- : face(io)
- , controller(face, keyChain)
- , commandSucceedCallback(bind(&CommandFixture::onCommandSucceed, this, _1))
- , commandFailCallback(bind(&CommandFixture::onCommandFail, this, _1, _2))
+ : succeedCallback(bind(&CommandFixture::succeed, this, _1))
{
}
private:
void
- onCommandSucceed(const ControlParameters& parameters)
+ succeed(const ControlParameters& parameters)
{
- commandSucceedHistory.push_back(boost::make_tuple(parameters));
- }
-
- void
- onCommandFail(uint32_t code, const std::string& reason)
- {
- commandFailHistory.push_back(boost::make_tuple(code, reason));
+ succeeds.push_back(parameters);
}
protected:
- DummyClientFace face;
- KeyChain keyChain;
- Controller controller;
-
- Controller::CommandSucceedCallback commandSucceedCallback;
- typedef boost::tuple<ControlParameters> CommandSucceedArgs;
- std::vector<CommandSucceedArgs> commandSucceedHistory;
-
- Controller::CommandFailCallback commandFailCallback;
- typedef boost::tuple<uint32_t,std::string> CommandFailArgs;
- std::vector<CommandFailArgs> commandFailHistory;
+ Controller::CommandSucceedCallback succeedCallback;
+ std::vector<ControlParameters> succeeds;
};
BOOST_FIXTURE_TEST_SUITE(TestNfdController, CommandFixture)
@@ -83,10 +64,7 @@
parameters.setUri("tcp4://192.0.2.1:6363");
BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
- parameters,
- commandSucceedCallback,
- commandFailCallback));
-
+ parameters, succeedCallback, failCallback));
advanceClocks(time::milliseconds(1));
BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
@@ -110,17 +88,16 @@
ControlResponse responsePayload(201, "created");
responsePayload.setBody(responseBody.wireEncode());
- auto responseData = util::makeData(requestInterest.getName());
+ auto responseData = makeData(requestInterest.getName());
responseData->setContent(responsePayload.wireEncode());
face.receive(*responseData);
advanceClocks(time::milliseconds(1));
- BOOST_CHECK_EQUAL(commandFailHistory.size(), 0);
- BOOST_REQUIRE_EQUAL(commandSucceedHistory.size(), 1);
- const ControlParameters& response = commandSucceedHistory[0].get<0>();
- BOOST_CHECK_EQUAL(response.getUri(), responseBody.getUri());
- BOOST_CHECK_EQUAL(response.getFaceId(), responseBody.getFaceId());
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+ BOOST_REQUIRE_EQUAL(succeeds.size(), 1);
+ BOOST_CHECK_EQUAL(succeeds.back().getUri(), responseBody.getUri());
+ BOOST_CHECK_EQUAL(succeeds.back().getFaceId(), responseBody.getFaceId());
}
BOOST_AUTO_TEST_CASE(CommandInvalidRequest)
@@ -130,9 +107,7 @@
// Uri is missing
BOOST_CHECK_THROW(controller.start<FaceCreateCommand>(
- parameters,
- commandSucceedCallback,
- commandFailCallback),
+ parameters, succeedCallback, failCallback),
ControlCommand::ArgumentError);
}
@@ -142,9 +117,7 @@
parameters.setUri("tcp4://192.0.2.1:6363");
BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
- parameters,
- commandSucceedCallback,
- commandFailCallback));
+ parameters, succeedCallback, failCallback));
advanceClocks(time::milliseconds(1));
BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
@@ -152,14 +125,14 @@
ControlResponse responsePayload(401, "Not Authenticated");
- auto responseData = util::makeData(requestInterest.getName());
+ auto responseData = 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_CHECK_EQUAL(commandFailHistory[0].get<0>(), 401);
+ BOOST_CHECK_EQUAL(succeeds.size(), 0);
+ BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
+ BOOST_CHECK_EQUAL(failCodes.back(), 401);
}
BOOST_AUTO_TEST_CASE(CommandInvalidResponse)
@@ -168,9 +141,7 @@
parameters.setUri("tcp4://192.0.2.1:6363");
BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
- parameters,
- commandSucceedCallback,
- commandFailCallback));
+ parameters, succeedCallback, failCallback));
advanceClocks(time::milliseconds(1));
BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
@@ -183,13 +154,13 @@
ControlResponse responsePayload(201, "created");
responsePayload.setBody(responseBody.wireEncode());
- auto responseData = util::makeData(requestInterest.getName());
+ auto responseData = 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_CHECK_EQUAL(succeeds.size(), 0);
+ BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
}
BOOST_AUTO_TEST_CASE(CommandNack)
@@ -198,20 +169,18 @@
parameters.setUri("tcp4://192.0.2.1:6363");
BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
- parameters,
- commandSucceedCallback,
- commandFailCallback));
+ parameters, succeedCallback, failCallback));
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);
+ auto responseNack = 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_REQUIRE_EQUAL(failCodes.size(), 1);
+ BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_NACK);
}
BOOST_AUTO_TEST_CASE(OptionsPrefix)
@@ -224,10 +193,7 @@
options.setPrefix("/localhop/net/example/router1/nfd");
BOOST_CHECK_NO_THROW(controller.start<RibRegisterCommand>(
- parameters,
- commandSucceedCallback,
- commandFailCallback,
- options));
+ parameters, succeedCallback, failCallback, options));
advanceClocks(time::milliseconds(1));
BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
@@ -247,19 +213,18 @@
options.setTimeout(time::milliseconds(50));
BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
- parameters,
- commandSucceedCallback,
- commandFailCallback,
- options));
+ parameters, succeedCallback, failCallback, options));
advanceClocks(time::milliseconds(1), 101); // Face's PIT granularity is 100ms
- BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1);
- BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), Controller::ERROR_TIMEOUT);
+ BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
+ BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_TIMEOUT);
}
BOOST_AUTO_TEST_SUITE_END() // TestController
BOOST_AUTO_TEST_SUITE_END() // Management
+// Controller::fetch<Dataset> has a separate test suite in nfd-status-dataset.t.cpp
+
} // namespace tests
} // namespace nfd
} // namespace ndn
diff --git a/tests/unit-tests/management/nfd-status-dataset.t.cpp b/tests/unit-tests/management/nfd-status-dataset.t.cpp
new file mode 100644
index 0000000..63b98aa
--- /dev/null
+++ b/tests/unit-tests/management/nfd-status-dataset.t.cpp
@@ -0,0 +1,387 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2016 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "management/nfd-status-dataset.hpp"
+#include "management/nfd-controller.hpp"
+
+#include "nfd-controller-fixture.hpp"
+#include "../make-interest-data.hpp"
+
+namespace ndn {
+namespace nfd {
+namespace tests {
+
+using ndn::util::makeData;
+using ndn::util::signData;
+
+BOOST_AUTO_TEST_SUITE(Management)
+
+class ControllerStatusDatasetFixture : public ControllerFixture
+{
+protected:
+ /** \brief send one WireEncodable as Data reply
+ * \param prefix dataset prefix without version and segment
+ * \param payload payload block
+ * \note payload must fit in one Data
+ */
+ template<typename T>
+ void
+ sendDataset(const Name& prefix, const T& payload)
+ {
+ BOOST_CONCEPT_ASSERT((WireEncodable<T>));
+
+ auto data = this->prepareDatasetReply(prefix);
+ data->setContent(payload.wireEncode());
+ face.receive(*signData(data));
+ }
+
+ /** \brief send two WireEncodables as Data reply
+ * \param prefix dataset prefix without version and segment
+ * \param payload1 first vector item
+ * \param payload2 second vector item
+ * \note all payloads must fit in one Data
+ */
+ template<typename T1, typename T2>
+ void
+ sendDataset(const Name& prefix, const T1& payload1, const T2& payload2)
+ {
+ // The test suite allows up to two items, and put them in the same Data packet,
+ // because this test suite focuses on Controller::fetch<StatusDataset>,
+ // and is not intended to cover SegmentFetcher behavior.
+
+ BOOST_CONCEPT_ASSERT((WireEncodable<T1>));
+ BOOST_CONCEPT_ASSERT((WireEncodable<T2>));
+
+ ndn::encoding::EncodingBuffer buffer;
+ payload2.wireEncode(buffer);
+ payload1.wireEncode(buffer);
+
+ auto data = this->prepareDatasetReply(prefix);
+ data->setContent(buffer.buf(), buffer.size());
+ face.receive(*signData(data));
+ }
+
+private:
+ shared_ptr<Data>
+ prepareDatasetReply(const Name& prefix)
+ {
+ Name name = prefix;
+ name.appendVersion().appendSegment(0);
+
+ // These warnings assist in debugging a `hasResult` check failure.
+ // They usually indicate a misspelled prefix or incorrect timing in the test case.
+ if (face.sentInterests.size() < 1) {
+ BOOST_WARN_MESSAGE(false, "no Interest expressed");
+ }
+ else {
+ BOOST_WARN_MESSAGE(face.sentInterests.back().getName().isPrefixOf(name),
+ "last Interest " << face.sentInterests.back().getName() <<
+ " cannot be satisfied by this Data " << name);
+ }
+
+ auto data = make_shared<Data>(name);
+ data->setFinalBlockId(data->getName()[-1]);
+ return data;
+ }
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestNfdStatusDataset, ControllerStatusDatasetFixture)
+
+BOOST_AUTO_TEST_SUITE(Failures)
+
+BOOST_AUTO_TEST_CASE(Timeout)
+{
+ CommandOptions options;
+ options.setTimeout(time::milliseconds(3000));
+ controller.fetch<FaceDataset>(
+ [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
+ failCallback,
+ options);
+ this->advanceClocks(time::milliseconds(500), 7);
+
+ BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
+ BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_TIMEOUT);
+}
+
+BOOST_AUTO_TEST_CASE(DataHasNoSegment)
+{
+ controller.fetch<FaceDataset>(
+ [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ face.receive(*makeData("/localhost/nfd/faces/list/%FD%00"));
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
+ BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
+}
+
+BOOST_AUTO_TEST_CASE(Nack)
+{
+ controller.fetch<FaceDataset>(
+ [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
+ face.receive(lp::Nack(face.sentInterests.back()));
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
+ BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_NACK);
+}
+
+BOOST_AUTO_TEST_CASE(ParseError1)
+{
+ controller.fetch<FaceDataset>(
+ [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ Name payload; // Name is not valid FaceStatus
+ this->sendDataset("/localhost/nfd/faces/list", payload);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
+ BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
+}
+
+BOOST_AUTO_TEST_CASE(ParseError2)
+{
+ controller.fetch<FaceDataset>(
+ [] (const std::vector<FaceStatus>& result) { BOOST_FAIL("fetchDataset should not succeed"); },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ FaceStatus payload1;
+ payload1.setFaceId(10930);
+ Name payload2; // Name is not valid FaceStatus
+ this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
+ BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_SERVER);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // Failures
+
+BOOST_AUTO_TEST_SUITE(Datasets)
+
+BOOST_AUTO_TEST_CASE(StatusGeneral)
+{
+ bool hasResult = false;
+ controller.fetch<ForwarderGeneralStatusDataset>(
+ [&hasResult] (const ForwarderStatus& result) {
+ hasResult = true;
+ BOOST_CHECK_EQUAL(result.getNfdVersion(), "0.4.2");
+ },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ ForwarderStatus payload;
+ payload.setNfdVersion("0.4.2");
+ this->sendDataset("/localhost/nfd/status/general", payload);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_CHECK(hasResult);
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(FaceList)
+{
+ bool hasResult = false;
+ controller.fetch<FaceDataset>(
+ [&hasResult] (const std::vector<FaceStatus>& result) {
+ hasResult = true;
+ BOOST_CHECK_EQUAL(result.size(), 2);
+ BOOST_CHECK_EQUAL(result.front().getFaceId(), 24485);
+ },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ FaceStatus payload1;
+ payload1.setFaceId(24485);
+ FaceStatus payload2;
+ payload2.setFaceId(12987);
+ this->sendDataset("/localhost/nfd/faces/list", payload1, payload2);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_CHECK(hasResult);
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(FaceQuery)
+{
+ FaceQueryFilter filter;
+ filter.setUriScheme("udp4");
+ bool hasResult = false;
+ controller.fetch<FaceQueryDataset>(
+ filter,
+ [&hasResult] (const std::vector<FaceStatus>& result) {
+ hasResult = true;
+ BOOST_CHECK_EQUAL(result.size(), 1);
+ BOOST_CHECK_EQUAL(result.front().getFaceId(), 8795);
+ },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ Name prefix("/localhost/nfd/faces/query");
+ prefix.append(filter.wireEncode());
+ FaceStatus payload;
+ payload.setFaceId(8795);
+ this->sendDataset(prefix, payload);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_CHECK(hasResult);
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(FaceQueryWithOptions)
+{
+ FaceQueryFilter filter;
+ filter.setUriScheme("udp4");
+ CommandOptions options;
+ options.setTimeout(time::milliseconds(3000));
+ bool hasResult = false;
+ controller.fetch<FaceQueryDataset>(
+ filter,
+ [&hasResult] (const std::vector<FaceStatus>& result) {
+ hasResult = true;
+ BOOST_CHECK_EQUAL(result.size(), 1);
+ BOOST_CHECK_EQUAL(result.front().getFaceId(), 14022);
+ },
+ failCallback,
+ options);
+ this->advanceClocks(time::milliseconds(500));
+
+ Name prefix("/localhost/nfd/faces/query");
+ prefix.append(filter.wireEncode());
+ FaceStatus payload;
+ payload.setFaceId(14022);
+ this->sendDataset(prefix, payload);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_CHECK(hasResult);
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(FibList)
+{
+ bool hasResult = false;
+ controller.fetch<FibDataset>(
+ [&hasResult] (const std::vector<FibEntry>& result) {
+ hasResult = true;
+ BOOST_CHECK_EQUAL(result.size(), 2);
+ BOOST_CHECK_EQUAL(result.front().getPrefix(), "/wYs7fzYcfG");
+ },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ FibEntry payload1;
+ payload1.setPrefix("/wYs7fzYcfG");
+ FibEntry payload2;
+ payload2.setPrefix("/LKvmnzY5S");
+ this->sendDataset("/localhost/nfd/fib/list", payload1, payload2);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_CHECK(hasResult);
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(StrategyChoiceList)
+{
+ bool hasResult = false;
+ controller.fetch<StrategyChoiceDataset>(
+ [&hasResult] (const std::vector<StrategyChoice>& result) {
+ hasResult = true;
+ BOOST_CHECK_EQUAL(result.size(), 2);
+ BOOST_CHECK_EQUAL(result.front().getName(), "/8MLz6N3B");
+ },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ StrategyChoice payload1;
+ payload1.setName("/8MLz6N3B");
+ StrategyChoice payload2;
+ payload2.setName("/svqcBu0YwU");
+ this->sendDataset("/localhost/nfd/strategy-choice/list", payload1, payload2);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_CHECK(hasResult);
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(RibList)
+{
+ bool hasResult = false;
+ controller.fetch<RibDataset>(
+ [&hasResult] (const std::vector<RibEntry>& result) {
+ hasResult = true;
+ BOOST_CHECK_EQUAL(result.size(), 2);
+ BOOST_CHECK_EQUAL(result.front().getName(), "/zXxBth97ee");
+ },
+ failCallback);
+ this->advanceClocks(time::milliseconds(500));
+
+ RibEntry payload1;
+ payload1.setName("/zXxBth97ee");
+ RibEntry payload2;
+ payload2.setName("/rJ8CvUpr4G");
+ this->sendDataset("/localhost/nfd/rib/list", payload1, payload2);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_CHECK(hasResult);
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(RibListWithOptions)
+{
+ CommandOptions options;
+ options.setPrefix("/localhop/nfd");
+ bool hasResult = false;
+ controller.fetch<RibDataset>(
+ [&hasResult] (const std::vector<RibEntry>& result) {
+ hasResult = true;
+ BOOST_CHECK_EQUAL(result.size(), 1);
+ BOOST_CHECK_EQUAL(result.front().getName(), "/e6L5K4ascd");
+ },
+ failCallback,
+ options);
+ this->advanceClocks(time::milliseconds(500));
+
+ RibEntry payload;
+ payload.setName("/e6L5K4ascd");
+ this->sendDataset("/localhop/nfd/rib/list", payload);
+ this->advanceClocks(time::milliseconds(500));
+
+ BOOST_CHECK(hasResult);
+ BOOST_CHECK_EQUAL(failCodes.size(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // Datasets
+
+BOOST_AUTO_TEST_SUITE_END() // TestNfdStatusDataset
+BOOST_AUTO_TEST_SUITE_END() // Management
+
+} // namespace tests
+} // namespace nfd
+} // namespace ndn