blob: bfbcc12351c22171f3214e3f5c9f15ae07a4eaef [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Ashlesh Gawande5a895472020-01-25 18:07:32 -08003 * Copyright (c) 2014-2020, 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/partial-producer.hpp"
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050021
Davide Pesavento5b3cf762020-04-03 16:20:04 -040022#include "tests/boost-test.hpp"
23
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050024#include <ndn-cxx/name.hpp>
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050025#include <ndn-cxx/mgmt/nfd/control-parameters.hpp>
Davide Pesavento5b3cf762020-04-03 16:20:04 -040026#include <ndn-cxx/util/dummy-client-face.hpp>
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050027
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050028namespace psync {
29
30using namespace ndn;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050031
32BOOST_AUTO_TEST_SUITE(TestPartialProducer)
33
34BOOST_AUTO_TEST_CASE(Constructor)
35{
36 util::DummyClientFace face({true, true});
Davide Pesaventof91d1df2020-11-25 14:50:41 -050037 BOOST_CHECK_NO_THROW(PartialProducer(40, face, Name("/psync"), Name("/testUser")));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050038}
39
40BOOST_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
Davide Pesaventof91d1df2020-11-25 14:50:41 -050046 face.processEvents(-1_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050047
48 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
Davide Pesavento5b3cf762020-04-03 16:20:04 -040049 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 Gawande0b2897e2018-06-20 14:40:47 -050052 BOOST_CHECK_EQUAL(params.getName(), syncPrefix);
53}
54
55BOOST_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
75BOOST_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 Gawande2e82df12018-12-08 21:42:29 -060083 Name syncInterestPrefix = syncInterestName;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050084
Davide Pesaventodb789562020-12-19 23:01:08 -050085 detail::BloomFilter bf(20, 0.001);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050086 bf.appendToName(syncInterestName);
87
88 producer.m_iblt.appendToName(syncInterestName);
89
90 Interest syncInterest(syncInterestName);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050091 syncInterest.setInterestLifetime(1_s);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050092 syncInterest.setNonce(1);
Davide Pesaventof91d1df2020-11-25 14:50:41 -050093 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
94 face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050095 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
96
Davide Pesaventof91d1df2020-11-25 14:50:41 -050097 face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050098
99 // Same interest again - size of pending interest should remain same, but expirationEvent should change
100 syncInterest.setNonce(2);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500101 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
102 face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500103 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
104
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500105 face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500106 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
107
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500108 face.processEvents(500_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500109 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 0);
110}
111
112BOOST_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);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500122 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500123
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);
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500131 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500132
133 // Sync interest with malicious IBF
134 syncInterestName = syncPrefix;
135 syncInterestName.append("sync");
Davide Pesaventodb789562020-12-19 23:01:08 -0500136 detail::BloomFilter bf(20, 0.001);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500137 bf.appendToName(syncInterestName);
138 syncInterestName.append("fake-name");
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500139 BOOST_CHECK_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500140}
141
142BOOST_AUTO_TEST_SUITE_END()
143
Davide Pesavento5b3cf762020-04-03 16:20:04 -0400144} // namespace psync