blob: ea5311b32e256a6b825f82ce6b7653a20924bfa1 [file] [log] [blame]
Junxiao Shicb766862017-07-07 22:21:04 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Junxiao Shicb766862017-07-07 22:21:04 +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
26#include "ndn-autoconfig/procedure.hpp"
27
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040028#include "tests/tools/mock-nfd-mgmt-fixture.hpp"
Davide Pesaventofa2aa502019-03-22 13:30:02 -040029
Junxiao Shicb766862017-07-07 22:21:04 +000030#include <boost/logic/tribool.hpp>
31
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace ndn::autoconfig::tests {
Junxiao Shicb766862017-07-07 22:21:04 +000033
34template<typename ProcedureClass>
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040035class ProcedureFixture : public ::nfd::tests::MockNfdMgmtFixture
Junxiao Shicb766862017-07-07 22:21:04 +000036{
37public:
38 void
39 initialize(const Options& options)
40 {
41 procedure = make_unique<ProcedureClass>(face, m_keyChain);
42 procedure->initialize(options);
43 }
44
45 bool
46 runOnce()
47 {
48 BOOST_ASSERT(procedure != nullptr);
Davide Pesaventofa2aa502019-03-22 13:30:02 -040049
Junxiao Shicb766862017-07-07 22:21:04 +000050 boost::logic::tribool result;
Davide Pesaventofa2aa502019-03-22 13:30:02 -040051 procedure->onComplete.connectSingleShot([&] (bool res) { result = res; });
Junxiao Shicb766862017-07-07 22:21:04 +000052 procedure->runOnce();
53 face.processEvents();
Davide Pesaventofa2aa502019-03-22 13:30:02 -040054
55 BOOST_REQUIRE_MESSAGE(!boost::logic::indeterminate(result), "onComplete was not invoked");
56 return bool(result);
Junxiao Shicb766862017-07-07 22:21:04 +000057 }
58
59public:
60 unique_ptr<ProcedureClass> procedure;
61};
62
63class DummyStage : public Stage
64{
65public:
66 /** \param stageName stage name
67 * \param nCalls pointer to a variable which is incremented each time doStart is invoked
68 * \param result expected result, nullopt to cause a failued
69 * \param io io_service to asynchronously post the result
70 */
Davide Pesaventofa2aa502019-03-22 13:30:02 -040071 DummyStage(const std::string& stageName, int* nCalls,
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040072 const std::optional<FaceUri>& result, boost::asio::io_service& io)
Junxiao Shicb766862017-07-07 22:21:04 +000073 : m_stageName(stageName)
74 , m_nCalls(nCalls)
75 , m_result(result)
76 , m_io(io)
77 {
78 }
79
80 const std::string&
81 getName() const override
82 {
83 return m_stageName;
84 }
85
86private:
87 void
88 doStart() override
89 {
90 if (m_nCalls != nullptr) {
91 ++(*m_nCalls);
92 }
93 m_io.post([this] {
94 if (m_result) {
95 this->succeed(*m_result);
96 }
97 else {
98 this->fail("DUMMY-STAGE-FAIL");
99 }
100 });
101 }
102
103private:
104 std::string m_stageName;
105 int* m_nCalls;
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400106 std::optional<FaceUri> m_result;
Junxiao Shicb766862017-07-07 22:21:04 +0000107 boost::asio::io_service& m_io;
108};
109
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400110/** \brief Two-stage Procedure where the first stage succeeds and the second stage fails.
Junxiao Shicb766862017-07-07 22:21:04 +0000111 *
112 * But the second stage shouldn't be invoked after the first stage succeeds.
113 */
114class ProcedureSuccessFailure : public Procedure
115{
116public:
117 ProcedureSuccessFailure(Face& face, KeyChain& keyChain)
118 : Procedure(face, keyChain)
119 , m_io(face.getIoService())
120 {
121 }
122
123private:
124 void
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400125 makeStages(const Options&) override
Junxiao Shicb766862017-07-07 22:21:04 +0000126 {
127 m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, FaceUri("udp://188.7.60.95"), m_io));
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400128 m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, std::nullopt, m_io));
Junxiao Shicb766862017-07-07 22:21:04 +0000129 }
130
131public:
132 int nCalls1 = 0;
133 int nCalls2 = 0;
134
135private:
136 boost::asio::io_service& m_io;
137};
138
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400139/** \brief Two-stage Procedure where the first stage fails and the second stage succeeds.
Junxiao Shicb766862017-07-07 22:21:04 +0000140 */
141class ProcedureFailureSuccess : public Procedure
142{
143public:
144 ProcedureFailureSuccess(Face& face, KeyChain& keyChain)
145 : Procedure(face, keyChain)
146 , m_io(face.getIoService())
147 {
148 }
149
150private:
151 void
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400152 makeStages(const Options&) override
Junxiao Shicb766862017-07-07 22:21:04 +0000153 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400154 m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, std::nullopt, m_io));
Junxiao Shicb766862017-07-07 22:21:04 +0000155 m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, FaceUri("tcp://40.23.174.71"), m_io));
156 }
157
158public:
159 int nCalls1 = 0;
160 int nCalls2 = 0;
161
162private:
163 boost::asio::io_service& m_io;
164};
165
166BOOST_AUTO_TEST_SUITE(NdnAutoconfig)
167BOOST_AUTO_TEST_SUITE(TestProcedure)
168
169BOOST_FIXTURE_TEST_CASE(Normal, ProcedureFixture<ProcedureSuccessFailure>)
170{
171 this->initialize(Options{});
172
173 int nRegisterNdn = 0, nRegisterLocalhopNfd = 0;
174 this->processInterest = [&] (const Interest& interest) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400175 auto req = parseCommand(interest, "/localhost/nfd/faces/create");
Junxiao Shicb766862017-07-07 22:21:04 +0000176 if (req) {
177 BOOST_REQUIRE(req->hasUri());
178 BOOST_CHECK_EQUAL(req->getUri(), "udp4://188.7.60.95:6363");
179
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400180 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000181 resp.setFaceId(1178)
182 .setUri("udp4://188.7.60.95:6363")
183 .setLocalUri("udp4://110.69.164.68:23197")
184 .setFacePersistency(nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT)
185 .setFlags(0);
186 this->succeedCommand(interest, resp);
187 return;
188 }
189
190 req = parseCommand(interest, "/localhost/nfd/rib/register");
191 if (req) {
192 BOOST_REQUIRE(req->hasFaceId());
193 BOOST_CHECK_EQUAL(req->getFaceId(), 1178);
194 BOOST_REQUIRE(req->hasOrigin());
195 BOOST_CHECK_EQUAL(req->getOrigin(), nfd::ROUTE_ORIGIN_AUTOCONF);
196 BOOST_REQUIRE(req->hasName());
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000197 if (req->getName() == "/") {
Junxiao Shicb766862017-07-07 22:21:04 +0000198 ++nRegisterNdn;
199 }
200 else if (req->getName() == "/localhop/nfd") {
201 ++nRegisterLocalhopNfd;
202 }
203 else {
204 BOOST_ERROR("unexpected prefix registration " << req->getName());
205 }
206
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400207 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000208 resp.setName(req->getName())
209 .setFaceId(1178)
210 .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF)
211 .setCost(1)
212 .setFlags(0);
213 this->succeedCommand(interest, resp);
214 return;
215 }
216
217 BOOST_FAIL("unrecognized command Interest " << interest);
218 };
219
220 BOOST_CHECK_EQUAL(this->runOnce(), true);
221 BOOST_CHECK_EQUAL(procedure->nCalls1, 1);
222 BOOST_CHECK_EQUAL(procedure->nCalls2, 0);
223 BOOST_CHECK_EQUAL(nRegisterNdn, 1);
224 BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1);
225}
226
227BOOST_FIXTURE_TEST_CASE(ExistingFace, ProcedureFixture<ProcedureFailureSuccess>)
228{
229 this->initialize(Options{});
230
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000231 int nRegisterDefault = 0, nRegisterLocalhopNfd = 0;
Junxiao Shicb766862017-07-07 22:21:04 +0000232 this->processInterest = [&] (const Interest& interest) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400233 auto req = parseCommand(interest, "/localhost/nfd/faces/create");
Junxiao Shicb766862017-07-07 22:21:04 +0000234 if (req) {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400235 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000236 resp.setFaceId(3146)
237 .setUri("tcp4://40.23.174.71:6363")
238 .setLocalUri("tcp4://34.213.69.67:14445")
239 .setFacePersistency(nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT)
240 .setFlags(0);
241 this->failCommand(interest, 409, "conflict-409", resp);
242 return;
243 }
244
245 req = parseCommand(interest, "/localhost/nfd/rib/register");
246 if (req) {
247 BOOST_REQUIRE(req->hasName());
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000248 if (req->getName() == "/") {
249 ++nRegisterDefault;
Junxiao Shicb766862017-07-07 22:21:04 +0000250 }
251 else if (req->getName() == "/localhop/nfd") {
252 ++nRegisterLocalhopNfd;
253 }
254 else {
255 BOOST_ERROR("unexpected prefix registration " << req->getName());
256 }
257
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400258 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000259 resp.setName(req->getName())
260 .setFaceId(3146)
261 .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF)
262 .setCost(1)
263 .setFlags(0);
264 this->succeedCommand(interest, resp);
265 return;
266 }
267
268 BOOST_FAIL("unrecognized command Interest " << interest);
269 };
270
271 BOOST_CHECK_EQUAL(this->runOnce(), true);
272 BOOST_CHECK_EQUAL(procedure->nCalls1, 1);
273 BOOST_CHECK_EQUAL(procedure->nCalls2, 1);
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000274 BOOST_CHECK_EQUAL(nRegisterDefault, 1);
Junxiao Shicb766862017-07-07 22:21:04 +0000275 BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1);
276}
277
278BOOST_AUTO_TEST_SUITE_END() // TestProcedure
279BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig
280
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400281} // namespace ndn::autoconfig::tests