blob: 2813bd14c40a1d2cb77e8c0ac906890134e2dd3c [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev1013fd02017-01-03 13:19:03 -08003 * Copyright (c) 2013-2017 Regents of the University of California.
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04004 *
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"
Junxiao Shia1478db2016-09-09 04:13:15 +000023#include "lp/tags.hpp"
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080024#include "transport/tcp-transport.hpp"
Alexander Afanasyev57e00362016-06-23 13:22:54 -070025#include "transport/unix-transport.hpp"
Junxiao Shia1478db2016-09-09 04:13:15 +000026#include "util/dummy-client-face.hpp"
27#include "util/scheduler.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040028
29#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070030#include "identity-management-time-fixture.hpp"
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080031#include "test-home-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080032#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040033
34namespace ndn {
35namespace tests {
36
Junxiao Shia60d9362014-11-12 09:38:21 -070037using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070038
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070039class FaceFixture : public IdentityManagementTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040040{
41public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080042 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070043 FaceFixture(bool enableRegistrationReply = true)
Junxiao Shif5b5ae22016-08-08 05:54:41 +000044 : face(io, m_keyChain, {true, enableRegistrationReply})
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040045 {
46 }
47
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080048public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080049 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040050};
51
Junxiao Shia1ea5062014-12-27 22:33:39 -070052class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070053{
54public:
55 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070056 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070057 {
58 }
59};
60
Junxiao Shia1ea5062014-12-27 22:33:39 -070061BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040062
Junxiao Shi103d8ed2016-08-07 20:34:10 +000063BOOST_AUTO_TEST_SUITE(Consumer)
64
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040065BOOST_AUTO_TEST_CASE(ExpressInterestData)
66{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080067 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080068 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
69 [&] (const Interest& i, const Data& d) {
70 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
71 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
72 ++nData;
73 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +000074 bind([] { BOOST_FAIL("Unexpected Nack"); }),
75 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -070076
Junxiao Shif5b5ae22016-08-08 05:54:41 +000077 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -070078
Junxiao Shi85d90832016-08-04 03:19:46 +000079 face.receive(*makeData("/Bye/World/a"));
80 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070081
Junxiao Shif5b5ae22016-08-08 05:54:41 +000082 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -070083
84 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080085 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
86 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -070087
88 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080089 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
90 bind([]{}),
91 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +000092 bind([&nTimeouts] { ++nTimeouts; }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +000093 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -070094 BOOST_CHECK_EQUAL(nTimeouts, 1);
95}
96
Alexander Afanasyev1013fd02017-01-03 13:19:03 -080097BOOST_AUTO_TEST_CASE(ExpressMultipleInterestData)
98{
99 size_t nData = 0;
100
101 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
102 [&] (const Interest& i, const Data& d) {
103 ++nData;
104 },
105 bind([] { BOOST_FAIL("Unexpected Nack"); }),
106 bind([] { BOOST_FAIL("Unexpected timeout"); }));
107
108 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
109 [&] (const Interest& i, const Data& d) {
110 ++nData;
111 },
112 bind([] { BOOST_FAIL("Unexpected Nack"); }),
113 bind([] { BOOST_FAIL("Unexpected timeout"); }));
114
115 advanceClocks(time::milliseconds(40));
116
117 face.receive(*makeData("/Hello/World/a/b"));
118
119 advanceClocks(time::milliseconds(50), 2);
120
121 BOOST_CHECK_EQUAL(nData, 2);
122 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
123 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
124}
125
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000126BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
127{
128 face.expressInterest(Interest("/Hello/World"),
129 nullptr,
130 bind([] { BOOST_FAIL("Unexpected Nack"); }),
131 bind([] { BOOST_FAIL("Unexpected timeout"); }));
132 advanceClocks(time::milliseconds(1));
133
134 BOOST_CHECK_NO_THROW(do {
135 face.receive(*makeData("/Hello/World/a"));
136 advanceClocks(time::milliseconds(1));
137 } while (false));
138}
139
Eric Newberry83872fd2015-08-06 17:01:24 -0700140BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData)
141{
142 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800143 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000144 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800145 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
146 ++nData;
147 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000148 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400149
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000150 advanceClocks(time::milliseconds(40));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400151
Junxiao Shi85d90832016-08-04 03:19:46 +0000152 face.receive(*makeData("/Bye/World/a"));
153 face.receive(*makeData("/Hello/World/a"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800154
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000155 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400156
157 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800158 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
159 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800160
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800161 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000162 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800163 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
164 ++nData;
165 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000166 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000167 advanceClocks(time::milliseconds(40));
Junxiao Shi85d90832016-08-04 03:19:46 +0000168 face.receive(*makeData("/Hello/World/a/1/xxxxx"));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800169
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000170 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800171
172 BOOST_CHECK_EQUAL(nData, 2);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800173 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
174 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400175}
176
177BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
178{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800179 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800180 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000181 bind([] { BOOST_FAIL("Unexpected Data"); }),
182 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800183 [&nTimeouts] (const Interest& i) {
184 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
185 ++nTimeouts;
186 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700187
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000188 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700189
190 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800191 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
192 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
193 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700194}
195
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000196BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
197{
198 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
199 bind([] { BOOST_FAIL("Unexpected Data"); }),
200 bind([] { BOOST_FAIL("Unexpected Nack"); }),
201 nullptr);
202 advanceClocks(time::milliseconds(40));
203
204 BOOST_CHECK_NO_THROW(do {
205 advanceClocks(time::milliseconds(6), 2);
206 } while (false));
207}
208
Eric Newberry83872fd2015-08-06 17:01:24 -0700209BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
210{
211 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800212 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000213 bind([] { BOOST_FAIL("Unexpected data"); }),
214 bind([&nTimeouts] { ++nTimeouts; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400215
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000216 advanceClocks(time::milliseconds(200), 5);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400217
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400218 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800219 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
220 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400221}
222
Eric Newberry83872fd2015-08-06 17:01:24 -0700223BOOST_AUTO_TEST_CASE(ExpressInterestNack)
224{
225 size_t nNacks = 0;
226
227 Interest interest("/Hello/World", time::milliseconds(50));
228
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800229 face.expressInterest(interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000230 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800231 [&] (const Interest& i, const lp::Nack& n) {
232 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
233 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
234 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
235 ++nNacks;
236 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000237 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700238
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000239 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -0700240
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000241 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700242
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000243 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700244
245 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800246 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700247}
248
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800249BOOST_AUTO_TEST_CASE(ExpressMultipleInterestNack)
250{
251 size_t nNacks = 0;
252
253 Interest interest("/Hello/World", time::milliseconds(50));
254 interest.setNonce(1);
255
256 face.expressInterest(interest,
257 bind([] { BOOST_FAIL("Unexpected Data"); }),
258 [&] (const Interest& i, const lp::Nack& n) {
259 ++nNacks;
260 },
261 bind([] { BOOST_FAIL("Unexpected timeout"); }));
262
263 interest.setNonce(2);
264 face.expressInterest(interest,
265 bind([] { BOOST_FAIL("Unexpected Data"); }),
266 [&] (const Interest& i, const lp::Nack& n) {
267 ++nNacks;
268 },
269 bind([] { BOOST_FAIL("Unexpected timeout"); }));
270
271 advanceClocks(time::milliseconds(40));
272
273 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
274
275 advanceClocks(time::milliseconds(50), 2);
276
277 BOOST_CHECK_EQUAL(nNacks, 2);
278 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
279}
280
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000281BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
282{
283 face.expressInterest(Interest("/Hello/World"),
284 bind([] { BOOST_FAIL("Unexpected Data"); }),
285 nullptr,
286 bind([] { BOOST_FAIL("Unexpected timeout"); }));
287 advanceClocks(time::milliseconds(1));
288
289 BOOST_CHECK_NO_THROW(do {
290 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
291 advanceClocks(time::milliseconds(1));
292 } while (false));
293}
294
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000295BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestNack)
296{
297 size_t nTimeouts = 0;
298 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
299 bind([] { BOOST_FAIL("Unexpected data"); }),
300 bind([&nTimeouts] { ++nTimeouts; }));
301 advanceClocks(time::milliseconds(1));
302
303 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::CONGESTION));
304 advanceClocks(time::milliseconds(1));
305
306 BOOST_CHECK_EQUAL(nTimeouts, 1);
307 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
308 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
309}
310
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800311BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400312{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800313 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800314 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000315 bind([] { BOOST_FAIL("Unexpected data"); }),
316 bind([] { BOOST_FAIL("Unexpected nack"); }),
317 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800318 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400319
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800320 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800321 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400322
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000323 face.receive(*makeData("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000324 advanceClocks(time::milliseconds(200), 5);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100325
326 // avoid "test case [...] did not check any assertions" message from Boost.Test
327 BOOST_CHECK(true);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400328}
329
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000330BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500331{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800332 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
333 bind([] { BOOST_FAIL("Unexpected data"); }),
334 bind([] { BOOST_FAIL("Unexpected nack"); }),
335 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500336
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800337 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
338 bind([] { BOOST_FAIL("Unexpected data"); }),
339 bind([] { BOOST_FAIL("Unexpected nack"); }),
340 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500341
342 advanceClocks(time::milliseconds(10));
343
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800344 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500345 advanceClocks(time::milliseconds(10));
346
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800347 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500348
Junxiao Shi85d90832016-08-04 03:19:46 +0000349 face.receive(*makeData("/Hello/World/0"));
350 face.receive(*makeData("/Hello/World/1"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000351 advanceClocks(time::milliseconds(200), 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500352}
353
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000354BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
355{
356 {
357 DummyClientFace face2(io, m_keyChain);
358 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
359 bind([]{}), bind([]{}));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000360 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000361 }
362
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100363 advanceClocks(time::milliseconds(50), 2); // should not crash
364
365 // avoid "test case [...] did not check any assertions" message from Boost.Test
366 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000367}
368
369BOOST_AUTO_TEST_SUITE_END() // Consumer
370
371BOOST_AUTO_TEST_SUITE(Producer)
372
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000373BOOST_AUTO_TEST_CASE(PutData)
374{
375 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
376
377 Data data("/4g7xxcuEow/KFvK5Kf2m");
378 signData(data);
379 face.put(data);
380
381 lp::CachePolicy cachePolicy;
382 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
383 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700384 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000385 face.put(data);
386
387 advanceClocks(time::milliseconds(10));
388 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
389 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700390 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000391 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700392 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000393}
394
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000395BOOST_AUTO_TEST_CASE(PutNack)
396{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000397 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
398
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000399 face.put(makeNack(Interest("/Hello/World", time::milliseconds(50)), lp::NackReason::NO_ROUTE));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000400
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000401 advanceClocks(time::milliseconds(10));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000402 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
Eric Newberry4d261b62016-11-10 13:40:09 -0700403
404 auto nack = makeNack(Interest("/another/prefix", time::milliseconds(50)), lp::NackReason::NO_ROUTE);
405 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
406 face.put(nack);
407
408 advanceClocks(time::milliseconds(10));
409 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
410 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
411 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000412}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500413
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400414BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
415{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800416 size_t nInterests = 0;
417 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400418 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800419 face.setInterestFilter("/Hello/World",
420 bind([&nInterests] { ++nInterests; }),
421 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000422 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000423 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800424 BOOST_CHECK_EQUAL(nRegs, 1);
425 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400426
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000427 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000428 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400429
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800430 BOOST_CHECK_EQUAL(nRegs, 1);
431 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400432
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000433 face.receive(Interest("/Bye/World/%21"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800434 advanceClocks(time::milliseconds(10000), 10);
435 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400436
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000437 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000438 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800439 BOOST_CHECK_EQUAL(nInterests, 2);
440
441 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800442 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000443 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400444
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000445 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800446 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400447
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000448 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000449 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400450
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000451 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000452 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800453}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400454
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000455BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
456{
457 face.setInterestFilter("/A", nullptr);
458 advanceClocks(time::milliseconds(1));
459
460 BOOST_CHECK_NO_THROW(do {
461 face.receive(*makeInterest("/A/1"));
462 advanceClocks(time::milliseconds(1));
463 } while (false));
464}
465
Joao Pereira0b3cac52015-07-02 14:49:49 -0400466BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
467{
468 size_t nInterests = 0;
469 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800470 face.setInterestFilter("/Hello/World",
471 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000472 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000473 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400474 BOOST_CHECK_EQUAL(nInterests, 0);
475
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000476 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000477 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400478
479 BOOST_CHECK_EQUAL(nInterests, 1);
480
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000481 face.receive(Interest("/Bye/World/%21"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400482 advanceClocks(time::milliseconds(10000), 10);
483 BOOST_CHECK_EQUAL(nInterests, 1);
484
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000485 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000486 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400487 BOOST_CHECK_EQUAL(nInterests, 2);
488
489 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800490 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000491 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400492
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000493 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400494 BOOST_CHECK_EQUAL(nInterests, 2);
495
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000496 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000497 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400498
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000499 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000500 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400501}
502
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800503BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
504{
505 // don't enable registration reply
506 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800507 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000508 bind([] { BOOST_FAIL("Unexpected Interest"); }),
509 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
510 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800511
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000512 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800513 BOOST_CHECK_EQUAL(nRegFailed, 0);
514
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000515 advanceClocks(time::milliseconds(2000), 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800516 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400517}
518
Joao Pereira0b3cac52015-07-02 14:49:49 -0400519BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
520{
521 // don't enable registration reply
522 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800523 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000524 bind([] { BOOST_FAIL("Unexpected Interest"); }),
525 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400526
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000527 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400528 BOOST_CHECK_EQUAL(nRegFailed, 0);
529
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000530 advanceClocks(time::milliseconds(2000), 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400531 BOOST_CHECK_EQUAL(nRegFailed, 1);
532}
533
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400534BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
535{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800536 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400537 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800538 face.registerPrefix("/Hello/World",
539 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000540 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400541
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000542 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400543 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
544
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800545 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800546 face.unregisterPrefix(regPrefixId,
547 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000548 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400549
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000550 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400551 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400552}
553
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800554BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400555{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800556 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800557 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000558 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800559 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400560
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000561 advanceClocks(time::milliseconds(5000), 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800562 BOOST_CHECK_EQUAL(nRegFailures, 1);
563}
564
565BOOST_AUTO_TEST_CASE(SimilarFilters)
566{
567 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800568 face.setInterestFilter("/Hello/World",
569 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000570 nullptr,
571 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800572
573 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800574 face.setInterestFilter("/Hello",
575 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000576 nullptr,
577 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400578
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800579 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800580 face.setInterestFilter("/Los/Angeles/Lakers",
581 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000582 nullptr,
583 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400584
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000585 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400586
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000587 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000588 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400589
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800590 BOOST_CHECK_EQUAL(nInInterests1, 1);
591 BOOST_CHECK_EQUAL(nInInterests2, 1);
592 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400593}
594
595BOOST_AUTO_TEST_CASE(SetRegexFilterError)
596{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800597 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
598 [] (const Name&, const Interest&) {
599 BOOST_FAIL("InterestFilter::Error should have been triggered");
600 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000601 nullptr,
602 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400603
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000604 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400605
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800606 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400607}
608
609BOOST_AUTO_TEST_CASE(SetRegexFilter)
610{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800611 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800612 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
613 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000614 nullptr,
615 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400616
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000617 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400618
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800619 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400620 BOOST_CHECK_EQUAL(nInInterests, 0);
621
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800622 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400623 BOOST_CHECK_EQUAL(nInInterests, 1);
624
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800625 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400626 BOOST_CHECK_EQUAL(nInInterests, 2);
627
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800628 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400629 BOOST_CHECK_EQUAL(nInInterests, 2);
630}
631
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400632BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
633{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800634 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800635 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
636 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400637
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800638 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800639 face.registerPrefix("/Hello/World",
640 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000641 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400642
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000643 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400644 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
645
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800646 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400647 BOOST_CHECK_EQUAL(nInInterests, 0);
648
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800649 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400650 BOOST_CHECK_EQUAL(nInInterests, 1);
651
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800652 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400653 BOOST_CHECK_EQUAL(nInInterests, 2);
654
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800655 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400656 BOOST_CHECK_EQUAL(nInInterests, 2);
657}
658
Junxiao Shia1ea5062014-12-27 22:33:39 -0700659BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
660{
661 // This behavior is specific to DummyClientFace.
662 // Regular Face won't accept incoming packets until something is sent.
663
664 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800665 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
666 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700667
668 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800669 face.receive(*interest);
670 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700671
672 BOOST_CHECK_EQUAL(hit, 1);
673}
674
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000675BOOST_AUTO_TEST_SUITE_END() // Producer
676
Junxiao Shiae0b4182016-08-08 22:53:17 +0000677BOOST_AUTO_TEST_SUITE(IoRoutines)
678
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500679BOOST_AUTO_TEST_CASE(ProcessEvents)
680{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800681 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500682
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800683 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800684 face.registerPrefix("/Hello/World",
685 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000686 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500687
688 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800689 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500690 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
691
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800692 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500693 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
694}
695
Junxiao Shiae0b4182016-08-08 22:53:17 +0000696BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
697{
698 auto face2 = make_unique<Face>(io);
699 face2.reset();
700
701 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100702
703 // avoid "test case [...] did not check any assertions" message from Boost.Test
704 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000705}
706
707BOOST_AUTO_TEST_SUITE_END() // IoRoutines
708
709BOOST_AUTO_TEST_SUITE(Transport)
710
711using ndn::Transport;
712
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700713struct PibDirWithDefaultTpm
714{
715 const std::string PATH = "build/keys-with-default-tpm";
716};
717
718BOOST_FIXTURE_TEST_CASE(FaceTransport, PibDirFixture<PibDirWithDefaultTpm>)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800719{
720 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700721 boost::asio::io_service io;
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800722
723 BOOST_CHECK(Face().getTransport() != nullptr);
724
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800725 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
726 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
727 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800728
729 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
730 BOOST_CHECK(Face(transport).getTransport() == transport);
731 BOOST_CHECK(Face(transport, io).getTransport() == transport);
732 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
733}
734
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700735class WithEnv : private IdentityManagementTimeFixture
736{
737public:
738 WithEnv()
739 {
740 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
741 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
742 unsetenv("NDN_CLIENT_TRANSPORT");
743 }
744 }
745
746 void
747 configure(const std::string& faceUri)
748 {
749 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
750 }
751
752 ~WithEnv()
753 {
754 if (!m_oldTransport.empty()) {
755 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
756 }
757 else {
758 unsetenv("NDN_CLIENT_TRANSPORT");
759 }
760 }
761
762private:
763 std::string m_oldTransport;
764};
765
766class WithConfig : private TestHomeFixture<DefaultPibDir>
767{
768public:
769 void
770 configure(const std::string& faceUri)
771 {
772 createClientConf({"transport=" + faceUri});
773 }
774};
775
776class WithEnvAndConfig : public WithEnv, public WithConfig
777{
778};
779
780typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
781
782BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700783{
784 shared_ptr<Face> face;
785 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
786 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700787}
788
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700789BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
790{
791 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700792
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700793 shared_ptr<Face> face;
794 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
795 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
796}
797
798BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
799{
800 this->configure("tcp://127.0.0.1:6000");
801
802 shared_ptr<Face> face;
803 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
804 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
805}
806
807BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
808{
809 this->configure("wrong-transport:");
810
811 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
812}
813
814BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
815{
816 this->configure("wrong-uri");
817
818 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
819}
820
821BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
822{
823 this->WithEnv::configure("tcp://127.0.0.1:6000");
824 this->WithConfig::configure("unix://some/path");
825
826 shared_ptr<Face> face;
827 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
828 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
829}
830
831BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
832{
833 this->WithEnv::configure("wrong-uri");
834 this->WithConfig::configure("wrong-transport:");
835
836 auto transport = make_shared<UnixTransport>("unix://some/path");
837 shared_ptr<Face> face;
838 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
839 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
840}
841
Junxiao Shiae0b4182016-08-08 22:53:17 +0000842BOOST_AUTO_TEST_SUITE_END() // Transport
843
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700844BOOST_AUTO_TEST_SUITE_END() // TestFace
845
846} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400847} // namespace ndn