chunks: segmented file transfer

refs #3071

Change-Id: I88e4fc1a8e33a0d61a95e2291cccc7b998647489
diff --git a/tests/chunks/discover-version-iterative.t.cpp b/tests/chunks/discover-version-iterative.t.cpp
new file mode 100644
index 0000000..233f36c
--- /dev/null
+++ b/tests/chunks/discover-version-iterative.t.cpp
@@ -0,0 +1,235 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2016,  Regents of the University of California,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools 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.
+ *
+ * ndn-tools 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
+ * ndn-tools, 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.
+ *
+ * @author Andrea Tosatto
+ */
+
+#include "tools/chunks/catchunks/discover-version-iterative.hpp"
+
+#include "discover-version-fixture.hpp"
+
+namespace ndn {
+namespace chunks {
+namespace tests {
+
+using namespace ndn::tests;
+
+class DiscoverVersionIterativeFixture : public DiscoverVersionFixture,
+                                        protected DiscoverVersionIterativeOptions
+{
+public:
+  typedef DiscoverVersionIterativeOptions Options;
+
+public:
+  explicit
+  DiscoverVersionIterativeFixture(const Options& opt = makeOptionsIterative())
+    : chunks::Options(opt)
+    , DiscoverVersionFixture(opt)
+    , Options(opt)
+  {
+    setDiscover(make_unique<DiscoverVersionIterative>(Name(name), face, opt));
+  }
+
+protected:
+  static Options
+  makeOptionsIterative()
+  {
+    Options options;
+    options.isVerbose = false;
+    options.maxRetriesOnTimeoutOrNack = 3;
+    options.maxRetriesAfterVersionFound = 1;
+    return options;
+  }
+};
+
+
+BOOST_AUTO_TEST_SUITE(Chunks)
+BOOST_AUTO_TEST_SUITE(TestDiscoverVersionIterative)
+
+BOOST_FIXTURE_TEST_CASE(SingleVersionAvailable, DiscoverVersionIterativeFixture)
+{
+  discover->run();
+  advanceClocks(io, time::nanoseconds(1), 1);
+
+  uint64_t version = 1449241767037;
+
+  BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
+  auto lastInterest = face.sentInterests.back();
+  BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0);
+  BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+  BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+  BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+
+  // Send first segment for the right version
+  face.receive(*makeDataWithVersion(version));
+  advanceClocks(io, time::nanoseconds(1), 1);
+
+  BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
+  lastInterest = face.sentInterests.back();
+  Exclude expectedExclude;
+  expectedExclude.excludeBefore(name::Component::fromVersion(version));
+  BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
+  BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+  BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+  BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+
+  // Generate the timeout
+  for (int retries = 0; retries < maxRetriesAfterVersionFound; ++retries) {
+    advanceClocks(io, interestLifetime, 1);
+
+    BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
+
+    BOOST_REQUIRE_EQUAL(face.sentInterests.size(), retries + 3);
+    lastInterest = face.sentInterests.back();
+    Exclude expectedExclude;
+    expectedExclude.excludeBefore(name::Component::fromVersion(version));
+    BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
+    BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+    BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+    BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+  }
+
+  advanceClocks(io, interestLifetime, 1);
+  BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
+  BOOST_CHECK_EQUAL(discoveredVersion, version);
+  BOOST_CHECK_EQUAL(face.sentInterests.size(), maxRetriesAfterVersionFound + 2);
+}
+
+
+BOOST_FIXTURE_TEST_CASE(NoVersionsAvailable, DiscoverVersionIterativeFixture)
+{
+  discover->run();
+  advanceClocks(io, time::nanoseconds(1), 1);
+  BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
+
+  for (int retries = 0; retries < maxRetriesOnTimeoutOrNack; ++retries) {
+    advanceClocks(io, interestLifetime, 1);
+    BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
+    BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2 + retries);
+  }
+
+  for (auto& lastInterest : face.sentInterests) {
+    BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0);
+    BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+    BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+    BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+  }
+
+  advanceClocks(io, interestLifetime, 1);
+  BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
+  BOOST_CHECK_EQUAL(face.sentInterests.size(), maxRetriesOnTimeoutOrNack + 1);
+}
+
+BOOST_FIXTURE_TEST_CASE(MultipleVersionsAvailable, DiscoverVersionIterativeFixture)
+{
+  // nVersions must be positive
+  const uint64_t nVersions = 5;
+
+  discover->run();
+  advanceClocks(io, time::nanoseconds(1), 1);
+
+  BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
+  auto lastInterest = face.sentInterests.back();
+  BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0);
+  BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+  BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+  BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+
+  for (uint64_t nSentVersions = 0; nSentVersions < nVersions; ++nSentVersions) {
+    face.receive(*makeDataWithVersion(nSentVersions));
+    advanceClocks(io, time::nanoseconds(1), 1);
+
+    BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2 + nSentVersions);
+    lastInterest = face.sentInterests.back();
+    Exclude expectedExclude;
+    expectedExclude.excludeBefore(name::Component::fromVersion(nSentVersions));
+    BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
+    BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+    BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+    BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+  }
+
+  for (int retries = 0; retries < maxRetriesAfterVersionFound; ++retries) {
+    advanceClocks(io, interestLifetime, 1);
+
+    BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
+
+    BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2 + retries + nVersions);
+    lastInterest = face.sentInterests.back();
+    Exclude expectedExclude;
+    expectedExclude.excludeBefore(name::Component::fromVersion(nVersions - 1));
+    BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
+    BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+    BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+    BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+  }
+
+  advanceClocks(io, interestLifetime, 1);
+  BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
+  BOOST_CHECK_EQUAL(discoveredVersion, nVersions - 1);
+  BOOST_CHECK_EQUAL(face.sentInterests.size(), nVersions + maxRetriesAfterVersionFound + 1);
+}
+
+BOOST_FIXTURE_TEST_CASE(MultipleVersionsAvailableDescendent, DiscoverVersionIterativeFixture)
+{
+  // nVersions must be positive
+  const uint64_t nVersions = 5;
+
+  discover->run();
+  advanceClocks(io, time::nanoseconds(1), 1);
+
+  BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
+  auto lastInterest = face.sentInterests.back();
+  BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
+  BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0);
+  BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+  BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+  BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+
+  for (uint64_t nVersionsToSend = nVersions; nVersionsToSend > 0; --nVersionsToSend) {
+    face.receive(*makeDataWithVersion(nVersionsToSend - 1));
+    advanceClocks(io, time::nanoseconds(1), 1);
+
+    BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
+
+    BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
+    lastInterest = face.sentInterests.back();
+    Exclude expectedExclude;
+    expectedExclude.excludeBefore(name::Component::fromVersion(nVersions - 1));
+    BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
+    BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
+    BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
+    BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
+  }
+
+  advanceClocks(io, interestLifetime, maxRetriesAfterVersionFound + 1);
+  BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
+  BOOST_CHECK_EQUAL(discoveredVersion, nVersions - 1);
+  BOOST_CHECK_EQUAL(face.sentInterests.size(), maxRetriesAfterVersionFound + 2);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestDiscoverVersionIterative
+BOOST_AUTO_TEST_SUITE_END() // Chunks
+
+} // namespace tests
+} // namespace chunks
+} // namespace ndn