blob: 1c6f70d2ba611f288944e20f2527c68d68ab3c72 [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 DiBenedetto3970c892014-01-31 23:31:13 -070079 face->onReceiveData +=
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070080 bind(&validateControlResponse, _1, 200, "OK");
Steve DiBenedetto042bfe92014-01-30 15:05:08 -070081
Steve DiBenedetto3970c892014-01-31 23:31:13 -070082 ndn::FibManagementOptions options;
83 options.setName("/hello");
84 options.setFaceId(1);
85 options.setCost(1);
86
87 Block encodedOptions(options.wireEncode());
88
Steve DiBenedetto43cd0372014-02-01 17:05:07 -070089 Name commandName("/localhost/nfd/fib");
Steve DiBenedetto3970c892014-01-31 23:31:13 -070090 commandName.append("add-nexthop");
91 commandName.append(encodedOptions);
92
93 Interest command(commandName);
94 face->sendInterest(command);
95
96 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
97
98 if (entry)
99 {
100 const fib::NextHopList& hops = entry->getNextHops();
101 BOOST_REQUIRE(hops.size() == 1);
102 BOOST_CHECK(hops[0].getCost() == 1);
103 }
104 else
105 {
106 BOOST_FAIL("Failed to find expected fib entry");
107 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700108}
109
110BOOST_AUTO_TEST_CASE(InvalidPrefixRegistration)
111{
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700112 InternalFaceFixture fixture;
113 fixture.addFace(make_shared<DummyFace>());
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700114
115 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700116 Fib fib;
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700117 FibManager manager(fib,
118 bind(&InternalFaceFixture::getFace,
119 &fixture, _1),
120 face);
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700121
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700122 face->onReceiveData +=
123 bind(&validateControlResponse, _1, 404, "MALFORMED");
124
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700125 Interest nonRegInterest("/hello");
126 face->sendInterest(nonRegInterest);
127
128 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
129
130 if (entry)
131 {
132 const fib::NextHopList& hops = entry->getNextHops();
133 BOOST_REQUIRE(hops.size() == 0);
134 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700135}
136
Steve DiBenedetto43cd0372014-02-01 17:05:07 -0700137void
138validateOnInterestCallback(const Name& name, const Interest& interest)
139{
140 NFD_LOG_DEBUG("Reached correct callback");
141}
142
143void
144validateNoOnInterestCallback(const Name& name, const Interest& interest)
145{
146 BOOST_FAIL("Reached wrong callback");
147}
148
149BOOST_AUTO_TEST_CASE(SendInterestHitEnd)
150{
151 InternalFaceFixture fixture;
152 fixture.addFace(make_shared<DummyFace>());
153
154 shared_ptr<InternalFace> face(new InternalFace);
155 Fib fib;
156 FibManager manager(fib,
157 bind(&InternalFaceFixture::getFace,
158 &fixture, _1),
159 face);
160
161 face->setInterestFilter("/localhost/nfd/fib",
162 &validateOnInterestCallback);
163
164 // generate command whose name is canonically
165 // ordered after /localhost/nfd/fib so that
166 // we hit the end of the std::map
167
168 Name commandName("/localhost/nfd/fib/end");
169 Interest command(commandName);
170 face->sendInterest(command);
171}
172
173
174
175BOOST_AUTO_TEST_CASE(SendInterestHitBegin)
176{
177 InternalFaceFixture fixture;
178 fixture.addFace(make_shared<DummyFace>());
179
180 shared_ptr<InternalFace> face(new InternalFace);
181 Fib fib;
182 FibManager manager(fib,
183 bind(&InternalFaceFixture::getFace,
184 &fixture, _1),
185 face);
186
187 face->setInterestFilter("/localhost/nfd/fib",
188 &validateNoOnInterestCallback);
189
190 // generate command whose name is canonically
191 // ordered before /localhost/nfd/fib so that
192 // we hit the beginning of the std::map
193
194 Name commandName("/localhost/nfd");
195 Interest command(commandName);
196 face->sendInterest(command);
197}
198
199
200
201BOOST_AUTO_TEST_CASE(SendInterestHitExact)
202{
203 InternalFaceFixture fixture;
204 fixture.addFace(make_shared<DummyFace>());
205
206 shared_ptr<InternalFace> face(new InternalFace);
207 Fib fib;
208 FibManager manager(fib,
209 bind(&InternalFaceFixture::getFace,
210 &fixture, _1),
211 face);
212
213 face->setInterestFilter("/localhost/nfd/eib",
214 &validateNoOnInterestCallback);
215
216 face->setInterestFilter("/localhost/nfd/fib",
217 &validateOnInterestCallback);
218
219 face->setInterestFilter("/localhost/nfd/gib",
220 &validateNoOnInterestCallback);
221
222 // generate command whose name exactly matches
223 // /localhost/nfd/fib
224
225 Name commandName("/localhost/nfd/fib");
226 Interest command(commandName);
227 face->sendInterest(command);
228}
229
230
231
232BOOST_AUTO_TEST_CASE(SendInterestHitPrevious)
233{
234 InternalFaceFixture fixture;
235 fixture.addFace(make_shared<DummyFace>());
236
237 shared_ptr<InternalFace> face(new InternalFace);
238 Fib fib;
239 FibManager manager(fib,
240 bind(&InternalFaceFixture::getFace,
241 &fixture, _1),
242 face);
243
244 face->setInterestFilter("/localhost/nfd/fib",
245 &validateOnInterestCallback);
246
247 face->setInterestFilter("/localhost/nfd/fib/zzzzzzzzzzzzz/",
248 &validateNoOnInterestCallback);
249
250 // generate command whose name exactly matches
251 // an Interest filter
252
253 Name commandName("/localhost/nfd/fib/previous");
254 Interest command(commandName);
255 face->sendInterest(command);
256}
257
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700258BOOST_AUTO_TEST_SUITE_END()
259
260} // namespace nfd