blob: 9fc5860ef7b434876dae38b7c773ae5611eef8fe [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)
22 , regFailedCount(0)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080023 {
24 }
25
26 void
27 onData()
28 {
29 ++dataCount;
30 }
31
32 void
33 onTimeout()
34 {
35 ++timeoutCount;
36 }
37
38 void
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080039 onInterest(Face& face)
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080040 {
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080041 ++inInterestCount;
42
43 face.unsetInterestFilter(regPrefixId);
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080044 }
45
46 void
47 onRegFailed()
48 {
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080049 ++regFailedCount;
50 }
51
52 void
53 expressInterest(Face& face, const Name& name)
54 {
55 Interest i(name);
56 i.setInterestLifetime(50);
57 face.expressInterest(i,
58 bind(&FacesFixture::onData, this),
59 bind(&FacesFixture::onTimeout, this));
60 }
61
62 void
63 terminate(Face& face)
64 {
65 face.shutdown();
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080066 }
67
68 uint32_t dataCount;
69 uint32_t timeoutCount;
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -080070
71 const RegisteredPrefixId* regPrefixId;
72 uint32_t inInterestCount;
73 uint32_t regFailedCount;
Alexander Afanasyev20d2c582014-01-26 15:32:51 -080074};
75
76BOOST_FIXTURE_TEST_CASE (Unix, FacesFixture)
77{
78 Face face;
79
80 face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", 1000),
81 ptr_lib::bind(&FacesFixture::onData, this),
82 ptr_lib::bind(&FacesFixture::onTimeout, this));
83
84 BOOST_REQUIRE_NO_THROW(face.processEvents());
85
86 BOOST_CHECK_EQUAL(dataCount, 1);
87 BOOST_CHECK_EQUAL(timeoutCount, 0);
88
89 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", 50),
90 ptr_lib::bind(&FacesFixture::onData, this),
91 ptr_lib::bind(&FacesFixture::onTimeout, this));
92
93 BOOST_REQUIRE_NO_THROW(face.processEvents());
94
95 BOOST_CHECK_EQUAL(dataCount, 1);
96 BOOST_CHECK_EQUAL(timeoutCount, 1);
97}
98
99BOOST_FIXTURE_TEST_CASE (Tcp, FacesFixture)
100{
101 Face face("localhost");
102
103 face.expressInterest(Interest("/%C1.M.S.localhost/%C1.M.SRV/ndnd/KEY", 1000),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800104 bind(&FacesFixture::onData, this),
105 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800106
107 BOOST_REQUIRE_NO_THROW(face.processEvents());
108
109 BOOST_CHECK_EQUAL(dataCount, 1);
110 BOOST_CHECK_EQUAL(timeoutCount, 0);
111
112 face.expressInterest(Interest("/localhost/non-existing/data/should/not/exist/anywhere", 50),
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800113 bind(&FacesFixture::onData, this),
114 bind(&FacesFixture::onTimeout, this));
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800115
116 BOOST_REQUIRE_NO_THROW(face.processEvents());
117
118 BOOST_CHECK_EQUAL(dataCount, 1);
119 BOOST_CHECK_EQUAL(timeoutCount, 1);
120}
121
Alexander Afanasyevbf9671d2014-02-11 13:44:13 -0800122
123BOOST_FIXTURE_TEST_CASE (SetFilter, FacesFixture)
124{
125 Face face;
126 Face face2(face.ioService());
127 Scheduler scheduler(*face.ioService());
128 scheduler.scheduleEvent(time::seconds(0.3),
129 bind(&FacesFixture::terminate, this, func_lib::ref(face)));
130
131 regPrefixId = face.setInterestFilter("/Hello/World",
132 bind(&FacesFixture::onInterest, this, func_lib::ref(face)),
133 bind(&FacesFixture::onRegFailed, this));
134
135 scheduler.scheduleEvent(time::seconds(0.2),
136 bind(&FacesFixture::expressInterest, this,
137 func_lib::ref(face2), Name("/Hello/World/!")));
138
139 BOOST_REQUIRE_NO_THROW(face.processEvents());
140
141 BOOST_CHECK_EQUAL(regFailedCount, 0);
142 BOOST_CHECK_EQUAL(inInterestCount, 1);
143 BOOST_CHECK_EQUAL(timeoutCount, 1);
144 BOOST_CHECK_EQUAL(dataCount, 0);
145}
146
Alexander Afanasyev20d2c582014-01-26 15:32:51 -0800147BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800148
149} // namespace ndn