blob: e3afcbf41a1405a87ad51cd94045b2aa26cf8cf7 [file] [log] [blame]
Nick Gordon94719252016-10-20 20:48:01 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventoe1bdc082018-10-11 21:20:23 -04002/*
Junxiao Shifeddc3c2019-01-17 19:06:00 +00003 * Copyright (c) 2014-2019, Regents of the University of California,
Nick Gordon94719252016-10-20 20:48:01 +00004 * 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
Nick Gordon94719252016-10-20 20:48:01 +000026#include "rib/readvertise/nfd-rib-readvertise-destination.hpp"
27
Junxiao Shi63b67e22017-03-06 19:46:02 +000028#include "tests/identity-management-fixture.hpp"
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040029#include "tests/test-common.hpp"
30
Nick Gordon94719252016-10-20 20:48:01 +000031#include <ndn-cxx/security/signing-info.hpp>
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040032#include <ndn-cxx/util/dummy-client-face.hpp>
Nick Gordon94719252016-10-20 20:48:01 +000033
Nick Gordon94719252016-10-20 20:48:01 +000034namespace nfd {
35namespace rib {
36namespace tests {
37
38using namespace nfd::tests;
39
40class NfdRibReadvertiseDestinationFixture : public IdentityManagementTimeFixture
41{
42public:
43 NfdRibReadvertiseDestinationFixture()
44 : nSuccessCallbacks(0)
45 , nFailureCallbacks(0)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040046 , face(g_io, m_keyChain, {true, false})
Nick Gordon94719252016-10-20 20:48:01 +000047 , controller(face, m_keyChain)
Yanbiao Lif48d0802018-06-01 03:00:02 -070048 , dest(controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr"))
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040049 , successCallback([this] { nSuccessCallbacks++; })
Davide Pesavento0a71dd32019-03-17 20:36:18 -040050 , failureCallback([this] (const std::string&) { nFailureCallbacks++; })
Nick Gordon94719252016-10-20 20:48:01 +000051 {
52 }
53
54public:
55 uint32_t nSuccessCallbacks;
56 uint32_t nFailureCallbacks;
57
58protected:
59 ndn::util::DummyClientFace face;
60 ndn::nfd::Controller controller;
Nick Gordon04262d92017-01-31 12:27:06 -060061 Rib rib;
Nick Gordon94719252016-10-20 20:48:01 +000062 NfdRibReadvertiseDestination dest;
63 std::function<void()> successCallback;
64 std::function<void(const std::string&)> failureCallback;
65};
66
Junxiao Shi63b67e22017-03-06 19:46:02 +000067BOOST_AUTO_TEST_SUITE(Readvertise)
Nick Gordon94719252016-10-20 20:48:01 +000068BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
69
70class AdvertiseSuccessScenario
71{
72public:
73 ndn::nfd::ControlResponse
74 makeResponse(const ControlParameters& sentCp)
75 {
76 ControlParameters response;
77
78 response.setFaceId(1)
79 .setName(sentCp.getName())
80 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)
81 .setCost(0)
82 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
83
84 ndn::nfd::ControlResponse responsePayload;
85 responsePayload.setCode(200)
86 .setText("Successfully registered.")
87 .setBody(response.wireEncode());
88 return responsePayload;
89 }
90
91 void
92 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
93 {
94 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
95 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
96 }
97};
98
99class AdvertiseFailureScenario
100{
101public:
102 ndn::nfd::ControlResponse
103 makeResponse(ControlParameters sentCp)
104 {
105 ndn::nfd::ControlResponse responsePayload(403, "Not Authenticated");
106 return responsePayload;
107 }
108
109 void
110 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
111 {
112 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
113 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
114 }
115};
116
Junxiao Shi63b67e22017-03-06 19:46:02 +0000117using AdvertiseScenarios = boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000118
119BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
120{
121 Scenario scenario;
122 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000123 ReadvertisedRoute rr(prefix);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000124 const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register");
Nick Gordon94719252016-10-20 20:48:01 +0000125
126 dest.advertise(rr, successCallback, failureCallback);
127 advanceClocks(time::milliseconds(100));
128
129 // Retrieve the sent Interest to build the response
130 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
131 const Interest& sentInterest = face.sentInterests[0];
132 BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
133
134 // Parse the sent command Interest to check correctness.
135 ControlParameters sentCp;
136 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue()));
137 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
138 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
139
140 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
141 auto responseData = makeData(sentInterest.getName());
142 responseData->setContent(responsePayload.wireEncode());
143 face.receive(*responseData);
144 this->advanceClocks(time::milliseconds(10));
145
146 scenario.checkCommandOutcome(this);
147}
148
149class WithdrawSuccessScenario
150{
151public:
152 ndn::nfd::ControlResponse
153 makeResponse(const ControlParameters& sentCp)
154 {
155 ControlParameters response;
156
157 response.setFaceId(1)
158 .setName(sentCp.getName())
159 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
160
161 ndn::nfd::ControlResponse responsePayload;
162 responsePayload.setCode(200)
163 .setText("Successfully removed")
164 .setBody(response.wireEncode());
165
166 return responsePayload;
167 }
168
169 void
170 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
171 {
172 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
173 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
174 }
175};
176
177class WithdrawFailureScenario
178{
179public:
180 ndn::nfd::ControlResponse
181 makeResponse(ControlParameters sentCp)
182 {
183 ndn::nfd::ControlResponse responsePayload(403, "Not authenticated");
184 return responsePayload;
185 }
186
187 void
188 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
189 {
190 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
191 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
192 }
193};
194
Junxiao Shi63b67e22017-03-06 19:46:02 +0000195using WithdrawScenarios = boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000196
197BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
198{
199 Scenario scenario;
200 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000201 ReadvertisedRoute rr(prefix);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000202 const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister");
Nick Gordon94719252016-10-20 20:48:01 +0000203
204 dest.withdraw(rr, successCallback, failureCallback);
205 this->advanceClocks(time::milliseconds(10));
206
207 // Retrieve the sent Interest to build the response
208 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
209 const Interest& sentInterest = face.sentInterests[0];
210 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
211
212 ControlParameters sentCp;
213 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue()));
214 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
215 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
216
217 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
218 auto responseData = makeData(sentInterest.getName());
219 responseData->setContent(responsePayload.wireEncode());
220
221 face.receive(*responseData);
222 this->advanceClocks(time::milliseconds(1));
223
224 scenario.checkCommandOutcome(this);
225}
226
Nick Gordon04262d92017-01-31 12:27:06 -0600227BOOST_AUTO_TEST_CASE(DestinationAvailability)
228{
229 std::vector<bool> availabilityChangeHistory;
Junxiao Shi63b67e22017-03-06 19:46:02 +0000230 Name commandPrefix("/localhost/nlsr");
Nick Gordon04262d92017-01-31 12:27:06 -0600231 Route route;
232
233 dest.afterAvailabilityChange.connect(
234 std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1));
235 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
236
Junxiao Shi63b67e22017-03-06 19:46:02 +0000237 rib.insert(commandPrefix, route);
238 this->advanceClocks(time::milliseconds(100));
Nick Gordon04262d92017-01-31 12:27:06 -0600239 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000240 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
Nick Gordon04262d92017-01-31 12:27:06 -0600241 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
242
Junxiao Shi63b67e22017-03-06 19:46:02 +0000243 rib.erase(commandPrefix, route);
244 this->advanceClocks(time::milliseconds(100));
Nick Gordon04262d92017-01-31 12:27:06 -0600245 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000246 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
Nick Gordon04262d92017-01-31 12:27:06 -0600247 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
248}
249
Nick Gordon94719252016-10-20 20:48:01 +0000250BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
Junxiao Shi63b67e22017-03-06 19:46:02 +0000251BOOST_AUTO_TEST_SUITE_END() // Readvertise
Nick Gordon94719252016-10-20 20:48:01 +0000252
253} // namespace tests
254} // namespace rib
255} // namespace nfd