blob: aa5df372bda1b2389d962b91d1cbcfaec6ce1009 [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/partial-producer.hpp"
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050021
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 Gawande0b2897e2018-06-20 14:40:47 -050027namespace psync {
28
29using namespace ndn;
30using namespace std;
31
32BOOST_AUTO_TEST_SUITE(TestPartialProducer)
33
34BOOST_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
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
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
58BOOST_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
78BOOST_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 Gawande2e82df12018-12-08 21:42:29 -060086 Name syncInterestPrefix = syncInterestName;
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050087
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 Gawande2e82df12018-12-08 21:42:29 -060096 BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050097 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 Gawande2e82df12018-12-08 21:42:29 -0600104 BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestPrefix, syncInterest));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -0500105 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
115BOOST_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
145BOOST_AUTO_TEST_SUITE_END()
146
147} // namespace psync