blob: e5f1d475783c21157609e0a93b9f38636e1b645f [file] [log] [blame]
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +00002/*
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 Afanasyev80782e02017-01-04 13:16:54 -080039class 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
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400140BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
141{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800142 size_t nTimeouts = 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 bind([] { BOOST_FAIL("Unexpected Data"); }),
145 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800146 [&nTimeouts] (const Interest& i) {
147 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
148 ++nTimeouts;
149 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700150
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000151 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700152
153 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800154 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
155 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
156 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700157}
158
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000159BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
160{
161 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
162 bind([] { BOOST_FAIL("Unexpected Data"); }),
163 bind([] { BOOST_FAIL("Unexpected Nack"); }),
164 nullptr);
165 advanceClocks(time::milliseconds(40));
166
167 BOOST_CHECK_NO_THROW(do {
168 advanceClocks(time::milliseconds(6), 2);
169 } while (false));
170}
171
Eric Newberry83872fd2015-08-06 17:01:24 -0700172BOOST_AUTO_TEST_CASE(ExpressInterestNack)
173{
174 size_t nNacks = 0;
175
176 Interest interest("/Hello/World", time::milliseconds(50));
177
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800178 face.expressInterest(interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000179 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800180 [&] (const Interest& i, const lp::Nack& n) {
181 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
182 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
183 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
184 ++nNacks;
185 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000186 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700187
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000188 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -0700189
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000190 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700191
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000192 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700193
194 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800195 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700196}
197
Alexander Afanasyev1013fd02017-01-03 13:19:03 -0800198BOOST_AUTO_TEST_CASE(ExpressMultipleInterestNack)
199{
200 size_t nNacks = 0;
201
202 Interest interest("/Hello/World", time::milliseconds(50));
203 interest.setNonce(1);
204
205 face.expressInterest(interest,
206 bind([] { BOOST_FAIL("Unexpected Data"); }),
207 [&] (const Interest& i, const lp::Nack& n) {
208 ++nNacks;
209 },
210 bind([] { BOOST_FAIL("Unexpected timeout"); }));
211
212 interest.setNonce(2);
213 face.expressInterest(interest,
214 bind([] { BOOST_FAIL("Unexpected Data"); }),
215 [&] (const Interest& i, const lp::Nack& n) {
216 ++nNacks;
217 },
218 bind([] { BOOST_FAIL("Unexpected timeout"); }));
219
220 advanceClocks(time::milliseconds(40));
221
222 face.receive(makeNack(face.sentInterests.at(1), lp::NackReason::DUPLICATE));
223
224 advanceClocks(time::milliseconds(50), 2);
225
226 BOOST_CHECK_EQUAL(nNacks, 2);
227 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
228}
229
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000230BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
231{
232 face.expressInterest(Interest("/Hello/World"),
233 bind([] { BOOST_FAIL("Unexpected Data"); }),
234 nullptr,
235 bind([] { BOOST_FAIL("Unexpected timeout"); }));
236 advanceClocks(time::milliseconds(1));
237
238 BOOST_CHECK_NO_THROW(do {
239 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
240 advanceClocks(time::milliseconds(1));
241 } while (false));
242}
243
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800244BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400245{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800246 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800247 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000248 bind([] { BOOST_FAIL("Unexpected data"); }),
249 bind([] { BOOST_FAIL("Unexpected nack"); }),
250 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800251 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400252
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800253 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800254 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400255
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000256 face.receive(*makeData("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000257 advanceClocks(time::milliseconds(200), 5);
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100258
259 // avoid "test case [...] did not check any assertions" message from Boost.Test
260 BOOST_CHECK(true);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400261}
262
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000263BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500264{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800265 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
266 bind([] { BOOST_FAIL("Unexpected data"); }),
267 bind([] { BOOST_FAIL("Unexpected nack"); }),
268 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500269
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800270 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
271 bind([] { BOOST_FAIL("Unexpected data"); }),
272 bind([] { BOOST_FAIL("Unexpected nack"); }),
273 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500274
275 advanceClocks(time::milliseconds(10));
276
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800277 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500278 advanceClocks(time::milliseconds(10));
279
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800280 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500281
Junxiao Shi85d90832016-08-04 03:19:46 +0000282 face.receive(*makeData("/Hello/World/0"));
283 face.receive(*makeData("/Hello/World/1"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000284 advanceClocks(time::milliseconds(200), 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500285}
286
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000287BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
288{
289 {
290 DummyClientFace face2(io, m_keyChain);
291 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Alexander Afanasyeve6835fe2017-01-19 20:05:01 -0800292 nullptr, nullptr, nullptr);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000293 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000294 }
295
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100296 advanceClocks(time::milliseconds(50), 2); // should not crash
297
298 // avoid "test case [...] did not check any assertions" message from Boost.Test
299 BOOST_CHECK(true);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000300}
301
302BOOST_AUTO_TEST_SUITE_END() // Consumer
303
304BOOST_AUTO_TEST_SUITE(Producer)
305
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000306BOOST_AUTO_TEST_CASE(PutData)
307{
308 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
309
310 Data data("/4g7xxcuEow/KFvK5Kf2m");
311 signData(data);
312 face.put(data);
313
314 lp::CachePolicy cachePolicy;
315 cachePolicy.setPolicy(lp::CachePolicyType::NO_CACHE);
316 data.setTag(make_shared<lp::CachePolicyTag>(cachePolicy));
Eric Newberry4d261b62016-11-10 13:40:09 -0700317 data.setTag(make_shared<lp::CongestionMarkTag>(1));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000318 face.put(data);
319
320 advanceClocks(time::milliseconds(10));
321 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
322 BOOST_CHECK(face.sentData[0].getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700323 BOOST_CHECK(face.sentData[0].getTag<lp::CongestionMarkTag>() == nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000324 BOOST_CHECK(face.sentData[1].getTag<lp::CachePolicyTag>() != nullptr);
Eric Newberry4d261b62016-11-10 13:40:09 -0700325 BOOST_CHECK(face.sentData[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000326}
327
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000328BOOST_AUTO_TEST_CASE(PutNack)
329{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000330 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
331
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000332 face.put(makeNack("/unsolicited", 18645250, lp::NackReason::NO_ROUTE));
Junxiao Shie7bb6c82016-08-08 23:16:35 +0000333 advanceClocks(time::milliseconds(10));
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000334 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); // unsolicited Nack would not be sent
Eric Newberry4d261b62016-11-10 13:40:09 -0700335
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000336 face.receive(*makeInterest("/Hello/World", 14247162));
337 face.receive(*makeInterest("/another/prefix", 92203002));
338 advanceClocks(time::milliseconds(10));
339
340 face.put(makeNack("/Hello/World", 14247162, lp::NackReason::DUPLICATE));
341 advanceClocks(time::milliseconds(10));
342 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 1);
343 BOOST_CHECK_EQUAL(face.sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
344 BOOST_CHECK(face.sentNacks[0].getTag<lp::CongestionMarkTag>() == nullptr);
345
346 auto nack = makeNack("/another/prefix", 92203002, lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700347 nack.setTag(make_shared<lp::CongestionMarkTag>(1));
348 face.put(nack);
Eric Newberry4d261b62016-11-10 13:40:09 -0700349 advanceClocks(time::milliseconds(10));
350 BOOST_REQUIRE_EQUAL(face.sentNacks.size(), 2);
Junxiao Shi1ad0b4b2017-08-18 14:19:14 +0000351 BOOST_CHECK_EQUAL(face.sentNacks[1].getReason(), lp::NackReason::NO_ROUTE);
Eric Newberry4d261b62016-11-10 13:40:09 -0700352 BOOST_CHECK(face.sentNacks[1].getTag<lp::CongestionMarkTag>() != nullptr);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000353}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500354
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400355BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
356{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800357 size_t nInterests = 0;
358 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400359 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800360 face.setInterestFilter("/Hello/World",
361 bind([&nInterests] { ++nInterests; }),
362 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000363 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000364 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800365 BOOST_CHECK_EQUAL(nRegs, 1);
366 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400367
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000368 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000369 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400370
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800371 BOOST_CHECK_EQUAL(nRegs, 1);
372 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400373
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000374 face.receive(Interest("/Bye/World/%21"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800375 advanceClocks(time::milliseconds(10000), 10);
376 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400377
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000378 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000379 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800380 BOOST_CHECK_EQUAL(nInterests, 2);
381
382 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800383 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000384 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400385
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000386 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800387 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400388
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000389 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000390 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400391
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000392 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000393 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800394}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400395
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000396BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
397{
398 face.setInterestFilter("/A", nullptr);
399 advanceClocks(time::milliseconds(1));
400
401 BOOST_CHECK_NO_THROW(do {
402 face.receive(*makeInterest("/A/1"));
403 advanceClocks(time::milliseconds(1));
404 } while (false));
405}
406
Joao Pereira0b3cac52015-07-02 14:49:49 -0400407BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
408{
409 size_t nInterests = 0;
410 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800411 face.setInterestFilter("/Hello/World",
412 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000413 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000414 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400415 BOOST_CHECK_EQUAL(nInterests, 0);
416
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000417 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000418 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400419
420 BOOST_CHECK_EQUAL(nInterests, 1);
421
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000422 face.receive(Interest("/Bye/World/%21"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400423 advanceClocks(time::milliseconds(10000), 10);
424 BOOST_CHECK_EQUAL(nInterests, 1);
425
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000426 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000427 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400428 BOOST_CHECK_EQUAL(nInterests, 2);
429
430 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800431 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000432 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400433
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000434 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400435 BOOST_CHECK_EQUAL(nInterests, 2);
436
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000437 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000438 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400439
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000440 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000441 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400442}
443
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800444BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
445{
446 // don't enable registration reply
447 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800448 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000449 bind([] { BOOST_FAIL("Unexpected Interest"); }),
450 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
451 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800452
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000453 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800454 BOOST_CHECK_EQUAL(nRegFailed, 0);
455
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000456 advanceClocks(time::milliseconds(2000), 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800457 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400458}
459
Joao Pereira0b3cac52015-07-02 14:49:49 -0400460BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
461{
462 // don't enable registration reply
463 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800464 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000465 bind([] { BOOST_FAIL("Unexpected Interest"); }),
466 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400467
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000468 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400469 BOOST_CHECK_EQUAL(nRegFailed, 0);
470
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000471 advanceClocks(time::milliseconds(2000), 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400472 BOOST_CHECK_EQUAL(nRegFailed, 1);
473}
474
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400475BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
476{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800477 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400478 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800479 face.registerPrefix("/Hello/World",
480 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000481 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400482
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000483 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400484 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
485
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800486 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800487 face.unregisterPrefix(regPrefixId,
488 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000489 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400490
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000491 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400492 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400493}
494
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800495BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400496{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800497 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800498 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000499 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800500 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400501
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000502 advanceClocks(time::milliseconds(5000), 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800503 BOOST_CHECK_EQUAL(nRegFailures, 1);
504}
505
506BOOST_AUTO_TEST_CASE(SimilarFilters)
507{
508 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800509 face.setInterestFilter("/Hello/World",
510 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000511 nullptr,
512 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800513
514 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800515 face.setInterestFilter("/Hello",
516 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000517 nullptr,
518 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400519
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800520 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800521 face.setInterestFilter("/Los/Angeles/Lakers",
522 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000523 nullptr,
524 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400525
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000526 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400527
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000528 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000529 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400530
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800531 BOOST_CHECK_EQUAL(nInInterests1, 1);
532 BOOST_CHECK_EQUAL(nInInterests2, 1);
533 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400534}
535
536BOOST_AUTO_TEST_CASE(SetRegexFilterError)
537{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800538 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
539 [] (const Name&, const Interest&) {
540 BOOST_FAIL("InterestFilter::Error should have been triggered");
541 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000542 nullptr,
543 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400544
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000545 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400546
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800547 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400548}
549
550BOOST_AUTO_TEST_CASE(SetRegexFilter)
551{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800552 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800553 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
554 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000555 nullptr,
556 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400557
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000558 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400559
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800560 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400561 BOOST_CHECK_EQUAL(nInInterests, 0);
562
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800563 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400564 BOOST_CHECK_EQUAL(nInInterests, 1);
565
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800566 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400567 BOOST_CHECK_EQUAL(nInInterests, 2);
568
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800569 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400570 BOOST_CHECK_EQUAL(nInInterests, 2);
571}
572
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400573BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
574{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800575 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800576 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
577 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400578
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800579 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800580 face.registerPrefix("/Hello/World",
581 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000582 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400583
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000584 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400585 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
586
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800587 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400588 BOOST_CHECK_EQUAL(nInInterests, 0);
589
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800590 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400591 BOOST_CHECK_EQUAL(nInInterests, 1);
592
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800593 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400594 BOOST_CHECK_EQUAL(nInInterests, 2);
595
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800596 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400597 BOOST_CHECK_EQUAL(nInInterests, 2);
598}
599
Junxiao Shia1ea5062014-12-27 22:33:39 -0700600BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
601{
602 // This behavior is specific to DummyClientFace.
603 // Regular Face won't accept incoming packets until something is sent.
604
605 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800606 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
607 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700608
609 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800610 face.receive(*interest);
611 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700612
613 BOOST_CHECK_EQUAL(hit, 1);
614}
615
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000616BOOST_AUTO_TEST_SUITE_END() // Producer
617
Junxiao Shiae0b4182016-08-08 22:53:17 +0000618BOOST_AUTO_TEST_SUITE(IoRoutines)
619
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500620BOOST_AUTO_TEST_CASE(ProcessEvents)
621{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800622 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500623
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800624 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800625 face.registerPrefix("/Hello/World",
626 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000627 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500628
629 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800630 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500631 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
632
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800633 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500634 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
635}
636
Junxiao Shiae0b4182016-08-08 22:53:17 +0000637BOOST_AUTO_TEST_CASE(DestroyWithoutProcessEvents) // Bug 3248
638{
639 auto face2 = make_unique<Face>(io);
640 face2.reset();
641
642 io.poll(); // should not crash
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100643
644 // avoid "test case [...] did not check any assertions" message from Boost.Test
645 BOOST_CHECK(true);
Junxiao Shiae0b4182016-08-08 22:53:17 +0000646}
647
648BOOST_AUTO_TEST_SUITE_END() // IoRoutines
649
650BOOST_AUTO_TEST_SUITE(Transport)
651
652using ndn::Transport;
653
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700654struct PibDirWithDefaultTpm
655{
656 const std::string PATH = "build/keys-with-default-tpm";
657};
658
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800659BOOST_FIXTURE_TEST_CASE(FaceTransport, IdentityManagementTimeFixture)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800660{
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800661 BOOST_CHECK(Face().getTransport() != nullptr);
662
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800663 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
664 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800665 BOOST_CHECK(Face(shared_ptr<Transport>(), io, m_keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800666
667 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
668 BOOST_CHECK(Face(transport).getTransport() == transport);
669 BOOST_CHECK(Face(transport, io).getTransport() == transport);
Alexander Afanasyev80782e02017-01-04 13:16:54 -0800670 BOOST_CHECK(Face(transport, io, m_keyChain).getTransport() == transport);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800671}
672
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700673class WithEnv : private IdentityManagementTimeFixture
674{
675public:
676 WithEnv()
677 {
678 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
679 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
680 unsetenv("NDN_CLIENT_TRANSPORT");
681 }
682 }
683
684 void
685 configure(const std::string& faceUri)
686 {
687 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
688 }
689
690 ~WithEnv()
691 {
692 if (!m_oldTransport.empty()) {
693 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
694 }
695 else {
696 unsetenv("NDN_CLIENT_TRANSPORT");
697 }
698 }
699
700private:
701 std::string m_oldTransport;
702};
703
704class WithConfig : private TestHomeFixture<DefaultPibDir>
705{
706public:
707 void
708 configure(const std::string& faceUri)
709 {
710 createClientConf({"transport=" + faceUri});
711 }
712};
713
714class WithEnvAndConfig : public WithEnv, public WithConfig
715{
716};
717
718typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
719
720BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700721{
722 shared_ptr<Face> face;
723 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
724 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700725}
726
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700727BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
728{
729 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700730
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700731 shared_ptr<Face> face;
732 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
733 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
734}
735
736BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
737{
738 this->configure("tcp://127.0.0.1:6000");
739
740 shared_ptr<Face> face;
741 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
742 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
743}
744
745BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
746{
747 this->configure("wrong-transport:");
748
749 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
750}
751
752BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
753{
754 this->configure("wrong-uri");
755
756 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
757}
758
759BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
760{
761 this->WithEnv::configure("tcp://127.0.0.1:6000");
762 this->WithConfig::configure("unix://some/path");
763
764 shared_ptr<Face> face;
765 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
766 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
767}
768
769BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
770{
771 this->WithEnv::configure("wrong-uri");
772 this->WithConfig::configure("wrong-transport:");
773
774 auto transport = make_shared<UnixTransport>("unix://some/path");
775 shared_ptr<Face> face;
776 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
777 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
778}
779
Junxiao Shiae0b4182016-08-08 22:53:17 +0000780BOOST_AUTO_TEST_SUITE_END() // Transport
781
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700782BOOST_AUTO_TEST_SUITE_END() // TestFace
783
784} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400785} // namespace ndn