blob: 22624b658b26a1b64e1c20ebb23e40357d04fc53 [file] [log] [blame]
spirosmastorakis4ff8c872016-04-14 09:51:38 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3* Copyright (c) 2016 Regents of the University of California.
4*
5* This file is part of the nTorrent codebase.
6*
7* nTorrent is free software: you can redistribute it and/or modify it under the
8* terms of the GNU Lesser General Public License as published by the Free Software
9* Foundation, either version 3 of the License, or (at your option) any later version.
10*
11* nTorrent is distributed in the hope that it will be useful, but WITHOUT ANY
12* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14*
15* You should have received copies of the GNU General Public License and GNU Lesser
16* General Public License along with nTorrent, e.g., in COPYING.md file. If not, see
17* <http://www.gnu.org/licenses/>.
18*
19* See AUTHORS for complete list of nTorrent authors and contributors.
20*/
21
22#include "boost-test.hpp"
23#include "update-handler.hpp"
24#include "unit-test-time-fixture.hpp"
25#include "dummy-parser-fixture.hpp"
26
27#include <ndn-cxx/name.hpp>
28#include <ndn-cxx/util/dummy-client-face.hpp>
29#include <ndn-cxx/util/io.hpp>
30
31namespace ndn {
32namespace ntorrent {
33namespace tests {
34
35using util::DummyClientFace;
36using std::vector;
37
38class TestUpdateHandler : public UpdateHandler {
39public:
40 TestUpdateHandler(Name torrentName, shared_ptr<KeyChain> keyChain,
41 shared_ptr<StatsTable> statsTable, shared_ptr<Face> face)
42 : UpdateHandler(torrentName, keyChain, statsTable, face)
43 {
44 }
45
46 ~TestUpdateHandler()
47 {
48 }
49
50 Name
51 getOwnRoutablePrefix()
52 {
53 return UpdateHandler::getOwnRoutablePrefix();
54 }
55};
56
57class FaceFixture : public UnitTestTimeFixture
58{
59public:
60 explicit
61 FaceFixture()
62 : face1(util::makeDummyClientFace(io, { true, true }))
63 , face2(util::makeDummyClientFace(io, { true, true }))
64 {
65 }
66
67public:
68 shared_ptr<DummyClientFace> face1;
69 shared_ptr<DummyClientFace> face2;
70};
71
72BOOST_FIXTURE_TEST_SUITE(TestUpdateHandlerClass, FaceFixture)
73
74BOOST_AUTO_TEST_CASE(TestInitialSetup)
75{
76 shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
77
78 shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
79
80 TestUpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
81 advanceClocks(time::milliseconds(1), 10);
82 // Create a data packet containing one name as content
83 shared_ptr<Data> d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
84 { Name("ucla") });
85 keyChain->sign(*d);
86 face1->receive(*d);
87
88 BOOST_CHECK_EQUAL(handler1.getOwnRoutablePrefix().toUri(), "/ucla");
89
90 shared_ptr<StatsTable> table2 = make_shared<StatsTable>(Name("linux15.01"));
91 TestUpdateHandler handler2(Name("linux15.01"), keyChain, table2, face2);
92 advanceClocks(time::milliseconds(1), 10);
93 // Create a data packet containing one name as content
94 d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
95 { Name("arizona") });
96 keyChain->sign(*d);
97 face2->receive(*d);
98
99 BOOST_CHECK_EQUAL(handler2.getOwnRoutablePrefix().toUri(), "/arizona");
100}
101
102BOOST_AUTO_TEST_CASE(TestAliveInterestExchange)
103{
104 shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
105 table1->insert(Name("isp1"));
106 table1->insert(Name("isp2"));
107 table1->insert(Name("isp3"));
108
109 shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
110
111 TestUpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
112 advanceClocks(time::milliseconds(1), 10);
113 // Create a data packet containing one name as content
114 shared_ptr<Data> d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
115 { Name("ucla") });
116 keyChain->sign(*d);
117 face1->receive(*d);
118
119 shared_ptr<StatsTable> table2 = make_shared<StatsTable>(Name("linux15.01"));
120 table2->insert(Name("ucla"));
121 TestUpdateHandler handler2(Name("linux15.01"), keyChain, table2, face2);
122 advanceClocks(time::milliseconds(1), 10);
123 // Create a data packet containing one name as content
124 d = DummyParser::createDataPacket(Name("/localhop/nfd/rib/routable-prefixes"),
125 { Name("arizona") });
126 keyChain->sign(*d);
127 face2->receive(*d);
128
129 handler2.sendAliveInterest(table2->begin());
130
131 advanceClocks(time::milliseconds(1), 40);
132 Interest interest(Name("/NTORRENT/linux15.01/ALIVE/test"));
133 face1->receive(interest);
134
135 advanceClocks(time::milliseconds(1), 10);
136 std::vector<Data> dataVec = face1->sentData;
137
138 BOOST_CHECK_EQUAL(dataVec.size(), 1);
139 BOOST_CHECK_EQUAL(dataVec[0].getName().toUri(), "/NTORRENT/linux15.01/ALIVE/test");
140
141 auto block = dataVec[0].getContent();
142 shared_ptr<vector<Name>> nameVec = DummyParser::decodeContent(block);
143 auto it = nameVec->begin();
144
145 BOOST_CHECK_EQUAL(it->toUri(), "/test");
146 ++it;
147 BOOST_CHECK_EQUAL(it->toUri(), "/isp3");
148 ++it;
149 BOOST_CHECK_EQUAL(it->toUri(), "/isp2");
150 ++it;
151 BOOST_CHECK_EQUAL(it->toUri(), "/isp1");
152
153 BOOST_CHECK_EQUAL((table1->end() - 1)->getRecordName().toUri(), "/test");
154
155 d = DummyParser::createDataPacket(Name("/NTORRENT/linux15.01/ALIVE/arizona"),
156 { Name("isp1"), Name("isp2"), Name("isp3") });
157 keyChain->sign(*d);
158
159 advanceClocks(time::milliseconds(1), 40);
160 face2->receive(*d);
161
162 auto i = table2->begin();
163 ++i;
164 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
165 ++i;
166 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
167 ++i;
168 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
169
170 table1->erase(Name("isp1"));
171 table1->erase(Name("isp2"));
172
173 table1->insert(Name("isp4"));
174 table1->insert(Name("isp5"));
175 table1->insert(Name("isp6"));
176 table1->insert(Name("isp7"));
177 table1->insert(Name("isp8"));
178 table1->insert(Name("isp9"));
179
180 handler2.sendAliveInterest(table2->begin());
181
182 advanceClocks(time::milliseconds(1), 40);
183 Interest interest2(Name("/NTORRENT/linux15.01/ALIVE/arizona"));
184 face1->receive(interest2);
185
186 advanceClocks(time::milliseconds(1), 10);
187
188 dataVec = face1->sentData;
189 BOOST_CHECK_EQUAL(dataVec.size(), 2);
190
191 auto iter = dataVec.begin() + 1;
192 advanceClocks(time::milliseconds(1), 30);
193 face2->receive(*iter);
194
195 i = table2->begin();
196 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/ucla");
197 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
198 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
199 BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
200 ++i;
201
202 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp1");
203 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
204 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
205 BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
206 ++i;
207
208 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp2");
209 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
210 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
211 BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
212 ++i;
213
214 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp3");
215 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
216 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
217 BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
218 ++i;
219
220 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/test");
221 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
222 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
223 BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
224 ++i;
225
226 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp4");
227 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
228 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
229 BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
230 ++i;
231
232 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp5");
233 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
234 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
235 BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
236 ++i;
237
238 BOOST_CHECK_EQUAL(i->getRecordName().toUri(), "/isp6");
239 BOOST_CHECK_EQUAL(i->getRecordSuccessRate(), 0);
240 BOOST_CHECK_EQUAL(i->getRecordSentInterests(), 0);
241 BOOST_CHECK_EQUAL(i->getRecordReceivedData(), 0);
242 ++i;
243
244 BOOST_CHECK(i == table2->end());
245}
246
247BOOST_AUTO_TEST_CASE(TestNeedsUpdate)
248{
249 shared_ptr<StatsTable> table1 = make_shared<StatsTable>(Name("linux15.01"));
250 table1->insert(Name("isp1"));
251 table1->insert(Name("isp2"));
252 table1->insert(Name("isp3"));
253
254 shared_ptr<KeyChain> keyChain = make_shared<KeyChain>();
255
256 UpdateHandler handler1(Name("linux15.01"), keyChain, table1, face1);
257
258 BOOST_CHECK(handler1.needsUpdate());
259
260 auto i = table1->begin() + 1;
261 i->incrementSentInterests();
262 i->incrementSentInterests();
263 i->incrementReceivedData();
264
265 BOOST_CHECK(handler1.needsUpdate());
266
267 table1->insert(Name("isp4"));
268 table1->insert(Name("isp5"));
269
270 BOOST_CHECK(!handler1.needsUpdate());
271}
272
273BOOST_AUTO_TEST_SUITE_END()
274
275} // namespace tests
276} // namespace ntorrent
277} // namespace ndn