Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, The University of Memphis |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 4 | * |
| 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 Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 9 | * of the GNU Lesser General Public License as published by the Free Software Foundation, |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 10 | * 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 Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 14 | * PURPOSE. See the GNU Lesser General Public License for more details. |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 15 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public License along with |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 17 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
Ashlesh Gawande | 78b94ad | 2018-12-13 15:29:19 -0600 | [diff] [blame] | 20 | #include "PSync/partial-producer.hpp" |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 21 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 22 | #include "tests/boost-test.hpp" |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 23 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 24 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 25 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 26 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 27 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 28 | namespace psync { |
| 29 | |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 30 | using ndn::Interest; |
| 31 | using ndn::Name; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 32 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 33 | class PartialProducerFixture : public tests::KeyChainFixture |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 34 | { |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 35 | protected: |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 36 | ndn::DummyClientFace m_face{m_keyChain, {true, true}}; |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | BOOST_FIXTURE_TEST_SUITE(TestPartialProducer, PartialProducerFixture) |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_CASE(RegisterPrefix) |
| 42 | { |
| 43 | Name syncPrefix("/psync"), userNode("/testUser"); |
Junxiao Shi | eecd84f | 2023-12-07 02:31:51 +0000 | [diff] [blame^] | 44 | PartialProducer producer(m_face, m_keyChain, syncPrefix, {}); |
| 45 | producer.addUserNode(userNode); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 46 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 47 | m_face.processEvents(-1_ms); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 48 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 49 | BOOST_REQUIRE_EQUAL(m_face.sentInterests.size(), 1); |
| 50 | auto interest = m_face.sentInterests.front(); |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(interest.getName().at(3), Name::Component("register")); |
| 52 | ndn::nfd::ControlParameters params(interest.getName().at(4).blockFromValue()); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(params.getName(), syncPrefix); |
| 54 | } |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(PublishName) |
| 57 | { |
| 58 | Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2"); |
Junxiao Shi | eecd84f | 2023-12-07 02:31:51 +0000 | [diff] [blame^] | 59 | PartialProducer producer(m_face, m_keyChain, syncPrefix, {}); |
| 60 | producer.addUserNode(userNode); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 61 | |
| 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 | |
| 76 | BOOST_AUTO_TEST_CASE(SameSyncInterest) |
| 77 | { |
| 78 | Name syncPrefix("/psync"), userNode("/testUser"); |
Junxiao Shi | eecd84f | 2023-12-07 02:31:51 +0000 | [diff] [blame^] | 79 | PartialProducer producer(m_face, m_keyChain, syncPrefix, {}); |
| 80 | producer.addUserNode(userNode); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 81 | |
| 82 | Name syncInterestName(syncPrefix); |
| 83 | syncInterestName.append("sync"); |
Ashlesh Gawande | 2e82df1 | 2018-12-08 21:42:29 -0600 | [diff] [blame] | 84 | Name syncInterestPrefix = syncInterestName; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 85 | |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 86 | detail::BloomFilter bf(20, 0.001); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 87 | bf.appendToName(syncInterestName); |
| 88 | |
| 89 | producer.m_iblt.appendToName(syncInterestName); |
| 90 | |
| 91 | Interest syncInterest(syncInterestName); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 92 | syncInterest.setInterestLifetime(1_s); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 93 | syncInterest.setNonce(1); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 94 | BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest)); |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 95 | m_face.processEvents(10_ms); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 97 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 98 | m_face.processEvents(500_ms); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 99 | |
| 100 | // Same interest again - size of pending interest should remain same, but expirationEvent should change |
| 101 | syncInterest.setNonce(2); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 102 | BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest)); |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 103 | m_face.processEvents(10_ms); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 105 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 106 | m_face.processEvents(500_ms); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 108 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 109 | m_face.processEvents(500_ms); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 0); |
| 111 | } |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(OnSyncInterest) |
| 114 | { |
| 115 | Name syncPrefix("/psync"), userNode("/testUser"); |
Junxiao Shi | eecd84f | 2023-12-07 02:31:51 +0000 | [diff] [blame^] | 116 | PartialProducer producer(m_face, m_keyChain, syncPrefix, {}); |
| 117 | producer.addUserNode(userNode); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 118 | |
| 119 | // Sync interest with no bloom filter attached |
| 120 | Name syncInterestName(syncPrefix); |
| 121 | syncInterestName.append("sync"); |
| 122 | producer.m_iblt.appendToName(syncInterestName); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 123 | BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 124 | |
| 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 Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 132 | BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 133 | |
| 134 | // Sync interest with malicious IBF |
| 135 | syncInterestName = syncPrefix; |
| 136 | syncInterestName.append("sync"); |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 137 | detail::BloomFilter bf(20, 0.001); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 138 | bf.appendToName(syncInterestName); |
| 139 | syncInterestName.append("fake-name"); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 140 | BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_SUITE_END() |
| 144 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 145 | } // namespace psync |