blob: 5a64bbea9d9ef875b0875563f9d43ba80c1af35d [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
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040028#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/key-chain-fixture.hpp"
30#include "tests/daemon/global-io-fixture.hpp"
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040031
Nick Gordon94719252016-10-20 20:48:01 +000032#include <ndn-cxx/security/signing-info.hpp>
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040033#include <ndn-cxx/util/dummy-client-face.hpp>
Nick Gordon94719252016-10-20 20:48:01 +000034
Nick Gordon94719252016-10-20 20:48:01 +000035namespace nfd {
36namespace rib {
37namespace tests {
38
39using namespace nfd::tests;
40
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040041class NfdRibReadvertiseDestinationFixture : public GlobalIoTimeFixture, public KeyChainFixture
Nick Gordon94719252016-10-20 20:48:01 +000042{
43public:
44 NfdRibReadvertiseDestinationFixture()
45 : nSuccessCallbacks(0)
46 , nFailureCallbacks(0)
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040047 , face(g_io, m_keyChain, {true, false})
Nick Gordon94719252016-10-20 20:48:01 +000048 , controller(face, m_keyChain)
Yanbiao Lif48d0802018-06-01 03:00:02 -070049 , dest(controller, rib, ndn::nfd::CommandOptions().setPrefix("/localhost/nlsr"))
Davide Pesaventoe1bdc082018-10-11 21:20:23 -040050 , successCallback([this] { nSuccessCallbacks++; })
Davide Pesavento0a71dd32019-03-17 20:36:18 -040051 , failureCallback([this] (const std::string&) { nFailureCallbacks++; })
Nick Gordon94719252016-10-20 20:48:01 +000052 {
53 }
54
55public:
56 uint32_t nSuccessCallbacks;
57 uint32_t nFailureCallbacks;
58
59protected:
60 ndn::util::DummyClientFace face;
61 ndn::nfd::Controller controller;
Nick Gordon04262d92017-01-31 12:27:06 -060062 Rib rib;
Nick Gordon94719252016-10-20 20:48:01 +000063 NfdRibReadvertiseDestination dest;
64 std::function<void()> successCallback;
65 std::function<void(const std::string&)> failureCallback;
66};
67
Junxiao Shi63b67e22017-03-06 19:46:02 +000068BOOST_AUTO_TEST_SUITE(Readvertise)
Nick Gordon94719252016-10-20 20:48:01 +000069BOOST_FIXTURE_TEST_SUITE(TestNfdRibReadvertiseDestination, NfdRibReadvertiseDestinationFixture)
70
71class AdvertiseSuccessScenario
72{
73public:
74 ndn::nfd::ControlResponse
75 makeResponse(const ControlParameters& sentCp)
76 {
77 ControlParameters response;
78
79 response.setFaceId(1)
80 .setName(sentCp.getName())
81 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT)
82 .setCost(0)
83 .setFlags(ndn::nfd::ROUTE_FLAG_CHILD_INHERIT);
84
85 ndn::nfd::ControlResponse responsePayload;
86 responsePayload.setCode(200)
87 .setText("Successfully registered.")
88 .setBody(response.wireEncode());
89 return responsePayload;
90 }
91
92 void
93 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
94 {
95 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
96 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
97 }
98};
99
100class AdvertiseFailureScenario
101{
102public:
103 ndn::nfd::ControlResponse
104 makeResponse(ControlParameters sentCp)
105 {
106 ndn::nfd::ControlResponse responsePayload(403, "Not Authenticated");
107 return responsePayload;
108 }
109
110 void
111 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
112 {
113 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
114 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
115 }
116};
117
Junxiao Shi63b67e22017-03-06 19:46:02 +0000118using AdvertiseScenarios = boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000119
120BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
121{
122 Scenario scenario;
123 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000124 ReadvertisedRoute rr(prefix);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000125 const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register");
Nick Gordon94719252016-10-20 20:48:01 +0000126
127 dest.advertise(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400128 advanceClocks(100_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000129
130 // Retrieve the sent Interest to build the response
131 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
132 const Interest& sentInterest = face.sentInterests[0];
133 BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
134
135 // Parse the sent command Interest to check correctness.
136 ControlParameters sentCp;
137 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue()));
138 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
139 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
140
141 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
142 auto responseData = makeData(sentInterest.getName());
143 responseData->setContent(responsePayload.wireEncode());
144 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400145 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000146
147 scenario.checkCommandOutcome(this);
148}
149
150class WithdrawSuccessScenario
151{
152public:
153 ndn::nfd::ControlResponse
154 makeResponse(const ControlParameters& sentCp)
155 {
156 ControlParameters response;
157
158 response.setFaceId(1)
159 .setName(sentCp.getName())
160 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
161
162 ndn::nfd::ControlResponse responsePayload;
163 responsePayload.setCode(200)
164 .setText("Successfully removed")
165 .setBody(response.wireEncode());
166
167 return responsePayload;
168 }
169
170 void
171 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
172 {
173 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
174 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
175 }
176};
177
178class WithdrawFailureScenario
179{
180public:
181 ndn::nfd::ControlResponse
182 makeResponse(ControlParameters sentCp)
183 {
184 ndn::nfd::ControlResponse responsePayload(403, "Not authenticated");
185 return responsePayload;
186 }
187
188 void
189 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
190 {
191 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
192 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
193 }
194};
195
Junxiao Shi63b67e22017-03-06 19:46:02 +0000196using WithdrawScenarios = boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000197
198BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
199{
200 Scenario scenario;
201 Name prefix("/ndn/memphis/test");
Junxiao Shifeddc3c2019-01-17 19:06:00 +0000202 ReadvertisedRoute rr(prefix);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000203 const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister");
Nick Gordon94719252016-10-20 20:48:01 +0000204
205 dest.withdraw(rr, successCallback, failureCallback);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400206 this->advanceClocks(10_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000207
208 // Retrieve the sent Interest to build the response
209 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
210 const Interest& sentInterest = face.sentInterests[0];
211 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
212
213 ControlParameters sentCp;
214 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue()));
215 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
216 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
217
218 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
219 auto responseData = makeData(sentInterest.getName());
220 responseData->setContent(responsePayload.wireEncode());
221
222 face.receive(*responseData);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400223 this->advanceClocks(1_ms);
Nick Gordon94719252016-10-20 20:48:01 +0000224
225 scenario.checkCommandOutcome(this);
226}
227
Nick Gordon04262d92017-01-31 12:27:06 -0600228BOOST_AUTO_TEST_CASE(DestinationAvailability)
229{
230 std::vector<bool> availabilityChangeHistory;
Junxiao Shi63b67e22017-03-06 19:46:02 +0000231 Name commandPrefix("/localhost/nlsr");
Nick Gordon04262d92017-01-31 12:27:06 -0600232 Route route;
233
234 dest.afterAvailabilityChange.connect(
235 std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1));
236 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
237
Junxiao Shi63b67e22017-03-06 19:46:02 +0000238 rib.insert(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400239 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600240 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000241 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
Nick Gordon04262d92017-01-31 12:27:06 -0600242 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
243
Junxiao Shi63b67e22017-03-06 19:46:02 +0000244 rib.erase(commandPrefix, route);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400245 this->advanceClocks(100_ms);
Nick Gordon04262d92017-01-31 12:27:06 -0600246 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000247 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
Nick Gordon04262d92017-01-31 12:27:06 -0600248 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
249}
250
Nick Gordon94719252016-10-20 20:48:01 +0000251BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
Junxiao Shi63b67e22017-03-06 19:46:02 +0000252BOOST_AUTO_TEST_SUITE_END() // Readvertise
Nick Gordon94719252016-10-20 20:48:01 +0000253
254} // namespace tests
255} // namespace rib
256} // namespace nfd