blob: 2f6d7ecea184c6ce01918b0cb072ca745cd58a8d [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev20d2c582014-01-26 15:32:51 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080011 */
12
Alexander Afanasyev09c613f2014-01-29 00:23:58 -080013#include "face.hpp"
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080014#include "util/scheduler.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080015
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070016#include "boost-test.hpp"
17
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080018namespace ndn {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080019
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080020
Alexander Afanasyev90164962014-03-06 08:29:59 +000021class FacesFixture
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080022{
Alexander Afanasyev90164962014-03-06 08:29:59 +000023public:
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080024 FacesFixture()
Alexander Afanasyev90164962014-03-06 08:29:59 +000025 : nData(0)
26 , nTimeouts(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080027 , regPrefixId(0)
Alexander Afanasyev90164962014-03-06 08:29:59 +000028 , nInInterests(0)
29 , nInInterests2(0)
30 , nRegFailures(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080031 {
32 }
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070033
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080034 void
35 onData()
36 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000037 ++nData;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080038 }
39
40 void
41 onTimeout()
42 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000043 ++nTimeouts;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080044 }
45
46 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000047 onInterest(Face& face,
48 const Name&, const Interest&)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080049 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000050 ++nInInterests;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080051
52 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080053 }
54
55 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000056 onInterest2(Face& face,
57 const Name&, const Interest&)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080058 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000059 ++nInInterests2;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080060
61 face.unsetInterestFilter(regPrefixId2);
62 }
63
64 void
Alexander Afanasyev90164962014-03-06 08:29:59 +000065 onInterestRegex(Face& face,
66 const InterestFilter&, const Interest&)
67 {
68 ++nInInterests;
69 }
70
71 void
72 onInterestRegexError(Face& face,
73 const Name&, const Interest&)
74 {
75 BOOST_FAIL("InterestFilter::Error should have been triggered");
76 }
77
78 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080079 onRegFailed()
80 {
Alexander Afanasyev90164962014-03-06 08:29:59 +000081 ++nRegFailures;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080082 }
83
84 void
85 expressInterest(Face& face, const Name& name)
86 {
87 Interest i(name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080089 face.expressInterest(i,
90 bind(&FacesFixture::onData, this),
91 bind(&FacesFixture::onTimeout, this));
92 }
93
94 void
95 terminate(Face& face)
96 {
97 face.shutdown();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080098 }
99
Alexander Afanasyev90164962014-03-06 08:29:59 +0000100 uint32_t nData;
101 uint32_t nTimeouts;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800102
103 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800104 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyev90164962014-03-06 08:29:59 +0000105 uint32_t nInInterests;
106 uint32_t nInInterests2;
107 uint32_t nRegFailures;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800108};
109
Alexander Afanasyev90164962014-03-06 08:29:59 +0000110BOOST_FIXTURE_TEST_SUITE(TestFaces, FacesFixture)
111
112BOOST_AUTO_TEST_CASE(Unix)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800113{
114 Face face;
115
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700116 face.expressInterest(Interest("/", time::milliseconds(1000)),
117 bind(&FacesFixture::onData, this),
118 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800119
120 BOOST_REQUIRE_NO_THROW(face.processEvents());
121
Alexander Afanasyev90164962014-03-06 08:29:59 +0000122 BOOST_CHECK_EQUAL(nData, 1);
123 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800124
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700125 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
126 time::milliseconds(50)),
127 bind(&FacesFixture::onData, this),
128 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700129
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800130 BOOST_REQUIRE_NO_THROW(face.processEvents());
131
Alexander Afanasyev90164962014-03-06 08:29:59 +0000132 BOOST_CHECK_EQUAL(nData, 1);
133 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800134}
135
Alexander Afanasyev90164962014-03-06 08:29:59 +0000136BOOST_AUTO_TEST_CASE(Tcp)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800137{
138 Face face("localhost");
139
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700140 face.expressInterest(Interest("/", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800141 bind(&FacesFixture::onData, this),
142 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800143
144 BOOST_REQUIRE_NO_THROW(face.processEvents());
145
Alexander Afanasyev90164962014-03-06 08:29:59 +0000146 BOOST_CHECK_EQUAL(nData, 1);
147 BOOST_CHECK_EQUAL(nTimeouts, 0);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800148
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700149 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere",
150 time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800151 bind(&FacesFixture::onData, this),
152 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800153
154 BOOST_REQUIRE_NO_THROW(face.processEvents());
155
Alexander Afanasyev90164962014-03-06 08:29:59 +0000156 BOOST_CHECK_EQUAL(nData, 1);
157 BOOST_CHECK_EQUAL(nTimeouts, 1);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800158}
159
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800160
Alexander Afanasyev90164962014-03-06 08:29:59 +0000161BOOST_AUTO_TEST_CASE(SetFilter)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800162{
163 Face face;
164 Face face2(face.ioService());
165 Scheduler scheduler(*face.ioService());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700166 scheduler.scheduleEvent(time::milliseconds(300),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700167 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700168
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800169 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000170 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800171 bind(&FacesFixture::onRegFailed, this));
172
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700173 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800174 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700175 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700176
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800177 BOOST_REQUIRE_NO_THROW(face.processEvents());
178
Alexander Afanasyev90164962014-03-06 08:29:59 +0000179 BOOST_CHECK_EQUAL(nRegFailures, 0);
180 BOOST_CHECK_EQUAL(nInInterests, 1);
181 BOOST_CHECK_EQUAL(nTimeouts, 1);
182 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800183}
184
Alexander Afanasyev90164962014-03-06 08:29:59 +0000185BOOST_AUTO_TEST_CASE(SetTwoFilters)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800186{
187 Face face;
188 Face face2(face.ioService());
189 Scheduler scheduler(*face.ioService());
Obaid6e7f5f12014-03-11 14:46:10 -0500190 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700191 bind(&FacesFixture::terminate, this, ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700192
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800193 regPrefixId = face.setInterestFilter("/Hello/World",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000194 bind(&FacesFixture::onInterest, this, ref(face), _1, _2),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800195 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700196
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800197 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
Alexander Afanasyev90164962014-03-06 08:29:59 +0000198 bind(&FacesFixture::onInterest2, this, ref(face), _1, _2),
199 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800200
201
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700202 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800203 bind(&FacesFixture::expressInterest, this,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -0700204 ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700205
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800206 BOOST_REQUIRE_NO_THROW(face.processEvents());
207
Alexander Afanasyev90164962014-03-06 08:29:59 +0000208 BOOST_CHECK_EQUAL(nRegFailures, 0);
209 BOOST_CHECK_EQUAL(nInInterests, 1);
210 BOOST_CHECK_EQUAL(nInInterests2, 0);
211 BOOST_CHECK_EQUAL(nTimeouts, 1);
212 BOOST_CHECK_EQUAL(nData, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800213}
214
Alexander Afanasyev90164962014-03-06 08:29:59 +0000215BOOST_AUTO_TEST_CASE(SetRegexFilterError)
216{
217 Face face;
218 Face face2(face.getIoService());
219 Scheduler scheduler(*face.ioService());
220 scheduler.scheduleEvent(time::seconds(1),
221 bind(&FacesFixture::terminate, this, ref(face)));
222
223 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
224 bind(&FacesFixture::onInterestRegexError, this,
225 ref(face), _1, _2),
226 bind(&FacesFixture::onRegFailed, this));
227
228 scheduler.scheduleEvent(time::milliseconds(300),
229 bind(&FacesFixture::expressInterest, this,
230 ref(face2), Name("/Hello/World/XXX/b/c"))); // should match
231
232 BOOST_REQUIRE_THROW(face.processEvents(), InterestFilter::Error);
233}
234
235BOOST_AUTO_TEST_CASE(SetRegexFilter)
236{
237 Face face;
238 Face face2(face.getIoService());
239 Scheduler scheduler(*face.ioService());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000240 scheduler.scheduleEvent(time::seconds(2),
241 bind(&FacesFixture::terminate, this, ref(face)));
242
243 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World", "<><b><c>?"),
244 bind(&FacesFixture::onInterestRegex, this,
245 ref(face), _1, _2),
246 bind(&FacesFixture::onRegFailed, this));
247
248 scheduler.scheduleEvent(time::milliseconds(200),
249 bind(&FacesFixture::expressInterest, this,
250 ref(face2), Name("/Hello/World/a"))); // shouldn't match
251
252 scheduler.scheduleEvent(time::milliseconds(300),
253 bind(&FacesFixture::expressInterest, this,
254 ref(face2), Name("/Hello/World/a/b"))); // should match
255
256 scheduler.scheduleEvent(time::milliseconds(400),
257 bind(&FacesFixture::expressInterest, this,
258 ref(face2), Name("/Hello/World/a/b/c"))); // should match
259
260 scheduler.scheduleEvent(time::milliseconds(500),
261 bind(&FacesFixture::expressInterest, this,
262 ref(face2), Name("/Hello/World/a/b/d"))); // should not match
263
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700264 BOOST_REQUIRE_NO_THROW(face.processEvents());
Alexander Afanasyev90164962014-03-06 08:29:59 +0000265
266 BOOST_CHECK_EQUAL(nRegFailures, 0);
267 BOOST_CHECK_EQUAL(nInInterests, 2);
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700268 BOOST_CHECK_EQUAL(nTimeouts, 4);
Alexander Afanasyev90164962014-03-06 08:29:59 +0000269 BOOST_CHECK_EQUAL(nData, 0);
270}
271
Alexander Afanasyevee8bb1e2014-05-02 17:39:54 -0700272class FacesFixture2 : public FacesFixture
273{
274public:
275 void
276 checkPrefix(bool doesExist)
277 {
278 int result = std::system("nfd-status | grep /Hello/World >/dev/null");
279
280 if (doesExist) {
281 BOOST_CHECK_EQUAL(result, 0);
282 }
283 else {
284 BOOST_CHECK_NE(result, 0);
285 }
286 }
287};
288
289BOOST_FIXTURE_TEST_CASE(RegisterUnregisterPrefix, FacesFixture2)
290{
291 Face face;
292 Scheduler scheduler(*face.ioService());
293 scheduler.scheduleEvent(time::seconds(2),
294 bind(&FacesFixture::terminate, this, ref(face)));
295
296 regPrefixId = face.setInterestFilter(InterestFilter("/Hello/World"),
297 bind(&FacesFixture::onInterest, this,
298 ref(face), _1, _2),
299 bind(&FacesFixture::onRegFailed, this));
300
301 scheduler.scheduleEvent(time::milliseconds(500),
302 bind(&FacesFixture2::checkPrefix, this, true));
303
304 scheduler.scheduleEvent(time::seconds(1),
305 bind(&Face::unsetInterestFilter, &face,
306 regPrefixId)); // shouldn't match
307
308 scheduler.scheduleEvent(time::milliseconds(1500),
309 bind(&FacesFixture2::checkPrefix, this, false));
310
311 BOOST_REQUIRE_NO_THROW(face.processEvents());
312}
Alexander Afanasyev90164962014-03-06 08:29:59 +0000313
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800314BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800315
316} // namespace ndn