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 | 6cda3e7 | 2024-01-19 12:49:21 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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/detail/iblt.hpp" |
| 21 | #include "PSync/detail/util.hpp" |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 22 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
| 24 | |
Davide Pesavento | 47eb6d9 | 2024-02-12 20:25:51 -0500 | [diff] [blame] | 25 | namespace psync::tests { |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 26 | |
Davide Pesavento | 47eb6d9 | 2024-02-12 20:25:51 -0500 | [diff] [blame] | 27 | using namespace psync::detail; |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 28 | using ndn::Name; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestIBLT) |
| 31 | |
| 32 | BOOST_AUTO_TEST_CASE(Equal) |
| 33 | { |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 34 | const size_t size = 10; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 35 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 36 | IBLT iblt1(size, CompressionScheme::DEFAULT); |
| 37 | IBLT iblt2(size, CompressionScheme::DEFAULT); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 38 | BOOST_CHECK_EQUAL(iblt1, iblt2); |
| 39 | |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 40 | auto prefix = Name("/test/memphis").appendNumber(1); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 41 | uint32_t newHash = murmurHash3(11, prefix); |
| 42 | iblt1.insert(newHash); |
| 43 | iblt2.insert(newHash); |
| 44 | |
| 45 | BOOST_CHECK_EQUAL(iblt1, iblt2); |
| 46 | |
| 47 | Name ibfName1("sync"), ibfName2("sync"); |
| 48 | iblt1.appendToName(ibfName1); |
| 49 | iblt2.appendToName(ibfName2); |
| 50 | BOOST_CHECK_EQUAL(ibfName1, ibfName2); |
| 51 | } |
| 52 | |
Ashlesh Gawande | 79c5baf | 2020-02-16 12:29:52 -0800 | [diff] [blame] | 53 | static const uint8_t IBF[] = { |
| 54 | // Header |
| 55 | 0x08, 0xB4, |
| 56 | // Uncompressed IBF |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 57 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 58 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x5C, 0x5B, 0xF2, 0x67, |
| 59 | 0x42, 0x24, 0xEE, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 61 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x5C, 0x5B, 0xF2, 0x67, |
| 62 | 0x42, 0x24, 0xEE, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Ashlesh Gawande | 79c5baf | 2020-02-16 12:29:52 -0800 | [diff] [blame] | 63 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 64 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 65 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 66 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 67 | 0x5C, 0x5B, 0xF2, 0x67, 0x42, 0x24, 0xEE, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
Ashlesh Gawande | 79c5baf | 2020-02-16 12:29:52 -0800 | [diff] [blame] | 68 | 0x00, 0x00, 0x00, 0x00 |
| 69 | }; |
| 70 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 71 | BOOST_AUTO_TEST_CASE(NameAppendAndExtract) |
| 72 | { |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 73 | const int size = 10; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 74 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 75 | IBLT iblt(size, CompressionScheme::DEFAULT); |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 76 | auto prefix = Name("/test/memphis").appendNumber(1); |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 77 | auto hash = murmurHash3(11, prefix); |
| 78 | iblt.insert(hash); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 79 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 80 | Name ibltName("/sync"); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 81 | iblt.appendToName(ibltName); |
| 82 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 83 | IBLT rcvd(size, CompressionScheme::DEFAULT); |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 84 | rcvd.initialize(ibltName.at(-1)); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(iblt, rcvd); |
| 86 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 87 | IBLT rcvdDiffSize(20, CompressionScheme::DEFAULT); |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 88 | BOOST_CHECK_THROW(rcvdDiffSize.initialize(ibltName.at(-1)), IBLT::Error); |
Ashlesh Gawande | 79c5baf | 2020-02-16 12:29:52 -0800 | [diff] [blame] | 89 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 90 | IBLT iblt2(size, CompressionScheme::NONE); |
| 91 | iblt2.insert(hash); |
| 92 | Name ibltName2("/sync"); |
| 93 | iblt2.appendToName(ibltName2); |
Davide Pesavento | c407dee | 2022-07-21 23:56:05 -0400 | [diff] [blame] | 94 | BOOST_TEST(ibltName2.at(-1).wireEncode() == IBF, boost::test_tools::per_element()); |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 95 | |
| 96 | Name malformedName("/test"); |
Ashlesh Gawande | 79c5baf | 2020-02-16 12:29:52 -0800 | [diff] [blame] | 97 | auto compressed = compress(CompressionScheme::DEFAULT, |
Davide Pesavento | f6fd2fb | 2022-03-18 21:00:36 -0400 | [diff] [blame] | 98 | malformedName.wireEncode().value_bytes()); |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 99 | malformedName.append(Name::Component(std::move(compressed))); |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 100 | IBLT rcvd2(size, CompressionScheme::DEFAULT); |
| 101 | BOOST_CHECK_THROW(rcvd2.initialize(malformedName.at(-1)), IBLT::Error); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(CopyInsertErase) |
| 105 | { |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 106 | const size_t size = 10; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 107 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 108 | IBLT iblt1(size, CompressionScheme::DEFAULT); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 109 | |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 110 | auto prefix = Name("/test/memphis").appendNumber(1); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 111 | uint32_t hash1 = murmurHash3(11, prefix); |
| 112 | iblt1.insert(hash1); |
| 113 | |
| 114 | IBLT iblt2(iblt1); |
| 115 | iblt2.erase(hash1); |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 116 | prefix = Name("/test/memphis").appendNumber(2); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 117 | uint32_t hash3 = murmurHash3(11, prefix); |
| 118 | iblt2.insert(hash3); |
| 119 | |
| 120 | iblt1.erase(hash1); |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 121 | prefix = Name("/test/memphis").appendNumber(5); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 122 | uint32_t hash5 = murmurHash3(11, prefix); |
| 123 | iblt1.insert(hash5); |
| 124 | |
| 125 | iblt2.erase(hash3); |
| 126 | iblt2.insert(hash5); |
| 127 | |
| 128 | BOOST_CHECK_EQUAL(iblt1, iblt2); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(HigherSeqTest) |
| 132 | { |
| 133 | // The case where we can't recognize if the rcvd IBF has higher sequence number |
| 134 | // Relevant to full sync case |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 135 | |
| 136 | const size_t size = 10; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 137 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 138 | IBLT ownIBF(size, CompressionScheme::DEFAULT); |
| 139 | IBLT rcvdIBF(size, CompressionScheme::DEFAULT); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 140 | |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 141 | auto prefix = Name("/test/memphis").appendNumber(3); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 142 | uint32_t hash1 = murmurHash3(11, prefix); |
| 143 | ownIBF.insert(hash1); |
| 144 | |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 145 | auto prefix2 = Name("/test/memphis").appendNumber(4); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 146 | uint32_t hash2 = murmurHash3(11, prefix2); |
| 147 | rcvdIBF.insert(hash2); |
| 148 | |
Junxiao Shi | 6cda3e7 | 2024-01-19 12:49:21 +0000 | [diff] [blame] | 149 | auto diff = ownIBF - rcvdIBF; |
| 150 | BOOST_CHECK(diff.canDecode); |
| 151 | BOOST_CHECK(*diff.positive.begin() == hash1); |
| 152 | BOOST_CHECK(*diff.negative.begin() == hash2); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(Difference) |
| 156 | { |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 157 | const size_t size = 10; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 158 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 159 | IBLT ownIBF(size, CompressionScheme::DEFAULT); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 160 | IBLT rcvdIBF = ownIBF; |
| 161 | |
Junxiao Shi | 6cda3e7 | 2024-01-19 12:49:21 +0000 | [diff] [blame] | 162 | auto diff = ownIBF - rcvdIBF; |
| 163 | // non-empty Positive means we have some elements that the others don't |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 164 | |
Junxiao Shi | 6cda3e7 | 2024-01-19 12:49:21 +0000 | [diff] [blame] | 165 | BOOST_CHECK(diff.canDecode); |
| 166 | BOOST_CHECK_EQUAL(diff.positive.size(), 0); |
| 167 | BOOST_CHECK_EQUAL(diff.negative.size(), 0); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 168 | |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 169 | auto prefix = Name("/test/memphis").appendNumber(1); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 170 | uint32_t newHash = murmurHash3(11, prefix); |
| 171 | ownIBF.insert(newHash); |
| 172 | |
| 173 | diff = ownIBF - rcvdIBF; |
Junxiao Shi | 6cda3e7 | 2024-01-19 12:49:21 +0000 | [diff] [blame] | 174 | BOOST_CHECK(diff.canDecode); |
| 175 | BOOST_CHECK_EQUAL(diff.positive.size(), 1); |
| 176 | BOOST_CHECK_EQUAL(diff.negative.size(), 0); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 177 | |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 178 | prefix = Name("/test/csu").appendNumber(1); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 179 | newHash = murmurHash3(11, prefix); |
| 180 | rcvdIBF.insert(newHash); |
| 181 | |
| 182 | diff = ownIBF - rcvdIBF; |
Junxiao Shi | 6cda3e7 | 2024-01-19 12:49:21 +0000 | [diff] [blame] | 183 | BOOST_CHECK(diff.canDecode); |
| 184 | BOOST_CHECK_EQUAL(diff.positive.size(), 1); |
| 185 | BOOST_CHECK_EQUAL(diff.negative.size(), 1); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_CASE(DifferenceBwOversizedIBFs) |
| 189 | { |
| 190 | // Insert 50 elements into IBF of size 10 |
| 191 | // Check that we can still list the difference |
| 192 | // even though we can't list the IBFs itself |
| 193 | |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 194 | const size_t size = 10; |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 195 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 196 | IBLT ownIBF(size, CompressionScheme::DEFAULT); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 197 | |
| 198 | for (int i = 0; i < 50; i++) { |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 199 | auto prefix = Name("/test/memphis" + std::to_string(i)).appendNumber(1); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 200 | uint32_t newHash = murmurHash3(11, prefix); |
| 201 | ownIBF.insert(newHash); |
| 202 | } |
| 203 | |
| 204 | IBLT rcvdIBF = ownIBF; |
| 205 | |
Junxiao Shi | 32ccfc4 | 2022-01-09 21:26:22 +0000 | [diff] [blame] | 206 | auto prefix = Name("/test/ucla").appendNumber(1); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 207 | uint32_t newHash = murmurHash3(11, prefix); |
| 208 | ownIBF.insert(newHash); |
| 209 | |
Junxiao Shi | 6cda3e7 | 2024-01-19 12:49:21 +0000 | [diff] [blame] | 210 | auto diff = ownIBF - rcvdIBF; |
| 211 | BOOST_CHECK(diff.canDecode); |
| 212 | BOOST_CHECK_EQUAL(diff.positive.size(), 1); |
| 213 | BOOST_CHECK_EQUAL(*diff.positive.begin(), newHash); |
| 214 | BOOST_CHECK_EQUAL(diff.negative.size(), 0); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 215 | |
Junxiao Shi | 6cda3e7 | 2024-01-19 12:49:21 +0000 | [diff] [blame] | 216 | IBLT emptyIBF(size, CompressionScheme::DEFAULT); |
| 217 | BOOST_CHECK(!(ownIBF - emptyIBF).canDecode); |
| 218 | BOOST_CHECK(!(rcvdIBF - emptyIBF).canDecode); |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_SUITE_END() |
| 222 | |
Davide Pesavento | 47eb6d9 | 2024-02-12 20:25:51 -0500 | [diff] [blame] | 223 | } // namespace psync::tests |