Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Ashlesh Gawande | 5a89547 | 2020-01-25 18:07:32 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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" |
| 23 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/name.hpp> |
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 | |
| 30 | using namespace ndn; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(TestPartialProducer) |
| 33 | |
| 34 | BOOST_AUTO_TEST_CASE(Constructor) |
| 35 | { |
| 36 | util::DummyClientFace face({true, true}); |
| 37 | BOOST_REQUIRE_NO_THROW(PartialProducer(40, face, Name("/psync"), Name("/testUser"))); |
| 38 | } |
| 39 | |
| 40 | BOOST_AUTO_TEST_CASE(RegisterPrefix) |
| 41 | { |
| 42 | Name syncPrefix("/psync"), userNode("/testUser"); |
| 43 | util::DummyClientFace face({true, true}); |
| 44 | PartialProducer producer(40, face, syncPrefix, userNode); |
| 45 | |
| 46 | face.processEvents(time::milliseconds(-1)); |
| 47 | |
| 48 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 49 | auto interest = face.sentInterests.front(); |
| 50 | BOOST_CHECK_EQUAL(interest.getName().at(3), name::Component("register")); |
| 51 | nfd::ControlParameters params(interest.getName().at(4).blockFromValue()); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(params.getName(), syncPrefix); |
| 53 | } |
| 54 | |
| 55 | BOOST_AUTO_TEST_CASE(PublishName) |
| 56 | { |
| 57 | Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2"); |
| 58 | util::DummyClientFace face({true, true}); |
| 59 | PartialProducer producer(40, face, syncPrefix, userNode); |
| 60 | |
| 61 | BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0); |
| 62 | producer.publishName(userNode); |
| 63 | BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 1); |
| 64 | |
| 65 | producer.publishName(userNode); |
| 66 | BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 2); |
| 67 | |
| 68 | producer.publishName(userNode, 10); |
| 69 | BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 10); |
| 70 | |
| 71 | producer.publishName(nonUser); |
| 72 | BOOST_CHECK_EQUAL(producer.getSeqNo(nonUser).value_or(-1), -1); |
| 73 | } |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(SameSyncInterest) |
| 76 | { |
| 77 | Name syncPrefix("/psync"), userNode("/testUser"); |
| 78 | util::DummyClientFace face({true, true}); |
| 79 | PartialProducer producer(40, face, syncPrefix, userNode); |
| 80 | |
| 81 | Name syncInterestName(syncPrefix); |
| 82 | syncInterestName.append("sync"); |
Ashlesh Gawande | 2e82df1 | 2018-12-08 21:42:29 -0600 | [diff] [blame] | 83 | Name syncInterestPrefix = syncInterestName; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 84 | |
| 85 | BloomFilter bf(20, 0.001); |
| 86 | bf.appendToName(syncInterestName); |
| 87 | |
| 88 | producer.m_iblt.appendToName(syncInterestName); |
| 89 | |
| 90 | Interest syncInterest(syncInterestName); |
| 91 | syncInterest.setInterestLifetime(time::milliseconds(1000)); |
| 92 | syncInterest.setNonce(1); |
Ashlesh Gawande | 2e82df1 | 2018-12-08 21:42:29 -0600 | [diff] [blame] | 93 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest)); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 94 | face.processEvents(time::milliseconds(10)); |
| 95 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 96 | |
| 97 | face.processEvents(time::milliseconds(500)); |
| 98 | |
| 99 | // Same interest again - size of pending interest should remain same, but expirationEvent should change |
| 100 | syncInterest.setNonce(2); |
Ashlesh Gawande | 2e82df1 | 2018-12-08 21:42:29 -0600 | [diff] [blame] | 101 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest)); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 102 | face.processEvents(time::milliseconds(10)); |
| 103 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 104 | |
| 105 | face.processEvents(time::milliseconds(500)); |
| 106 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 107 | |
| 108 | face.processEvents(time::milliseconds(500)); |
| 109 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 0); |
| 110 | } |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE(OnSyncInterest) |
| 113 | { |
| 114 | Name syncPrefix("/psync"), userNode("/testUser"); |
| 115 | util::DummyClientFace face({true, true}); |
| 116 | PartialProducer producer(40, face, syncPrefix, userNode); |
| 117 | |
| 118 | // Sync interest with no bloom filter attached |
| 119 | Name syncInterestName(syncPrefix); |
| 120 | syncInterestName.append("sync"); |
| 121 | producer.m_iblt.appendToName(syncInterestName); |
| 122 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
| 123 | |
| 124 | // Sync interest with malicious bloom filter |
| 125 | syncInterestName = syncPrefix; |
| 126 | syncInterestName.append("sync"); |
| 127 | syncInterestName.appendNumber(20); // count of bloom filter |
| 128 | syncInterestName.appendNumber(1); // false positive probability * 1000 of bloom filter |
| 129 | syncInterestName.append("fake-name"); |
| 130 | producer.m_iblt.appendToName(syncInterestName); |
| 131 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
| 132 | |
| 133 | // Sync interest with malicious IBF |
| 134 | syncInterestName = syncPrefix; |
| 135 | syncInterestName.append("sync"); |
| 136 | BloomFilter bf(20, 0.001); |
| 137 | bf.appendToName(syncInterestName); |
| 138 | syncInterestName.append("fake-name"); |
| 139 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_SUITE_END() |
| 143 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 144 | } // namespace psync |