blob: 433c92f2014b228e726740223cfe2306f802404a [file] [log] [blame]
Nick Gordon94719252016-10-20 20:48:01 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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.
10 *
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/>.
24 */
25
26#include "tests/test-common.hpp"
27
28#include "rib/readvertise/nfd-rib-readvertise-destination.hpp"
29
30#include <ndn-cxx/util/dummy-client-face.hpp>
31#include <ndn-cxx/security/signing-info.hpp>
32
33#include "tests/identity-management-fixture.hpp"
34
35namespace nfd {
36namespace rib {
37namespace tests {
38
39using namespace nfd::tests;
40
41class NfdRibReadvertiseDestinationFixture : public IdentityManagementTimeFixture
42{
43public:
44 NfdRibReadvertiseDestinationFixture()
45 : nSuccessCallbacks(0)
46 , nFailureCallbacks(0)
47 , face(getGlobalIoService(), m_keyChain, {true, false})
48 , controller(face, m_keyChain)
49 , dest(controller, Name("/localhop/nfd/rib/readvertise"))
50 , successCallback([this] () {
51 nSuccessCallbacks++;
52 })
53 , failureCallback([this] (const std::string& str) {
54 nFailureCallbacks++;
55 })
56 {
57 }
58
59public:
60 uint32_t nSuccessCallbacks;
61 uint32_t nFailureCallbacks;
62
63protected:
64 ndn::util::DummyClientFace face;
65 ndn::nfd::Controller controller;
66 NfdRibReadvertiseDestination dest;
67 std::function<void()> successCallback;
68 std::function<void(const std::string&)> failureCallback;
69};
70
71BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
72
73class AdvertiseSuccessScenario
74{
75public:
76 ndn::nfd::ControlResponse
77 makeResponse(const ControlParameters& sentCp)
78 {
79 ControlParameters response;
80
81 response.setFaceId(1)
82 .setName(sentCp.getName())
83 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)
84 .setCost(0)
85 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
86
87 ndn::nfd::ControlResponse responsePayload;
88 responsePayload.setCode(200)
89 .setText("Successfully registered.")
90 .setBody(response.wireEncode());
91 return responsePayload;
92 }
93
94 void
95 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
96 {
97 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
98 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
99 }
100};
101
102class AdvertiseFailureScenario
103{
104public:
105 ndn::nfd::ControlResponse
106 makeResponse(ControlParameters sentCp)
107 {
108 ndn::nfd::ControlResponse responsePayload(403, "Not Authenticated");
109 return responsePayload;
110 }
111
112 void
113 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
114 {
115 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
116 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
117 }
118};
119
120typedef boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario> AdvertiseScenarios;
121
122BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
123{
124 Scenario scenario;
125 Name prefix("/ndn/memphis/test");
126 ndn::security::SigningInfo secInfo;
127 std::vector<RibRouteRef> routes;
128 ReadvertisedRoute rr(prefix, secInfo, routes);
129 const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nfd/rib/register");
130
131 dest.advertise(rr, successCallback, failureCallback);
132 advanceClocks(time::milliseconds(100));
133
134 // Retrieve the sent Interest to build the response
135 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
136 const Interest& sentInterest = face.sentInterests[0];
137 BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
138
139 // Parse the sent command Interest to check correctness.
140 ControlParameters sentCp;
141 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue()));
142 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
143 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
144
145 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
146 auto responseData = makeData(sentInterest.getName());
147 responseData->setContent(responsePayload.wireEncode());
148 face.receive(*responseData);
149 this->advanceClocks(time::milliseconds(10));
150
151 scenario.checkCommandOutcome(this);
152}
153
154class WithdrawSuccessScenario
155{
156public:
157 ndn::nfd::ControlResponse
158 makeResponse(const ControlParameters& sentCp)
159 {
160 ControlParameters response;
161
162 response.setFaceId(1)
163 .setName(sentCp.getName())
164 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
165
166 ndn::nfd::ControlResponse responsePayload;
167 responsePayload.setCode(200)
168 .setText("Successfully removed")
169 .setBody(response.wireEncode());
170
171 return responsePayload;
172 }
173
174 void
175 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
176 {
177 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
178 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
179 }
180};
181
182class WithdrawFailureScenario
183{
184public:
185 ndn::nfd::ControlResponse
186 makeResponse(ControlParameters sentCp)
187 {
188 ndn::nfd::ControlResponse responsePayload(403, "Not authenticated");
189 return responsePayload;
190 }
191
192 void
193 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
194 {
195 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
196 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
197 }
198};
199
200typedef boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario> WithdrawScenarios;
201
202BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
203{
204 Scenario scenario;
205 Name prefix("/ndn/memphis/test");
206 ndn::security::SigningInfo secInfo;
207 std::vector<RibRouteRef> routes;
208 ReadvertisedRoute rr(prefix, secInfo, routes);
209 const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nfd/rib/unregister");
210
211 dest.withdraw(rr, successCallback, failureCallback);
212 this->advanceClocks(time::milliseconds(10));
213
214 // Retrieve the sent Interest to build the response
215 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
216 const Interest& sentInterest = face.sentInterests[0];
217 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
218
219 ControlParameters sentCp;
220 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue()));
221 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
222 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
223
224 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
225 auto responseData = makeData(sentInterest.getName());
226 responseData->setContent(responsePayload.wireEncode());
227
228 face.receive(*responseData);
229 this->advanceClocks(time::milliseconds(1));
230
231 scenario.checkCommandOutcome(this);
232}
233
234BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
235
236} // namespace tests
237} // namespace rib
238} // namespace nfd