blob: 97bff4d341a9f5b26523e53b811064ca765aa7d6 [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
20BOOST_AUTO_TEST_SUITE(TestFaces)
21
22struct FacesFixture
23{
24 FacesFixture()
25 : dataCount(0)
26 , timeoutCount(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080027 , regPrefixId(0)
28 , inInterestCount(0)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080029 , inInterestCount2(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080030 , regFailedCount(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 {
37 ++dataCount;
38 }
39
40 void
41 onTimeout()
42 {
43 ++timeoutCount;
44 }
45
46 void
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080047 onInterest(Face& face)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080048 {
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080049 ++inInterestCount;
50
51 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080052 }
53
54 void
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080055 onInterest2(Face& face)
56 {
57 ++inInterestCount2;
58
59 face.unsetInterestFilter(regPrefixId2);
60 }
61
62 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080063 onRegFailed()
64 {
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080065 ++regFailedCount;
66 }
67
68 void
69 expressInterest(Face& face, const Name& name)
70 {
71 Interest i(name);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070072 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080073 face.expressInterest(i,
74 bind(&FacesFixture::onData, this),
75 bind(&FacesFixture::onTimeout, this));
76 }
77
78 void
79 terminate(Face& face)
80 {
81 face.shutdown();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080082 }
83
84 uint32_t dataCount;
85 uint32_t timeoutCount;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080086
87 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080088 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080089 uint32_t inInterestCount;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080090 uint32_t inInterestCount2;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080091 uint32_t regFailedCount;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080092};
93
94BOOST_FIXTURE_TEST_CASE (Unix, FacesFixture)
95{
96 Face face;
97
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070098 face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", time::milliseconds(1000)),
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080099 ptr_lib::bind(&FacesFixture::onData, this),
100 ptr_lib::bind(&FacesFixture::onTimeout, this));
101
102 BOOST_REQUIRE_NO_THROW(face.processEvents());
103
104 BOOST_CHECK_EQUAL(dataCount, 1);
105 BOOST_CHECK_EQUAL(timeoutCount, 0);
106
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700107 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", time::milliseconds(50)),
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800108 ptr_lib::bind(&FacesFixture::onData, this),
109 ptr_lib::bind(&FacesFixture::onTimeout, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700110
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800111 BOOST_REQUIRE_NO_THROW(face.processEvents());
112
113 BOOST_CHECK_EQUAL(dataCount, 1);
114 BOOST_CHECK_EQUAL(timeoutCount, 1);
115}
116
117BOOST_FIXTURE_TEST_CASE (Tcp, FacesFixture)
118{
119 Face face("localhost");
120
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121 face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", time::milliseconds(1000)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800122 bind(&FacesFixture::onData, this),
123 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800124
125 BOOST_REQUIRE_NO_THROW(face.processEvents());
126
127 BOOST_CHECK_EQUAL(dataCount, 1);
128 BOOST_CHECK_EQUAL(timeoutCount, 0);
129
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700130 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", time::milliseconds(50)),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800131 bind(&FacesFixture::onData, this),
132 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800133
134 BOOST_REQUIRE_NO_THROW(face.processEvents());
135
136 BOOST_CHECK_EQUAL(dataCount, 1);
137 BOOST_CHECK_EQUAL(timeoutCount, 1);
138}
139
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800140
141BOOST_FIXTURE_TEST_CASE (SetFilter, FacesFixture)
142{
143 Face face;
144 Face face2(face.ioService());
145 Scheduler scheduler(*face.ioService());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700146 scheduler.scheduleEvent(time::milliseconds(300),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800147 bind(&FacesFixture::terminate, this, func_lib::ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700148
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800149 regPrefixId = face.setInterestFilter("/Hello/World",
150 bind(&FacesFixture::onInterest, this, func_lib::ref(face)),
151 bind(&FacesFixture::onRegFailed, this));
152
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700153 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800154 bind(&FacesFixture::expressInterest, this,
155 func_lib::ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700156
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800157 BOOST_REQUIRE_NO_THROW(face.processEvents());
158
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700159 BOOST_CHECK_EQUAL(regFailedCount, 0);
160 BOOST_CHECK_EQUAL(inInterestCount, 1);
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800161 BOOST_CHECK_EQUAL(timeoutCount, 1);
162 BOOST_CHECK_EQUAL(dataCount, 0);
163}
164
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800165BOOST_FIXTURE_TEST_CASE (SetTwoFilters, FacesFixture)
166{
167 Face face;
168 Face face2(face.ioService());
169 Scheduler scheduler(*face.ioService());
Obaid6e7f5f12014-03-11 14:46:10 -0500170 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800171 bind(&FacesFixture::terminate, this, func_lib::ref(face)));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700172
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800173 regPrefixId = face.setInterestFilter("/Hello/World",
174 bind(&FacesFixture::onInterest, this, func_lib::ref(face)),
175 bind(&FacesFixture::onRegFailed, this));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700176
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800177 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
178 bind(&FacesFixture::onInterest2, this, func_lib::ref(face)),
179 bind(&FacesFixture::onRegFailed, this));
180
181
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700182 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800183 bind(&FacesFixture::expressInterest, this,
184 func_lib::ref(face2), Name("/Hello/World/!")));
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700185
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800186 BOOST_REQUIRE_NO_THROW(face.processEvents());
187
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -0700188 BOOST_CHECK_EQUAL(regFailedCount, 0);
189 BOOST_CHECK_EQUAL(inInterestCount, 1);
190 BOOST_CHECK_EQUAL(inInterestCount2, 0);
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800191 BOOST_CHECK_EQUAL(timeoutCount, 1);
192 BOOST_CHECK_EQUAL(dataCount, 0);
193}
194
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800195BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800196
197} // namespace ndn