fw: instantiate strategy of next higher version

When an exact strategy version is unavailable, strategy registry now
instantiates the next higher version which can offer backwards compatibility.

All existing strategies do not support other versions.

refs #3868

Change-Id: Id2cf9ae3a1fa078ce8be815eea9396f5aa81ecbb
diff --git a/tests/daemon/fw/strategy-instantiation.t.cpp b/tests/daemon/fw/strategy-instantiation.t.cpp
new file mode 100644
index 0000000..21ab9a5
--- /dev/null
+++ b/tests/daemon/fw/strategy-instantiation.t.cpp
@@ -0,0 +1,130 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/** \file
+ *  This test suite tests instantiation logic in strategies.
+ */
+
+// All strategies, sorted alphabetically.
+#include "fw/access-strategy.hpp"
+#include "fw/asf-strategy.hpp"
+#include "fw/best-route-strategy.hpp"
+#include "fw/best-route-strategy2.hpp"
+#include "fw/client-control-strategy.hpp"
+#include "fw/multicast-strategy.hpp"
+#include "fw/ncc-strategy.hpp"
+
+#include "tests/test-common.hpp"
+#include <boost/mpl/vector.hpp>
+
+namespace nfd {
+namespace fw {
+namespace tests {
+
+using namespace nfd::tests;
+
+BOOST_AUTO_TEST_SUITE(Fw)
+BOOST_AUTO_TEST_SUITE(TestStrategyInstantiation)
+
+template<typename S, bool CanAcceptParameters, uint64_t MinVersion>
+class Test
+{
+public:
+  using Strategy = S;
+
+  static bool
+  canAcceptParameters()
+  {
+    return CanAcceptParameters;
+  }
+
+  static uint64_t
+  getMinVersion()
+  {
+    return MinVersion;
+  }
+
+  static Name
+  getVersionedStrategyName(uint64_t version)
+  {
+    return S::getStrategyName().getPrefix(-1).appendVersion(version);
+  }
+};
+
+using Tests = boost::mpl::vector<
+  Test<AccessStrategy, false, 1>,
+  Test<AsfStrategy, true, 1>,
+  Test<BestRouteStrategy, false, 1>,
+  Test<BestRouteStrategy2, false, 4>,
+  Test<ClientControlStrategy, false, 2>,
+  Test<MulticastStrategy, false, 1>,
+  Test<NccStrategy, false, 1>
+>;
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(Registration, T, Tests)
+{
+  BOOST_CHECK_EQUAL(Strategy::listRegistered().count(T::Strategy::getStrategyName()), 1);
+}
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(InstanceName, T, Tests)
+{
+  BOOST_REQUIRE(T::Strategy::getStrategyName().at(-1).isVersion());
+  uint64_t maxVersion = T::Strategy::getStrategyName().at(-1).toVersion();
+  BOOST_REQUIRE_LE(T::getMinVersion(), maxVersion);
+
+  Forwarder forwarder;
+  for (uint64_t version = T::getMinVersion(); version <= maxVersion; ++version) {
+    Name versionedName = T::getVersionedStrategyName(version);
+    unique_ptr<typename T::Strategy> instance;
+    BOOST_CHECK_NO_THROW(instance = make_unique<typename T::Strategy>(forwarder, versionedName));
+    BOOST_CHECK_EQUAL(instance->getInstanceName(), versionedName);
+
+    if (!T::canAcceptParameters()) {
+      Name nameWithParameters = Name(versionedName).append("param");
+      BOOST_CHECK_THROW(typename T::Strategy(forwarder, nameWithParameters), std::invalid_argument);
+    }
+  }
+
+  if (T::getMinVersion() > 0) {
+    Name version0Name = T::getVersionedStrategyName(0);
+    BOOST_CHECK_THROW(typename T::Strategy(forwarder, version0Name), std::invalid_argument);
+    Name earlyVersionName = T::getVersionedStrategyName(T::getMinVersion() - 1);
+    BOOST_CHECK_THROW(typename T::Strategy(forwarder, earlyVersionName), std::invalid_argument);
+  }
+
+  if (maxVersion < std::numeric_limits<uint64_t>::max()) {
+    Name versionMaxName = T::getVersionedStrategyName(std::numeric_limits<uint64_t>::max());
+    BOOST_CHECK_THROW(typename T::Strategy(forwarder, versionMaxName), std::invalid_argument);
+    Name lateVersionName = T::getVersionedStrategyName(maxVersion + 1);
+    BOOST_CHECK_THROW(typename T::Strategy(forwarder, lateVersionName), std::invalid_argument);
+  }
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestStrategyInstantiation
+BOOST_AUTO_TEST_SUITE_END() // Fw
+
+} // namespace tests
+} // namespace fw
+} // namespace nfd