blob: 50d738689c8c161fef2ad086b4817325b6e7a8f7 [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shic5f5eb12023-08-11 08:05:23 +00003 * Copyright (c) 2014-2023, 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
Junxiao Shic5f5eb12023-08-11 08:05:23 +000030using ndn::Interest;
31using ndn::Name;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050032
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040033class PartialProducerFixture : public tests::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");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040044 PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050045
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040046 m_face.processEvents(-1_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050047
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040048 BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 1);
49 auto interest = m_face.sentInterests.front();
Junxiao Shic5f5eb12023-08-11 08:05:23 +000050 BOOST_CHECK_EQUAL(interest.getName().at(3), Name::Component("register"));
51 ndn::nfd::ControlParameters params(interest.getName().at(4).blockFromValue());
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050052 BOOST_CHECK_EQUAL(params.getName(), syncPrefix);
53}
54
55BOOST_AUTO_TEST_CASE(PublishName)
56{
57 Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040058 PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050059
60 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0);
61 producer.publishName(userNode);
62 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 1);
63
64 producer.publishName(userNode);
65 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 2);
66
67 producer.publishName(userNode, 10);
68 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 10);
69
70 producer.publishName(nonUser);
71 BOOST_CHECK_EQUAL(producer.getSeqNo(nonUser).value_or(-1), -1);
72}
73
74BOOST_AUTO_TEST_CASE(SameSyncInterest)
75{
76 Name syncPrefix("/psync"), userNode("/testUser");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040077 PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050078
79 Name syncInterestName(syncPrefix);
80 syncInterestName.append("sync");
Ashlesh Gawande2e82df12018-12-08 21:42:29 -060081 Name syncInterestPrefix = syncInterestName;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050082
Davide Pesaventodb789562020-12-19 23:01:08 -050083 detail::BloomFilter bf(20, 0.001);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050084 bf.appendToName(syncInterestName);
85
86 producer.m_iblt.appendToName(syncInterestName);
87
88 Interest syncInterest(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050089 syncInterest.setInterestLifetime(1_s);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050090 syncInterest.setNonce(1);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050091 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040092 m_face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050093 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
94
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040095 m_face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050096
97 // Same interest again - size of pending interest should remain same, but expirationEvent should change
98 syncInterest.setNonce(2);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050099 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400100 m_face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500101 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
102
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400103 m_face.processEvents(500_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(), 0);
108}
109
110BOOST_AUTO_TEST_CASE(OnSyncInterest)
111{
112 Name syncPrefix("/psync"), userNode("/testUser");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400113 PartialProducer producer(m_face, m_keyChain, 40, syncPrefix, userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500114
115 // Sync interest with no bloom filter attached
116 Name syncInterestName(syncPrefix);
117 syncInterestName.append("sync");
118 producer.m_iblt.appendToName(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500119 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500120
121 // Sync interest with malicious bloom filter
122 syncInterestName = syncPrefix;
123 syncInterestName.append("sync");
124 syncInterestName.appendNumber(20); // count of bloom filter
125 syncInterestName.appendNumber(1); // false positive probability * 1000 of bloom filter
126 syncInterestName.append("fake-name");
127 producer.m_iblt.appendToName(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500128 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500129
130 // Sync interest with malicious IBF
131 syncInterestName = syncPrefix;
132 syncInterestName.append("sync");
Davide Pesaventodb789562020-12-19 23:01:08 -0500133 detail::BloomFilter bf(20, 0.001);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500134 bf.appendToName(syncInterestName);
135 syncInterestName.append("fake-name");
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500136 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500137}
138
139BOOST_AUTO_TEST_SUITE_END()
140
Davide Pesavento5b3cf762020-04-03 16:20:04 -0400141} // namespace psync