blob: bf87de68a6e39d1dfdd4b1d9bd4766c0941c7746 [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"
23#include "util/scheduler.hpp"
24#include "security/key-chain.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080025#include "util/dummy-client-face.hpp"
Alexander Afanasyev3a6da362015-12-29 20:31:03 -080026#include "transport/tcp-transport.hpp"
Alexander Afanasyev57e00362016-06-23 13:22:54 -070027#include "transport/unix-transport.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"
31#include "key-chain-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
Junxiao Shi76e0eb22016-08-08 05:54:10 +000097BOOST_AUTO_TEST_CASE(ExpressInterestEmptyDataCallback)
98{
99 face.expressInterest(Interest("/Hello/World"),
100 nullptr,
101 bind([] { BOOST_FAIL("Unexpected Nack"); }),
102 bind([] { BOOST_FAIL("Unexpected timeout"); }));
103 advanceClocks(time::milliseconds(1));
104
105 BOOST_CHECK_NO_THROW(do {
106 face.receive(*makeData("/Hello/World/a"));
107 advanceClocks(time::milliseconds(1));
108 } while (false));
109}
110
Eric Newberry83872fd2015-08-06 17:01:24 -0700111BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestData)
112{
113 size_t nData = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800114 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000115 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800116 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
117 ++nData;
118 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000119 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400120
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000121 advanceClocks(time::milliseconds(40));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400122
Junxiao Shi85d90832016-08-04 03:19:46 +0000123 face.receive(*makeData("/Bye/World/a"));
124 face.receive(*makeData("/Hello/World/a"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800125
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000126 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400127
128 BOOST_CHECK_EQUAL(nData, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800129 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
130 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800131
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800132 face.expressInterest(Interest("/Hello/World/a", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000133 [&] (const Interest& i, Data& d) {
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800134 BOOST_CHECK(i.getName().isPrefixOf(d.getName()));
135 ++nData;
136 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000137 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000138 advanceClocks(time::milliseconds(40));
Junxiao Shi85d90832016-08-04 03:19:46 +0000139 face.receive(*makeData("/Hello/World/a/1/xxxxx"));
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800140
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000141 advanceClocks(time::milliseconds(50), 2);
Alexander Afanasyev9d158f02015-02-17 21:30:19 -0800142
143 BOOST_CHECK_EQUAL(nData, 2);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800144 BOOST_CHECK_EQUAL(face.sentInterests.size(), 2);
145 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400146}
147
148BOOST_AUTO_TEST_CASE(ExpressInterestTimeout)
149{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800150 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800151 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000152 bind([] { BOOST_FAIL("Unexpected Data"); }),
153 bind([] { BOOST_FAIL("Unexpected Nack"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800154 [&nTimeouts] (const Interest& i) {
155 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
156 ++nTimeouts;
157 });
Eric Newberry83872fd2015-08-06 17:01:24 -0700158
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000159 advanceClocks(time::milliseconds(200), 5);
Eric Newberry83872fd2015-08-06 17:01:24 -0700160
161 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800162 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
163 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
164 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
Eric Newberry83872fd2015-08-06 17:01:24 -0700165}
166
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000167BOOST_AUTO_TEST_CASE(ExpressInterestEmptyTimeoutCallback)
168{
169 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
170 bind([] { BOOST_FAIL("Unexpected Data"); }),
171 bind([] { BOOST_FAIL("Unexpected Nack"); }),
172 nullptr);
173 advanceClocks(time::milliseconds(40));
174
175 BOOST_CHECK_NO_THROW(do {
176 advanceClocks(time::milliseconds(6), 2);
177 } while (false));
178}
179
Eric Newberry83872fd2015-08-06 17:01:24 -0700180BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestTimeout)
181{
182 size_t nTimeouts = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800183 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000184 bind([] { BOOST_FAIL("Unexpected data"); }),
185 bind([&nTimeouts] { ++nTimeouts; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400186
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000187 advanceClocks(time::milliseconds(200), 5);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400188
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400189 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800190 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
191 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400192}
193
Eric Newberry83872fd2015-08-06 17:01:24 -0700194BOOST_AUTO_TEST_CASE(ExpressInterestNack)
195{
196 size_t nNacks = 0;
197
198 Interest interest("/Hello/World", time::milliseconds(50));
199
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800200 face.expressInterest(interest,
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000201 bind([] { BOOST_FAIL("Unexpected Data"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800202 [&] (const Interest& i, const lp::Nack& n) {
203 BOOST_CHECK(i.getName().isPrefixOf(n.getInterest().getName()));
204 BOOST_CHECK_EQUAL(i.getName(), "/Hello/World");
205 BOOST_CHECK_EQUAL(n.getReason(), lp::NackReason::DUPLICATE);
206 ++nNacks;
207 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000208 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Eric Newberry83872fd2015-08-06 17:01:24 -0700209
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000210 advanceClocks(time::milliseconds(40));
Eric Newberry83872fd2015-08-06 17:01:24 -0700211
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000212 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
Eric Newberry83872fd2015-08-06 17:01:24 -0700213
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000214 advanceClocks(time::milliseconds(50), 2);
Eric Newberry83872fd2015-08-06 17:01:24 -0700215
216 BOOST_CHECK_EQUAL(nNacks, 1);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800217 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
Eric Newberry83872fd2015-08-06 17:01:24 -0700218}
219
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000220BOOST_AUTO_TEST_CASE(ExpressInterestEmptyNackCallback)
221{
222 face.expressInterest(Interest("/Hello/World"),
223 bind([] { BOOST_FAIL("Unexpected Data"); }),
224 nullptr,
225 bind([] { BOOST_FAIL("Unexpected timeout"); }));
226 advanceClocks(time::milliseconds(1));
227
228 BOOST_CHECK_NO_THROW(do {
229 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::DUPLICATE));
230 advanceClocks(time::milliseconds(1));
231 } while (false));
232}
233
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000234BOOST_AUTO_TEST_CASE(DeprecatedExpressInterestNack)
235{
236 size_t nTimeouts = 0;
237 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
238 bind([] { BOOST_FAIL("Unexpected data"); }),
239 bind([&nTimeouts] { ++nTimeouts; }));
240 advanceClocks(time::milliseconds(1));
241
242 face.receive(makeNack(face.sentInterests.at(0), lp::NackReason::CONGESTION));
243 advanceClocks(time::milliseconds(1));
244
245 BOOST_CHECK_EQUAL(nTimeouts, 1);
246 BOOST_CHECK_EQUAL(face.sentInterests.size(), 1);
247 BOOST_CHECK_EQUAL(face.sentData.size(), 0);
248}
249
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800250BOOST_AUTO_TEST_CASE(RemovePendingInterest)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400251{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800252 const PendingInterestId* interestId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800253 face.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000254 bind([] { BOOST_FAIL("Unexpected data"); }),
255 bind([] { BOOST_FAIL("Unexpected nack"); }),
256 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800257 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400258
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800259 face.removePendingInterest(interestId);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800260 advanceClocks(time::milliseconds(10));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400261
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000262 face.receive(*makeData("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000263 advanceClocks(time::milliseconds(200), 5);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400264}
265
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000266BOOST_AUTO_TEST_CASE(RemoveAllPendingInterests)
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500267{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800268 face.expressInterest(Interest("/Hello/World/0", time::milliseconds(50)),
269 bind([] { BOOST_FAIL("Unexpected data"); }),
270 bind([] { BOOST_FAIL("Unexpected nack"); }),
271 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500272
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800273 face.expressInterest(Interest("/Hello/World/1", time::milliseconds(50)),
274 bind([] { BOOST_FAIL("Unexpected data"); }),
275 bind([] { BOOST_FAIL("Unexpected nack"); }),
276 bind([] { BOOST_FAIL("Unexpected timeout"); }));
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500277
278 advanceClocks(time::milliseconds(10));
279
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800280 face.removeAllPendingInterests();
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500281 advanceClocks(time::milliseconds(10));
282
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800283 BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500284
Junxiao Shi85d90832016-08-04 03:19:46 +0000285 face.receive(*makeData("/Hello/World/0"));
286 face.receive(*makeData("/Hello/World/1"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000287 advanceClocks(time::milliseconds(200), 5);
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500288}
289
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000290BOOST_AUTO_TEST_CASE(DestructionWithoutCancellingPendingInterests) // Bug #2518
291{
292 {
293 DummyClientFace face2(io, m_keyChain);
294 face2.expressInterest(Interest("/Hello/World", time::milliseconds(50)),
295 bind([]{}), bind([]{}));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000296 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000297 }
298
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000299 advanceClocks(time::milliseconds(50), 2);
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000300 // should not segfault
301}
302
303BOOST_AUTO_TEST_SUITE_END() // Consumer
304
305BOOST_AUTO_TEST_SUITE(Producer)
306
307BOOST_AUTO_TEST_CASE(PutNack)
308{
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000309 BOOST_CHECK_EQUAL(face.sentNacks.size(), 0);
310
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000311 face.put(makeNack(Interest("/Hello/World", time::milliseconds(50)), lp::NackReason::NO_ROUTE));
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000312 advanceClocks(time::milliseconds(10));
313
314 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
315}
Ilya Moiseenko56b0bf82015-11-08 11:14:28 -0500316
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400317BOOST_AUTO_TEST_CASE(SetUnsetInterestFilter)
318{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800319 size_t nInterests = 0;
320 size_t nRegs = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400321 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800322 face.setInterestFilter("/Hello/World",
323 bind([&nInterests] { ++nInterests; }),
324 bind([&nRegs] { ++nRegs; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000325 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000326 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800327 BOOST_CHECK_EQUAL(nRegs, 1);
328 BOOST_CHECK_EQUAL(nInterests, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400329
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000330 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000331 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400332
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800333 BOOST_CHECK_EQUAL(nRegs, 1);
334 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400335
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000336 face.receive(Interest("/Bye/World/%21"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800337 advanceClocks(time::milliseconds(10000), 10);
338 BOOST_CHECK_EQUAL(nInterests, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400339
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000340 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000341 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800342 BOOST_CHECK_EQUAL(nInterests, 2);
343
344 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800345 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000346 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400347
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000348 face.receive(Interest("/Hello/World/%21/3"));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800349 BOOST_CHECK_EQUAL(nInterests, 2);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400350
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000351 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000352 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400353
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000354 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000355 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800356}
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400357
Junxiao Shi76e0eb22016-08-08 05:54:10 +0000358BOOST_AUTO_TEST_CASE(SetInterestFilterEmptyInterestCallback)
359{
360 face.setInterestFilter("/A", nullptr);
361 advanceClocks(time::milliseconds(1));
362
363 BOOST_CHECK_NO_THROW(do {
364 face.receive(*makeInterest("/A/1"));
365 advanceClocks(time::milliseconds(1));
366 } while (false));
367}
368
Joao Pereira0b3cac52015-07-02 14:49:49 -0400369BOOST_AUTO_TEST_CASE(SetUnsetInterestFilterWithoutSucessCallback)
370{
371 size_t nInterests = 0;
372 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800373 face.setInterestFilter("/Hello/World",
374 bind([&nInterests] { ++nInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000375 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000376 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400377 BOOST_CHECK_EQUAL(nInterests, 0);
378
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000379 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000380 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400381
382 BOOST_CHECK_EQUAL(nInterests, 1);
383
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000384 face.receive(Interest("/Bye/World/%21"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400385 advanceClocks(time::milliseconds(10000), 10);
386 BOOST_CHECK_EQUAL(nInterests, 1);
387
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000388 face.receive(Interest("/Hello/World/%21/2"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000389 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400390 BOOST_CHECK_EQUAL(nInterests, 2);
391
392 // removing filter
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800393 face.unsetInterestFilter(regPrefixId);
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000394 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400395
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000396 face.receive(Interest("/Hello/World/%21/3"));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400397 BOOST_CHECK_EQUAL(nInterests, 2);
398
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000399 face.unsetInterestFilter(static_cast<const RegisteredPrefixId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000400 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400401
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000402 face.unsetInterestFilter(static_cast<const InterestFilterId*>(nullptr));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000403 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400404}
405
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800406BOOST_FIXTURE_TEST_CASE(SetInterestFilterFail, FacesNoRegistrationReplyFixture)
407{
408 // don't enable registration reply
409 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800410 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000411 bind([] { BOOST_FAIL("Unexpected Interest"); }),
412 bind([] { BOOST_FAIL("Unexpected success of setInterestFilter"); }),
413 bind([&nRegFailed] { ++nRegFailed; }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800414
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000415 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800416 BOOST_CHECK_EQUAL(nRegFailed, 0);
417
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000418 advanceClocks(time::milliseconds(2000), 5);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800419 BOOST_CHECK_EQUAL(nRegFailed, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400420}
421
Joao Pereira0b3cac52015-07-02 14:49:49 -0400422BOOST_FIXTURE_TEST_CASE(SetInterestFilterFailWithoutSuccessCallback, FacesNoRegistrationReplyFixture)
423{
424 // don't enable registration reply
425 size_t nRegFailed = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800426 face.setInterestFilter("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000427 bind([] { BOOST_FAIL("Unexpected Interest"); }),
428 bind([&nRegFailed] { ++nRegFailed; }));
Joao Pereira0b3cac52015-07-02 14:49:49 -0400429
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000430 advanceClocks(time::milliseconds(25), 4);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400431 BOOST_CHECK_EQUAL(nRegFailed, 0);
432
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000433 advanceClocks(time::milliseconds(2000), 5);
Joao Pereira0b3cac52015-07-02 14:49:49 -0400434 BOOST_CHECK_EQUAL(nRegFailed, 1);
435}
436
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400437BOOST_AUTO_TEST_CASE(RegisterUnregisterPrefix)
438{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800439 size_t nRegSuccesses = 0;
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400440 const RegisteredPrefixId* regPrefixId =
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800441 face.registerPrefix("/Hello/World",
442 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000443 bind([] { BOOST_FAIL("Unexpected registerPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400444
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000445 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400446 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
447
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800448 size_t nUnregSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800449 face.unregisterPrefix(regPrefixId,
450 bind([&nUnregSuccesses] { ++nUnregSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000451 bind([] { BOOST_FAIL("Unexpected unregisterPrefix failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400452
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000453 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400454 BOOST_CHECK_EQUAL(nUnregSuccesses, 1);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400455}
456
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800457BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefixFail, FacesNoRegistrationReplyFixture)
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400458{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800459 size_t nRegFailures = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800460 face.registerPrefix("/Hello/World",
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000461 bind([] { BOOST_FAIL("Unexpected registerPrefix success"); }),
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800462 bind([&nRegFailures] { ++nRegFailures; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400463
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000464 advanceClocks(time::milliseconds(5000), 20);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800465 BOOST_CHECK_EQUAL(nRegFailures, 1);
466}
467
468BOOST_AUTO_TEST_CASE(SimilarFilters)
469{
470 size_t nInInterests1 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800471 face.setInterestFilter("/Hello/World",
472 bind([&nInInterests1] { ++nInInterests1; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000473 nullptr,
474 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800475
476 size_t nInInterests2 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800477 face.setInterestFilter("/Hello",
478 bind([&nInInterests2] { ++nInInterests2; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000479 nullptr,
480 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400481
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800482 size_t nInInterests3 = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800483 face.setInterestFilter("/Los/Angeles/Lakers",
484 bind([&nInInterests3] { ++nInInterests3; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000485 nullptr,
486 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400487
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000488 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400489
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000490 face.receive(Interest("/Hello/World/%21"));
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000491 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400492
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800493 BOOST_CHECK_EQUAL(nInInterests1, 1);
494 BOOST_CHECK_EQUAL(nInInterests2, 1);
495 BOOST_CHECK_EQUAL(nInInterests3, 0);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400496}
497
498BOOST_AUTO_TEST_CASE(SetRegexFilterError)
499{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800500 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
501 [] (const Name&, const Interest&) {
502 BOOST_FAIL("InterestFilter::Error should have been triggered");
503 },
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000504 nullptr,
505 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400506
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000507 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400508
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800509 BOOST_REQUIRE_THROW(face.receive(Interest("/Hello/World/XXX/b/c")), InterestFilter::Error);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400510}
511
512BOOST_AUTO_TEST_CASE(SetRegexFilter)
513{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800514 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800515 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
516 bind([&nInInterests] { ++nInInterests; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000517 nullptr,
518 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400519
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000520 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400521
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800522 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400523 BOOST_CHECK_EQUAL(nInInterests, 0);
524
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800525 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400526 BOOST_CHECK_EQUAL(nInInterests, 1);
527
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800528 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400529 BOOST_CHECK_EQUAL(nInInterests, 2);
530
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800531 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400532 BOOST_CHECK_EQUAL(nInInterests, 2);
533}
534
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400535BOOST_AUTO_TEST_CASE(SetRegexFilterAndRegister)
536{
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800537 size_t nInInterests = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800538 face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
539 bind([&nInInterests] { ++nInInterests; }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400540
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800541 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800542 face.registerPrefix("/Hello/World",
543 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000544 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400545
Junxiao Shif5b5ae22016-08-08 05:54:41 +0000546 advanceClocks(time::milliseconds(25), 4);
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400547 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
548
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800549 face.receive(Interest("/Hello/World/a")); // shouldn't match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400550 BOOST_CHECK_EQUAL(nInInterests, 0);
551
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800552 face.receive(Interest("/Hello/World/a/b")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400553 BOOST_CHECK_EQUAL(nInInterests, 1);
554
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800555 face.receive(Interest("/Hello/World/a/b/c")); // should match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400556 BOOST_CHECK_EQUAL(nInInterests, 2);
557
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800558 face.receive(Interest("/Hello/World/a/b/d")); // should not match
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400559 BOOST_CHECK_EQUAL(nInInterests, 2);
560}
561
Junxiao Shia1ea5062014-12-27 22:33:39 -0700562BOOST_FIXTURE_TEST_CASE(SetInterestFilterNoReg, FacesNoRegistrationReplyFixture) // Bug 2318
563{
564 // This behavior is specific to DummyClientFace.
565 // Regular Face won't accept incoming packets until something is sent.
566
567 int hit = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800568 face.setInterestFilter(Name("/"), bind([&hit] { ++hit; }));
569 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700570
571 auto interest = make_shared<Interest>("/A");
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800572 face.receive(*interest);
573 face.processEvents(time::milliseconds(-1));
Junxiao Shia1ea5062014-12-27 22:33:39 -0700574
575 BOOST_CHECK_EQUAL(hit, 1);
576}
577
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000578BOOST_AUTO_TEST_SUITE_END() // Producer
579
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500580BOOST_AUTO_TEST_CASE(ProcessEvents)
581{
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800582 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500583
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800584 size_t nRegSuccesses = 0;
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800585 face.registerPrefix("/Hello/World",
586 bind([&nRegSuccesses] { ++nRegSuccesses; }),
Junxiao Shi103d8ed2016-08-07 20:34:10 +0000587 bind([] { BOOST_FAIL("Unexpected setInterestFilter failure"); }));
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500588
589 // io_service::poll() without reset
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800590 face.getIoService().poll();
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500591 BOOST_CHECK_EQUAL(nRegSuccesses, 0);
592
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800593 face.processEvents(time::milliseconds(-1)); // io_service::reset()/poll() inside
Alexander Afanasyev8e158542014-11-18 00:47:18 -0500594 BOOST_CHECK_EQUAL(nRegSuccesses, 1);
595}
596
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700597struct PibDirWithDefaultTpm
598{
599 const std::string PATH = "build/keys-with-default-tpm";
600};
601
602BOOST_FIXTURE_TEST_CASE(FaceTransport, PibDirFixture<PibDirWithDefaultTpm>)
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800603{
604 KeyChain keyChain;
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700605 boost::asio::io_service io;
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800606
607 BOOST_CHECK(Face().getTransport() != nullptr);
608
Alexander Afanasyevbb64c172015-12-29 20:32:45 -0800609 BOOST_CHECK(Face(shared_ptr<Transport>()).getTransport() != nullptr);
610 BOOST_CHECK(Face(shared_ptr<Transport>(), io).getTransport() != nullptr);
611 BOOST_CHECK(Face(shared_ptr<Transport>(), io, keyChain).getTransport() != nullptr);
Alexander Afanasyev3a6da362015-12-29 20:31:03 -0800612
613 auto transport = make_shared<TcpTransport>("localhost", "6363"); // no real io operations will be scheduled
614 BOOST_CHECK(Face(transport).getTransport() == transport);
615 BOOST_CHECK(Face(transport, io).getTransport() == transport);
616 BOOST_CHECK(Face(transport, io, keyChain).getTransport() == transport);
617}
618
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700619BOOST_AUTO_TEST_SUITE(CustomizeTransport)
620
621class WithEnv : private IdentityManagementTimeFixture
622{
623public:
624 WithEnv()
625 {
626 if (getenv("NDN_CLIENT_TRANSPORT") != nullptr) {
627 m_oldTransport = getenv("NDN_CLIENT_TRANSPORT");
628 unsetenv("NDN_CLIENT_TRANSPORT");
629 }
630 }
631
632 void
633 configure(const std::string& faceUri)
634 {
635 setenv("NDN_CLIENT_TRANSPORT", faceUri.c_str(), true);
636 }
637
638 ~WithEnv()
639 {
640 if (!m_oldTransport.empty()) {
641 setenv("NDN_CLIENT_TRANSPORT", m_oldTransport.c_str(), true);
642 }
643 else {
644 unsetenv("NDN_CLIENT_TRANSPORT");
645 }
646 }
647
648private:
649 std::string m_oldTransport;
650};
651
652class WithConfig : private TestHomeFixture<DefaultPibDir>
653{
654public:
655 void
656 configure(const std::string& faceUri)
657 {
658 createClientConf({"transport=" + faceUri});
659 }
660};
661
662class WithEnvAndConfig : public WithEnv, public WithConfig
663{
664};
665
666typedef boost::mpl::vector<WithEnv, WithConfig> ConfigOptions;
667
668BOOST_FIXTURE_TEST_CASE(NoConfig, WithEnvAndConfig) // fixture configures test HOME and PIB/TPM path
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700669{
670 shared_ptr<Face> face;
671 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
672 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700673}
674
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700675BOOST_FIXTURE_TEST_CASE_TEMPLATE(Unix, T, ConfigOptions, T)
676{
677 this->configure("unix://some/path");
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700678
Alexander Afanasyevcf490552016-06-27 22:51:36 -0700679 shared_ptr<Face> face;
680 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
681 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
682}
683
684BOOST_FIXTURE_TEST_CASE_TEMPLATE(Tcp, T, ConfigOptions, T)
685{
686 this->configure("tcp://127.0.0.1:6000");
687
688 shared_ptr<Face> face;
689 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
690 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
691}
692
693BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongTransport, T, ConfigOptions, T)
694{
695 this->configure("wrong-transport:");
696
697 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
698}
699
700BOOST_FIXTURE_TEST_CASE_TEMPLATE(WrongUri, T, ConfigOptions, T)
701{
702 this->configure("wrong-uri");
703
704 BOOST_CHECK_THROW(make_shared<Face>(), ConfigFile::Error);
705}
706
707BOOST_FIXTURE_TEST_CASE(EnvOverride, WithEnvAndConfig)
708{
709 this->WithEnv::configure("tcp://127.0.0.1:6000");
710 this->WithConfig::configure("unix://some/path");
711
712 shared_ptr<Face> face;
713 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>());
714 BOOST_CHECK(dynamic_pointer_cast<TcpTransport>(face->getTransport()) != nullptr);
715}
716
717BOOST_FIXTURE_TEST_CASE(ExplicitTransport, WithEnvAndConfig)
718{
719 this->WithEnv::configure("wrong-uri");
720 this->WithConfig::configure("wrong-transport:");
721
722 auto transport = make_shared<UnixTransport>("unix://some/path");
723 shared_ptr<Face> face;
724 BOOST_REQUIRE_NO_THROW(face = make_shared<Face>(transport));
725 BOOST_CHECK(dynamic_pointer_cast<UnixTransport>(face->getTransport()) != nullptr);
726}
727
728BOOST_AUTO_TEST_SUITE_END() // CustomizeTransport
Alexander Afanasyev57e00362016-06-23 13:22:54 -0700729BOOST_AUTO_TEST_SUITE_END() // TestFace
730
731} // namespace tests
Alexander Afanasyev5fc795f2014-10-20 23:06:56 -0400732} // namespace ndn