management: CommandOptions
refs #2039
Change-Id: I51111d84c73c25265d03f303fad238f314264a08
diff --git a/src/management/nfd-command-options.cpp b/src/management/nfd-command-options.cpp
new file mode 100644
index 0000000..f8141d3
--- /dev/null
+++ b/src/management/nfd-command-options.cpp
@@ -0,0 +1,84 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 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-command-options.hpp"
+
+namespace ndn {
+namespace nfd {
+
+const time::milliseconds CommandOptions::DEFAULT_TIMEOUT(10000);
+const Name CommandOptions::DEFAULT_PREFIX("ndn:/localhost/nfd");
+
+CommandOptions::CommandOptions()
+ : m_timeout(DEFAULT_TIMEOUT)
+ , m_prefix(DEFAULT_PREFIX)
+ , m_signingParamsKind(SIGNING_PARAMS_DEFAULT)
+{
+}
+
+CommandOptions&
+CommandOptions::setTimeout(const time::milliseconds& timeout)
+{
+ if (timeout <= time::milliseconds::zero()) {
+ throw std::out_of_range("timeout must be positive");
+ }
+
+ m_timeout = timeout;
+ return *this;
+}
+
+CommandOptions&
+CommandOptions::setPrefix(const Name& prefix)
+{
+ m_prefix = prefix;
+ return *this;
+}
+
+CommandOptions&
+CommandOptions::setSigningIdentity(const Name& identityName)
+{
+ m_signingParamsKind = SIGNING_PARAMS_IDENTITY;
+ m_identity = identityName;
+ return *this;
+}
+
+CommandOptions&
+CommandOptions::setSigningCertificate(const Name& certificateName)
+{
+ // A valid IdentityCertificate has at least 4 name components,
+ // as it follows `<...>/KEY/<...>/<key-id>/ID-CERT/<version>` naming model.
+ if (certificateName.size() < 4) {
+ throw std::invalid_argument("certificate is invalid");
+ }
+
+ m_signingParamsKind = SIGNING_PARAMS_CERTIFICATE;
+ m_identity = certificateName;
+ return *this;
+}
+
+CommandOptions&
+CommandOptions::setSigningCertificate(const IdentityCertificate& certificate)
+{
+ return this->setSigningCertificate(certificate.getName());
+}
+
+} // namespace nfd
+} // namespace ndn
diff --git a/src/management/nfd-command-options.hpp b/src/management/nfd-command-options.hpp
new file mode 100644
index 0000000..54f744f
--- /dev/null
+++ b/src/management/nfd-command-options.hpp
@@ -0,0 +1,166 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 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_COMMAND_OPTIONS_HPP
+#define NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_HPP
+
+#include "../security/identity-certificate.hpp"
+
+namespace ndn {
+namespace nfd {
+
+/** \ingroup management
+ * \brief contains options for ControlCommand execution
+ * \note This type is intentionally copyable
+ */
+class CommandOptions
+{
+public:
+ CommandOptions();
+
+ /** \return command timeout
+ */
+ const time::milliseconds&
+ getTimeout() const
+ {
+ return m_timeout;
+ }
+
+ /** \brief sets command timeout
+ * \param timeout the new command timeout, must be positive
+ * \throw std::out_of_range if timeout is non-positive
+ * \return self
+ */
+ CommandOptions&
+ setTimeout(const time::milliseconds& timeout);
+
+ /** \return command prefix
+ */
+ const Name&
+ getPrefix() const
+ {
+ return m_prefix;
+ }
+
+ /** \brief sets command prefix
+ * \return self
+ */
+ CommandOptions&
+ setPrefix(const Name& prefix);
+
+public: // signing parameters
+ /** \brief indicates the selection of signing parameters
+ */
+ enum SigningParamsKind {
+ /** \brief picks the default signing identity and certificate
+ */
+ SIGNING_PARAMS_DEFAULT,
+ /** \brief picks the default certificate of a specific identity Name
+ */
+ SIGNING_PARAMS_IDENTITY,
+ /** \brief picks a specific identity certificate
+ */
+ SIGNING_PARAMS_CERTIFICATE
+ };
+
+ /** \return selection of signing parameters
+ */
+ SigningParamsKind
+ getSigningParamsKind() const
+ {
+ return m_signingParamsKind;
+ }
+
+ /** \return identity Name
+ * \pre getSigningParamsKind() == SIGNING_PARAMS_IDENTITY
+ */
+ const Name&
+ getSigningIdentity() const
+ {
+ BOOST_ASSERT(m_signingParamsKind == SIGNING_PARAMS_IDENTITY);
+ return m_identity;
+ }
+
+ /** \return certificate Name
+ * \pre getSigningParamsKind() == SIGNING_PARAMS_CERTIFICATE
+ */
+ const Name&
+ getSigningCertificate() const
+ {
+ BOOST_ASSERT(m_signingParamsKind == SIGNING_PARAMS_CERTIFICATE);
+ return m_identity;
+ }
+
+ /** \brief chooses to use default identity and certificate
+ * \post getSigningParamsKind() == SIGNING_PARAMS_DEFAULT
+ * \return self
+ */
+ CommandOptions&
+ setSigningDefault()
+ {
+ m_signingParamsKind = SIGNING_PARAMS_DEFAULT;
+ return *this;
+ }
+
+ /** \brief chooses to use a specific identity and its default certificate
+ * \post getSigningParamsKind() == SIGNING_PARAMS_IDENTITY
+ * \post getIdentityName() == identityName
+ * \return self
+ */
+ CommandOptions&
+ setSigningIdentity(const Name& identityName);
+
+ /** \brief chooses to use a specific identity certificate
+ * \param certificateName identity certificate Name
+ * \throw std::invalid_argument if certificateName is invalid
+ * \post getSigningParamsKind() == SIGNING_PARAMS_CERTIFICATE
+ * \post getIdentityCertificate() is a copy of certificate
+ * \return self
+ */
+ CommandOptions&
+ setSigningCertificate(const Name& certificateName);
+
+ /** \brief chooses to use a specific identity certificate
+ * \details This is equivalent to .setIdentityCertificate(certificate.getName())
+ */
+ CommandOptions&
+ setSigningCertificate(const IdentityCertificate& certificate);
+
+public:
+ /** \brief gives the default command timeout: 10000ms
+ */
+ static const time::milliseconds DEFAULT_TIMEOUT;
+
+ /** \brief gives the default command prefix: ndn:/localhost/nfd
+ */
+ static const Name DEFAULT_PREFIX;
+
+private:
+ time::milliseconds m_timeout;
+ Name m_prefix;
+ SigningParamsKind m_signingParamsKind;
+ Name m_identity; // identityName or certificateName
+};
+
+} // namespace nfd
+} // namespace ndn
+
+#endif // NDN_MANAGEMENT_NFD_COMMAND_OPTIONS_HPP
diff --git a/tests/unit-tests/management/test-nfd-command-options.cpp b/tests/unit-tests/management/test-nfd-command-options.cpp
new file mode 100644
index 0000000..b1afb35
--- /dev/null
+++ b/tests/unit-tests/management/test-nfd-command-options.cpp
@@ -0,0 +1,104 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2014 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-command-options.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace nfd {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(ManagementTestNfdCommandOptions)
+
+BOOST_AUTO_TEST_CASE(Timeout)
+{
+ CommandOptions co;
+ BOOST_CHECK_EQUAL(co.getTimeout(), CommandOptions::DEFAULT_TIMEOUT);
+
+ co.setTimeout(time::milliseconds(7414));
+ BOOST_CHECK_EQUAL(co.getTimeout(), time::milliseconds(7414));
+
+ BOOST_CHECK_THROW(co.setTimeout(time::milliseconds::zero()), std::out_of_range);
+ BOOST_CHECK_THROW(co.setTimeout(time::milliseconds(-1)), std::out_of_range);
+ BOOST_CHECK_EQUAL(co.getTimeout(), time::milliseconds(7414)); // unchanged after throw
+
+ co.setTimeout(time::milliseconds(1));
+ BOOST_CHECK_EQUAL(co.getTimeout(), time::milliseconds(1));
+}
+
+BOOST_AUTO_TEST_CASE(Prefix)
+{
+ CommandOptions co;
+ BOOST_CHECK_EQUAL(co.getPrefix(), CommandOptions::DEFAULT_PREFIX);
+
+ co.setPrefix(Name()); // empty Name is okay
+ BOOST_CHECK_EQUAL(co.getPrefix(), Name());
+
+ co.setPrefix("ndn:/localhop/net/example/nfd");
+ BOOST_CHECK_EQUAL(co.getPrefix(), Name("ndn:/localhop/net/example/nfd"));
+}
+
+BOOST_AUTO_TEST_CASE(SigningParams)
+{
+ CommandOptions co;
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_DEFAULT);
+
+ co.setSigningIdentity("ndn:/tmp/identity");
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_IDENTITY);
+ BOOST_CHECK_EQUAL(co.getSigningIdentity(), Name("ndn:/tmp/identity"));
+
+ co.setSigningDefault(); // reset
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_DEFAULT);
+
+ co.setSigningIdentity(Name()); // empty Name is valid identity Name
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_IDENTITY);
+ BOOST_CHECK_EQUAL(co.getSigningIdentity(), Name());
+
+ co.setSigningDefault(); // reset
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_DEFAULT);
+
+ co.setSigningCertificate("ndn:/tmp/KEY/identity/dsk-1/ID-CERT/%FD%01");
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_CERTIFICATE);
+ BOOST_CHECK_EQUAL(co.getSigningCertificate(),
+ Name("ndn:/tmp/KEY/identity/dsk-1/ID-CERT/%FD%01"));
+
+ co.setSigningDefault(); // reset
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_DEFAULT);
+
+ co.setSigningCertificate("ndn:/KEY/dsk-1/ID-CERT/%FD%01"); // minimal certificateName
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_CERTIFICATE);
+ BOOST_CHECK_EQUAL(co.getSigningCertificate(), Name("ndn:/KEY/dsk-1/ID-CERT/%FD%01"));
+
+ co.setSigningDefault(); // reset
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(), CommandOptions::SIGNING_PARAMS_DEFAULT);
+
+ BOOST_CHECK_THROW(co.setSigningCertificate(IdentityCertificate()), // invalid certificate
+ std::invalid_argument);
+ BOOST_CHECK_EQUAL(co.getSigningParamsKind(),
+ CommandOptions::SIGNING_PARAMS_DEFAULT); // unchanged after throw
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
+} // namespace ndn