PSync: initial commit

refs: #4641

Change-Id: Iabed3ad7632544d97559e6798547b7972b416784
diff --git a/tests/test-partial-producer.cpp b/tests/test-partial-producer.cpp
new file mode 100644
index 0000000..3bed1ac
--- /dev/null
+++ b/tests/test-partial-producer.cpp
@@ -0,0 +1,148 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014-2018,  The University of Memphis
+ *
+ * This file is part of PSync.
+ * See AUTHORS.md for complete list of PSync authors and contributors.
+ *
+ * PSync 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.
+ *
+ * PSync 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
+ * PSync, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ **/
+
+#include "partial-producer.hpp"
+
+#include <boost/test/unit_test.hpp>
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
+#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
+
+#include <iostream>
+
+namespace psync {
+
+using namespace ndn;
+using namespace std;
+
+BOOST_AUTO_TEST_SUITE(TestPartialProducer)
+
+BOOST_AUTO_TEST_CASE(Constructor)
+{
+  util::DummyClientFace face({true, true});
+  BOOST_REQUIRE_NO_THROW(PartialProducer(40, face, Name("/psync"), Name("/testUser")));
+}
+
+BOOST_AUTO_TEST_CASE(RegisterPrefix)
+{
+  Name syncPrefix("/psync"), userNode("/testUser");
+  util::DummyClientFace face({true, true});
+  PartialProducer producer(40, face, syncPrefix, userNode);
+
+  face.processEvents(time::milliseconds(-1));
+
+  BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
+
+  face.sentInterests.back();
+  Interest interest = *face.sentInterests.begin();
+  BOOST_CHECK_EQUAL(interest.getName().get(3), name::Component("register"));
+  name::Component test = interest.getName().get(4);
+  nfd::ControlParameters params(test.blockFromValue());
+  BOOST_CHECK_EQUAL(params.getName(), syncPrefix);
+}
+
+BOOST_AUTO_TEST_CASE(PublishName)
+{
+  Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2");
+  util::DummyClientFace face({true, true});
+  PartialProducer producer(40, face, syncPrefix, userNode);
+
+  BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0);
+  producer.publishName(userNode);
+  BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 1);
+
+  producer.publishName(userNode);
+  BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 2);
+
+  producer.publishName(userNode, 10);
+  BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 10);
+
+  producer.publishName(nonUser);
+  BOOST_CHECK_EQUAL(producer.getSeqNo(nonUser).value_or(-1), -1);
+}
+
+BOOST_AUTO_TEST_CASE(SameSyncInterest)
+{
+  Name syncPrefix("/psync"), userNode("/testUser");
+  util::DummyClientFace face({true, true});
+  PartialProducer producer(40, face, syncPrefix, userNode);
+
+  Name syncInterestName(syncPrefix);
+  syncInterestName.append("sync");
+
+  BloomFilter bf(20, 0.001);
+  bf.appendToName(syncInterestName);
+
+  producer.m_iblt.appendToName(syncInterestName);
+
+  Interest syncInterest(syncInterestName);
+  syncInterest.setInterestLifetime(time::milliseconds(1000));
+  syncInterest.setNonce(1);
+  BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, syncInterest));
+  face.processEvents(time::milliseconds(10));
+  BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
+
+  face.processEvents(time::milliseconds(500));
+
+  // Same interest again - size of pending interest should remain same, but expirationEvent should change
+  syncInterest.setNonce(2);
+  BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, syncInterest));
+  face.processEvents(time::milliseconds(10));
+  BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
+
+  face.processEvents(time::milliseconds(500));
+  BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
+
+  face.processEvents(time::milliseconds(500));
+  BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 0);
+}
+
+BOOST_AUTO_TEST_CASE(OnSyncInterest)
+{
+  Name syncPrefix("/psync"), userNode("/testUser");
+  util::DummyClientFace face({true, true});
+  PartialProducer producer(40, face, syncPrefix, userNode);
+
+  // Sync interest with no bloom filter attached
+  Name syncInterestName(syncPrefix);
+  syncInterestName.append("sync");
+  producer.m_iblt.appendToName(syncInterestName);
+  BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
+
+  // Sync interest with malicious bloom filter
+  syncInterestName = syncPrefix;
+  syncInterestName.append("sync");
+  syncInterestName.appendNumber(20); // count of bloom filter
+  syncInterestName.appendNumber(1);  // false positive probability * 1000 of bloom filter
+  syncInterestName.append("fake-name");
+  producer.m_iblt.appendToName(syncInterestName);
+  BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
+
+  // Sync interest with malicious IBF
+  syncInterestName = syncPrefix;
+  syncInterestName.append("sync");
+  BloomFilter bf(20, 0.001);
+  bf.appendToName(syncInterestName);
+  syncInterestName.append("fake-name");
+  BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace psync
\ No newline at end of file