From c45a4eaef4d5f164fd925557afc1536835eded7c Mon Sep 17 00:00:00 2001 From: Davide Pesavento Date: Mon, 19 Sep 2022 02:10:53 -0400 Subject: [PATCH] **breaking** Add KeyChain parameter to the producer API Applications must be able to specify the KeyChain instance to use Change-Id: If5054dbb8c00e2d18af8bf6a9199ded7266688f3 --- PSync/full-producer.cpp | 9 ++++--- PSync/full-producer.hpp | 14 +++++----- PSync/partial-producer.cpp | 11 ++++---- PSync/partial-producer.hpp | 14 +++++----- PSync/producer-base.cpp | 16 +++++++----- PSync/producer-base.hpp | 45 ++++++++++++++++---------------- examples/full-sync.cpp | 12 ++++----- examples/producer.cpp | 14 +++++----- tests/clock-fixture.cpp | 8 +++--- tests/clock-fixture.hpp | 10 ++++--- tests/io-fixture.hpp | 4 +-- tests/key-chain-fixture.hpp | 37 ++++++++++++++++++++++++++ tests/test-full-producer.cpp | 33 ++++++++++------------- tests/test-full-sync.cpp | 14 +++++----- tests/test-partial-producer.cpp | 44 ++++++++++++++----------------- tests/test-partial-sync.cpp | 17 ++++++------ tests/test-producer-base.cpp | 31 ++++++++++------------ tests/test-segment-publisher.cpp | 36 ++++++++++++------------- 18 files changed, 201 insertions(+), 168 deletions(-) create mode 100644 tests/key-chain-fixture.hpp diff --git a/PSync/full-producer.cpp b/PSync/full-producer.cpp index d899965..b5cf256 100644 --- a/PSync/full-producer.cpp +++ b/PSync/full-producer.cpp @@ -32,8 +32,9 @@ namespace psync { NDN_LOG_INIT(psync.FullProducer); -FullProducer::FullProducer(const size_t expectedNumEntries, - ndn::Face& face, +FullProducer::FullProducer(ndn::Face& face, + ndn::KeyChain& keyChain, + size_t expectedNumEntries, const ndn::Name& syncPrefix, const ndn::Name& userPrefix, const UpdateCallback& onUpdateCallBack, @@ -41,8 +42,8 @@ FullProducer::FullProducer(const size_t expectedNumEntries, ndn::time::milliseconds syncReplyFreshness, CompressionScheme ibltCompression, CompressionScheme contentCompression) - : ProducerBase(expectedNumEntries, face, syncPrefix, userPrefix, syncReplyFreshness, - ibltCompression, contentCompression) + : ProducerBase(face, keyChain, expectedNumEntries, syncPrefix, userPrefix, + syncReplyFreshness, ibltCompression, contentCompression) , m_syncInterestLifetime(syncInterestLifetime) , m_onUpdate(onUpdateCallBack) , m_jitter(100, 500) diff --git a/PSync/full-producer.hpp b/PSync/full-producer.hpp index d660b66..205a89c 100644 --- a/PSync/full-producer.hpp +++ b/PSync/full-producer.hpp @@ -42,12 +42,13 @@ class FullProducer : public ProducerBase { public: /** - * @brief constructor + * @brief Constructor * - * Registers syncPrefix in NFD and sends a sync interest + * Registers syncPrefix in NFD and sends a sync interest. * - * @param expectedNumEntries expected entries in IBF - * @param face application's face + * @param face Application's face + * @param keyChain KeyChain instance to use for signing + * @param expectedNumEntries Expected number of entries in IBF * @param syncPrefix The prefix of the sync group * @param userPrefix The prefix of the first user in the group * @param onUpdateCallBack The call back to be called when there is new data @@ -56,8 +57,9 @@ public: * @param ibltCompression Compression scheme to use for IBF * @param contentCompression Compression scheme to use for Data content */ - FullProducer(size_t expectedNumEntries, - ndn::Face& face, + FullProducer(ndn::Face& face, + ndn::KeyChain& keyChain, + size_t expectedNumEntries, const ndn::Name& syncPrefix, const ndn::Name& userPrefix, const UpdateCallback& onUpdateCallBack, diff --git a/PSync/partial-producer.cpp b/PSync/partial-producer.cpp index 5b02903..b76658a 100644 --- a/PSync/partial-producer.cpp +++ b/PSync/partial-producer.cpp @@ -32,16 +32,17 @@ NDN_LOG_INIT(psync.PartialProducer); const ndn::name::Component HELLO{"hello"}; const ndn::name::Component SYNC{"sync"}; -PartialProducer::PartialProducer(size_t expectedNumEntries, - ndn::Face& face, +PartialProducer::PartialProducer(ndn::Face& face, + ndn::KeyChain& keyChain, + size_t expectedNumEntries, const ndn::Name& syncPrefix, const ndn::Name& userPrefix, ndn::time::milliseconds helloReplyFreshness, ndn::time::milliseconds syncReplyFreshness, CompressionScheme ibltCompression) - : ProducerBase(expectedNumEntries, face, syncPrefix, - userPrefix, syncReplyFreshness, ibltCompression) - , m_helloReplyFreshness(helloReplyFreshness) + : ProducerBase(face, keyChain, expectedNumEntries, syncPrefix, userPrefix, + syncReplyFreshness, ibltCompression, CompressionScheme::NONE) + , m_helloReplyFreshness(helloReplyFreshness) { m_registeredPrefix = m_face.registerPrefix(m_syncPrefix, [this] (const ndn::Name& syncPrefix) { diff --git a/PSync/partial-producer.hpp b/PSync/partial-producer.hpp index 48ff01e..0a29cb1 100644 --- a/PSync/partial-producer.hpp +++ b/PSync/partial-producer.hpp @@ -43,21 +43,23 @@ class PartialProducer : public ProducerBase { public: /** - * @brief constructor + * @brief Constructor * * Registers syncPrefix in NFD and sets internal filters for - * "sync" and "hello" under syncPrefix + * "sync" and "hello" under syncPrefix. * - * @param expectedNumEntries expected entries in IBF - * @param face application's face + * @param face Application's face + * @param keyChain KeyChain instance to use for signing + * @param expectedNumEntries Expected number of entries in IBF * @param syncPrefix The prefix of the sync group * @param userPrefix The prefix of the first user in the group * @param syncReplyFreshness freshness of sync data * @param helloReplyFreshness freshness of hello data * @param ibltCompression Compression scheme to use for IBF */ - PartialProducer(size_t expectedNumEntries, - ndn::Face& face, + PartialProducer(ndn::Face& face, + ndn::KeyChain& keyChain, + size_t expectedNumEntries, const ndn::Name& syncPrefix, const ndn::Name& userPrefix, ndn::time::milliseconds helloReplyFreshness = HELLO_REPLY_FRESHNESS, diff --git a/PSync/producer-base.cpp b/PSync/producer-base.cpp index ff6870d..d350a69 100644 --- a/PSync/producer-base.cpp +++ b/PSync/producer-base.cpp @@ -30,23 +30,25 @@ namespace psync { NDN_LOG_INIT(psync.ProducerBase); -ProducerBase::ProducerBase(size_t expectedNumEntries, - ndn::Face& face, +ProducerBase::ProducerBase(ndn::Face& face, + ndn::KeyChain& keyChain, + size_t expectedNumEntries, const ndn::Name& syncPrefix, const ndn::Name& userPrefix, ndn::time::milliseconds syncReplyFreshness, CompressionScheme ibltCompression, CompressionScheme contentCompression) - : m_iblt(expectedNumEntries, ibltCompression) + : m_face(face) + , m_keyChain(keyChain) + , m_scheduler(m_face.getIoService()) + , m_rng(ndn::random::getRandomNumberEngine()) + , m_iblt(expectedNumEntries, ibltCompression) + , m_segmentPublisher(m_face, m_keyChain) , m_expectedNumEntries(expectedNumEntries) , m_threshold(expectedNumEntries / 2) - , m_face(face) - , m_scheduler(m_face.getIoService()) , m_syncPrefix(syncPrefix) , m_userPrefix(userPrefix) , m_syncReplyFreshness(syncReplyFreshness) - , m_segmentPublisher(m_face, m_keyChain) - , m_rng(ndn::random::getRandomNumberEngine()) , m_ibltCompression(ibltCompression) , m_contentCompression(contentCompression) { diff --git a/PSync/producer-base.hpp b/PSync/producer-base.hpp index 29e682d..e810111 100644 --- a/PSync/producer-base.hpp +++ b/PSync/producer-base.hpp @@ -56,23 +56,26 @@ public: PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED: /** - * @brief constructor + * @brief Constructor * - * @param expectedNumEntries expected number entries in IBF - * @param face application's face + * @param face Application's face + * @param keyChain KeyChain instance to use for signing + * @param expectedNumEntries Expected number of entries in IBF * @param syncPrefix The prefix of the sync group * @param userPrefix The prefix of the first user in the group - * @param syncReplyFreshness freshness of sync data + * @param syncReplyFreshness FreshnessPeriod of sync data * @param ibltCompression Compression scheme to use for IBF * @param contentCompression Compression scheme to use for Data content */ - ProducerBase(size_t expectedNumEntries, - ndn::Face& face, + ProducerBase(ndn::Face& face, + ndn::KeyChain& keyChain, + size_t expectedNumEntries, const ndn::Name& syncPrefix, const ndn::Name& userPrefix, ndn::time::milliseconds syncReplyFreshness = SYNC_REPLY_FRESHNESS, CompressionScheme ibltCompression = CompressionScheme::NONE, CompressionScheme contentCompression = CompressionScheme::NONE); + public: /** * @brief Returns the current sequence number of the given prefix @@ -152,11 +155,12 @@ PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED: onRegisterFailed(const ndn::Name& prefix, const std::string& msg) const; PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED: + ndn::Face& m_face; + ndn::KeyChain& m_keyChain; + ndn::Scheduler m_scheduler; + ndn::random::RandomNumberEngine& m_rng; + detail::IBLT m_iblt; - uint32_t m_expectedNumEntries; - // Threshold is used check if the differences are greater - // than it and whether we need to update the other side. - uint32_t m_threshold; // prefix and sequence number std::map m_prefixes; @@ -165,20 +169,17 @@ PSYNC_PUBLIC_WITH_TESTS_ELSE_PROTECTED: bm::unordered_set_of>>; HashNameBiMap m_biMap; - ndn::Face& m_face; - ndn::KeyChain m_keyChain; - ndn::Scheduler m_scheduler; - - ndn::Name m_syncPrefix; - ndn::Name m_userPrefix; - - ndn::time::milliseconds m_syncReplyFreshness; - SegmentPublisher m_segmentPublisher; - ndn::random::RandomNumberEngine& m_rng; - CompressionScheme m_ibltCompression; - CompressionScheme m_contentCompression; + const size_t m_expectedNumEntries; + // Threshold is used check if the differences are greater + // than it and whether we need to update the other side. + const size_t m_threshold; + const ndn::Name m_syncPrefix; + const ndn::Name m_userPrefix; + const ndn::time::milliseconds m_syncReplyFreshness; + const CompressionScheme m_ibltCompression; + const CompressionScheme m_contentCompression; }; } // namespace psync diff --git a/examples/full-sync.cpp b/examples/full-sync.cpp index 01da1dd..3ed9814 100644 --- a/examples/full-sync.cpp +++ b/examples/full-sync.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -42,17 +43,15 @@ public: */ Producer(const ndn::Name& syncPrefix, const std::string& userPrefix, int numDataStreams, int maxNumPublish) - : m_scheduler(m_face.getIoService()) - , m_fullProducer(80, m_face, syncPrefix, userPrefix, + : m_fullProducer(m_face, m_keyChain, 80, syncPrefix, userPrefix, std::bind(&Producer::processSyncUpdate, this, _1), 1600_ms, 1600_ms) - , m_numDataStreams(numDataStreams) , m_maxNumPublish(maxNumPublish) , m_rng(ndn::random::getRandomNumberEngine()) , m_rangeUniformRandom(0, 60000) { // Add user prefixes and schedule updates for them in specified interval - for (int i = 0; i < m_numDataStreams; i++) { + for (int i = 0; i < numDataStreams; i++) { ndn::Name prefix(userPrefix + "-" + std::to_string(i)); m_fullProducer.addUserNode(prefix); m_scheduler.schedule(ndn::time::milliseconds(m_rangeUniformRandom(m_rng)), @@ -93,11 +92,10 @@ private: private: ndn::Face m_face; - ndn::Scheduler m_scheduler; + ndn::KeyChain m_keyChain; + ndn::Scheduler m_scheduler{m_face.getIoService()}; psync::FullProducer m_fullProducer; - - int m_numDataStreams; uint64_t m_maxNumPublish; ndn::random::RandomNumberEngine& m_rng; diff --git a/examples/producer.cpp b/examples/producer.cpp index a6f4ca1..4e4c77a 100644 --- a/examples/producer.cpp +++ b/examples/producer.cpp @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -37,16 +38,14 @@ public: * IBF size is set to 40 in m_producer as the expected number of update to IBF in a sync cycle */ PSyncPartialProducer(const ndn::Name& syncPrefix, const std::string& userPrefix, - int nDataStreams, int maxNumPublish) - : m_scheduler(m_face.getIoService()) - , m_producer(40, m_face, syncPrefix, userPrefix + "-0") - , m_nDataStreams(nDataStreams) + int numDataStreams, int maxNumPublish) + : m_producer(m_face, m_keyChain, 40, syncPrefix, userPrefix + "-0") , m_maxNumPublish(maxNumPublish) , m_rng(ndn::random::getRandomNumberEngine()) , m_rangeUniformRandom(0, 60000) { // Add user prefixes and schedule updates for them - for (int i = 0; i < m_nDataStreams; i++) { + for (int i = 0; i < numDataStreams; i++) { ndn::Name updateName(userPrefix + "-" + std::to_string(i)); // Add the user prefix to the producer @@ -84,11 +83,10 @@ private: private: ndn::Face m_face; - ndn::Scheduler m_scheduler; + ndn::KeyChain m_keyChain; + ndn::Scheduler m_scheduler{m_face.getIoService()}; psync::PartialProducer m_producer; - - int m_nDataStreams; uint64_t m_maxNumPublish; ndn::random::RandomNumberEngine& m_rng; diff --git a/tests/clock-fixture.cpp b/tests/clock-fixture.cpp index d47f695..e900f16 100644 --- a/tests/clock-fixture.cpp +++ b/tests/clock-fixture.cpp @@ -19,11 +19,11 @@ #include "tests/clock-fixture.hpp" -namespace ndn::tests { +namespace psync::tests { ClockFixture::ClockFixture() - : m_steadyClock(make_shared()) - , m_systemClock(make_shared()) + : m_steadyClock(std::make_shared()) + , m_systemClock(std::make_shared()) { time::setCustomClocks(m_steadyClock, m_systemClock); } @@ -49,4 +49,4 @@ ClockFixture::advanceClocks(time::nanoseconds tick, time::nanoseconds total) } } -} // namespace ndn::tests +} // namespace psync::tests diff --git a/tests/clock-fixture.hpp b/tests/clock-fixture.hpp index 6f79950..8523041 100644 --- a/tests/clock-fixture.hpp +++ b/tests/clock-fixture.hpp @@ -22,7 +22,9 @@ #include -namespace ndn::tests { +namespace psync::tests { + +namespace time = ndn::time; /** \brief A test fixture that overrides steady clock and system clock. */ @@ -72,10 +74,10 @@ private: } protected: - shared_ptr m_steadyClock; - shared_ptr m_systemClock; + std::shared_ptr m_steadyClock; + std::shared_ptr m_systemClock; }; -} // namespace ndn::tests +} // namespace psync::tests #endif // PSYNC_TESTS_CLOCK_FIXTURE_HPP diff --git a/tests/io-fixture.hpp b/tests/io-fixture.hpp index cdd2713..20d3e5b 100644 --- a/tests/io-fixture.hpp +++ b/tests/io-fixture.hpp @@ -24,7 +24,7 @@ #include -namespace ndn::tests { +namespace psync::tests { class IoFixture : public ClockFixture { @@ -46,6 +46,6 @@ protected: boost::asio::io_service m_io; }; -} // namespace ndn::tests +} // namespace psync::tests #endif // PSYNC_TESTS_IO_FIXTURE_HPP diff --git a/tests/key-chain-fixture.hpp b/tests/key-chain-fixture.hpp new file mode 100644 index 0000000..bdd705a --- /dev/null +++ b/tests/key-chain-fixture.hpp @@ -0,0 +1,37 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2013-2022 Regents of the University of California. + * + * This file is part of PSync. + * + * PSync 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. + * + * 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License along with + * PSync, e.g., in COPYING.md file. If not, see . + */ + +#ifndef PSYNC_TESTS_KEY_CHAIN_FIXTURE_HPP +#define PSYNC_TESTS_KEY_CHAIN_FIXTURE_HPP + +#include + +namespace psync::tests { + +/** + * @brief A fixture providing an in-memory KeyChain. + */ +class KeyChainFixture +{ +protected: + ndn::KeyChain m_keyChain{"pib-memory:", "tpm-memory:"}; +}; + +} // namespace psync::tests + +#endif // PSYNC_TESTS_KEY_CHAIN_FIXTURE_HPP diff --git a/tests/test-full-producer.cpp b/tests/test-full-producer.cpp index 1ea3119..7218e6b 100644 --- a/tests/test-full-producer.cpp +++ b/tests/test-full-producer.cpp @@ -22,55 +22,50 @@ #include "tests/boost-test.hpp" #include "tests/io-fixture.hpp" +#include "tests/key-chain-fixture.hpp" -#include #include namespace psync { using namespace ndn; -BOOST_AUTO_TEST_SUITE(TestFullProducer) - -BOOST_AUTO_TEST_CASE(Constructor) +class FullProducerFixture : public tests::IoFixture, public tests::KeyChainFixture { - util::DummyClientFace face({true, true}); - BOOST_CHECK_NO_THROW(FullProducer(40, face, Name("/psync"), Name("/testUser"), nullptr)); -} +protected: + util::DummyClientFace m_face{m_io, m_keyChain, {true, true}}; +}; + +BOOST_FIXTURE_TEST_SUITE(TestFullProducer, FullProducerFixture) BOOST_AUTO_TEST_CASE(OnInterest) { Name syncPrefix("/psync"), userNode("/testUser"); - util::DummyClientFace face({true, true}); - - FullProducer node(40, face, syncPrefix, userNode, nullptr); + FullProducer node(m_face, m_keyChain, 40, syncPrefix, userNode, nullptr); Name syncInterestName(syncPrefix); syncInterestName.append("malicious-IBF"); - BOOST_REQUIRE_NO_THROW(node.onSyncInterest(syncPrefix, Interest(syncInterestName))); + BOOST_CHECK_NO_THROW(node.onSyncInterest(syncPrefix, Interest(syncInterestName))); } -BOOST_FIXTURE_TEST_CASE(ConstantTimeoutForFirstSegment, tests::IoFixture) +BOOST_AUTO_TEST_CASE(ConstantTimeoutForFirstSegment) { Name syncPrefix("/psync"), userNode("/testUser"); - util::DummyClientFace face(m_io, {true, true}); + FullProducer node(m_face, m_keyChain, 40, syncPrefix, userNode, nullptr, 8_s); - FullProducer node(40, face, syncPrefix, userNode, nullptr, 8_s); advanceClocks(10_ms); - face.sentInterests.clear(); + m_face.sentInterests.clear(); // full sync sends the next one in interest lifetime / 2 +- jitter advanceClocks(6_s); - BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); + BOOST_CHECK_EQUAL(m_face.sentInterests.size(), 1); } BOOST_AUTO_TEST_CASE(OnSyncDataDecodeFailure) { Name syncPrefix("/psync"), userNode("/testUser"); - util::DummyClientFace face({true, true}); - - FullProducer node(40, face, syncPrefix, userNode, nullptr); + FullProducer node(m_face, m_keyChain, 40, syncPrefix, userNode, nullptr); Name syncInterestName(syncPrefix); node.m_iblt.appendToName(syncInterestName); diff --git a/tests/test-full-sync.cpp b/tests/test-full-sync.cpp index 16ba4bc..81a4aef 100644 --- a/tests/test-full-sync.cpp +++ b/tests/test-full-sync.cpp @@ -24,24 +24,26 @@ #include "tests/boost-test.hpp" #include "tests/io-fixture.hpp" +#include "tests/key-chain-fixture.hpp" +#include #include namespace psync { -using ndn::Name; -using ndn::util::DummyClientFace; +using namespace ndn; -class FullSyncFixture : public ndn::tests::IoFixture +class FullSyncFixture : public tests::IoFixture, public tests::KeyChainFixture { protected: void addNode(int id) { BOOST_ASSERT(id >= 0 && id < MAX_NODES); - faces[id] = std::make_shared(m_io, DummyClientFace::Options{true, true}); userPrefixes[id] = "/userPrefix" + std::to_string(id); - nodes[id] = std::make_shared(40, *faces[id], syncPrefix, userPrefixes[id], + faces[id] = std::make_shared(m_io, m_keyChain, + util::DummyClientFace::Options{true, true}); + nodes[id] = std::make_shared(*faces[id], m_keyChain, 40, syncPrefix, userPrefixes[id], [] (const auto&) {}); } @@ -170,8 +172,8 @@ protected: protected: const Name syncPrefix = "/psync"; static constexpr int MAX_NODES = 4; - std::array, MAX_NODES> faces; std::array userPrefixes; + std::array, MAX_NODES> faces; std::array, MAX_NODES> nodes; static constexpr uint64_t NOT_EXIST = std::numeric_limits::max(); }; diff --git a/tests/test-partial-producer.cpp b/tests/test-partial-producer.cpp index bfbcc12..3f2ab95 100644 --- a/tests/test-partial-producer.cpp +++ b/tests/test-partial-producer.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2020, The University of Memphis + * Copyright (c) 2014-2022, The University of Memphis * * This file is part of PSync. * See AUTHORS.md for complete list of PSync authors and contributors. @@ -20,8 +20,8 @@ #include "PSync/partial-producer.hpp" #include "tests/boost-test.hpp" +#include "tests/key-chain-fixture.hpp" -#include #include #include @@ -29,24 +29,23 @@ namespace psync { using namespace ndn; -BOOST_AUTO_TEST_SUITE(TestPartialProducer) - -BOOST_AUTO_TEST_CASE(Constructor) +class PartialProducerFixture : public tests::KeyChainFixture { - util::DummyClientFace face({true, true}); - BOOST_CHECK_NO_THROW(PartialProducer(40, face, Name("/psync"), Name("/testUser"))); -} +protected: + util::DummyClientFace m_face{m_keyChain, {true, true}}; +}; + +BOOST_FIXTURE_TEST_SUITE(TestPartialProducer, PartialProducerFixture) BOOST_AUTO_TEST_CASE(RegisterPrefix) { Name syncPrefix("/psync"), userNode("/testUser"); - util::DummyClientFace face({true, true}); - PartialProducer producer(40, face, syncPrefix, userNode); + PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode); - face.processEvents(-1_ms); + m_face.processEvents(-1_ms); - BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); - auto interest = face.sentInterests.front(); + BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 1); + auto interest = m_face.sentInterests.front(); BOOST_CHECK_EQUAL(interest.getName().at(3), name::Component("register")); nfd::ControlParameters params(interest.getName().at(4).blockFromValue()); BOOST_CHECK_EQUAL(params.getName(), syncPrefix); @@ -55,8 +54,7 @@ BOOST_AUTO_TEST_CASE(RegisterPrefix) BOOST_AUTO_TEST_CASE(PublishName) { Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2"); - util::DummyClientFace face({true, true}); - PartialProducer producer(40, face, syncPrefix, userNode); + PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode); BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0); producer.publishName(userNode); @@ -75,8 +73,7 @@ BOOST_AUTO_TEST_CASE(PublishName) BOOST_AUTO_TEST_CASE(SameSyncInterest) { Name syncPrefix("/psync"), userNode("/testUser"); - util::DummyClientFace face({true, true}); - PartialProducer producer(40, face, syncPrefix, userNode); + PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode); Name syncInterestName(syncPrefix); syncInterestName.append("sync"); @@ -91,29 +88,28 @@ BOOST_AUTO_TEST_CASE(SameSyncInterest) syncInterest.setInterestLifetime(1_s); syncInterest.setNonce(1); BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest)); - face.processEvents(10_ms); + m_face.processEvents(10_ms); BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); - face.processEvents(500_ms); + m_face.processEvents(500_ms); // Same interest again - size of pending interest should remain same, but expirationEvent should change syncInterest.setNonce(2); BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest)); - face.processEvents(10_ms); + m_face.processEvents(10_ms); BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); - face.processEvents(500_ms); + m_face.processEvents(500_ms); BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); - face.processEvents(500_ms); + m_face.processEvents(500_ms); 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); + PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode); // Sync interest with no bloom filter attached Name syncInterestName(syncPrefix); diff --git a/tests/test-partial-sync.cpp b/tests/test-partial-sync.cpp index 7353c5e..d7a0b4f 100644 --- a/tests/test-partial-sync.cpp +++ b/tests/test-partial-sync.cpp @@ -22,6 +22,7 @@ #include "tests/boost-test.hpp" #include "tests/io-fixture.hpp" +#include "tests/key-chain-fixture.hpp" #include #include @@ -30,12 +31,12 @@ namespace psync { using namespace ndn; -class PartialSyncFixture : public tests::IoFixture +class PartialSyncFixture : public tests::IoFixture, public tests::KeyChainFixture { public: PartialSyncFixture() { - producer = make_shared(40, face, syncPrefix, userPrefix); + producer = make_shared(face, m_keyChain, 40, syncPrefix, userPrefix); addUserNodes("testUser", 10); } @@ -51,9 +52,8 @@ public: void addConsumer(int id, const std::vector& subscribeTo, bool linkToProducer = true) { - consumerFaces[id] = - std::make_shared(m_io, util::DummyClientFace::Options{true, true}); - + consumerFaces[id] = std::make_shared(m_io, m_keyChain, + util::DummyClientFace::Options{true, true}); if (linkToProducer) { face.linkTo(*consumerFaces[id]); } @@ -131,7 +131,8 @@ public: producer->updateSeqNo(prefix, seq); } - util::DummyClientFace face{m_io, {true, true}}; +protected: + util::DummyClientFace face{m_io, m_keyChain, {true, true}}; Name syncPrefix{"psync"}; Name userPrefix{"testUser-0"}; @@ -288,8 +289,8 @@ BOOST_AUTO_TEST_CASE(ReplicatedProducer) // Link to first producer goes down face.unlink(); - util::DummyClientFace face2(m_io, {true, true}); - PartialProducer replicatedProducer(40, face2, syncPrefix, userPrefix); + util::DummyClientFace face2(m_io, m_keyChain, {true, true}); + PartialProducer replicatedProducer(face2, m_keyChain, 40, syncPrefix, userPrefix); for (int i = 1; i < 10; i++) { replicatedProducer.addUserNode("testUser-" + std::to_string(i)); } diff --git a/tests/test-producer-base.cpp b/tests/test-producer-base.cpp index deab560..67d4aa2 100644 --- a/tests/test-producer-base.cpp +++ b/tests/test-producer-base.cpp @@ -20,28 +20,27 @@ #include "PSync/producer-base.hpp" #include "tests/boost-test.hpp" +#include "tests/key-chain-fixture.hpp" -#include -#include #include namespace psync { using namespace ndn; -BOOST_AUTO_TEST_SUITE(TestProducerBase) - -BOOST_AUTO_TEST_CASE(Constructor) +class ProducerBaseFixture : public tests::KeyChainFixture { - util::DummyClientFace face; - BOOST_CHECK_NO_THROW(ProducerBase(40, face, Name("/psync"), Name("/testUser"))); -} +protected: + util::DummyClientFace m_face; +}; + +BOOST_FIXTURE_TEST_SUITE(TestProducerBase, ProducerBaseFixture) BOOST_AUTO_TEST_CASE(Basic) { - util::DummyClientFace face; Name userNode("/testUser"); - ProducerBase producerBase(40, face, Name("/psync"), userNode); + ProducerBase producerBase(m_face, m_keyChain, 40, Name("/psync"), userNode); + // Hash table size should be 40 + 40/2 = 60 (which is perfectly divisible by N_HASH = 3) BOOST_CHECK_EQUAL(producerBase.m_iblt.getHashTable().size(), 60); BOOST_CHECK_EQUAL(producerBase.getSeqNo(userNode).value(), 0); @@ -67,16 +66,14 @@ BOOST_AUTO_TEST_CASE(Basic) BOOST_AUTO_TEST_CASE(ApplicationNack) { - util::DummyClientFace face; - ProducerBase producerBase(40, face, Name("/psync"), Name("/testUser")); + ProducerBase producerBase(m_face, m_keyChain, 40, Name("/psync"), Name("/testUser")); - BOOST_CHECK_EQUAL(face.sentData.size(), 0); - producerBase.m_syncReplyFreshness = 1_s; + BOOST_CHECK_EQUAL(m_face.sentData.size(), 0); producerBase.sendApplicationNack(Name("test")); - face.processEvents(10_ms); + m_face.processEvents(10_ms); - BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); - BOOST_CHECK_EQUAL(face.sentData.front().getContentType(), tlv::ContentType_Nack); + BOOST_REQUIRE_EQUAL(m_face.sentData.size(), 1); + BOOST_CHECK_EQUAL(m_face.sentData.front().getContentType(), tlv::ContentType_Nack); } BOOST_AUTO_TEST_SUITE_END() diff --git a/tests/test-segment-publisher.cpp b/tests/test-segment-publisher.cpp index 4874c49..bccaee1 100644 --- a/tests/test-segment-publisher.cpp +++ b/tests/test-segment-publisher.cpp @@ -1,6 +1,6 @@ /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* - * Copyright (c) 2014-2020, The University of Memphis + * Copyright (c) 2014-2022, The University of Memphis * * This file is part of PSync. * See AUTHORS.md for complete list of PSync authors and contributors. @@ -22,9 +22,8 @@ #include "tests/boost-test.hpp" #include "tests/io-fixture.hpp" +#include "tests/key-chain-fixture.hpp" -#include -#include #include #include #include @@ -33,16 +32,14 @@ namespace psync { using namespace ndn; -class SegmentPublisherFixture : public tests::IoFixture +class SegmentPublisherFixture : public tests::IoFixture, public tests::KeyChainFixture { -public: +protected: SegmentPublisherFixture() - : face(m_io, util::DummyClientFace::Options{true, true}) - , publisher(face, keyChain) { - face.setInterestFilter(InterestFilter("/hello/world"), - bind(&SegmentPublisherFixture::onInterest, this, _1, _2), - [] (auto&&...) { BOOST_CHECK(false); }); + m_face.setInterestFilter(InterestFilter("/hello/world"), + bind(&SegmentPublisherFixture::onInterest, this, _1, _2), + [] (auto&&...) { BOOST_CHECK(false); }); advanceClocks(10_ms); for (int i = 0; i < 1000; ++i) { @@ -58,7 +55,7 @@ public: void expressInterest(const Interest& interest) { - fetcher = util::SegmentFetcher::start(face, interest, security::getAcceptAllValidator()); + fetcher = util::SegmentFetcher::start(m_face, interest, security::getAcceptAllValidator()); fetcher->onComplete.connect([this] (auto&&...) { numComplete++; }); fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); }); @@ -82,16 +79,17 @@ public: } } - util::DummyClientFace face; - KeyChain keyChain; - SegmentPublisher publisher; +protected: + util::DummyClientFace m_face{m_io, m_keyChain, {true, true}}; + SegmentPublisher publisher{m_face, m_keyChain}; shared_ptr fetcher; Name dataName; - time::milliseconds freshness = 1_s; detail::State state; int numComplete = 0; int numRepliesFromStore = 0; + + static constexpr time::milliseconds freshness = 1_s; }; BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture) @@ -120,10 +118,10 @@ BOOST_AUTO_TEST_CASE(Basic) BOOST_CHECK_EQUAL(numRepliesFromStore, 2); numRepliesFromStore = 0; - face.expressInterest(Interest("/hello/world/").setCanBePrefix(true), - [this] (auto&&...) { this->numComplete++; }, - [] (auto&&...) { BOOST_CHECK(false); }, - [] (auto&&...) { BOOST_CHECK(false); }); + m_face.expressInterest(Interest("/hello/world/").setCanBePrefix(true), + [this] (auto&&...) { this->numComplete++; }, + [] (auto&&...) { BOOST_CHECK(false); }, + [] (auto&&...) { BOOST_CHECK(false); }); advanceClocks(10_ms); BOOST_CHECK_EQUAL(numComplete, 4); BOOST_CHECK_EQUAL(numRepliesFromStore, 1);