blob: d547e33e92bfcee42bb46b8260398f0f6bbbf017 [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev57e00362016-06-23 13:22:54 -07003 * Copyright (c) 2013-2016 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 Afanasyev5fc795f2014-10-20 23:06:56 -040024#include "security/key-chain.hpp"
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080025#include "transport/tcp-transport.hpp"
Alexander Afanasyev57e00362016-06-23 13:22:54 -070026#include "transport/unix-transport.hpp"
Junxiao Shia1478db2016-09-09 04:13:15 +000027#include "util/dummy-client-face.hpp"
28#include "util/scheduler.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040029
30#include "boost-test.hpp"
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070031#include "identity-management-time-fixture.hpp"
32#include "key-chain-fixture.hpp"
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080033#include "make-interest-data.hpp"
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040034
35namespace ndn {
36namespace tests {
37
Junxiao Shia60d9362014-11-12 09:38:21 -070038using ndn::util::DummyClientFace;
Junxiao Shia60d9362014-11-12 09:38:21 -070039
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070040class FaceFixture : public IdentityManagementTimeFixture
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040041{
42public:
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080043 explicit
Junxiao Shia1ea5062014-12-27 22:33:39 -070044 FaceFixture(bool enableRegistrationReply = true)
Junxiao Shif5b5ae22016-08-08 05:54:41 +000045 : face(io, m_keyChain, {true, enableRegistrationReply})
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040046 {
47 }
48
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080049public:
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080050 DummyClientFace face;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040051};
52
Junxiao Shia1ea5062014-12-27 22:33:39 -070053class FacesNoRegistrationReplyFixture : public FaceFixture
Junxiao Shia60d9362014-11-12 09:38:21 -070054{
55public:
56 FacesNoRegistrationReplyFixture()
Junxiao Shia1ea5062014-12-27 22:33:39 -070057 : FaceFixture(false)
Junxiao Shia60d9362014-11-12 09:38:21 -070058 {
59 }
60};
61
Junxiao Shia1ea5062014-12-27 22:33:39 -070062BOOST_FIXTURE_TEST_SUITE(TestFace, FaceFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040063
Junxiao Shi103d8ed2016-08-07 20:34:10 +000064BOOST_AUTO_TEST_SUITE(Consumer)
65
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -040066BOOST_AUTO_TEST_CASE(ExpressInterestData)
67{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080068 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080069 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
70 [&] (const Interest& i, const Data& d) {
71 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
72 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
73 ++nData;
74 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +000075 bind([] { BOOST_FAIL("Unexpected Nack"); }),
76 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -070077
Junxiao Shif5b5ae22016-08-08 05:54:41 +000078 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -070079
Junxiao Shi85d90832016-08-04 03:19:46 +000080 face.receive(*makeData("/Bye/World/a"));
81 face.receive(*makeData("/Hello/World/a"));
Eric Newberry83872fd2015-08-06 17:01:24 -070082
Junxiao Shif5b5ae22016-08-08 05:54:41 +000083 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -070084
85 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080086 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
87 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -070088
89 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080090 face.expressInterest(Interest("/Hello/World/a/2", time::milliseconds(50)),
91 bind([]{}),
92 bind([]{}),
Junxiao Shi103d8ed2016-08-07 20:34:10 +000093 bind([&nTimeouts] { ++nTimeouts; }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +000094 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -070095 BOOST_CHECK_EQUAL(nTimeouts, 1);
96}
97
Junxiao Shi76e0eb22016-08-08 05:54:10 +000098BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
99{
100 face.expressInterest(Interest("/Hello/World"),
101 nullptr,
102 bind([] { BOOST_FAIL("Unexpected Nack"); }),
103 bind([] { BOOST_FAIL("Unexpected timeout"); }));
104 advanceClocks(time::milliseconds(1));
105
106 BOOST_CHECK_NO_THROW(do {
107 face.receive(*makeData("/Hello/World/a"));
108 advanceClocks(time::milliseconds(1));
109 } while (false));
110}
111
Eric Newberry83872fd2015-08-06 17:01:24 -0700112BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData)
113{
114 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800115 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000116 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800117 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
118 ++nData;
119 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000120 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400121
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000122 advanceClocks(time::milliseconds(40));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400123
Junxiao Shi85d90832016-08-04 03:19:46 +0000124 face.receive(*makeData("/Bye/World/a"));
125 face.receive(*makeData("/Hello/World/a"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800126
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000127 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400128
129 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800130 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
131 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800132
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800133 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000134 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800135 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
136 ++nData;
137 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000138 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000139 advanceClocks(time::milliseconds(40));
Junxiao Shi85d90832016-08-04 03:19:46 +0000140 face.receive(*makeData("/Hello/World/a/1/xxxxx"));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800141
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000142 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800143
144 BOOST_CHECK_EQUAL(nData, 2);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800145 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
146 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400147}
148
149BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
150{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800151 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800152 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000153 bind([] { BOOST_FAIL("Unexpected Data"); }),
154 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800155 [&nTimeouts] (const Interest& i) {
156 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
157 ++nTimeouts;
158 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700159
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000160 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700161
162 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800163 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
164 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
165 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700166}
167
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000168BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
169{
170 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
171 bind([] { BOOST_FAIL("Unexpected Data"); }),
172 bind([] { BOOST_FAIL("Unexpected Nack"); }),
173 nullptr);
174 advanceClocks(time::milliseconds(40));
175
176 BOOST_CHECK_NO_THROW(do {
177 advanceClocks(time::milliseconds(6), 2);
178 } while (false));
179}
180
Eric Newberry83872fd2015-08-06 17:01:24 -0700181BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
182{
183 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800184 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000185 bind([] { BOOST_FAIL("Unexpected data"); }),
186 bind([&nTimeouts] { ++nTimeouts; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400187
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000188 advanceClocks(time::milliseconds(200), 5);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400189
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400190 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);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400193}
194
Eric Newberry83872fd2015-08-06 17:01:24 -0700195BOOST_AUTO_TEST_CASE(ExpressInterestNack)
196{
197 size_t nNacks = 0;
198
199 Interest interest("/Hello/World", time::milliseconds(50));
200
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800201 face.expressInterest(interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000202 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800203 [&] (const Interest& i, const lp::Nack& n) {
204 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
205 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
206 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
207 ++nNacks;
208 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000209 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700210
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000211 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -0700212
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000213 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700214
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000215 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700216
217 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800218 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700219}
220
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000221BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
222{
223 face.expressInterest(Interest("/Hello/World"),
224 bind([] { BOOST_FAIL("Unexpected Data"); }),
225 nullptr,
226 bind([] { BOOST_FAIL("Unexpected timeout"); }));
227 advanceClocks(time::milliseconds(1));
228
229 BOOST_CHECK_NO_THROW(do {
230 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
231 advanceClocks(time::milliseconds(1));
232 } while (false));
233}
234
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000235BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestNack)
236{
237 size_t nTimeouts = 0;
238 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
239 bind([] { BOOST_FAIL("Unexpected data"); }),
240 bind([&nTimeouts] { ++nTimeouts; }));
241 advanceClocks(time::milliseconds(1));
242
243 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::CONGESTION));
244 advanceClocks(time::milliseconds(1));
245
246 BOOST_CHECK_EQUAL(nTimeouts, 1);
247 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
248 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
249}
250
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800251BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400252{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800253 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800254 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000255 bind([] { BOOST_FAIL("Unexpected data"); }),
256 bind([] { BOOST_FAIL("Unexpected nack"); }),
257 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800258 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400259
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800260 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800261 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400262
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000263 face.receive(*makeData("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000264 advanceClocks(time::milliseconds(200), 5);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100265
266 // avoid "test case [...] did not check any assertions" message from Boost.Test
267 BOOST_CHECK(true);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400268}
269
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000270BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500271{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800272 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
273 bind([] { BOOST_FAIL("Unexpected data"); }),
274 bind([] { BOOST_FAIL("Unexpected nack"); }),
275 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500276
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800277 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
278 bind([] { BOOST_FAIL("Unexpected data"); }),
279 bind([] { BOOST_FAIL("Unexpected nack"); }),
280 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500281
282 advanceClocks(time::milliseconds(10));
283
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800284 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500285 advanceClocks(time::milliseconds(10));
286
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800287 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500288
Junxiao Shi85d90832016-08-04 03:19:46 +0000289 face.receive(*makeData("/Hello/World/0"));
290 face.receive(*makeData("/Hello/World/1"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000291 advanceClocks(time::milliseconds(200), 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500292}
293
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000294BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
295{
296 {
297 DummyClientFace face2(io, m_keyChain);
298 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
299 bind([]{}), bind([]{}));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000300 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000301 }
302
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100303 advanceClocks(time::milliseconds(50), 2); // should not crash
304
305 // avoid "test case [...] did not check any assertions" message from Boost.Test
306 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000307}
308
309BOOST_AUTO_TEST_SUITE_END() // Consumer
310
311BOOST_AUTO_TEST_SUITE(Producer)
312
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000313BOOST_AUTO_TEST_CASE(PutData)
314{
315 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
316
317 Data data("/4g7xxcuEow/KFvK5Kf2m");
318 signData(data);
319 face.put(data);
320
321 lp::CachePolicy cachePolicy;
322 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
323 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700324 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000325 face.put(data);
326
327 advanceClocks(time::milliseconds(10));
328 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
329 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700330 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000331 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700332 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000333}
334
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000335BOOST_AUTO_TEST_CASE(PutNack)
336{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000337 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
338
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000339 face.put(makeNack(Interest("/Hello/World", time::milliseconds(50)), lp::NackReason::NO_ROUTE));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000340
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000341 advanceClocks(time::milliseconds(10));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000342 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
Eric Newberry4d261b62016-11-10 13:40:09 -0700343
344 auto nack = makeNack(Interest("/another/prefix", time::milliseconds(50)), lp::NackReason::NO_ROUTE);
345 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
346 face.put(nack);
347
348 advanceClocks(time::milliseconds(10));
349 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
350 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
351 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000352}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500353
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400354BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
355{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800356 size_t nInterests = 0;
357 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400358 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800359 face.setInterestFilter("/Hello/World",
360 bind([&nInterests] { ++nInterests; }),
361 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000362 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000363 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800364 BOOST_CHECK_EQUAL(nRegs, 1);
365 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400366
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000367 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000368 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400369
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800370 BOOST_CHECK_EQUAL(nRegs, 1);
371 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400372
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000373 face.receive(Interest("/Bye/World/%21"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800374 advanceClocks(time::milliseconds(10000), 10);
375 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400376
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000377 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000378 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800379 BOOST_CHECK_EQUAL(nInterests, 2);
380
381 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800382 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000383 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400384
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000385 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800386 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400387
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000388 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000389 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400390
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000391 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000392 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800393}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400394
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000395BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
396{
397 face.setInterestFilter("/A", nullptr);
398 advanceClocks(time::milliseconds(1));
399
400 BOOST_CHECK_NO_THROW(do {
401 face.receive(*makeInterest("/A/1"));
402 advanceClocks(time::milliseconds(1));
403 } while (false));
404}
405
Joao Pereira0b3cac52015-07-02 14:49:49 -0400406BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
407{
408 size_t nInterests = 0;
409 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800410 face.setInterestFilter("/Hello/World",
411 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000412 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000413 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400414 BOOST_CHECK_EQUAL(nInterests, 0);
415
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000416 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000417 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400418
419 BOOST_CHECK_EQUAL(nInterests, 1);
420
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000421 face.receive(Interest("/Bye/World/%21"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400422 advanceClocks(time::milliseconds(10000), 10);
423 BOOST_CHECK_EQUAL(nInterests, 1);
424
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000425 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000426 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400427 BOOST_CHECK_EQUAL(nInterests, 2);
428
429 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800430 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000431 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400432
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000433 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400434 BOOST_CHECK_EQUAL(nInterests, 2);
435
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000436 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000437 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400438
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000439 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000440 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400441}
442
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800443BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
444{
445 // don't enable registration reply
446 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800447 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000448 bind([] { BOOST_FAIL("Unexpected Interest"); }),
449 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
450 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800451
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000452 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800453 BOOST_CHECK_EQUAL(nRegFailed, 0);
454
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000455 advanceClocks(time::milliseconds(2000), 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800456 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400457}
458
Joao Pereira0b3cac52015-07-02 14:49:49 -0400459BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
460{
461 // don't enable registration reply
462 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800463 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000464 bind([] { BOOST_FAIL("Unexpected Interest"); }),
465 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400466
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000467 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400468 BOOST_CHECK_EQUAL(nRegFailed, 0);
469
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000470 advanceClocks(time::milliseconds(2000), 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400471 BOOST_CHECK_EQUAL(nRegFailed, 1);
472}
473
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400474BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
475{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800476 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400477 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800478 face.registerPrefix("/Hello/World",
479 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000480 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400481
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000482 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400483 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
484
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800485 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800486 face.unregisterPrefix(regPrefixId,
487 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000488 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400489
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000490 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400491 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400492}
493
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800494BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400495{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800496 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800497 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000498 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800499 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400500
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000501 advanceClocks(time::milliseconds(5000), 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800502 BOOST_CHECK_EQUAL(nRegFailures, 1);
503}
504
505BOOST_AUTO_TEST_CASE(SimilarFilters)
506{
507 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800508 face.setInterestFilter("/Hello/World",
509 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000510 nullptr,
511 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800512
513 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800514 face.setInterestFilter("/Hello",
515 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000516 nullptr,
517 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400518
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800519 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800520 face.setInterestFilter("/Los/Angeles/Lakers",
521 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000522 nullptr,
523 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400524
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000525 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400526
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000527 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000528 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400529
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800530 BOOST_CHECK_EQUAL(nInInterests1, 1);
531 BOOST_CHECK_EQUAL(nInInterests2, 1);
532 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400533}
534
535BOOST_AUTO_TEST_CASE(SetRegexFilterError)
536{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800537 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
538 [] (const Name&, const Interest&) {
539 BOOST_FAIL("InterestFilter::Error should have been triggered");
540 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000541 nullptr,
542 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400543
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000544 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400545
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800546 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400547}
548
549BOOST_AUTO_TEST_CASE(SetRegexFilter)
550{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800551 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800552 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
553 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000554 nullptr,
555 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400556
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000557 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400558
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800559 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400560 BOOST_CHECK_EQUAL(nInInterests, 0);
561
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800562 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400563 BOOST_CHECK_EQUAL(nInInterests, 1);
564
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800565 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400566 BOOST_CHECK_EQUAL(nInInterests, 2);
567
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800568 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400569 BOOST_CHECK_EQUAL(nInInterests, 2);
570}
571
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400572BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
573{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800574 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800575 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
576 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400577
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800578 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800579 face.registerPrefix("/Hello/World",
580 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000581 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400582
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000583 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400584 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
585
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800586 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400587 BOOST_CHECK_EQUAL(nInInterests, 0);
588
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800589 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400590 BOOST_CHECK_EQUAL(nInInterests, 1);
591
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800592 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400593 BOOST_CHECK_EQUAL(nInInterests, 2);
594
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800595 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400596 BOOST_CHECK_EQUAL(nInInterests, 2);
597}
598
Junxiao Shia1ea5062014-12-27 22:33:39 -0700599BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
600{
601 // This behavior is specific to DummyClientFace.
602 // Regular Face won't accept incoming packets until something is sent.
603
604 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800605 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
606 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700607
608 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800609 face.receive(*interest);
610 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700611
612 BOOST_CHECK_EQUAL(hit, 1);
613}
614
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000615BOOST_AUTO_TEST_SUITE_END() // Producer
616
Junxiao Shiae0b4182016-08-08 22:53:17 +0000617BOOST_AUTO_TEST_SUITE(IoRoutines)
618
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500619BOOST_AUTO_TEST_CASE(ProcessEvents)
620{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800621 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500622
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800623 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800624 face.registerPrefix("/Hello/World",
625 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000626 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500627
628 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800629 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500630 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
631
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800632 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500633 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
634}
635
Junxiao Shiae0b4182016-08-08 22:53:17 +0000636BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
637{
638 auto face2 = make_unique<Face>(io);
639 face2.reset();
640
641 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100642
643 // avoid "test case [...] did not check any assertions" message from Boost.Test
644 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000645}
646
647BOOST_AUTO_TEST_SUITE_END() // IoRoutines
648
649BOOST_AUTO_TEST_SUITE(Transport)
650
651using ndn::Transport;
652
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700653struct PibDirWithDefaultTpm
654{
655 const std::string PATH = "build/keys-with-default-tpm";
656};
657
658BOOST_FIXTURE_TEST_CASE(FaceTransport, PibDirFixture<PibDirWithDefaultTpm>)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800659{
660 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700661 boost::asio::io_service io;
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800662
663 BOOST_CHECK(Face().getTransport() != nullptr);
664
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800665 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
666 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
667 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800668
669 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
670 BOOST_CHECK(Face(transport).getTransport() == transport);
671 BOOST_CHECK(Face(transport, io).getTransport() == transport);
672 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
673}
674
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700675class WithEnv : private IdentityManagementTimeFixture
676{
677public:
678 WithEnv()
679 {
680 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
681 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
682 unsetenv("NDN_CLIENT_TRANSPORT");
683 }
684 }
685
686 void
687 configure(const std::string& faceUri)
688 {
689 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
690 }
691
692 ~WithEnv()
693 {
694 if (!m_oldTransport.empty()) {
695 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
696 }
697 else {
698 unsetenv("NDN_CLIENT_TRANSPORT");
699 }
700 }
701
702private:
703 std::string m_oldTransport;
704};
705
706class WithConfig : private TestHomeFixture<DefaultPibDir>
707{
708public:
709 void
710 configure(const std::string& faceUri)
711 {
712 createClientConf({"transport=" + faceUri});
713 }
714};
715
716class WithEnvAndConfig : public WithEnv, public WithConfig
717{
718};
719
720typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
721
722BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700723{
724 shared_ptr<Face> face;
725 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
726 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700727}
728
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700729BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
730{
731 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700732
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700733 shared_ptr<Face> face;
734 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
735 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
736}
737
738BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
739{
740 this->configure("tcp://127.0.0.1:6000");
741
742 shared_ptr<Face> face;
743 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
744 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
745}
746
747BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
748{
749 this->configure("wrong-transport:");
750
751 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
752}
753
754BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
755{
756 this->configure("wrong-uri");
757
758 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
759}
760
761BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
762{
763 this->WithEnv::configure("tcp://127.0.0.1:6000");
764 this->WithConfig::configure("unix://some/path");
765
766 shared_ptr<Face> face;
767 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
768 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
769}
770
771BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
772{
773 this->WithEnv::configure("wrong-uri");
774 this->WithConfig::configure("wrong-transport:");
775
776 auto transport = make_shared<UnixTransport>("unix://some/path");
777 shared_ptr<Face> face;
778 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
779 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
780}
781
Junxiao Shiae0b4182016-08-08 22:53:17 +0000782BOOST_AUTO_TEST_SUITE_END() // Transport
783
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700784BOOST_AUTO_TEST_SUITE_END() // TestFace
785
786} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400787} // namespace ndn