blob: d6a024da4c137795af00e3e560b60e325cc2665a [file] [log] [blame]
Junxiao Shicb766862017-07-07 22:21:04 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -04003 * Copyright (c) 2014-2023, 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
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040069 * \param io io_context to asynchronously post the result
Junxiao Shicb766862017-07-07 22:21:04 +000070 */
Davide Pesaventofa2aa502019-03-22 13:30:02 -040071 DummyStage(const std::string& stageName, int* nCalls,
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040072 const std::optional<FaceUri>& result, boost::asio::io_context& 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 }
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -040093
94 boost::asio::post(m_io, [this] {
Junxiao Shicb766862017-07-07 22:21:04 +000095 if (m_result) {
96 this->succeed(*m_result);
97 }
98 else {
99 this->fail("DUMMY-STAGE-FAIL");
100 }
101 });
102 }
103
104private:
105 std::string m_stageName;
106 int* m_nCalls;
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400107 std::optional<FaceUri> m_result;
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400108 boost::asio::io_context& m_io;
Junxiao Shicb766862017-07-07 22:21:04 +0000109};
110
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400111/** \brief Two-stage Procedure where the first stage succeeds and the second stage fails.
Junxiao Shicb766862017-07-07 22:21:04 +0000112 *
113 * But the second stage shouldn't be invoked after the first stage succeeds.
114 */
115class ProcedureSuccessFailure : public Procedure
116{
117public:
118 ProcedureSuccessFailure(Face& face, KeyChain& keyChain)
119 : Procedure(face, keyChain)
120 , m_io(face.getIoService())
121 {
122 }
123
124private:
125 void
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400126 makeStages(const Options&) override
Junxiao Shicb766862017-07-07 22:21:04 +0000127 {
128 m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, FaceUri("udp://188.7.60.95"), m_io));
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400129 m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, std::nullopt, m_io));
Junxiao Shicb766862017-07-07 22:21:04 +0000130 }
131
132public:
133 int nCalls1 = 0;
134 int nCalls2 = 0;
135
136private:
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400137 boost::asio::io_context& m_io;
Junxiao Shicb766862017-07-07 22:21:04 +0000138};
139
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400140/** \brief Two-stage Procedure where the first stage fails and the second stage succeeds.
Junxiao Shicb766862017-07-07 22:21:04 +0000141 */
142class ProcedureFailureSuccess : public Procedure
143{
144public:
145 ProcedureFailureSuccess(Face& face, KeyChain& keyChain)
146 : Procedure(face, keyChain)
147 , m_io(face.getIoService())
148 {
149 }
150
151private:
152 void
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400153 makeStages(const Options&) override
Junxiao Shicb766862017-07-07 22:21:04 +0000154 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400155 m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, std::nullopt, m_io));
Junxiao Shicb766862017-07-07 22:21:04 +0000156 m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, FaceUri("tcp://40.23.174.71"), m_io));
157 }
158
159public:
160 int nCalls1 = 0;
161 int nCalls2 = 0;
162
163private:
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400164 boost::asio::io_context& m_io;
Junxiao Shicb766862017-07-07 22:21:04 +0000165};
166
167BOOST_AUTO_TEST_SUITE(NdnAutoconfig)
168BOOST_AUTO_TEST_SUITE(TestProcedure)
169
170BOOST_FIXTURE_TEST_CASE(Normal, ProcedureFixture<ProcedureSuccessFailure>)
171{
172 this->initialize(Options{});
173
174 int nRegisterNdn = 0, nRegisterLocalhopNfd = 0;
175 this->processInterest = [&] (const Interest& interest) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400176 auto req = parseCommand(interest, "/localhost/nfd/faces/create");
Junxiao Shicb766862017-07-07 22:21:04 +0000177 if (req) {
178 BOOST_REQUIRE(req->hasUri());
179 BOOST_CHECK_EQUAL(req->getUri(), "udp4://188.7.60.95:6363");
180
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400181 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000182 resp.setFaceId(1178)
183 .setUri("udp4://188.7.60.95:6363")
184 .setLocalUri("udp4://110.69.164.68:23197")
185 .setFacePersistency(nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT)
186 .setFlags(0);
187 this->succeedCommand(interest, resp);
188 return;
189 }
190
191 req = parseCommand(interest, "/localhost/nfd/rib/register");
192 if (req) {
193 BOOST_REQUIRE(req->hasFaceId());
194 BOOST_CHECK_EQUAL(req->getFaceId(), 1178);
195 BOOST_REQUIRE(req->hasOrigin());
196 BOOST_CHECK_EQUAL(req->getOrigin(), nfd::ROUTE_ORIGIN_AUTOCONF);
197 BOOST_REQUIRE(req->hasName());
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000198 if (req->getName() == "/") {
Junxiao Shicb766862017-07-07 22:21:04 +0000199 ++nRegisterNdn;
200 }
201 else if (req->getName() == "/localhop/nfd") {
202 ++nRegisterLocalhopNfd;
203 }
204 else {
205 BOOST_ERROR("unexpected prefix registration " << req->getName());
206 }
207
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400208 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000209 resp.setName(req->getName())
210 .setFaceId(1178)
211 .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF)
212 .setCost(1)
213 .setFlags(0);
214 this->succeedCommand(interest, resp);
215 return;
216 }
217
218 BOOST_FAIL("unrecognized command Interest " << interest);
219 };
220
221 BOOST_CHECK_EQUAL(this->runOnce(), true);
222 BOOST_CHECK_EQUAL(procedure->nCalls1, 1);
223 BOOST_CHECK_EQUAL(procedure->nCalls2, 0);
224 BOOST_CHECK_EQUAL(nRegisterNdn, 1);
225 BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1);
226}
227
228BOOST_FIXTURE_TEST_CASE(ExistingFace, ProcedureFixture<ProcedureFailureSuccess>)
229{
230 this->initialize(Options{});
231
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000232 int nRegisterDefault = 0, nRegisterLocalhopNfd = 0;
Junxiao Shicb766862017-07-07 22:21:04 +0000233 this->processInterest = [&] (const Interest& interest) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400234 auto req = parseCommand(interest, "/localhost/nfd/faces/create");
Junxiao Shicb766862017-07-07 22:21:04 +0000235 if (req) {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400236 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000237 resp.setFaceId(3146)
238 .setUri("tcp4://40.23.174.71:6363")
239 .setLocalUri("tcp4://34.213.69.67:14445")
240 .setFacePersistency(nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT)
241 .setFlags(0);
242 this->failCommand(interest, 409, "conflict-409", resp);
243 return;
244 }
245
246 req = parseCommand(interest, "/localhost/nfd/rib/register");
247 if (req) {
248 BOOST_REQUIRE(req->hasName());
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000249 if (req->getName() == "/") {
250 ++nRegisterDefault;
Junxiao Shicb766862017-07-07 22:21:04 +0000251 }
252 else if (req->getName() == "/localhop/nfd") {
253 ++nRegisterLocalhopNfd;
254 }
255 else {
256 BOOST_ERROR("unexpected prefix registration " << req->getName());
257 }
258
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400259 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000260 resp.setName(req->getName())
261 .setFaceId(3146)
262 .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF)
263 .setCost(1)
264 .setFlags(0);
265 this->succeedCommand(interest, resp);
266 return;
267 }
268
269 BOOST_FAIL("unrecognized command Interest " << interest);
270 };
271
272 BOOST_CHECK_EQUAL(this->runOnce(), true);
273 BOOST_CHECK_EQUAL(procedure->nCalls1, 1);
274 BOOST_CHECK_EQUAL(procedure->nCalls2, 1);
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000275 BOOST_CHECK_EQUAL(nRegisterDefault, 1);
Junxiao Shicb766862017-07-07 22:21:04 +0000276 BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1);
277}
278
279BOOST_AUTO_TEST_SUITE_END() // TestProcedure
280BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig
281
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400282} // namespace ndn::autoconfig::tests