blob: 2215c0e2a0b37e6e9df8a9e0f952879cc9e277bd [file] [log] [blame]
Steve DiBenedetto5b433982014-01-29 17:14:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Vince Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto5b433982014-01-29 17:14:27 -070025
26#include "mgmt/internal-face.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070027#include "tests/daemon/face/dummy-face.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070028
Junxiao Shid9ee45c2014-02-27 15:38:11 -070029#include "tests/test-common.hpp"
Steve DiBenedetto5b433982014-01-29 17:14:27 -070030
31namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Steve DiBenedetto5b433982014-01-29 17:14:27 -070033
Steve DiBenedetto3970c892014-01-31 23:31:13 -070034NFD_LOG_INIT("InternalFaceTest");
35
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036class InternalFaceFixture : protected BaseFixture
Steve DiBenedetto3970c892014-01-31 23:31:13 -070037{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070038public:
Steve DiBenedetto3970c892014-01-31 23:31:13 -070039
Steve DiBenedettobdedce92014-02-02 22:49:39 -070040 InternalFaceFixture()
41 : m_onInterestFired(false),
42 m_noOnInterestFired(false)
43 {
44
45 }
46
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070047 void
Steve DiBenedettobdedce92014-02-02 22:49:39 -070048 validateOnInterestCallback(const Name& name, const Interest& interest)
49 {
50 m_onInterestFired = true;
51 }
52
53 void
54 validateNoOnInterestCallback(const Name& name, const Interest& interest)
55 {
56 m_noOnInterestFired = true;
57 }
58
59 void
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070060 addFace(shared_ptr<Face> face)
61 {
62 m_faces.push_back(face);
63 }
Steve DiBenedetto3970c892014-01-31 23:31:13 -070064
Steve DiBenedettobdedce92014-02-02 22:49:39 -070065 bool
66 didOnInterestFire()
67 {
68 return m_onInterestFired;
69 }
70
71 bool
72 didNoOnInterestFire()
73 {
74 return m_noOnInterestFired;
75 }
76
77 void
78 resetOnInterestFired()
79 {
80 m_onInterestFired = false;
81 }
82
83 void
84 resetNoOnInterestFired()
85 {
86 m_noOnInterestFired = false;
87 }
88
Vince Lehman5144f822014-07-23 15:12:56 -070089protected:
90 ndn::KeyChain m_keyChain;
91
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070092private:
93 std::vector<shared_ptr<Face> > m_faces;
Steve DiBenedettobdedce92014-02-02 22:49:39 -070094 bool m_onInterestFired;
95 bool m_noOnInterestFired;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070096};
Steve DiBenedetto3970c892014-01-31 23:31:13 -070097
Junxiao Shid9ee45c2014-02-27 15:38:11 -070098BOOST_FIXTURE_TEST_SUITE(MgmtInternalFace, InternalFaceFixture)
Steve DiBenedetto5b433982014-01-29 17:14:27 -070099
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700100void
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700101validatePutData(bool& called, const Name& expectedName, const Data& data)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700102{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700103 called = true;
104 BOOST_CHECK_EQUAL(expectedName, data.getName());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700105}
106
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700107BOOST_AUTO_TEST_CASE(PutData)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700108{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700109 addFace(make_shared<DummyFace>());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700110
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700111 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700112
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700113 bool didPutData = false;
114 Name dataName("/hello");
Alexander Afanasyevf6980282014-05-13 18:28:40 -0700115 face->onReceiveData += bind(&validatePutData, ref(didPutData), dataName, _1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700116
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700117 Data testData(dataName);
Vince Lehman5144f822014-07-23 15:12:56 -0700118 m_keyChain.sign(testData);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700119 face->put(testData);
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700120
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700121 BOOST_REQUIRE(didPutData);
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -0800122
123 BOOST_CHECK_THROW(face->close(), InternalFace::Error);
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700124}
125
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700126BOOST_AUTO_TEST_CASE(SendInterestHitEnd)
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700127{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700128 addFace(make_shared<DummyFace>());
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700129
130 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700131
132 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700133 bind(&InternalFaceFixture::validateOnInterestCallback,
134 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700135
136 // generate command whose name is canonically
137 // ordered after /localhost/nfd/fib so that
138 // we hit the end of the std::map
139
140 Name commandName("/localhost/nfd/fib/end");
Junxiao Shi72c3e042014-04-08 15:02:37 -0700141 shared_ptr<Interest> command = makeInterest(commandName);
142 face->sendInterest(*command);
Junxiao Shi16d1b7d2014-03-27 21:29:09 -0700143 g_io.run_one();
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700144
145 BOOST_REQUIRE(didOnInterestFire());
146 BOOST_REQUIRE(didNoOnInterestFire() == false);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700147}
148
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700149BOOST_AUTO_TEST_CASE(SendInterestHitBegin)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700150{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700151 addFace(make_shared<DummyFace>());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700152
153 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700154
155 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700156 bind(&InternalFaceFixture::validateNoOnInterestCallback,
157 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700158
159 // generate command whose name is canonically
160 // ordered before /localhost/nfd/fib so that
161 // we hit the beginning of the std::map
162
163 Name commandName("/localhost/nfd");
Junxiao Shi72c3e042014-04-08 15:02:37 -0700164 shared_ptr<Interest> command = makeInterest(commandName);
165 face->sendInterest(*command);
Junxiao Shi16d1b7d2014-03-27 21:29:09 -0700166 g_io.run_one();
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700167
168 BOOST_REQUIRE(didNoOnInterestFire() == false);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700169}
170
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700171BOOST_AUTO_TEST_CASE(SendInterestHitExact)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700172{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700173 addFace(make_shared<DummyFace>());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700174
175 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700176
177 face->setInterestFilter("/localhost/nfd/eib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700178 bind(&InternalFaceFixture::validateNoOnInterestCallback,
179 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700180
181 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700182 bind(&InternalFaceFixture::validateOnInterestCallback,
183 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700184
185 face->setInterestFilter("/localhost/nfd/gib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700186 bind(&InternalFaceFixture::validateNoOnInterestCallback,
187 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700188
189 // generate command whose name exactly matches
190 // /localhost/nfd/fib
191
192 Name commandName("/localhost/nfd/fib");
Junxiao Shi72c3e042014-04-08 15:02:37 -0700193 shared_ptr<Interest> command = makeInterest(commandName);
194 face->sendInterest(*command);
Junxiao Shi16d1b7d2014-03-27 21:29:09 -0700195 g_io.run_one();
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700196
197 BOOST_REQUIRE(didOnInterestFire());
198 BOOST_REQUIRE(didNoOnInterestFire() == false);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700199}
200
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700201BOOST_AUTO_TEST_CASE(SendInterestHitPrevious)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700202{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700203 addFace(make_shared<DummyFace>());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700204
205 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700206
207 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700208 bind(&InternalFaceFixture::validateOnInterestCallback,
209 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700210
211 face->setInterestFilter("/localhost/nfd/fib/zzzzzzzzzzzzz/",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700212 bind(&InternalFaceFixture::validateNoOnInterestCallback,
213 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700214
215 // generate command whose name exactly matches
216 // an Interest filter
217
218 Name commandName("/localhost/nfd/fib/previous");
Junxiao Shi72c3e042014-04-08 15:02:37 -0700219 shared_ptr<Interest> command = makeInterest(commandName);
220 face->sendInterest(*command);
Junxiao Shi16d1b7d2014-03-27 21:29:09 -0700221 g_io.run_one();
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700222
223 BOOST_REQUIRE(didOnInterestFire());
224 BOOST_REQUIRE(didNoOnInterestFire() == false);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700225}
226
Junxiao Shi72c3e042014-04-08 15:02:37 -0700227BOOST_AUTO_TEST_CASE(InterestGone)
228{
229 shared_ptr<InternalFace> face = make_shared<InternalFace>();
230 shared_ptr<Interest> interest = makeInterest("ndn:/localhost/nfd/gone");
231 face->sendInterest(*interest);
232
233 interest.reset();
234 BOOST_CHECK_NO_THROW(g_io.poll());
235}
236
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700237BOOST_AUTO_TEST_SUITE_END()
238
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700239} // namespace tests
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700240} // namespace nfd