Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2018, The University of Memphis |
| 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 |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 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 |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 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 | |
| 22 | #include <boost/test/unit_test.hpp> |
| 23 | #include <ndn-cxx/name.hpp> |
| 24 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 25 | #include <ndn-cxx/mgmt/nfd/control-parameters.hpp> |
| 26 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 27 | namespace psync { |
| 28 | |
| 29 | using namespace ndn; |
| 30 | using namespace std; |
| 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); |
| 49 | |
| 50 | face.sentInterests.back(); |
| 51 | Interest interest = *face.sentInterests.begin(); |
| 52 | BOOST_CHECK_EQUAL(interest.getName().get(3), name::Component("register")); |
| 53 | name::Component test = interest.getName().get(4); |
| 54 | nfd::ControlParameters params(test.blockFromValue()); |
| 55 | BOOST_CHECK_EQUAL(params.getName(), syncPrefix); |
| 56 | } |
| 57 | |
| 58 | BOOST_AUTO_TEST_CASE(PublishName) |
| 59 | { |
| 60 | Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2"); |
| 61 | util::DummyClientFace face({true, true}); |
| 62 | PartialProducer producer(40, face, syncPrefix, userNode); |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0); |
| 65 | producer.publishName(userNode); |
| 66 | BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 1); |
| 67 | |
| 68 | producer.publishName(userNode); |
| 69 | BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 2); |
| 70 | |
| 71 | producer.publishName(userNode, 10); |
| 72 | BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 10); |
| 73 | |
| 74 | producer.publishName(nonUser); |
| 75 | BOOST_CHECK_EQUAL(producer.getSeqNo(nonUser).value_or(-1), -1); |
| 76 | } |
| 77 | |
| 78 | BOOST_AUTO_TEST_CASE(SameSyncInterest) |
| 79 | { |
| 80 | Name syncPrefix("/psync"), userNode("/testUser"); |
| 81 | util::DummyClientFace face({true, true}); |
| 82 | PartialProducer producer(40, face, syncPrefix, userNode); |
| 83 | |
| 84 | Name syncInterestName(syncPrefix); |
| 85 | syncInterestName.append("sync"); |
Ashlesh Gawande | 2e82df1 | 2018-12-08 21:42:29 -0600 | [diff] [blame] | 86 | Name syncInterestPrefix = syncInterestName; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 87 | |
| 88 | BloomFilter bf(20, 0.001); |
| 89 | bf.appendToName(syncInterestName); |
| 90 | |
| 91 | producer.m_iblt.appendToName(syncInterestName); |
| 92 | |
| 93 | Interest syncInterest(syncInterestName); |
| 94 | syncInterest.setInterestLifetime(time::milliseconds(1000)); |
| 95 | syncInterest.setNonce(1); |
Ashlesh Gawande | 2e82df1 | 2018-12-08 21:42:29 -0600 | [diff] [blame] | 96 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest)); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 97 | face.processEvents(time::milliseconds(10)); |
| 98 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 99 | |
| 100 | face.processEvents(time::milliseconds(500)); |
| 101 | |
| 102 | // Same interest again - size of pending interest should remain same, but expirationEvent should change |
| 103 | syncInterest.setNonce(2); |
Ashlesh Gawande | 2e82df1 | 2018-12-08 21:42:29 -0600 | [diff] [blame] | 104 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest)); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 105 | face.processEvents(time::milliseconds(10)); |
| 106 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 107 | |
| 108 | face.processEvents(time::milliseconds(500)); |
| 109 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1); |
| 110 | |
| 111 | face.processEvents(time::milliseconds(500)); |
| 112 | BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 0); |
| 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_CASE(OnSyncInterest) |
| 116 | { |
| 117 | Name syncPrefix("/psync"), userNode("/testUser"); |
| 118 | util::DummyClientFace face({true, true}); |
| 119 | PartialProducer producer(40, face, syncPrefix, userNode); |
| 120 | |
| 121 | // Sync interest with no bloom filter attached |
| 122 | Name syncInterestName(syncPrefix); |
| 123 | syncInterestName.append("sync"); |
| 124 | producer.m_iblt.appendToName(syncInterestName); |
| 125 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
| 126 | |
| 127 | // Sync interest with malicious bloom filter |
| 128 | syncInterestName = syncPrefix; |
| 129 | syncInterestName.append("sync"); |
| 130 | syncInterestName.appendNumber(20); // count of bloom filter |
| 131 | syncInterestName.appendNumber(1); // false positive probability * 1000 of bloom filter |
| 132 | syncInterestName.append("fake-name"); |
| 133 | producer.m_iblt.appendToName(syncInterestName); |
| 134 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
| 135 | |
| 136 | // Sync interest with malicious IBF |
| 137 | syncInterestName = syncPrefix; |
| 138 | syncInterestName.append("sync"); |
| 139 | BloomFilter bf(20, 0.001); |
| 140 | bf.appendToName(syncInterestName); |
| 141 | syncInterestName.append("fake-name"); |
| 142 | BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName))); |
| 143 | } |
| 144 | |
| 145 | BOOST_AUTO_TEST_SUITE_END() |
| 146 | |
| 147 | } // namespace psync |