blob: 3f2ab95fb0f6d18ea357ce287edeefcf107d9a3e [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -04003 * Copyright (c) 2014-2022, The University of Memphis
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05004 *
5 * This file is part of PSync.
6 * See AUTHORS.md for complete list of PSync authors and contributors.
7 *
8 * PSync is free software: you can redistribute it and/or modify it under the terms
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -06009 * of the GNU Lesser General Public License as published by the Free Software Foundation,
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050010 * either version 3 of the License, or (at your option) any later version.
11 *
12 * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -060014 * PURPOSE. See the GNU Lesser General Public License for more details.
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050015 *
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -060016 * You should have received a copy of the GNU Lesser General Public License along with
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050017 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
Ashlesh Gawande78b94ad2018-12-13 15:29:19 -060020#include "PSync/partial-producer.hpp"
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050021
Davide Pesavento5b3cf762020-04-03 16:20:04 -040022#include "tests/boost-test.hpp"
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040023#include "tests/key-chain-fixture.hpp"
Davide Pesavento5b3cf762020-04-03 16:20:04 -040024
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050025#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Davide Pesavento5b3cf762020-04-03 16:20:04 -040026#include <ndn-cxx/util/dummy-client-face.hpp>
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050027
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050028namespace psync {
29
30using namespace ndn;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050031
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040032class PartialProducerFixture : public tests::KeyChainFixture
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050033{
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040034protected:
35 util::DummyClientFace m_face{m_keyChain, {true, true}};
36};
37
38BOOST_FIXTURE_TEST_SUITE(TestPartialProducer, PartialProducerFixture)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050039
40BOOST_AUTO_TEST_CASE(RegisterPrefix)
41{
42 Name syncPrefix("/psync"), userNode("/testUser");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040043 PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050044
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040045 m_face.processEvents(-1_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050046
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040047 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 1);
48 auto interest = m_face.sentInterests.front();
Davide Pesavento5b3cf762020-04-03 16:20:04 -040049 BOOST_CHECK_EQUAL(interest.getName().at(3), name::Component("register"));
50 nfd::ControlParameters params(interest.getName().at(4).blockFromValue());
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050051 BOOST_CHECK_EQUAL(params.getName(), syncPrefix);
52}
53
54BOOST_AUTO_TEST_CASE(PublishName)
55{
56 Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040057 PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050058
59 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0);
60 producer.publishName(userNode);
61 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 1);
62
63 producer.publishName(userNode);
64 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 2);
65
66 producer.publishName(userNode, 10);
67 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 10);
68
69 producer.publishName(nonUser);
70 BOOST_CHECK_EQUAL(producer.getSeqNo(nonUser).value_or(-1), -1);
71}
72
73BOOST_AUTO_TEST_CASE(SameSyncInterest)
74{
75 Name syncPrefix("/psync"), userNode("/testUser");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040076 PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050077
78 Name syncInterestName(syncPrefix);
79 syncInterestName.append("sync");
Ashlesh Gawande2e82df12018-12-08 21:42:29 -060080 Name syncInterestPrefix = syncInterestName;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050081
Davide Pesaventodb789562020-12-19 23:01:08 -050082 detail::BloomFilter bf(20, 0.001);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050083 bf.appendToName(syncInterestName);
84
85 producer.m_iblt.appendToName(syncInterestName);
86
87 Interest syncInterest(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050088 syncInterest.setInterestLifetime(1_s);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050089 syncInterest.setNonce(1);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050090 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040091 m_face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050092 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
93
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040094 m_face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050095
96 // Same interest again - size of pending interest should remain same, but expirationEvent should change
97 syncInterest.setNonce(2);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050098 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040099 m_face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500100 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
101
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400102 m_face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500103 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
104
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400105 m_face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500106 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 0);
107}
108
109BOOST_AUTO_TEST_CASE(OnSyncInterest)
110{
111 Name syncPrefix("/psync"), userNode("/testUser");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400112 PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500113
114 // Sync interest with no bloom filter attached
115 Name syncInterestName(syncPrefix);
116 syncInterestName.append("sync");
117 producer.m_iblt.appendToName(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500118 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500119
120 // Sync interest with malicious bloom filter
121 syncInterestName = syncPrefix;
122 syncInterestName.append("sync");
123 syncInterestName.appendNumber(20); // count of bloom filter
124 syncInterestName.appendNumber(1); // false positive probability * 1000 of bloom filter
125 syncInterestName.append("fake-name");
126 producer.m_iblt.appendToName(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500127 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500128
129 // Sync interest with malicious IBF
130 syncInterestName = syncPrefix;
131 syncInterestName.append("sync");
Davide Pesaventodb789562020-12-19 23:01:08 -0500132 detail::BloomFilter bf(20, 0.001);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500133 bf.appendToName(syncInterestName);
134 syncInterestName.append("fake-name");
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500135 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500136}
137
138BOOST_AUTO_TEST_SUITE_END()
139
Davide Pesavento5b3cf762020-04-03 16:20:04 -0400140} // namespace psync