blob: 1fab695cb413a185b73cf667b0b41900dbc54b06 [file] [log] [blame]
Alexander Afanasyev20d2c582014-01-26 15:32:51 -08001/**
2 * Copyright (C) 2013 Regents of the University of California.
Alexander Afanasyev20d2c582014-01-26 15:32:51 -08003 * See COPYING for copyright and distribution information.
4 */
5
6#include <boost/test/unit_test.hpp>
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08007#include "face.hpp"
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -08008#include "util/scheduler.hpp"
Alexander Afanasyev20d2c582014-01-26 15:32:51 -08009
10using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080011namespace ndn {
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080012
13BOOST_AUTO_TEST_SUITE(TestFaces)
14
15struct FacesFixture
16{
17 FacesFixture()
18 : dataCount(0)
19 , timeoutCount(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080020 , regPrefixId(0)
21 , inInterestCount(0)
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080022 , inInterestCount2(0)
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080023 , regFailedCount(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080024 {
25 }
26
27 void
28 onData()
29 {
30 ++dataCount;
31 }
32
33 void
34 onTimeout()
35 {
36 ++timeoutCount;
37 }
38
39 void
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080040 onInterest(Face& face)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080041 {
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080042 ++inInterestCount;
43
44 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080045 }
46
47 void
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080048 onInterest2(Face& face)
49 {
50 ++inInterestCount2;
51
52 face.unsetInterestFilter(regPrefixId2);
53 }
54
55 void
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080056 onRegFailed()
57 {
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080058 ++regFailedCount;
59 }
60
61 void
62 expressInterest(Face& face, const Name& name)
63 {
64 Interest i(name);
65 i.setInterestLifetime(50);
66 face.expressInterest(i,
67 bind(&FacesFixture::onData, this),
68 bind(&FacesFixture::onTimeout, this));
69 }
70
71 void
72 terminate(Face& face)
73 {
74 face.shutdown();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080075 }
76
77 uint32_t dataCount;
78 uint32_t timeoutCount;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080079
80 const RegisteredPrefixId* regPrefixId;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080081 const RegisteredPrefixId* regPrefixId2;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080082 uint32_t inInterestCount;
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -080083 uint32_t inInterestCount2;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080084 uint32_t regFailedCount;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080085};
86
87BOOST_FIXTURE_TEST_CASE (Unix, FacesFixture)
88{
89 Face face;
90
91 face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", 1000),
92 ptr_lib::bind(&FacesFixture::onData, this),
93 ptr_lib::bind(&FacesFixture::onTimeout, this));
94
95 BOOST_REQUIRE_NO_THROW(face.processEvents());
96
97 BOOST_CHECK_EQUAL(dataCount, 1);
98 BOOST_CHECK_EQUAL(timeoutCount, 0);
99
100 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", 50),
101 ptr_lib::bind(&FacesFixture::onData, this),
102 ptr_lib::bind(&FacesFixture::onTimeout, this));
103
104 BOOST_REQUIRE_NO_THROW(face.processEvents());
105
106 BOOST_CHECK_EQUAL(dataCount, 1);
107 BOOST_CHECK_EQUAL(timeoutCount, 1);
108}
109
110BOOST_FIXTURE_TEST_CASE (Tcp, FacesFixture)
111{
112 Face face("localhost");
113
114 face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", 1000),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800115 bind(&FacesFixture::onData, this),
116 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800117
118 BOOST_REQUIRE_NO_THROW(face.processEvents());
119
120 BOOST_CHECK_EQUAL(dataCount, 1);
121 BOOST_CHECK_EQUAL(timeoutCount, 0);
122
123 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", 50),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800124 bind(&FacesFixture::onData, this),
125 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800126
127 BOOST_REQUIRE_NO_THROW(face.processEvents());
128
129 BOOST_CHECK_EQUAL(dataCount, 1);
130 BOOST_CHECK_EQUAL(timeoutCount, 1);
131}
132
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800133
134BOOST_FIXTURE_TEST_CASE (SetFilter, FacesFixture)
135{
136 Face face;
137 Face face2(face.ioService());
138 Scheduler scheduler(*face.ioService());
139 scheduler.scheduleEvent(time::seconds(0.3),
140 bind(&FacesFixture::terminate, this, func_lib::ref(face)));
141
142 regPrefixId = face.setInterestFilter("/Hello/World",
143 bind(&FacesFixture::onInterest, this, func_lib::ref(face)),
144 bind(&FacesFixture::onRegFailed, this));
145
146 scheduler.scheduleEvent(time::seconds(0.2),
147 bind(&FacesFixture::expressInterest, this,
148 func_lib::ref(face2), Name("/Hello/World/!")));
149
150 BOOST_REQUIRE_NO_THROW(face.processEvents());
151
152 BOOST_CHECK_EQUAL(regFailedCount, 0);
153 BOOST_CHECK_EQUAL(inInterestCount, 1);
154 BOOST_CHECK_EQUAL(timeoutCount, 1);
155 BOOST_CHECK_EQUAL(dataCount, 0);
156}
157
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800158BOOST_FIXTURE_TEST_CASE (SetTwoFilters, FacesFixture)
159{
160 Face face;
161 Face face2(face.ioService());
162 Scheduler scheduler(*face.ioService());
163 scheduler.scheduleEvent(time::seconds(0.3),
164 bind(&FacesFixture::terminate, this, func_lib::ref(face)));
165
166 regPrefixId = face.setInterestFilter("/Hello/World",
167 bind(&FacesFixture::onInterest, this, func_lib::ref(face)),
168 bind(&FacesFixture::onRegFailed, this));
169
170 regPrefixId2 = face.setInterestFilter("/Los/Angeles/Lakers",
171 bind(&FacesFixture::onInterest2, this, func_lib::ref(face)),
172 bind(&FacesFixture::onRegFailed, this));
173
174
175 scheduler.scheduleEvent(time::seconds(0.2),
176 bind(&FacesFixture::expressInterest, this,
177 func_lib::ref(face2), Name("/Hello/World/!")));
178
179 BOOST_REQUIRE_NO_THROW(face.processEvents());
180
181 BOOST_CHECK_EQUAL(regFailedCount, 0);
182 BOOST_CHECK_EQUAL(inInterestCount, 1);
183 BOOST_CHECK_EQUAL(inInterestCount2, 0);
184 BOOST_CHECK_EQUAL(timeoutCount, 1);
185 BOOST_CHECK_EQUAL(dataCount, 0);
186}
187
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800188BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800189
190} // namespace ndn