blob: 83e87086778460f1dff5367148b0a07f92b22330 [file] [log] [blame]
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -06003 * Copyright (c) 2012-2018 University of California, Los Angeles
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -08004 *
5 * This file is part of ChronoSync, synchronization library for distributed realtime
6 * applications for NDN.
7 *
8 * ChronoSync 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, either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * ChronoSync 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 * ChronoSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "logic.hpp"
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050021#include "bzip2-helper.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050022
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080023#include "boost-test.hpp"
Ashlesh Gawande08784d42017-09-06 23:40:21 -050024#include "../identity-management-fixture.hpp"
25
26#include "dummy-forwarder.hpp"
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080027
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -050028#include <ndn-cxx/util/random.hpp>
29
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080030namespace chronosync {
31namespace test {
32
33using std::vector;
Ashlesh Gawande08784d42017-09-06 23:40:21 -050034using ndn::chronosync::DummyForwarder;
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080035
36class Handler
37{
38public:
Ashlesh Gawande08784d42017-09-06 23:40:21 -050039 Handler(ndn::Face& face,
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080040 const Name& syncPrefix,
41 const Name& userPrefix)
42 : logic(face,
43 syncPrefix,
44 userPrefix,
45 bind(&Handler::onUpdate, this, _1))
46 {
47 }
48
49 void
50 onUpdate(const vector<MissingDataInfo>& v)
51 {
52 for (size_t i = 0; i < v.size(); i++) {
53 update(v[i].session, v[i].high, v[i].low);
54 }
55 }
56
57 void
58 update(const Name& p, const SeqNo& high, const SeqNo& low)
59 {
60 map[p] = high;
61 }
62
63 void
64 updateSeqNo(const SeqNo& seqNo)
65 {
66 logic.updateSeqNo(seqNo);
67 }
68
Junxiao Shi0c7f56a2016-07-14 15:27:14 +000069public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080070 Logic logic;
71 std::map<Name, SeqNo> map;
72};
73
Ashlesh Gawande08784d42017-09-06 23:40:21 -050074class LogicFixture : public ndn::tests::IdentityManagementTimeFixture
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080075{
76public:
77 LogicFixture()
78 : syncPrefix("/ndn/broadcast/sync")
Ashlesh Gawande08784d42017-09-06 23:40:21 -050079 , fw(io, m_keyChain)
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080080 {
81 syncPrefix.appendVersion();
82 userPrefix[0] = Name("/user0");
83 userPrefix[1] = Name("/user1");
84 userPrefix[2] = Name("/user2");
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080085 userPrefix[3] = Name("/user3");
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080086 }
87
Junxiao Shi0c7f56a2016-07-14 15:27:14 +000088public:
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080089 Name syncPrefix;
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080090 Name userPrefix[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080091
Ashlesh Gawande08784d42017-09-06 23:40:21 -050092 DummyForwarder fw;
93 // std::unique_ptr<DummyClientFace> faces[4];
Sonu Mishra4d3a2e02017-01-18 20:27:51 -080094 shared_ptr<Handler> handler[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080095
Ashlesh Gawande08784d42017-09-06 23:40:21 -050096 // size_t readInterestOffset[4];
97 // size_t readDataOffset[4];
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -080098};
99
100BOOST_FIXTURE_TEST_SUITE(LogicTests, LogicFixture)
101
102void
103onUpdate(const vector<MissingDataInfo>& v)
104{
105}
106
107BOOST_AUTO_TEST_CASE(Constructor)
108{
109 Name syncPrefix("/ndn/broadcast/sync");
110 Name userPrefix("/user");
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500111 ndn::util::DummyClientFace face(io, {true, true});
112 BOOST_REQUIRE_NO_THROW(Logic(face, syncPrefix, userPrefix, bind(onUpdate, _1)));
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800113}
114
115BOOST_AUTO_TEST_CASE(TwoBasic)
116{
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500117 handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]);
118 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800119
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500120 handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]);
121 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800122
123 handler[0]->updateSeqNo(1);
124
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500125 advanceClocks(ndn::time::milliseconds(10), 100);
126
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800127 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500128 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800129
130 handler[0]->updateSeqNo(2);
131
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500132 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800133 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 2);
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500134 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800135
136 handler[1]->updateSeqNo(2);
137
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500138 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800139 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
140}
141
142BOOST_AUTO_TEST_CASE(ThreeBasic)
143{
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500144 handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]);
145 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800146
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500147 handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]);
148 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800149
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500150 handler[2] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]);
151 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800152
153 handler[0]->updateSeqNo(1);
154
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500155 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800156 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
157 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
158
159 handler[1]->updateSeqNo(2);
160
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500161 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800162 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
163 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
164
165 handler[2]->updateSeqNo(4);
166
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500167 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800168 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4);
169 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4);
170}
171
172BOOST_AUTO_TEST_CASE(ResetRecover)
173{
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500174 handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]);
175 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800176
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500177 handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]);
178 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800179
180 handler[0]->updateSeqNo(1);
181
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500182 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800183 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
184
185 handler[1]->updateSeqNo(2);
186
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500187 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800188 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
189
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500190 advanceClocks(ndn::time::milliseconds(10), 100);
191 handler[2] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800192
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500193 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800194 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
195 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800196
197 handler[2]->updateSeqNo(4);
198
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500199 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800200 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4);
201 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4);
202}
203
204BOOST_AUTO_TEST_CASE(RecoverConflict)
205{
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500206 handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]);
207 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800208
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500209 handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]);
210 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800211
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500212 handler[2] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]);
213 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800214
215 handler[0]->updateSeqNo(1);
216
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500217 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800218 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
219 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
220
221 handler[1]->updateSeqNo(2);
222 handler[2]->updateSeqNo(4);
223
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500224 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800225 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 2);
226 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 4);
227 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 4);
228 BOOST_CHECK_EQUAL(handler[2]->map[handler[1]->logic.getSessionName()], 2);
229}
230
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800231BOOST_AUTO_TEST_CASE(PartitionRecover)
232{
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500233 handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]);
234 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800235
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500236 handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[1]);
237 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800238
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500239 handler[2] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]);
240 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800241
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500242 handler[3] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[3]);
243 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800244
245 handler[0]->updateSeqNo(1);
246
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500247 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800248 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
249 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 1);
250 BOOST_CHECK_EQUAL(handler[3]->map[handler[0]->logic.getSessionName()], 1);
251
252 handler[2]->updateSeqNo(2);
253
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500254 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800255 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 2);
256 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 2);
257 BOOST_CHECK_EQUAL(handler[3]->map[handler[2]->logic.getSessionName()], 2);
258
259 // Network Partition start
260
261 handler[1]->updateSeqNo(3);
262
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500263 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800264 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 3);
265 handler[2]->map[handler[1]->logic.getSessionName()] = 0;
266 handler[3]->map[handler[1]->logic.getSessionName()] = 0;
267
268 handler[3]->updateSeqNo(4);
269
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500270 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800271 BOOST_CHECK_EQUAL(handler[2]->map[handler[3]->logic.getSessionName()], 4);
272 handler[0]->map[handler[3]->logic.getSessionName()] = 0;
273 handler[1]->map[handler[3]->logic.getSessionName()] = 0;
274
275 // Network partition over
276
277 handler[0]->updateSeqNo(5);
278
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500279 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800280 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 5);
281 BOOST_CHECK_EQUAL(handler[2]->map[handler[0]->logic.getSessionName()], 5);
282 BOOST_CHECK_EQUAL(handler[3]->map[handler[0]->logic.getSessionName()], 5);
283
284 handler[2]->updateSeqNo(6);
285
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500286 advanceClocks(ndn::time::milliseconds(10), 100);
Sonu Mishra4d3a2e02017-01-18 20:27:51 -0800287 BOOST_CHECK_EQUAL(handler[0]->map[handler[2]->logic.getSessionName()], 6);
288 BOOST_CHECK_EQUAL(handler[1]->map[handler[2]->logic.getSessionName()], 6);
289 BOOST_CHECK_EQUAL(handler[3]->map[handler[2]->logic.getSessionName()], 6);
290}
291
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800292BOOST_AUTO_TEST_CASE(MultipleUserUnderOneLogic)
293{
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500294 handler[0] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[0]);
295 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800296
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500297 handler[1] = make_shared<Handler>(ref(fw.addFace()), syncPrefix, userPrefix[2]);
298 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800299
300 handler[0]->logic.addUserNode(userPrefix[1]);
301
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500302 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800303
304 handler[0]->updateSeqNo(1);
305
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500306 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800307 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName()], 1);
308
309 handler[0]->logic.updateSeqNo(2, userPrefix[1]);
310
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500311 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800312 BOOST_CHECK_EQUAL(handler[1]->map[handler[0]->logic.getSessionName(userPrefix[1])], 2);
313
314 handler[1]->updateSeqNo(4);
315
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500316 advanceClocks(ndn::time::milliseconds(10), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800317 BOOST_CHECK_EQUAL(handler[0]->map[handler[1]->logic.getSessionName()], 4);
318
319 handler[0]->logic.removeUserNode(userPrefix[0]);
320
Ashlesh Gawande08784d42017-09-06 23:40:21 -0500321 advanceClocks(ndn::time::milliseconds(50), 100);
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800322 BOOST_CHECK_EQUAL(handler[1]->logic.getSessionNames().size(), 2);
323}
324
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500325BOOST_FIXTURE_TEST_CASE(TrimState, ndn::tests::IdentityManagementTimeFixture)
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600326{
327 Name syncPrefix("/ndn/broadcast/sync");
328 Name userPrefix("/user");
329 ndn::util::DummyClientFace face;
330 Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1));
331
332 State state;
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500333 for (size_t i = 0; i != 100; ++i) {
334 state.update(Name("/to/trim").appendNumber(i), 42);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600335 }
336
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500337 State partial;
338 logic.trimState(partial, state, 1);
339 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 99);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600340
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500341 logic.trimState(partial, state, 100);
342 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600343
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500344 logic.trimState(partial, state, 101);
345 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 1);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600346
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500347 logic.trimState(partial, state, 42);
348 BOOST_CHECK_EQUAL(partial.getLeaves().size(), 58);
349}
350
351BOOST_FIXTURE_TEST_CASE(VeryLargeState, ndn::tests::IdentityManagementTimeFixture)
352{
353 addIdentity("/bla");
354 Name syncPrefix("/ndn/broadcast/sync");
355 Name userPrefix("/user");
356 ndn::util::DummyClientFace face;
357 Logic logic(face, syncPrefix, userPrefix, bind(onUpdate, _1));
358
359 State state;
360 for (size_t i = 0; i < 50000 && bzip2::compress(reinterpret_cast<const char*>(state.wireEncode().wire()),
361 state.wireEncode().size())->size() < ndn::MAX_NDN_PACKET_SIZE;
362 i += 10) {
363 Name prefix("/to/trim");
364 prefix.appendNumber(i);
365 for (size_t j = 0; j != 20; ++j) {
366 prefix.appendNumber(ndn::random::generateWord32());
367 }
368 state.update(prefix, ndn::random::generateWord32());
369 }
370 BOOST_TEST_MESSAGE("Got state with " << state.getLeaves().size() << " leaves");
371
372 auto data = logic.encodeSyncReply(userPrefix, "/fake/prefix/of/interest", state);
373 BOOST_CHECK_LE(data.wireEncode().size(), ndn::MAX_NDN_PACKET_SIZE);
Ashlesh Gawande4a9ecd52018-02-06 14:36:19 -0600374}
375
Alexander Afanasyev89036292018-02-13 17:19:50 -0500376class MaxPacketCustomizationFixture
377{
378public:
379 MaxPacketCustomizationFixture()
380 {
381 if (getenv("CHRONOSYNC_MAX_PACKET_SIZE") != nullptr) {
382 oldSize = std::string(getenv("CHRONOSYNC_MAX_PACKET_SIZE"));
383 unsetenv("CHRONOSYNC_MAX_PACKET_SIZE");
384 }
385 }
386
387 ~MaxPacketCustomizationFixture()
388 {
Alexander Afanasyev6ee98ff2018-02-13 19:12:28 -0500389 unsetenv("CHRONOSYNC_MAX_PACKET_SIZE");
Alexander Afanasyev89036292018-02-13 17:19:50 -0500390 if (oldSize) {
391 setenv("CHRONOSYNC_MAX_PACKET_SIZE", oldSize->c_str(), 1);
392 }
393 }
394private:
395 ndn::optional<std::string> oldSize;
396};
397
398BOOST_FIXTURE_TEST_CASE(MaxPacketCustomization, MaxPacketCustomizationFixture)
399{
400 BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE);
401
402 setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1500", 1);
403 BOOST_CHECK_EQUAL(getMaxPacketLimit(), 1500);
404
405 setenv("CHRONOSYNC_MAX_PACKET_SIZE", ndn::to_string(ndn::MAX_NDN_PACKET_SIZE * 100).c_str(), 1);
406 BOOST_CHECK_EQUAL(getMaxPacketLimit(), ndn::MAX_NDN_PACKET_SIZE);
407
408 setenv("CHRONOSYNC_MAX_PACKET_SIZE", "1", 1);
409 BOOST_CHECK_EQUAL(getMaxPacketLimit(), 500);
410}
411
Qiuhan Dingfb8c9e02015-01-30 14:04:55 -0800412BOOST_AUTO_TEST_SUITE_END()
413
414} // namespace test
415} // namespace chronosync