blob: 0f09eea779b80638e10f39bddfc7ef90cd6fcbe6 [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 Pesaventoe277f8b2023-11-11 17:14:02 -0500111/**
112 * Two-stage Procedure where the first stage succeeds and the second stage fails,
113 * but the second stage shouldn't be invoked after the first stage succeeds.
Junxiao Shicb766862017-07-07 22:21:04 +0000114 */
115class ProcedureSuccessFailure : public Procedure
116{
117public:
118 ProcedureSuccessFailure(Face& face, KeyChain& keyChain)
119 : Procedure(face, keyChain)
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500120 , m_io(face.getIoContext())
Junxiao Shicb766862017-07-07 22:21:04 +0000121 {
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 Pesaventoe277f8b2023-11-11 17:14:02 -0500140/**
141 * Two-stage Procedure where the first stage fails and the second stage succeeds.
Junxiao Shicb766862017-07-07 22:21:04 +0000142 */
143class ProcedureFailureSuccess : public Procedure
144{
145public:
146 ProcedureFailureSuccess(Face& face, KeyChain& keyChain)
147 : Procedure(face, keyChain)
Davide Pesaventoe277f8b2023-11-11 17:14:02 -0500148 , m_io(face.getIoContext())
Junxiao Shicb766862017-07-07 22:21:04 +0000149 {
150 }
151
152private:
153 void
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400154 makeStages(const Options&) override
Junxiao Shicb766862017-07-07 22:21:04 +0000155 {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400156 m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, std::nullopt, m_io));
Junxiao Shicb766862017-07-07 22:21:04 +0000157 m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, FaceUri("tcp://40.23.174.71"), m_io));
158 }
159
160public:
161 int nCalls1 = 0;
162 int nCalls2 = 0;
163
164private:
Davide Pesaventoa9e1ab22023-10-02 22:10:45 -0400165 boost::asio::io_context& m_io;
Junxiao Shicb766862017-07-07 22:21:04 +0000166};
167
168BOOST_AUTO_TEST_SUITE(NdnAutoconfig)
169BOOST_AUTO_TEST_SUITE(TestProcedure)
170
171BOOST_FIXTURE_TEST_CASE(Normal, ProcedureFixture<ProcedureSuccessFailure>)
172{
173 this->initialize(Options{});
174
175 int nRegisterNdn = 0, nRegisterLocalhopNfd = 0;
176 this->processInterest = [&] (const Interest& interest) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400177 auto req = parseCommand(interest, "/localhost/nfd/faces/create");
Junxiao Shicb766862017-07-07 22:21:04 +0000178 if (req) {
179 BOOST_REQUIRE(req->hasUri());
180 BOOST_CHECK_EQUAL(req->getUri(), "udp4://188.7.60.95:6363");
181
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400182 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000183 resp.setFaceId(1178)
184 .setUri("udp4://188.7.60.95:6363")
185 .setLocalUri("udp4://110.69.164.68:23197")
186 .setFacePersistency(nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT)
187 .setFlags(0);
188 this->succeedCommand(interest, resp);
189 return;
190 }
191
192 req = parseCommand(interest, "/localhost/nfd/rib/register");
193 if (req) {
194 BOOST_REQUIRE(req->hasFaceId());
195 BOOST_CHECK_EQUAL(req->getFaceId(), 1178);
196 BOOST_REQUIRE(req->hasOrigin());
197 BOOST_CHECK_EQUAL(req->getOrigin(), nfd::ROUTE_ORIGIN_AUTOCONF);
198 BOOST_REQUIRE(req->hasName());
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000199 if (req->getName() == "/") {
Junxiao Shicb766862017-07-07 22:21:04 +0000200 ++nRegisterNdn;
201 }
202 else if (req->getName() == "/localhop/nfd") {
203 ++nRegisterLocalhopNfd;
204 }
205 else {
206 BOOST_ERROR("unexpected prefix registration " << req->getName());
207 }
208
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400209 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000210 resp.setName(req->getName())
211 .setFaceId(1178)
212 .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF)
213 .setCost(1)
214 .setFlags(0);
215 this->succeedCommand(interest, resp);
216 return;
217 }
218
219 BOOST_FAIL("unrecognized command Interest " << interest);
220 };
221
222 BOOST_CHECK_EQUAL(this->runOnce(), true);
223 BOOST_CHECK_EQUAL(procedure->nCalls1, 1);
224 BOOST_CHECK_EQUAL(procedure->nCalls2, 0);
225 BOOST_CHECK_EQUAL(nRegisterNdn, 1);
226 BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1);
227}
228
229BOOST_FIXTURE_TEST_CASE(ExistingFace, ProcedureFixture<ProcedureFailureSuccess>)
230{
231 this->initialize(Options{});
232
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000233 int nRegisterDefault = 0, nRegisterLocalhopNfd = 0;
Junxiao Shicb766862017-07-07 22:21:04 +0000234 this->processInterest = [&] (const Interest& interest) {
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400235 auto req = parseCommand(interest, "/localhost/nfd/faces/create");
Junxiao Shicb766862017-07-07 22:21:04 +0000236 if (req) {
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400237 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000238 resp.setFaceId(3146)
239 .setUri("tcp4://40.23.174.71:6363")
240 .setLocalUri("tcp4://34.213.69.67:14445")
241 .setFacePersistency(nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT)
242 .setFlags(0);
243 this->failCommand(interest, 409, "conflict-409", resp);
244 return;
245 }
246
247 req = parseCommand(interest, "/localhost/nfd/rib/register");
248 if (req) {
249 BOOST_REQUIRE(req->hasName());
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000250 if (req->getName() == "/") {
251 ++nRegisterDefault;
Junxiao Shicb766862017-07-07 22:21:04 +0000252 }
253 else if (req->getName() == "/localhop/nfd") {
254 ++nRegisterLocalhopNfd;
255 }
256 else {
257 BOOST_ERROR("unexpected prefix registration " << req->getName());
258 }
259
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400260 nfd::ControlParameters resp;
Junxiao Shicb766862017-07-07 22:21:04 +0000261 resp.setName(req->getName())
262 .setFaceId(3146)
263 .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF)
264 .setCost(1)
265 .setFlags(0);
266 this->succeedCommand(interest, resp);
267 return;
268 }
269
270 BOOST_FAIL("unrecognized command Interest " << interest);
271 };
272
273 BOOST_CHECK_EQUAL(this->runOnce(), true);
274 BOOST_CHECK_EQUAL(procedure->nCalls1, 1);
275 BOOST_CHECK_EQUAL(procedure->nCalls2, 1);
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000276 BOOST_CHECK_EQUAL(nRegisterDefault, 1);
Junxiao Shicb766862017-07-07 22:21:04 +0000277 BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1);
278}
279
280BOOST_AUTO_TEST_SUITE_END() // TestProcedure
281BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig
282
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400283} // namespace ndn::autoconfig::tests