blob: 2fce7f1ea59d602014b1f406dc1e9cb3d137b71a [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 DiBenedetto43cd0372014-02-01 17:05:07 -070026 shared_ptr<Face>
27 getFace(FaceId id)
Steve DiBenedetto3970c892014-01-31 23:31:13 -070028 {
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070029 if (m_faces.size() < id)
30 {
31 BOOST_FAIL("Attempted to access invalid FaceId: " << id);
32 }
33 return m_faces[id-1];
Steve DiBenedetto3970c892014-01-31 23:31:13 -070034 }
35
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070036 void
37 addFace(shared_ptr<Face> face)
38 {
39 m_faces.push_back(face);
40 }
Steve DiBenedetto3970c892014-01-31 23:31:13 -070041
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070042private:
43 std::vector<shared_ptr<Face> > m_faces;
44};
Steve DiBenedetto3970c892014-01-31 23:31:13 -070045
Steve DiBenedetto5b433982014-01-29 17:14:27 -070046BOOST_AUTO_TEST_SUITE(MgmtInternalFace)
47
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070048void
49validateControlResponse(const Data& response,
50 uint32_t expectedCode,
51 const std::string& expectedText)
52{
53 Block controlRaw = response.getContent().blockFromValue();
54
55 ndn::ControlResponse control;
56 control.wireDecode(controlRaw);
57
58 NFD_LOG_DEBUG("received control response"
59 << " Name: " << response.getName()
60 << " code: " << control.getCode()
61 << " text: " << control.getText());
62
63 BOOST_REQUIRE(control.getCode() == expectedCode);
64 BOOST_REQUIRE(control.getText() == expectedText);
65}
66
Steve DiBenedetto3970c892014-01-31 23:31:13 -070067BOOST_AUTO_TEST_CASE(ValidAddNextHop)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070068{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070069 InternalFaceFixture fixture;
70 fixture.addFace(make_shared<DummyFace>());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070071
Steve DiBenedetto3970c892014-01-31 23:31:13 -070072 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070073 Fib fib;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070074 FibManager manager(fib,
75 bind(&InternalFaceFixture::getFace,
76 &fixture, _1),
77 face);
Steve DiBenedetto3970c892014-01-31 23:31:13 -070078
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070079 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedetto3970c892014-01-31 23:31:13 -070080 bind(&FibManager::onFibRequest,
81 &manager, _2));
82
83 face->onReceiveData +=
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070084 bind(&validateControlResponse, _1, 200, "OK");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070085
Steve DiBenedetto3970c892014-01-31 23:31:13 -070086 ndn::FibManagementOptions options;
87 options.setName("/hello");
88 options.setFaceId(1);
89 options.setCost(1);
90
91 Block encodedOptions(options.wireEncode());
92
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070093 Name commandName("/localhost/nfd/fib");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070094 commandName.append("add-nexthop");
95 commandName.append(encodedOptions);
96
97 Interest command(commandName);
98 face->sendInterest(command);
99
100 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
101
102 if (entry)
103 {
104 const fib::NextHopList& hops = entry->getNextHops();
105 BOOST_REQUIRE(hops.size() == 1);
106 BOOST_CHECK(hops[0].getCost() == 1);
107 }
108 else
109 {
110 BOOST_FAIL("Failed to find expected fib entry");
111 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700112}
113
114BOOST_AUTO_TEST_CASE(InvalidPrefixRegistration)
115{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700116 InternalFaceFixture fixture;
117 fixture.addFace(make_shared<DummyFace>());
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700118
119 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700120 Fib fib;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700121 FibManager manager(fib,
122 bind(&InternalFaceFixture::getFace,
123 &fixture, _1),
124 face);
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700125
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700126 face->setInterestFilter("/localhost/nfd/fib",
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700127 bind(&FibManager::onFibRequest,
128 &manager, _2));
129
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700130 face->onReceiveData +=
131 bind(&validateControlResponse, _1, 404, "MALFORMED");
132
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700133 Interest nonRegInterest("/hello");
134 face->sendInterest(nonRegInterest);
135
136 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
137
138 if (entry)
139 {
140 const fib::NextHopList& hops = entry->getNextHops();
141 BOOST_REQUIRE(hops.size() == 0);
142 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700143}
144
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700145void
146validateOnInterestCallback(const Name& name, const Interest& interest)
147{
148 NFD_LOG_DEBUG("Reached correct callback");
149}
150
151void
152validateNoOnInterestCallback(const Name& name, const Interest& interest)
153{
154 BOOST_FAIL("Reached wrong callback");
155}
156
157BOOST_AUTO_TEST_CASE(SendInterestHitEnd)
158{
159 InternalFaceFixture fixture;
160 fixture.addFace(make_shared<DummyFace>());
161
162 shared_ptr<InternalFace> face(new InternalFace);
163 Fib fib;
164 FibManager manager(fib,
165 bind(&InternalFaceFixture::getFace,
166 &fixture, _1),
167 face);
168
169 face->setInterestFilter("/localhost/nfd/fib",
170 &validateOnInterestCallback);
171
172 // generate command whose name is canonically
173 // ordered after /localhost/nfd/fib so that
174 // we hit the end of the std::map
175
176 Name commandName("/localhost/nfd/fib/end");
177 Interest command(commandName);
178 face->sendInterest(command);
179}
180
181
182
183BOOST_AUTO_TEST_CASE(SendInterestHitBegin)
184{
185 InternalFaceFixture fixture;
186 fixture.addFace(make_shared<DummyFace>());
187
188 shared_ptr<InternalFace> face(new InternalFace);
189 Fib fib;
190 FibManager manager(fib,
191 bind(&InternalFaceFixture::getFace,
192 &fixture, _1),
193 face);
194
195 face->setInterestFilter("/localhost/nfd/fib",
196 &validateNoOnInterestCallback);
197
198 // generate command whose name is canonically
199 // ordered before /localhost/nfd/fib so that
200 // we hit the beginning of the std::map
201
202 Name commandName("/localhost/nfd");
203 Interest command(commandName);
204 face->sendInterest(command);
205}
206
207
208
209BOOST_AUTO_TEST_CASE(SendInterestHitExact)
210{
211 InternalFaceFixture fixture;
212 fixture.addFace(make_shared<DummyFace>());
213
214 shared_ptr<InternalFace> face(new InternalFace);
215 Fib fib;
216 FibManager manager(fib,
217 bind(&InternalFaceFixture::getFace,
218 &fixture, _1),
219 face);
220
221 face->setInterestFilter("/localhost/nfd/eib",
222 &validateNoOnInterestCallback);
223
224 face->setInterestFilter("/localhost/nfd/fib",
225 &validateOnInterestCallback);
226
227 face->setInterestFilter("/localhost/nfd/gib",
228 &validateNoOnInterestCallback);
229
230 // generate command whose name exactly matches
231 // /localhost/nfd/fib
232
233 Name commandName("/localhost/nfd/fib");
234 Interest command(commandName);
235 face->sendInterest(command);
236}
237
238
239
240BOOST_AUTO_TEST_CASE(SendInterestHitPrevious)
241{
242 InternalFaceFixture fixture;
243 fixture.addFace(make_shared<DummyFace>());
244
245 shared_ptr<InternalFace> face(new InternalFace);
246 Fib fib;
247 FibManager manager(fib,
248 bind(&InternalFaceFixture::getFace,
249 &fixture, _1),
250 face);
251
252 face->setInterestFilter("/localhost/nfd/fib",
253 &validateOnInterestCallback);
254
255 face->setInterestFilter("/localhost/nfd/fib/zzzzzzzzzzzzz/",
256 &validateNoOnInterestCallback);
257
258 // generate command whose name exactly matches
259 // an Interest filter
260
261 Name commandName("/localhost/nfd/fib/previous");
262 Interest command(commandName);
263 face->sendInterest(command);
264}
265
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700266BOOST_AUTO_TEST_SUITE_END()
267
268} // namespace nfd