blob: 33c364310f20287c01950e8c1957c189978ec370 [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library 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 * ndn-cxx library 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 ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "face.hpp"
23#include "util/scheduler.hpp"
24#include "security/key-chain.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080025#include "util/dummy-client-face.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040026
27#include "boost-test.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080028#include "unit-test-time-fixture.hpp"
29#include "test-make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040030
31namespace ndn {
32namespace tests {
33
Junxiao Shia60d9362014-11-12 09:38:21 -070034using ndn::util::DummyClientFace;
35using ndn::util::makeDummyClientFace;
36
Junxiao Shia1ea5062014-12-27 22:33:39 -070037class FaceFixture : public UnitTestTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040038{
39public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080040 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070041 FaceFixture(bool enableRegistrationReply = true)
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080042 : face(makeDummyClientFace(io, { true, enableRegistrationReply }))
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040043 {
44 }
45
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080046public:
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040047 shared_ptr<DummyClientFace> face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040048};
49
Junxiao Shia1ea5062014-12-27 22:33:39 -070050class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070051{
52public:
53 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070054 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070055 {
56 }
57};
58
Junxiao Shia1ea5062014-12-27 22:33:39 -070059BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040060
61BOOST_AUTO_TEST_CASE(ExpressInterestData)
62{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080063 size_t nData = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040064 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080065 [&] (const Interest& i, const Data& d) {
66 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
67 ++nData;
68 },
69 bind([] {
70 BOOST_FAIL("Unexpected timeout");
71 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040072
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080073 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040074
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080075 face->receive(*util::makeData("/Bye/World/!"));
76 face->receive(*util::makeData("/Hello/World/!"));
77
78 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040079
80 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080081 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
82 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040083}
84
85BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
86{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080087 size_t nTimeouts = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040088 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080089 bind([] {
90 BOOST_FAIL("Unexpected data");
91 }),
92 bind([&nTimeouts] {
93 ++nTimeouts;
94 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040095
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080096 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040097
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040098 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080099 BOOST_CHECK_EQUAL(face->sentInterests.size(), 1);
100 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400101}
102
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800103BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400104{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800105 const PendingInterestId* interestId =
106 face->expressInterest(Interest("/Hello/World", time::milliseconds(50)),
107 bind([] {
108 BOOST_FAIL("Unexpected data");
109 }),
110 bind([] {
111 BOOST_FAIL("Unexpected timeout");
112 }));
113 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400114
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800115 face->removePendingInterest(interestId);
116 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400117
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800118 face->receive(*util::makeData("/Hello/World/!"));
119 advanceClocks(time::milliseconds(10), 100);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400120}
121
122BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
123{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800124 size_t nInterests = 0;
125 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400126 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800127 face->setInterestFilter("/Hello/World",
128 bind([&nInterests] { ++nInterests; }),
129 bind([&nRegs] { ++nRegs; }),
130 bind([] {
131 BOOST_FAIL("Unexpected setInterestFilter failure");
132 }));
133 advanceClocks(time::milliseconds(10), 10);
134 BOOST_CHECK_EQUAL(nRegs, 1);
135 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400136
137 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800138 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400139
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800140 BOOST_CHECK_EQUAL(nRegs, 1);
141 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400142
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800143 face->receive(Interest("/Bye/World/!"));
144 advanceClocks(time::milliseconds(10000), 10);
145 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400146
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800147 face->receive(Interest("/Hello/World/!/2"));
148 advanceClocks(time::milliseconds(10), 10);
149 BOOST_CHECK_EQUAL(nInterests, 2);
150
151 // removing filter
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400152 face->unsetInterestFilter(regPrefixId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800153 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400154
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800155 face->receive(Interest("/Hello/World/!/3"));
156 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400157
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800158 face->unsetInterestFilter(static_cast<const RegisteredPrefixId*>(0));
159 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400160
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800161 face->unsetInterestFilter(static_cast<const InterestFilterId*>(0));
162 advanceClocks(time::milliseconds(10), 10);
163}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400164
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800165BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
166{
167 // don't enable registration reply
168 size_t nRegFailed = 0;
169 face->setInterestFilter("/Hello/World",
170 bind([] {
171 BOOST_FAIL("Unexpected Interest");
172 }),
173 bind([] {
174 BOOST_FAIL("Unexpected success of setInterestFilter");
175 }),
176 bind([&nRegFailed] {
177 ++nRegFailed;
178 }));
179
180 advanceClocks(time::milliseconds(10), 10);
181 BOOST_CHECK_EQUAL(nRegFailed, 0);
182
183 advanceClocks(time::milliseconds(1000), 10);
184 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400185}
186
187BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
188{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800189 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400190 const RegisteredPrefixId* regPrefixId =
191 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800192 bind([&nRegSuccesses] { ++nRegSuccesses; }),
193 bind([] {
194 BOOST_FAIL("Unexpected registerPrefix failure");
195 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400196
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800197 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400198 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
199
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800200 size_t nUnregSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400201 face->unregisterPrefix(regPrefixId,
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800202 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
203 bind([] {
204 BOOST_FAIL("Unexpected unregisterPrefix failure");
205 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400206
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800207 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400208 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400209}
210
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800211BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400212{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800213 size_t nRegFailures = 0;
214 face->registerPrefix("/Hello/World",
215 bind([] {
216 BOOST_FAIL("Unexpected registerPrefix success");
217 }),
218 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400219
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800220 advanceClocks(time::milliseconds(1000), 100);
221 BOOST_CHECK_EQUAL(nRegFailures, 1);
222}
223
224BOOST_AUTO_TEST_CASE(SimilarFilters)
225{
226 size_t nInInterests1 = 0;
227 face->setInterestFilter("/Hello/World",
228 bind([&nInInterests1] { ++nInInterests1; }),
229 RegisterPrefixSuccessCallback(),
230 bind([] {
231 BOOST_FAIL("Unexpected setInterestFilter failure");
232 }));
233
234 size_t nInInterests2 = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400235 face->setInterestFilter("/Hello",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800236 bind([&nInInterests2] { ++nInInterests2; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400237 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800238 bind([] {
239 BOOST_FAIL("Unexpected setInterestFilter failure");
240 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400241
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800242 size_t nInInterests3 = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400243 face->setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800244 bind([&nInInterests3] { ++nInInterests3; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400245 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800246 bind([] {
247 BOOST_FAIL("Unexpected setInterestFilter failure");
248 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400249
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800250 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400251
252 face->receive(Interest("/Hello/World/!"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800253 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400254
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800255 BOOST_CHECK_EQUAL(nInInterests1, 1);
256 BOOST_CHECK_EQUAL(nInInterests2, 1);
257 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400258}
259
260BOOST_AUTO_TEST_CASE(SetRegexFilterError)
261{
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400262 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800263 [] (const Name&, const Interest&) {
264 BOOST_FAIL("InterestFilter::Error should have been triggered");
265 },
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400266 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800267 bind([] {
268 BOOST_FAIL("Unexpected setInterestFilter failure");
269 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400270
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800271 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400272
273 BOOST_REQUIRE_THROW(face->receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
274}
275
276BOOST_AUTO_TEST_CASE(SetRegexFilter)
277{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800278 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400279 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800280 bind([&nInInterests] { ++nInInterests; }),
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400281 RegisterPrefixSuccessCallback(),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800282 bind([] {
283 BOOST_FAIL("Unexpected setInterestFilter failure");
284 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400285
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800286 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400287
288 face->receive(Interest("/Hello/World/a")); // shouldn't match
289 BOOST_CHECK_EQUAL(nInInterests, 0);
290
291 face->receive(Interest("/Hello/World/a/b")); // should match
292 BOOST_CHECK_EQUAL(nInInterests, 1);
293
294 face->receive(Interest("/Hello/World/a/b/c")); // should match
295 BOOST_CHECK_EQUAL(nInInterests, 2);
296
297 face->receive(Interest("/Hello/World/a/b/d")); // should not match
298 BOOST_CHECK_EQUAL(nInInterests, 2);
299}
300
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400301BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
302{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800303 size_t nInInterests = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400304 face->setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800305 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400306
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800307 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400308 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800309 bind([&nRegSuccesses] { ++nRegSuccesses; }),
310 bind([] {
311 BOOST_FAIL("Unexpected setInterestFilter failure");
312 }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400313
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800314 advanceClocks(time::milliseconds(10), 10);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400315 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
316
317 face->receive(Interest("/Hello/World/a")); // shouldn't match
318 BOOST_CHECK_EQUAL(nInInterests, 0);
319
320 face->receive(Interest("/Hello/World/a/b")); // should match
321 BOOST_CHECK_EQUAL(nInInterests, 1);
322
323 face->receive(Interest("/Hello/World/a/b/c")); // should match
324 BOOST_CHECK_EQUAL(nInInterests, 2);
325
326 face->receive(Interest("/Hello/World/a/b/d")); // should not match
327 BOOST_CHECK_EQUAL(nInInterests, 2);
328}
329
Junxiao Shia1ea5062014-12-27 22:33:39 -0700330BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
331{
332 // This behavior is specific to DummyClientFace.
333 // Regular Face won't accept incoming packets until something is sent.
334
335 int hit = 0;
336 face->setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
337 face->processEvents(time::milliseconds(-1));
338
339 auto interest = make_shared<Interest>("/A");
340 face->receive(*interest);
341 face->processEvents(time::milliseconds(-1));
342
343 BOOST_CHECK_EQUAL(hit, 1);
344}
345
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500346BOOST_AUTO_TEST_CASE(ProcessEvents)
347{
348 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
349
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800350 size_t nRegSuccesses = 0;
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500351 face->registerPrefix("/Hello/World",
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800352 bind([&nRegSuccesses] { ++nRegSuccesses; }),
353 bind([] {
354 BOOST_FAIL("Unexpected setInterestFilter failure");
355 }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500356
357 // io_service::poll() without reset
358 face->getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500359 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
360
361 face->processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500362 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
363}
364
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400365BOOST_AUTO_TEST_SUITE_END()
366
367} // tests
368} // namespace ndn