blob: 75e8cfa90ae1f029c622457d41231f1559c9f554 [file] [log] [blame]
Nick Gordon94719252016-10-20 20:48:01 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordon04262d92017-01-31 12:27:06 -06003 * Copyright (c) 2014-2017, 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/test-common.hpp"
29#include "tests/identity-management-fixture.hpp"
Nick Gordon94719252016-10-20 20:48:01 +000030#include <ndn-cxx/util/dummy-client-face.hpp>
31#include <ndn-cxx/security/signing-info.hpp>
32
Nick Gordon94719252016-10-20 20:48:01 +000033namespace nfd {
34namespace rib {
35namespace tests {
36
37using namespace nfd::tests;
38
39class NfdRibReadvertiseDestinationFixture : public IdentityManagementTimeFixture
40{
41public:
42 NfdRibReadvertiseDestinationFixture()
43 : nSuccessCallbacks(0)
44 , nFailureCallbacks(0)
45 , face(getGlobalIoService(), m_keyChain, {true, false})
46 , controller(face, m_keyChain)
Junxiao Shi63b67e22017-03-06 19:46:02 +000047 , dest(controller, Name("/localhost/nlsr"), rib)
48 , successCallback([this] {
Nick Gordon94719252016-10-20 20:48:01 +000049 nSuccessCallbacks++;
50 })
51 , failureCallback([this] (const std::string& str) {
52 nFailureCallbacks++;
53 })
54 {
55 }
56
57public:
58 uint32_t nSuccessCallbacks;
59 uint32_t nFailureCallbacks;
60
61protected:
62 ndn::util::DummyClientFace face;
63 ndn::nfd::Controller controller;
Nick Gordon04262d92017-01-31 12:27:06 -060064 Rib rib;
Nick Gordon94719252016-10-20 20:48:01 +000065 NfdRibReadvertiseDestination dest;
66 std::function<void()> successCallback;
67 std::function<void(const std::string&)> failureCallback;
68};
69
Junxiao Shi63b67e22017-03-06 19:46:02 +000070BOOST_AUTO_TEST_SUITE(Readvertise)
Nick Gordon94719252016-10-20 20:48:01 +000071BOOST_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
Junxiao Shi63b67e22017-03-06 19:46:02 +0000120using AdvertiseScenarios = boost::mpl::vector<AdvertiseSuccessScenario, AdvertiseFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000121
122BOOST_AUTO_TEST_CASE_TEMPLATE(Advertise, Scenario, AdvertiseScenarios)
123{
124 Scenario scenario;
125 Name prefix("/ndn/memphis/test");
Junxiao Shi63b67e22017-03-06 19:46:02 +0000126 ReadvertisedRoute rr(prefix);
127 const Name RIB_REGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/register");
Nick Gordon94719252016-10-20 20:48:01 +0000128
129 dest.advertise(rr, successCallback, failureCallback);
130 advanceClocks(time::milliseconds(100));
131
132 // Retrieve the sent Interest to build the response
133 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
134 const Interest& sentInterest = face.sentInterests[0];
135 BOOST_CHECK(RIB_REGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
136
137 // Parse the sent command Interest to check correctness.
138 ControlParameters sentCp;
139 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_REGISTER_COMMAND_PREFIX.size()).blockFromValue()));
140 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
141 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
142
143 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
144 auto responseData = makeData(sentInterest.getName());
145 responseData->setContent(responsePayload.wireEncode());
146 face.receive(*responseData);
147 this->advanceClocks(time::milliseconds(10));
148
149 scenario.checkCommandOutcome(this);
150}
151
152class WithdrawSuccessScenario
153{
154public:
155 ndn::nfd::ControlResponse
156 makeResponse(const ControlParameters& sentCp)
157 {
158 ControlParameters response;
159
160 response.setFaceId(1)
161 .setName(sentCp.getName())
162 .setOrigin(ndn::nfd::ROUTE_ORIGIN_CLIENT);
163
164 ndn::nfd::ControlResponse responsePayload;
165 responsePayload.setCode(200)
166 .setText("Successfully removed")
167 .setBody(response.wireEncode());
168
169 return responsePayload;
170 }
171
172 void
173 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
174 {
175 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 1);
176 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 0);
177 }
178};
179
180class WithdrawFailureScenario
181{
182public:
183 ndn::nfd::ControlResponse
184 makeResponse(ControlParameters sentCp)
185 {
186 ndn::nfd::ControlResponse responsePayload(403, "Not authenticated");
187 return responsePayload;
188 }
189
190 void
191 checkCommandOutcome(NfdRibReadvertiseDestinationFixture* fixture)
192 {
193 BOOST_CHECK_EQUAL(fixture->nFailureCallbacks, 1);
194 BOOST_CHECK_EQUAL(fixture->nSuccessCallbacks, 0);
195 }
196};
197
Junxiao Shi63b67e22017-03-06 19:46:02 +0000198using WithdrawScenarios = boost::mpl::vector<WithdrawSuccessScenario, WithdrawFailureScenario>;
Nick Gordon94719252016-10-20 20:48:01 +0000199
200BOOST_AUTO_TEST_CASE_TEMPLATE(Withdraw, Scenario, WithdrawScenarios)
201{
202 Scenario scenario;
203 Name prefix("/ndn/memphis/test");
Junxiao Shi63b67e22017-03-06 19:46:02 +0000204 ReadvertisedRoute rr(prefix);
205 const Name RIB_UNREGISTER_COMMAND_PREFIX("/localhost/nlsr/rib/unregister");
Nick Gordon94719252016-10-20 20:48:01 +0000206
207 dest.withdraw(rr, successCallback, failureCallback);
208 this->advanceClocks(time::milliseconds(10));
209
210 // Retrieve the sent Interest to build the response
211 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
212 const Interest& sentInterest = face.sentInterests[0];
213 BOOST_CHECK(RIB_UNREGISTER_COMMAND_PREFIX.isPrefixOf(sentInterest.getName()));
214
215 ControlParameters sentCp;
216 BOOST_CHECK_NO_THROW(sentCp.wireDecode(sentInterest.getName().get(RIB_UNREGISTER_COMMAND_PREFIX.size()).blockFromValue()));
217 BOOST_CHECK_EQUAL(sentCp.getOrigin(), ndn::nfd::ROUTE_ORIGIN_CLIENT);
218 BOOST_CHECK_EQUAL(sentCp.getName(), prefix);
219
220 ndn::nfd::ControlResponse responsePayload = scenario.makeResponse(sentCp);
221 auto responseData = makeData(sentInterest.getName());
222 responseData->setContent(responsePayload.wireEncode());
223
224 face.receive(*responseData);
225 this->advanceClocks(time::milliseconds(1));
226
227 scenario.checkCommandOutcome(this);
228}
229
Nick Gordon04262d92017-01-31 12:27:06 -0600230BOOST_AUTO_TEST_CASE(DestinationAvailability)
231{
232 std::vector<bool> availabilityChangeHistory;
Junxiao Shi63b67e22017-03-06 19:46:02 +0000233 Name commandPrefix("/localhost/nlsr");
Nick Gordon04262d92017-01-31 12:27:06 -0600234 Route route;
235
236 dest.afterAvailabilityChange.connect(
237 std::bind(&std::vector<bool>::push_back, &availabilityChangeHistory, _1));
238 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
239
Junxiao Shi63b67e22017-03-06 19:46:02 +0000240 rib.insert(commandPrefix, route);
241 this->advanceClocks(time::milliseconds(100));
Nick Gordon04262d92017-01-31 12:27:06 -0600242 BOOST_CHECK_EQUAL(dest.isAvailable(), true);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000243 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 1);
Nick Gordon04262d92017-01-31 12:27:06 -0600244 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), true);
245
Junxiao Shi63b67e22017-03-06 19:46:02 +0000246 rib.erase(commandPrefix, route);
247 this->advanceClocks(time::milliseconds(100));
Nick Gordon04262d92017-01-31 12:27:06 -0600248 BOOST_CHECK_EQUAL(dest.isAvailable(), false);
Junxiao Shi63b67e22017-03-06 19:46:02 +0000249 BOOST_REQUIRE_EQUAL(availabilityChangeHistory.size(), 2);
Nick Gordon04262d92017-01-31 12:27:06 -0600250 BOOST_CHECK_EQUAL(availabilityChangeHistory.back(), false);
251}
252
Nick Gordon94719252016-10-20 20:48:01 +0000253BOOST_AUTO_TEST_SUITE_END() // TestNfdRibReadvertiseDestination
Junxiao Shi63b67e22017-03-06 19:46:02 +0000254BOOST_AUTO_TEST_SUITE_END() // Readvertise
Nick Gordon94719252016-10-20 20:48:01 +0000255
256} // namespace tests
257} // namespace rib
258} // namespace nfd