tools: modularize ndn-autoconfig-server

refs #4158

Change-Id: I90eb570e2ca2ae611d48aeb1e456712c1d148531
diff --git a/tests/tools/ndn-autoconfig-server/program.t.cpp b/tests/tools/ndn-autoconfig-server/program.t.cpp
new file mode 100644
index 0000000..afcf908
--- /dev/null
+++ b/tests/tools/ndn-autoconfig-server/program.t.cpp
@@ -0,0 +1,146 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2017,  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/>.
+ */
+
+#include "ndn-autoconfig-server/program.hpp"
+
+#include "tests/identity-management-fixture.hpp"
+#include <ndn-cxx/encoding/buffer.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
+
+namespace ndn {
+namespace tools {
+namespace autoconfig_server {
+namespace tests {
+
+using namespace ::nfd::tests;
+
+class AutoconfigServerFixture : public IdentityManagementFixture
+{
+public:
+  AutoconfigServerFixture()
+    : face({true, true})
+  {
+  }
+
+  void
+  initialize(const Options& options)
+  {
+    program = make_unique<Program>(options, face, m_keyChain);
+    face.processEvents(time::milliseconds(1));
+    face.sentInterests.clear();
+  }
+
+public:
+  util::DummyClientFace face;
+  unique_ptr<Program> program;
+};
+
+BOOST_AUTO_TEST_SUITE(NdnAutoconfigServer)
+BOOST_FIXTURE_TEST_SUITE(TestProgram, AutoconfigServerFixture)
+
+BOOST_AUTO_TEST_CASE(HubData)
+{
+  Options options;
+  options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363");
+  this->initialize(options);
+
+  Interest interest("/localhop/ndn-autoconf/hub");
+  face.receive(interest);
+  face.processEvents(time::milliseconds(1));
+
+  BOOST_CHECK_EQUAL(face.sentData.size(), 1);
+  const Name& dataName = face.sentData.at(0).getName();
+  BOOST_CHECK_EQUAL(dataName.size(), interest.getName().size() + 1);
+  BOOST_CHECK(interest.getName().isPrefixOf(dataName));
+  BOOST_CHECK(dataName.at(-1).isVersion());
+
+  // interest2 asks for a different version, and should not be responded
+  Interest interest2(Name(interest.getName()).appendVersion(dataName.at(-1).toVersion() - 1));
+  face.receive(interest2);
+  face.processEvents(time::milliseconds(1));
+  BOOST_CHECK_EQUAL(face.sentData.size(), 1);
+}
+
+BOOST_AUTO_TEST_CASE(RoutablePrefixesDataset)
+{
+  Options options;
+  options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363");
+  const int nRoutablePrefixes = 2000;
+  for (int i = 0; i < nRoutablePrefixes; ++i) {
+    options.routablePrefixes.push_back(Name("/PREFIX").appendNumber(i));
+  }
+  this->initialize(options);
+
+  Interest interest("/localhop/nfd/rib/routable-prefixes");
+  face.receive(interest);
+  face.processEvents(time::milliseconds(1));
+
+  BOOST_CHECK_EQUAL(face.sentData.size(), 1);
+  const Name& dataName0 = face.sentData.at(0).getName();
+  BOOST_CHECK_EQUAL(dataName0.size(), interest.getName().size() + 2);
+  BOOST_CHECK(interest.getName().isPrefixOf(dataName0));
+  BOOST_CHECK(dataName0.at(-2).isVersion());
+  BOOST_CHECK(dataName0.at(-1).isSegment());
+
+  while (face.sentData.back().getFinalBlockId() != face.sentData.back().getName().at(-1)) {
+    const Name& lastName = face.sentData.back().getName();
+    Interest interest2(lastName.getPrefix(-1).appendSegment(lastName.at(-1).toSegment() + 1));
+    face.receive(interest2);
+    face.processEvents(time::milliseconds(1));
+  }
+
+  auto buffer = make_shared<Buffer>();
+  for (const Data& data : face.sentData) {
+    const Block& content = data.getContent();
+    std::copy(content.value_begin(), content.value_end(), std::back_inserter(*buffer));
+  }
+
+  Block payload(tlv::Content, buffer);
+  payload.parse();
+  BOOST_CHECK_EQUAL(payload.elements_size(), nRoutablePrefixes);
+  for (int i = 0; i < nRoutablePrefixes; ++i) {
+    BOOST_CHECK_EQUAL(Name(payload.elements().at(i)), options.routablePrefixes.at(i));
+  }
+}
+
+BOOST_AUTO_TEST_CASE(RoutablePrefixesDisabled)
+{
+  Options options;
+  options.hubFaceUri = FaceUri("udp4://192.0.2.1:6363");
+  this->initialize(options);
+
+  Interest interest("/localhop/nfd/rib/routable-prefixes");
+  face.receive(interest);
+  face.processEvents(time::milliseconds(1));
+  BOOST_CHECK_EQUAL(face.sentData.size(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestProgram
+BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfigServer
+
+} // namespace tests
+} // namespace autoconfig_server
+} // namespace tools
+} // namespace ndn