blob: 03bfa9bde54efaa1c94225163150828dc13e4dc7 [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);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070065 i.setInterestLifetime(time::milliseconds(50));
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080066 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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091 face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", time::milliseconds(1000)),
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080092 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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", time::milliseconds(50)),
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800101 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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700114 face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", time::milliseconds(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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", time::milliseconds(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());
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700139 scheduler.scheduleEvent(time::milliseconds(300),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800140 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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700146 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800147 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());
Obaid6e7f5f12014-03-11 14:46:10 -0500163 scheduler.scheduleEvent(time::seconds(1),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800164 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
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700175 scheduler.scheduleEvent(time::milliseconds(200),
Alexander Afanasyeva68aa7f2014-02-11 15:42:33 -0800176 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