blob: 3bed1ac2702d5839fee62ad0e90067d2ed47393b [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2018, The University of Memphis
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
9 * of the GNU General Public License as published by the Free Software Foundation,
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
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 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
20#include "partial-producer.hpp"
21
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
27#include <iostream>
28
29namespace psync {
30
31using namespace ndn;
32using namespace std;
33
34BOOST_AUTO_TEST_SUITE(TestPartialProducer)
35
36BOOST_AUTO_TEST_CASE(Constructor)
37{
38 util::DummyClientFace face({true, true});
39 BOOST_REQUIRE_NO_THROW(PartialProducer(40, face, Name("/psync"), Name("/testUser")));
40}
41
42BOOST_AUTO_TEST_CASE(RegisterPrefix)
43{
44 Name syncPrefix("/psync"), userNode("/testUser");
45 util::DummyClientFace face({true, true});
46 PartialProducer producer(40, face, syncPrefix, userNode);
47
48 face.processEvents(time::milliseconds(-1));
49
50 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
51
52 face.sentInterests.back();
53 Interest interest = *face.sentInterests.begin();
54 BOOST_CHECK_EQUAL(interest.getName().get(3), name::Component("register"));
55 name::Component test = interest.getName().get(4);
56 nfd::ControlParameters params(test.blockFromValue());
57 BOOST_CHECK_EQUAL(params.getName(), syncPrefix);
58}
59
60BOOST_AUTO_TEST_CASE(PublishName)
61{
62 Name syncPrefix("/psync"), userNode("/testUser"), nonUser("/testUser2");
63 util::DummyClientFace face({true, true});
64 PartialProducer producer(40, face, syncPrefix, userNode);
65
66 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 0);
67 producer.publishName(userNode);
68 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 1);
69
70 producer.publishName(userNode);
71 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 2);
72
73 producer.publishName(userNode, 10);
74 BOOST_CHECK_EQUAL(producer.getSeqNo(userNode).value_or(-1), 10);
75
76 producer.publishName(nonUser);
77 BOOST_CHECK_EQUAL(producer.getSeqNo(nonUser).value_or(-1), -1);
78}
79
80BOOST_AUTO_TEST_CASE(SameSyncInterest)
81{
82 Name syncPrefix("/psync"), userNode("/testUser");
83 util::DummyClientFace face({true, true});
84 PartialProducer producer(40, face, syncPrefix, userNode);
85
86 Name syncInterestName(syncPrefix);
87 syncInterestName.append("sync");
88
89 BloomFilter bf(20, 0.001);
90 bf.appendToName(syncInterestName);
91
92 producer.m_iblt.appendToName(syncInterestName);
93
94 Interest syncInterest(syncInterestName);
95 syncInterest.setInterestLifetime(time::milliseconds(1000));
96 syncInterest.setNonce(1);
97 BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, syncInterest));
98 face.processEvents(time::milliseconds(10));
99 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
100
101 face.processEvents(time::milliseconds(500));
102
103 // Same interest again - size of pending interest should remain same, but expirationEvent should change
104 syncInterest.setNonce(2);
105 BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, syncInterest));
106 face.processEvents(time::milliseconds(10));
107 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
108
109 face.processEvents(time::milliseconds(500));
110 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 1);
111
112 face.processEvents(time::milliseconds(500));
113 BOOST_CHECK_EQUAL(producer.m_pendingEntries.size(), 0);
114}
115
116BOOST_AUTO_TEST_CASE(OnSyncInterest)
117{
118 Name syncPrefix("/psync"), userNode("/testUser");
119 util::DummyClientFace face({true, true});
120 PartialProducer producer(40, face, syncPrefix, userNode);
121
122 // Sync interest with no bloom filter attached
123 Name syncInterestName(syncPrefix);
124 syncInterestName.append("sync");
125 producer.m_iblt.appendToName(syncInterestName);
126 BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
127
128 // Sync interest with malicious bloom filter
129 syncInterestName = syncPrefix;
130 syncInterestName.append("sync");
131 syncInterestName.appendNumber(20); // count of bloom filter
132 syncInterestName.appendNumber(1); // false positive probability * 1000 of bloom filter
133 syncInterestName.append("fake-name");
134 producer.m_iblt.appendToName(syncInterestName);
135 BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
136
137 // Sync interest with malicious IBF
138 syncInterestName = syncPrefix;
139 syncInterestName.append("sync");
140 BloomFilter bf(20, 0.001);
141 bf.appendToName(syncInterestName);
142 syncInterestName.append("fake-name");
143 BOOST_REQUIRE_NO_THROW(producer.onSyncInterest(syncInterestName, Interest(syncInterestName)));
144}
145
146BOOST_AUTO_TEST_SUITE_END()
147
148} // namespace psync