blob: 5953fc67401ca47b526a5e02862ec2b6d9387771 [file] [log] [blame]
Steve DiBenedetto5b433982014-01-29 17:14:27 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "mgmt/internal-face.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -07008#include "mgmt/fib-manager.hpp"
9#include "table/fib.hpp"
Steve DiBenedetto3970c892014-01-31 23:31:13 -070010#include "../face/dummy-face.hpp"
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070011
Steve DiBenedetto3970c892014-01-31 23:31:13 -070012#include <ndn-cpp-dev/management/fib-management-options.hpp>
13#include <ndn-cpp-dev/management/control-response.hpp>
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070014#include <ndn-cpp-dev/encoding/block.hpp>
Steve DiBenedetto5b433982014-01-29 17:14:27 -070015
16#include <boost/test/unit_test.hpp>
17
18namespace nfd {
19
Steve DiBenedetto3970c892014-01-31 23:31:13 -070020NFD_LOG_INIT("InternalFaceTest");
21
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070022class InternalFaceFixture
Steve DiBenedetto3970c892014-01-31 23:31:13 -070023{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070024public:
Steve DiBenedetto3970c892014-01-31 23:31:13 -070025
Steve DiBenedettobdedce92014-02-02 22:49:39 -070026 InternalFaceFixture()
27 : m_onInterestFired(false),
28 m_noOnInterestFired(false)
29 {
30
31 }
32
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070033 shared_ptr<Face>
34 getFace(FaceId id)
Steve DiBenedetto3970c892014-01-31 23:31:13 -070035 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070036 if (m_faces.size() < id)
37 {
38 BOOST_FAIL("Attempted to access invalid FaceId: " << id);
39 }
40 return m_faces[id-1];
Steve DiBenedetto3970c892014-01-31 23:31:13 -070041 }
42
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070043 void
Steve DiBenedettobdedce92014-02-02 22:49:39 -070044 validateOnInterestCallback(const Name& name, const Interest& interest)
45 {
46 m_onInterestFired = true;
47 }
48
49 void
50 validateNoOnInterestCallback(const Name& name, const Interest& interest)
51 {
52 m_noOnInterestFired = true;
53 }
54
55 void
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070056 addFace(shared_ptr<Face> face)
57 {
58 m_faces.push_back(face);
59 }
Steve DiBenedetto3970c892014-01-31 23:31:13 -070060
Steve DiBenedettobdedce92014-02-02 22:49:39 -070061 bool
62 didOnInterestFire()
63 {
64 return m_onInterestFired;
65 }
66
67 bool
68 didNoOnInterestFire()
69 {
70 return m_noOnInterestFired;
71 }
72
73 void
74 resetOnInterestFired()
75 {
76 m_onInterestFired = false;
77 }
78
79 void
80 resetNoOnInterestFired()
81 {
82 m_noOnInterestFired = false;
83 }
84
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070085private:
86 std::vector<shared_ptr<Face> > m_faces;
Steve DiBenedettobdedce92014-02-02 22:49:39 -070087 bool m_onInterestFired;
88 bool m_noOnInterestFired;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070089};
Steve DiBenedetto3970c892014-01-31 23:31:13 -070090
Steve DiBenedetto5b433982014-01-29 17:14:27 -070091BOOST_AUTO_TEST_SUITE(MgmtInternalFace)
92
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070093void
Steve DiBenedettobdedce92014-02-02 22:49:39 -070094validatePutData(bool& called, const Name& expectedName, const Data& data)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070095{
Steve DiBenedettobdedce92014-02-02 22:49:39 -070096 called = true;
97 BOOST_CHECK_EQUAL(expectedName, data.getName());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070098}
99
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700100BOOST_FIXTURE_TEST_CASE(PutData, InternalFaceFixture)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700101{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700102 addFace(make_shared<DummyFace>());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700103
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700104 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700105 Fib fib;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700106 FibManager manager(fib,
107 bind(&InternalFaceFixture::getFace,
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700108 this, _1),
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700109 face);
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700110
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700111 bool didPutData = false;
112 Name dataName("/hello");
113 face->onReceiveData += bind(&validatePutData, boost::ref(didPutData), dataName, _1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700114
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700115 Data testData(dataName);
116 face->sign(testData);
117 face->put(testData);
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700118
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700119 BOOST_REQUIRE(didPutData);
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700120}
121
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700122BOOST_FIXTURE_TEST_CASE(SendInterestHitEnd, InternalFaceFixture)
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700123{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700124 addFace(make_shared<DummyFace>());
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700125
126 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700127 Fib fib;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700128 FibManager manager(fib,
129 bind(&InternalFaceFixture::getFace,
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700130 this, _1),
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700131 face);
132
133 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700134 bind(&InternalFaceFixture::validateOnInterestCallback,
135 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700136
137 // generate command whose name is canonically
138 // ordered after /localhost/nfd/fib so that
139 // we hit the end of the std::map
140
141 Name commandName("/localhost/nfd/fib/end");
142 Interest command(commandName);
143 face->sendInterest(command);
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
149
150
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700151BOOST_FIXTURE_TEST_CASE(SendInterestHitBegin, InternalFaceFixture)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700152{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700153 addFace(make_shared<DummyFace>());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700154
155 shared_ptr<InternalFace> face(new InternalFace);
156 Fib fib;
157 FibManager manager(fib,
158 bind(&InternalFaceFixture::getFace,
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700159 this, _1),
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700160 face);
161
162 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700163 bind(&InternalFaceFixture::validateNoOnInterestCallback,
164 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700165
166 // generate command whose name is canonically
167 // ordered before /localhost/nfd/fib so that
168 // we hit the beginning of the std::map
169
170 Name commandName("/localhost/nfd");
171 Interest command(commandName);
172 face->sendInterest(command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700173
174 BOOST_REQUIRE(didNoOnInterestFire() == false);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700175}
176
177
178
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700179BOOST_FIXTURE_TEST_CASE(SendInterestHitExact, InternalFaceFixture)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700180{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700181 addFace(make_shared<DummyFace>());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700182
183 shared_ptr<InternalFace> face(new InternalFace);
184 Fib fib;
185 FibManager manager(fib,
186 bind(&InternalFaceFixture::getFace,
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700187 this, _1),
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700188 face);
189
190 face->setInterestFilter("/localhost/nfd/eib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700191 bind(&InternalFaceFixture::validateNoOnInterestCallback,
192 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700193
194 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700195 bind(&InternalFaceFixture::validateOnInterestCallback,
196 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700197
198 face->setInterestFilter("/localhost/nfd/gib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700199 bind(&InternalFaceFixture::validateNoOnInterestCallback,
200 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700201
202 // generate command whose name exactly matches
203 // /localhost/nfd/fib
204
205 Name commandName("/localhost/nfd/fib");
206 Interest command(commandName);
207 face->sendInterest(command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700208
209 BOOST_REQUIRE(didOnInterestFire());
210 BOOST_REQUIRE(didNoOnInterestFire() == false);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700211}
212
213
214
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700215BOOST_FIXTURE_TEST_CASE(SendInterestHitPrevious, InternalFaceFixture)
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700216{
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700217 addFace(make_shared<DummyFace>());
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700218
219 shared_ptr<InternalFace> face(new InternalFace);
220 Fib fib;
221 FibManager manager(fib,
222 bind(&InternalFaceFixture::getFace,
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700223 this, _1),
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700224 face);
225
226 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700227 bind(&InternalFaceFixture::validateOnInterestCallback,
228 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700229
230 face->setInterestFilter("/localhost/nfd/fib/zzzzzzzzzzzzz/",
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700231 bind(&InternalFaceFixture::validateNoOnInterestCallback,
232 this, _1, _2));
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700233
234 // generate command whose name exactly matches
235 // an Interest filter
236
237 Name commandName("/localhost/nfd/fib/previous");
238 Interest command(commandName);
239 face->sendInterest(command);
Steve DiBenedettobdedce92014-02-02 22:49:39 -0700240
241 BOOST_REQUIRE(didOnInterestFire());
242 BOOST_REQUIRE(didNoOnInterestFire() == false);
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700243}
244
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700245BOOST_AUTO_TEST_SUITE_END()
246
247} // namespace nfd