blob: 86b510c78cfdc3f575450b473f977c8a039cd2a5 [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47eb6d92024-02-12 20:25:51 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento47eb6d92024-02-12 20:25:51 -050028namespace psync::tests {
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050029
Junxiao Shic5f5eb12023-08-11 08:05:23 +000030using ndn::Interest;
31using ndn::Name;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050032
Davide Pesavento47eb6d92024-02-12 20:25:51 -050033class PartialProducerFixture : public KeyChainFixture
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050034{
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040035protected:
Junxiao Shic5f5eb12023-08-11 08:05:23 +000036 ndn::DummyClientFace m_face{m_keyChain, {true, true}};
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040037};
38
39BOOST_FIXTURE_TEST_SUITE(TestPartialProducer, PartialProducerFixture)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050040
41BOOST_AUTO_TEST_CASE(RegisterPrefix)
42{
43 Name syncPrefix("/psync"), userNode("/testUser");
Junxiao Shieecd84f2023-12-07 02:31:51 +000044 PartialProducer producer(m_face, m_keyChain, syncPrefix, {});
45 producer.addUserNode(userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050046
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040047 m_face.processEvents(-1_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050048
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040049 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 1);
50 auto interest = m_face.sentInterests.front();
Junxiao Shic5f5eb12023-08-11 08:05:23 +000051 BOOST_CHECK_EQUAL(interest.getName().at(3), Name::Component("register"));
52 ndn::nfd::ControlParameters params(interest.getName().at(4).blockFromValue());
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050053 BOOST_CHECK_EQUAL(params.getName(), syncPrefix);
54}
55
56BOOST_AUTO_TEST_CASE(PublishName)
57{
58 Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2");
Junxiao Shieecd84f2023-12-07 02:31:51 +000059 PartialProducer producer(m_face, m_keyChain, syncPrefix, {});
60 producer.addUserNode(userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050061
62 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0);
63 producer.publishName(userNode);
64 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 1);
65
66 producer.publishName(userNode);
67 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 2);
68
69 producer.publishName(userNode, 10);
70 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 10);
71
72 producer.publishName(nonUser);
73 BOOST_CHECK_EQUAL(producer.getSeqNo(nonUser).value_or(-1), -1);
74}
75
76BOOST_AUTO_TEST_CASE(SameSyncInterest)
77{
78 Name syncPrefix("/psync"), userNode("/testUser");
Junxiao Shieecd84f2023-12-07 02:31:51 +000079 PartialProducer producer(m_face, m_keyChain, syncPrefix, {});
80 producer.addUserNode(userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050081
82 Name syncInterestName(syncPrefix);
83 syncInterestName.append("sync");
Ashlesh Gawande2e82df12018-12-08 21:42:29 -060084 Name syncInterestPrefix = syncInterestName;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050085
Davide Pesaventodb789562020-12-19 23:01:08 -050086 detail::BloomFilter bf(20, 0.001);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050087 bf.appendToName(syncInterestName);
88
89 producer.m_iblt.appendToName(syncInterestName);
90
91 Interest syncInterest(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050092 syncInterest.setInterestLifetime(1_s);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050093 syncInterest.setNonce(1);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050094 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040095 m_face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050096 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
97
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040098 m_face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050099
100 // Same interest again - size of pending interest should remain same, but expirationEvent should change
101 syncInterest.setNonce(2);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500102 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400103 m_face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500104 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
105
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400106 m_face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500107 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
108
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400109 m_face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500110 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 0);
111}
112
113BOOST_AUTO_TEST_CASE(OnSyncInterest)
114{
115 Name syncPrefix("/psync"), userNode("/testUser");
Junxiao Shieecd84f2023-12-07 02:31:51 +0000116 PartialProducer producer(m_face, m_keyChain, syncPrefix, {});
117 producer.addUserNode(userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500118
119 // Sync interest with no bloom filter attached
120 Name syncInterestName(syncPrefix);
121 syncInterestName.append("sync");
122 producer.m_iblt.appendToName(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500123 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500124
125 // Sync interest with malicious bloom filter
126 syncInterestName = syncPrefix;
127 syncInterestName.append("sync");
128 syncInterestName.appendNumber(20); // count of bloom filter
129 syncInterestName.appendNumber(1); // false positive probability * 1000 of bloom filter
130 syncInterestName.append("fake-name");
131 producer.m_iblt.appendToName(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500132 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500133
134 // Sync interest with malicious IBF
135 syncInterestName = syncPrefix;
136 syncInterestName.append("sync");
Davide Pesaventodb789562020-12-19 23:01:08 -0500137 detail::BloomFilter bf(20, 0.001);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500138 bf.appendToName(syncInterestName);
139 syncInterestName.append("fake-name");
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500140 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500141}
142
143BOOST_AUTO_TEST_SUITE_END()
144
Davide Pesavento47eb6d92024-02-12 20:25:51 -0500145} // namespace psync::tests