read: auto-register prefixes for inserted data

Change-Id: Iebddca056a4c74f179f7af7203881adfe1cba777
refs: #4247
diff --git a/tests/unit/read-handle.t.cpp b/tests/unit/read-handle.t.cpp
new file mode 100644
index 0000000..cb9d233
--- /dev/null
+++ b/tests/unit/read-handle.t.cpp
@@ -0,0 +1,138 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2017,  Regents of the University of California.
+ *
+ * This file is part of NDN repo-ng (Next generation of NDN repository).
+ * See AUTHORS.md for complete list of repo-ng authors and contributors.
+ *
+ * repo-ng 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.
+ *
+ * repo-ng 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
+ * repo-ng, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "handles/read-handle.hpp"
+#include "storage/sqlite-storage.hpp"
+#include "storage/repo-storage.hpp"
+
+#include "../repo-storage-fixture.hpp"
+
+#include <boost/test/unit_test.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
+
+#define CHECK_INTERESTS(NAME,COMPONENT,BOOL)                  \
+  do {                                                        \
+    didMatch = false;                                         \
+    for (const auto interest : face.sentInterests) {          \
+      bool isPresent = false;                                 \
+      for (const auto section : NAME) {                       \
+        isPresent = isPresent || section == COMPONENT;        \
+      }                                                       \
+      didMatch = didMatch || isPresent;                       \
+    }                                                         \
+    BOOST_CHECK_EQUAL(didMatch,BOOL);                         \
+  } while (0)
+
+namespace repo {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(TestReadHandle)
+
+class Fixture : public RepoStorageFixture
+{
+public:
+  Fixture()
+    : face(ndn::util::DummyClientFace::Options{true, true})
+    , scheduler(face.getIoService())
+    , subsetLength(1)
+    , dataPrefix("/ndn/test/prefix")
+    , identity("/ndn/test/identity")
+    , readHandle(face, *handle, keyChain, scheduler, subsetLength)
+    , numPrefixRegistrations(0)
+    , numPrefixUnregistrations(0)
+  {
+    readHandle.connectAutoListen();
+  }
+
+public:
+  bool
+  containsNameComponent(const Name& name, const ndn::name::Component& component)
+  {
+    bool isPresent = false;
+    for (const auto section : name) {
+      isPresent = isPresent || section == component;
+    }
+    return isPresent;
+  }
+
+public:
+  ndn::util::DummyClientFace face;
+  ndn::KeyChain keyChain;
+  ndn::Scheduler scheduler;
+
+  size_t subsetLength;
+  ndn::Name dataPrefix;
+  ndn::Name identity;
+  ReadHandle readHandle;
+
+  size_t numPrefixRegistrations;
+  size_t numPrefixUnregistrations;
+};
+
+BOOST_FIXTURE_TEST_CASE(DataPrefixes, Fixture)
+{
+  const std::vector<uint8_t> content(100, 'x');
+  std::shared_ptr<Data> data1 = std::make_shared<Data>(Name{dataPrefix}.appendNumber(1));
+  std::shared_ptr<Data> data2 = std::make_shared<Data>(Name{dataPrefix}.appendNumber(2));
+
+  data1->setContent(&content[0], content.size());
+  data2->setContent(&content[0], content.size());
+
+  keyChain.createIdentity(identity);
+  keyChain.sign(*data1, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
+                                                  identity));
+  keyChain.sign(*data2, ndn::security::SigningInfo(ndn::security::SigningInfo::SIGNER_TYPE_ID,
+                                                  identity));
+
+  bool didMatch = false;
+  face.sentInterests.clear();
+  handle->insertData(*data1);
+  face.processEvents(ndn::time::milliseconds(-1));
+  CHECK_INTERESTS(interest.getName(), name::Component{"register"}, true);
+
+  face.sentInterests.clear();
+  handle->deleteData(data1->getFullName());
+  face.processEvents(ndn::time::milliseconds(-1));
+  CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, true);
+
+  face.sentInterests.clear();
+  handle->insertData(*data1);
+  face.processEvents(ndn::time::milliseconds(-1));
+  CHECK_INTERESTS(interest.getName(), name::Component{"register"}, true);
+
+  face.sentInterests.clear();
+  handle->insertData(*data2);
+  face.processEvents(ndn::time::milliseconds(-1));
+  CHECK_INTERESTS(interest.getName(), name::Component{"register"}, false);
+
+  face.sentInterests.clear();
+  handle->deleteData(data1->getFullName());
+  face.processEvents(ndn::time::milliseconds(-1));
+  CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, false);
+
+  face.sentInterests.clear();
+  handle->deleteData(data2->getFullName());
+  face.processEvents(ndn::time::milliseconds(-1));
+  CHECK_INTERESTS(interest.getName(), name::Component{"unregister"}, true);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestReadHandle
+
+} // namespace tests
+} // namespace repo