blob: c472a740eb57c93a75bffe16e4b85beac9a66c13 [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -06003 * Copyright (c) 2014-2019, 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/detail/iblt.hpp"
21#include "PSync/detail/util.hpp"
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050022
23#include <boost/test/unit_test.hpp>
24#include <ndn-cxx/name.hpp>
25#include <ndn-cxx/interest.hpp>
26
27namespace psync {
28
29using namespace ndn;
30
31BOOST_AUTO_TEST_SUITE(TestIBLT)
32
33BOOST_AUTO_TEST_CASE(Equal)
34{
35 int size = 10;
36
37 IBLT iblt1(size);
38 IBLT iblt2(size);
39 BOOST_CHECK_EQUAL(iblt1, iblt2);
40
41 std::string prefix = Name("/test/memphis").appendNumber(1).toUri();
42 uint32_t newHash = murmurHash3(11, prefix);
43 iblt1.insert(newHash);
44 iblt2.insert(newHash);
45
46 BOOST_CHECK_EQUAL(iblt1, iblt2);
47
48 Name ibfName1("sync"), ibfName2("sync");
49 iblt1.appendToName(ibfName1);
50 iblt2.appendToName(ibfName2);
51 BOOST_CHECK_EQUAL(ibfName1, ibfName2);
52}
53
54BOOST_AUTO_TEST_CASE(NameAppendAndExtract)
55{
56 int size = 10;
57
58 IBLT iblt(size);
59 std::string prefix = Name("/test/memphis").appendNumber(1).toUri();
60 uint32_t newHash = murmurHash3(11, prefix);
61 iblt.insert(newHash);
62
63 Name ibltName("sync");
64 iblt.appendToName(ibltName);
65
66 IBLT rcvd(size);
67 rcvd.initialize(ibltName.get(-1));
68
69 BOOST_CHECK_EQUAL(iblt, rcvd);
70
71 IBLT rcvdDiffSize(20);
72 BOOST_CHECK_THROW(rcvdDiffSize.initialize(ibltName.get(-1)), std::runtime_error);
73}
74
75BOOST_AUTO_TEST_CASE(CopyInsertErase)
76{
77 int size = 10;
78
79 IBLT iblt1(size);
80
81 std::string prefix = Name("/test/memphis").appendNumber(1).toUri();
82 uint32_t hash1 = murmurHash3(11, prefix);
83 iblt1.insert(hash1);
84
85 IBLT iblt2(iblt1);
86 iblt2.erase(hash1);
87 prefix = Name("/test/memphis").appendNumber(2).toUri();
88 uint32_t hash3 = murmurHash3(11, prefix);
89 iblt2.insert(hash3);
90
91 iblt1.erase(hash1);
92 prefix = Name("/test/memphis").appendNumber(5).toUri();
93 uint32_t hash5 = murmurHash3(11, prefix);
94 iblt1.insert(hash5);
95
96 iblt2.erase(hash3);
97 iblt2.insert(hash5);
98
99 BOOST_CHECK_EQUAL(iblt1, iblt2);
100}
101
102BOOST_AUTO_TEST_CASE(HigherSeqTest)
103{
104 // The case where we can't recognize if the rcvd IBF has higher sequence number
105 // Relevant to full sync case
106 int size = 10;
107
108 IBLT ownIBF(size);
109 IBLT rcvdIBF(size);
110
111 std::string prefix = Name("/test/memphis").appendNumber(3).toUri();
112 uint32_t hash1 = murmurHash3(11, prefix);
113 ownIBF.insert(hash1);
114
115 std::string prefix2 = Name("/test/memphis").appendNumber(4).toUri();
116 uint32_t hash2 = murmurHash3(11, prefix2);
117 rcvdIBF.insert(hash2);
118
119 IBLT diff = ownIBF - rcvdIBF;
120 std::set<uint32_t> positive;
121 std::set<uint32_t> negative;
122
123 BOOST_CHECK(diff.listEntries(positive, negative));
124 BOOST_CHECK(*positive.begin() == hash1);
125 BOOST_CHECK(*negative.begin() == hash2);
126}
127
128BOOST_AUTO_TEST_CASE(Difference)
129{
130 int size = 10;
131
132 IBLT ownIBF(size);
133
134 IBLT rcvdIBF = ownIBF;
135
136 IBLT diff = ownIBF - rcvdIBF;
137
138 std::set<uint32_t> positive; // non-empty Positive means we have some elements that the others don't
139 std::set<uint32_t> negative;
140
141 BOOST_CHECK(diff.listEntries(positive, negative));
142 BOOST_CHECK_EQUAL(positive.size(), 0);
143 BOOST_CHECK_EQUAL(negative.size(), 0);
144
145 std::string prefix = Name("/test/memphis").appendNumber(1).toUri();
146 uint32_t newHash = murmurHash3(11, prefix);
147 ownIBF.insert(newHash);
148
149 diff = ownIBF - rcvdIBF;
150 BOOST_CHECK(diff.listEntries(positive, negative));
151 BOOST_CHECK_EQUAL(positive.size(), 1);
152 BOOST_CHECK_EQUAL(negative.size(), 0);
153
154 prefix = Name("/test/csu").appendNumber(1).toUri();
155 newHash = murmurHash3(11, prefix);
156 rcvdIBF.insert(newHash);
157
158 diff = ownIBF - rcvdIBF;
159 BOOST_CHECK(diff.listEntries(positive, negative));
160 BOOST_CHECK_EQUAL(positive.size(), 1);
161 BOOST_CHECK_EQUAL(negative.size(), 1);
162}
163
164BOOST_AUTO_TEST_CASE(DifferenceBwOversizedIBFs)
165{
166 // Insert 50 elements into IBF of size 10
167 // Check that we can still list the difference
168 // even though we can't list the IBFs itself
169
170 int size = 10;
171
172 IBLT ownIBF(size);
173
174 for (int i = 0; i < 50; i++) {
175 std::string prefix = Name("/test/memphis" + std::to_string(i)).appendNumber(1).toUri();
176 uint32_t newHash = murmurHash3(11, prefix);
177 ownIBF.insert(newHash);
178 }
179
180 IBLT rcvdIBF = ownIBF;
181
182 std::string prefix = Name("/test/ucla").appendNumber(1).toUri();
183 uint32_t newHash = murmurHash3(11, prefix);
184 ownIBF.insert(newHash);
185
186 IBLT diff = ownIBF - rcvdIBF;
187
188 std::set<uint32_t> positive;
189 std::set<uint32_t> negative;
190 BOOST_CHECK(diff.listEntries(positive, negative));
191 BOOST_CHECK_EQUAL(positive.size(), 1);
192 BOOST_CHECK_EQUAL(*positive.begin(), newHash);
193 BOOST_CHECK_EQUAL(negative.size(), 0);
194
195 BOOST_CHECK(!ownIBF.listEntries(positive, negative));
196 BOOST_CHECK(!rcvdIBF.listEntries(positive, negative));
197}
198
199BOOST_AUTO_TEST_SUITE_END()
200
201} // namespace psync