Add Update Handler class
Change-Id: I465297bfa3b8c4c8e6e7f7cd028b2d4afeb4e768
Refs: #3598
diff --git a/tests/unit-tests/update-handler.t.cpp b/tests/unit-tests/update-handler.t.cpp
new file mode 100644
index 0000000..22624b6
--- /dev/null
+++ b/tests/unit-tests/update-handler.t.cpp
@@ -0,0 +1,277 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+* Copyright (c) 2016 Regents of the University of California.
+*
+* This file is part of the nTorrent codebase.
+*
+* nTorrent is free software: you can redistribute it and/or modify it under the
+* terms of the GNU Lesser General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later version.
+*
+* nTorrent 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 Lesser General Public License for more details.
+*
+* You should have received copies of the GNU General Public License and GNU Lesser
+* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
+* <http://www.gnu.org/licenses/>.
+*
+* See AUTHORS for complete list of nTorrent authors and contributors.
+*/
+
+#include "boost-test.hpp"
+#include "update-handler.hpp"
+#include "unit-test-time-fixture.hpp"
+#include "dummy-parser-fixture.hpp"
+
+#include <ndn-cxx/name.hpp>
+#include <ndn-cxx/util/dummy-client-face.hpp>
+#include <ndn-cxx/util/io.hpp>
+
+namespace ndn {
+namespace ntorrent {
+namespace tests {
+
+using util::DummyClientFace;
+using std::vector;
+
+class TestUpdateHandler : public UpdateHandler {
+public:
+ TestUpdateHandler(Name torrentName, shared_ptr<KeyChain> keyChain,
+ shared_ptr<StatsTable> statsTable, shared_ptr<Face> face)
+ : UpdateHandler(torrentName, keyChain, statsTable, face)
+ {
+ }
+
+ ~TestUpdateHandler()
+ {
+ }
+
+ Name
+ getOwnRoutablePrefix()
+ {
+ return UpdateHandler::getOwnRoutablePrefix();
+ }
+};
+
+class FaceFixture : public UnitTestTimeFixture
+{
+public:
+ explicit
+ FaceFixture()
+ : face1(util::makeDummyClientFace(io, { true, true }))
+ , face2(util::makeDummyClientFace(io, { true, true }))
+ {
+ }
+
+public:
+ shared_ptr<DummyClientFace> face1;
+ shared_ptr<DummyClientFace> face2;
+};
+
+BOOST_FIXTURE_TEST_SUITE(TestUpdateHandlerClass, FaceFixture)
+
+BOOST_AUTO_TEST_CASE(TestInitialSetup)
+{
+ shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
+
+ shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
+
+ TestUpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
+ advanceClocks(time::milliseconds(1), 10);
+ // Create a data packet containing one name as content
+ shared_ptr<Data> d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+ { Name("ucla") });
+ keyChain->sign(*d);
+ face1->receive(*d);
+
+ BOOST_CHECK_EQUAL(handler1.getOwnRoutablePrefix().toUri(), "/ucla");
+
+ shared_ptr<StatsTable> table2 = make_shared<StatsTable>(Name("linux15.01"));
+ TestUpdateHandler handler2(Name("linux15.01"), keyChain, table2, face2);
+ advanceClocks(time::milliseconds(1), 10);
+ // Create a data packet containing one name as content
+ d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+ { Name("arizona") });
+ keyChain->sign(*d);
+ face2->receive(*d);
+
+ BOOST_CHECK_EQUAL(handler2.getOwnRoutablePrefix().toUri(), "/arizona");
+}
+
+BOOST_AUTO_TEST_CASE(TestAliveInterestExchange)
+{
+ shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
+ table1->insert(Name("isp1"));
+ table1->insert(Name("isp2"));
+ table1->insert(Name("isp3"));
+
+ shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
+
+ TestUpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
+ advanceClocks(time::milliseconds(1), 10);
+ // Create a data packet containing one name as content
+ shared_ptr<Data> d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+ { Name("ucla") });
+ keyChain->sign(*d);
+ face1->receive(*d);
+
+ shared_ptr<StatsTable> table2 = make_shared<StatsTable>(Name("linux15.01"));
+ table2->insert(Name("ucla"));
+ TestUpdateHandler handler2(Name("linux15.01"), keyChain, table2, face2);
+ advanceClocks(time::milliseconds(1), 10);
+ // Create a data packet containing one name as content
+ d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
+ { Name("arizona") });
+ keyChain->sign(*d);
+ face2->receive(*d);
+
+ handler2.sendAliveInterest(table2->begin());
+
+ advanceClocks(time::milliseconds(1), 40);
+ Interest interest(Name("/NTORRENT/linux15.01/ALIVE/test"));
+ face1->receive(interest);
+
+ advanceClocks(time::milliseconds(1), 10);
+ std::vector<Data> dataVec = face1->sentData;
+
+ BOOST_CHECK_EQUAL(dataVec.size(), 1);
+ BOOST_CHECK_EQUAL(dataVec[0].getName().toUri(), "/NTORRENT/linux15.01/ALIVE/test");
+
+ auto block = dataVec[0].getContent();
+ shared_ptr<vector<Name>> nameVec = DummyParser::decodeContent(block);
+ auto it = nameVec->begin();
+
+ BOOST_CHECK_EQUAL(it->toUri(), "/test");
+ ++it;
+ BOOST_CHECK_EQUAL(it->toUri(), "/isp3");
+ ++it;
+ BOOST_CHECK_EQUAL(it->toUri(), "/isp2");
+ ++it;
+ BOOST_CHECK_EQUAL(it->toUri(), "/isp1");
+
+ BOOST_CHECK_EQUAL((table1->end() - 1)->getRecordName().toUri(), "/test");
+
+ d = DummyParser::createDataPacket(Name("/NTORRENT/linux15.01/ALIVE/arizona"),
+ { Name("isp1"), Name("isp2"), Name("isp3") });
+ keyChain->sign(*d);
+
+ advanceClocks(time::milliseconds(1), 40);
+ face2->receive(*d);
+
+ auto i = table2->begin();
+ ++i;
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
+ ++i;
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
+ ++i;
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
+
+ table1->erase(Name("isp1"));
+ table1->erase(Name("isp2"));
+
+ table1->insert(Name("isp4"));
+ table1->insert(Name("isp5"));
+ table1->insert(Name("isp6"));
+ table1->insert(Name("isp7"));
+ table1->insert(Name("isp8"));
+ table1->insert(Name("isp9"));
+
+ handler2.sendAliveInterest(table2->begin());
+
+ advanceClocks(time::milliseconds(1), 40);
+ Interest interest2(Name("/NTORRENT/linux15.01/ALIVE/arizona"));
+ face1->receive(interest2);
+
+ advanceClocks(time::milliseconds(1), 10);
+
+ dataVec = face1->sentData;
+ BOOST_CHECK_EQUAL(dataVec.size(), 2);
+
+ auto iter = dataVec.begin() + 1;
+ advanceClocks(time::milliseconds(1), 30);
+ face2->receive(*iter);
+
+ i = table2->begin();
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/ucla");
+ BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+ ++i;
+
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
+ BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+ ++i;
+
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
+ BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+ ++i;
+
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
+ BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+ ++i;
+
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/test");
+ BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+ ++i;
+
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp4");
+ BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+ ++i;
+
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp5");
+ BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+ ++i;
+
+ BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp6");
+ BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
+ BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
+ ++i;
+
+ BOOST_CHECK(i == table2->end());
+}
+
+BOOST_AUTO_TEST_CASE(TestNeedsUpdate)
+{
+ shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
+ table1->insert(Name("isp1"));
+ table1->insert(Name("isp2"));
+ table1->insert(Name("isp3"));
+
+ shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
+
+ UpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
+
+ BOOST_CHECK(handler1.needsUpdate());
+
+ auto i = table1->begin() + 1;
+ i->incrementSentInterests();
+ i->incrementSentInterests();
+ i->incrementReceivedData();
+
+ BOOST_CHECK(handler1.needsUpdate());
+
+ table1->insert(Name("isp4"));
+ table1->insert(Name("isp5"));
+
+ BOOST_CHECK(!handler1.needsUpdate());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace ntorrent
+} // namespace ndn