blob: 72ce74a4c0bc510f4801b56ccc86c00b4198f221 [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 DiBenedetto5b433982014-01-29 17:14:27 -070014
15#include <boost/test/unit_test.hpp>
16
Steve DiBenedetto3970c892014-01-31 23:31:13 -070017static nfd::FaceId g_faceCount = 1;
18static std::vector<nfd::shared_ptr<nfd::Face> > g_faces;
19
20static nfd::shared_ptr<nfd::Face>
21getFace(nfd::FaceId id)
22{
23 if (g_faces.size() < id)
24 {
25 BOOST_FAIL("Attempted to access invalid FaceId: " << id);
26 }
27 return g_faces[id-1];
28}
29
30
31
Steve DiBenedetto5b433982014-01-29 17:14:27 -070032namespace nfd {
33
Steve DiBenedetto3970c892014-01-31 23:31:13 -070034NFD_LOG_INIT("InternalFaceTest");
35
36void
37receiveValidNextHopControlResponse(const ndn::Data& response)
38{
39 // Path 1 - runtime exception on wireDecode.
40 // Extract Block from response's payload and attempt
41 // to decode.
42 // {
43 // ndn::ControlResponse control;
44 // Block controlRaw(response.getContent());
45
46 // NFD_LOG_DEBUG("received raw control block size = " << controlRaw.size());
47
48 // control.wireDecode(controlRaw);
49
50 // NFD_LOG_DEBUG("received control response (Path 1)"
51 // << " Name: " << response.getName()
52 // << " code: " << control.getCode()
53 // << " text: " << control.getText());
54
55 // BOOST_REQUIRE(control.getCode() == 200);
56 // BOOST_REQUIRE(control.getText() == "OK");
57 // }
58
59 // Path 1.5 - same as Path 1, but offset the payload's
60 // encoded block by 2 bytes before decoding. 2 bytes
61 // is the measured Block size difference between
62 // ManagerBase's sendResponse and above Path 1's
63 // received size.
64 {
65 ndn::ControlResponse control;
66 Block controlRaw(response.getContent());
67
68 NFD_LOG_DEBUG("received raw control block size = " << controlRaw.size());
69 // controlRaw is currently 2 bytes larger than what was sent
70 // try to offset it manually and create a new block
71
72 BOOST_REQUIRE(controlRaw.hasWire());
73 const uint8_t* buf = controlRaw.wire() + 2;
74 size_t bufSize = controlRaw.size() - 2;
75
76 Block alt(buf, bufSize);
77 control.wireDecode(alt);
78
79 NFD_LOG_DEBUG("received control response (Path 1)"
80 << " Name: " << response.getName()
81 << " code: " << control.getCode()
82 << " text: " << control.getText());
83
84 BOOST_REQUIRE(control.getCode() == 200);
85 BOOST_REQUIRE(control.getText() == "OK");
86 }
87
88 // Path 2 - works, but not conformant to protocol.
89 // Extract decode and ControlResponse from last
90 // component of response's name.
91 // {
92 // const Name& responseName = response.getName();
93 // const ndn::Buffer& controlBuffer =
94 // responseName[responseName.size()-1].getValue();
95
96 // shared_ptr<const ndn::Buffer> tmpBuffer(new ndn::Buffer(controlBuffer));
97 // Block controlRaw(tmpBuffer);
98
99 // NFD_LOG_DEBUG("received raw control block size = " << controlRaw.size());
100
101 // ndn::ControlResponse control;
102 // control.wireDecode(controlRaw);
103
104 // NFD_LOG_DEBUG("received control response (Path 2)"
105 // << " Name: " << response.getName()
106 // << " code: " << control.getCode()
107 // << " text: " << control.getText());
108
109 // BOOST_REQUIRE(control.getCode() == 200);
110 // BOOST_REQUIRE(control.getText() == "OK");
111 // }
112
113
114}
115
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700116BOOST_AUTO_TEST_SUITE(MgmtInternalFace)
117
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700118BOOST_AUTO_TEST_CASE(ValidAddNextHop)
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700119{
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700120 g_faceCount = 1;
121 g_faces.clear();
122 g_faces.push_back(make_shared<DummyFace>());
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700123
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700124 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700125 Fib fib;
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700126 FibManager manager(fib, &getFace, face);
127
128 face->setInterestFilter(manager.getRequestPrefix(),
129 bind(&FibManager::onFibRequest,
130 &manager, _2));
131
132 face->onReceiveData +=
133 bind(&receiveValidNextHopControlResponse, _1);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700134
135 Name regName(manager.getRequestPrefix());
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700136 ndn::FibManagementOptions options;
137 options.setName("/hello");
138 options.setFaceId(1);
139 options.setCost(1);
140
141 Block encodedOptions(options.wireEncode());
142
143 Name commandName(manager.getRequestPrefix());
144 commandName.append("add-nexthop");
145 commandName.append(encodedOptions);
146
147 Interest command(commandName);
148 face->sendInterest(command);
149
150 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
151
152 if (entry)
153 {
154 const fib::NextHopList& hops = entry->getNextHops();
155 BOOST_REQUIRE(hops.size() == 1);
156 BOOST_CHECK(hops[0].getCost() == 1);
157 }
158 else
159 {
160 BOOST_FAIL("Failed to find expected fib entry");
161 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700162}
163
164BOOST_AUTO_TEST_CASE(InvalidPrefixRegistration)
165{
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700166 g_faceCount = 1;
167 g_faces.clear();
168 g_faces.push_back(make_shared<DummyFace>());
169
170 shared_ptr<InternalFace> face(new InternalFace);
Steve DiBenedetto042bfe92014-01-30 15:05:08 -0700171 Fib fib;
Steve DiBenedetto3970c892014-01-31 23:31:13 -0700172 FibManager manager(fib, &getFace, face);
173
174 face->setInterestFilter(manager.getRequestPrefix(),
175 bind(&FibManager::onFibRequest,
176 &manager, _2));
177
178 Interest nonRegInterest("/hello");
179 face->sendInterest(nonRegInterest);
180
181 shared_ptr<fib::Entry> entry = fib.findLongestPrefixMatch("/hello");
182
183 if (entry)
184 {
185 const fib::NextHopList& hops = entry->getNextHops();
186 BOOST_REQUIRE(hops.size() == 0);
187 }
Steve DiBenedetto5b433982014-01-29 17:14:27 -0700188}
189
190BOOST_AUTO_TEST_SUITE_END()
191
192} // namespace nfd