Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 4 | * 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 Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 28 | #include "tests/tools/mock-nfd-mgmt-fixture.hpp" |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 29 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 30 | #include <boost/logic/tribool.hpp> |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace ndn::autoconfig::tests { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 33 | |
| 34 | template<typename ProcedureClass> |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 35 | class ProcedureFixture : public ::nfd::tests::MockNfdMgmtFixture |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 36 | { |
| 37 | public: |
| 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 Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 49 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 50 | boost::logic::tribool result; |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 51 | procedure->onComplete.connectSingleShot([&] (bool res) { result = res; }); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 52 | procedure->runOnce(); |
| 53 | face.processEvents(); |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 54 | |
| 55 | BOOST_REQUIRE_MESSAGE(!boost::logic::indeterminate(result), "onComplete was not invoked"); |
| 56 | return bool(result); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | public: |
| 60 | unique_ptr<ProcedureClass> procedure; |
| 61 | }; |
| 62 | |
| 63 | class DummyStage : public Stage |
| 64 | { |
| 65 | public: |
| 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 Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 71 | DummyStage(const std::string& stageName, int* nCalls, |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 72 | const std::optional<FaceUri>& result, boost::asio::io_service& io) |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 73 | : 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 | |
| 86 | private: |
| 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 | |
| 103 | private: |
| 104 | std::string m_stageName; |
| 105 | int* m_nCalls; |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 106 | std::optional<FaceUri> m_result; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 107 | boost::asio::io_service& m_io; |
| 108 | }; |
| 109 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 110 | /** \brief Two-stage Procedure where the first stage succeeds and the second stage fails. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 111 | * |
| 112 | * But the second stage shouldn't be invoked after the first stage succeeds. |
| 113 | */ |
| 114 | class ProcedureSuccessFailure : public Procedure |
| 115 | { |
| 116 | public: |
| 117 | ProcedureSuccessFailure(Face& face, KeyChain& keyChain) |
| 118 | : Procedure(face, keyChain) |
| 119 | , m_io(face.getIoService()) |
| 120 | { |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | void |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 125 | makeStages(const Options&) override |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 126 | { |
| 127 | m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, FaceUri("udp://188.7.60.95"), m_io)); |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 128 | m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, std::nullopt, m_io)); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | public: |
| 132 | int nCalls1 = 0; |
| 133 | int nCalls2 = 0; |
| 134 | |
| 135 | private: |
| 136 | boost::asio::io_service& m_io; |
| 137 | }; |
| 138 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 139 | /** \brief Two-stage Procedure where the first stage fails and the second stage succeeds. |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 140 | */ |
| 141 | class ProcedureFailureSuccess : public Procedure |
| 142 | { |
| 143 | public: |
| 144 | ProcedureFailureSuccess(Face& face, KeyChain& keyChain) |
| 145 | : Procedure(face, keyChain) |
| 146 | , m_io(face.getIoService()) |
| 147 | { |
| 148 | } |
| 149 | |
| 150 | private: |
| 151 | void |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 152 | makeStages(const Options&) override |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 153 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 154 | m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, std::nullopt, m_io)); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 155 | m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, FaceUri("tcp://40.23.174.71"), m_io)); |
| 156 | } |
| 157 | |
| 158 | public: |
| 159 | int nCalls1 = 0; |
| 160 | int nCalls2 = 0; |
| 161 | |
| 162 | private: |
| 163 | boost::asio::io_service& m_io; |
| 164 | }; |
| 165 | |
| 166 | BOOST_AUTO_TEST_SUITE(NdnAutoconfig) |
| 167 | BOOST_AUTO_TEST_SUITE(TestProcedure) |
| 168 | |
| 169 | BOOST_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 Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 175 | auto req = parseCommand(interest, "/localhost/nfd/faces/create"); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 176 | if (req) { |
| 177 | BOOST_REQUIRE(req->hasUri()); |
| 178 | BOOST_CHECK_EQUAL(req->getUri(), "udp4://188.7.60.95:6363"); |
| 179 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 180 | nfd::ControlParameters resp; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 181 | 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 Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 197 | if (req->getName() == "/") { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 198 | ++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 Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 207 | nfd::ControlParameters resp; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 208 | 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 | |
| 227 | BOOST_FIXTURE_TEST_CASE(ExistingFace, ProcedureFixture<ProcedureFailureSuccess>) |
| 228 | { |
| 229 | this->initialize(Options{}); |
| 230 | |
Md Ashiqur Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 231 | int nRegisterDefault = 0, nRegisterLocalhopNfd = 0; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 232 | this->processInterest = [&] (const Interest& interest) { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 233 | auto req = parseCommand(interest, "/localhost/nfd/faces/create"); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 234 | if (req) { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 235 | nfd::ControlParameters resp; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 236 | 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 Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 248 | if (req->getName() == "/") { |
| 249 | ++nRegisterDefault; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 250 | } |
| 251 | else if (req->getName() == "/localhop/nfd") { |
| 252 | ++nRegisterLocalhopNfd; |
| 253 | } |
| 254 | else { |
| 255 | BOOST_ERROR("unexpected prefix registration " << req->getName()); |
| 256 | } |
| 257 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 258 | nfd::ControlParameters resp; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 259 | 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 Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 274 | BOOST_CHECK_EQUAL(nRegisterDefault, 1); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 275 | BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1); |
| 276 | } |
| 277 | |
| 278 | BOOST_AUTO_TEST_SUITE_END() // TestProcedure |
| 279 | BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig |
| 280 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 281 | } // namespace ndn::autoconfig::tests |