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 | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, 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 |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 69 | * \param io io_context to asynchronously post the result |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 70 | */ |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 71 | DummyStage(const std::string& stageName, int* nCalls, |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 72 | const std::optional<FaceUri>& result, boost::asio::io_context& 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 | } |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 93 | |
| 94 | boost::asio::post(m_io, [this] { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 95 | if (m_result) { |
| 96 | this->succeed(*m_result); |
| 97 | } |
| 98 | else { |
| 99 | this->fail("DUMMY-STAGE-FAIL"); |
| 100 | } |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | std::string m_stageName; |
| 106 | int* m_nCalls; |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 107 | std::optional<FaceUri> m_result; |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 108 | boost::asio::io_context& m_io; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Davide Pesavento | e277f8b | 2023-11-11 17:14:02 -0500 | [diff] [blame] | 111 | /** |
| 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 Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 114 | */ |
| 115 | class ProcedureSuccessFailure : public Procedure |
| 116 | { |
| 117 | public: |
| 118 | ProcedureSuccessFailure(Face& face, KeyChain& keyChain) |
| 119 | : Procedure(face, keyChain) |
Davide Pesavento | e277f8b | 2023-11-11 17:14:02 -0500 | [diff] [blame] | 120 | , m_io(face.getIoContext()) |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 121 | { |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | void |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 126 | makeStages(const Options&) override |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 127 | { |
| 128 | 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] | 129 | 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] | 130 | } |
| 131 | |
| 132 | public: |
| 133 | int nCalls1 = 0; |
| 134 | int nCalls2 = 0; |
| 135 | |
| 136 | private: |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 137 | boost::asio::io_context& m_io; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 138 | }; |
| 139 | |
Davide Pesavento | e277f8b | 2023-11-11 17:14:02 -0500 | [diff] [blame] | 140 | /** |
| 141 | * 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] | 142 | */ |
| 143 | class ProcedureFailureSuccess : public Procedure |
| 144 | { |
| 145 | public: |
| 146 | ProcedureFailureSuccess(Face& face, KeyChain& keyChain) |
| 147 | : Procedure(face, keyChain) |
Davide Pesavento | e277f8b | 2023-11-11 17:14:02 -0500 | [diff] [blame] | 148 | , m_io(face.getIoContext()) |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 149 | { |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | void |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 154 | makeStages(const Options&) override |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 155 | { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 156 | 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] | 157 | m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, FaceUri("tcp://40.23.174.71"), m_io)); |
| 158 | } |
| 159 | |
| 160 | public: |
| 161 | int nCalls1 = 0; |
| 162 | int nCalls2 = 0; |
| 163 | |
| 164 | private: |
Davide Pesavento | a9e1ab2 | 2023-10-02 22:10:45 -0400 | [diff] [blame] | 165 | boost::asio::io_context& m_io; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
| 168 | BOOST_AUTO_TEST_SUITE(NdnAutoconfig) |
| 169 | BOOST_AUTO_TEST_SUITE(TestProcedure) |
| 170 | |
| 171 | BOOST_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 Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 177 | auto req = parseCommand(interest, "/localhost/nfd/faces/create"); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 178 | if (req) { |
| 179 | BOOST_REQUIRE(req->hasUri()); |
| 180 | BOOST_CHECK_EQUAL(req->getUri(), "udp4://188.7.60.95:6363"); |
| 181 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 182 | nfd::ControlParameters resp; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 183 | 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 Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 199 | if (req->getName() == "/") { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 200 | ++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 Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 209 | nfd::ControlParameters resp; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 210 | 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 | |
| 229 | BOOST_FIXTURE_TEST_CASE(ExistingFace, ProcedureFixture<ProcedureFailureSuccess>) |
| 230 | { |
| 231 | this->initialize(Options{}); |
| 232 | |
Md Ashiqur Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 233 | int nRegisterDefault = 0, nRegisterLocalhopNfd = 0; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 234 | this->processInterest = [&] (const Interest& interest) { |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 235 | auto req = parseCommand(interest, "/localhost/nfd/faces/create"); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 236 | if (req) { |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 237 | nfd::ControlParameters resp; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 238 | 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 Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 250 | if (req->getName() == "/") { |
| 251 | ++nRegisterDefault; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 252 | } |
| 253 | else if (req->getName() == "/localhop/nfd") { |
| 254 | ++nRegisterLocalhopNfd; |
| 255 | } |
| 256 | else { |
| 257 | BOOST_ERROR("unexpected prefix registration " << req->getName()); |
| 258 | } |
| 259 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 260 | nfd::ControlParameters resp; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 261 | 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 Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 276 | BOOST_CHECK_EQUAL(nRegisterDefault, 1); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 277 | BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1); |
| 278 | } |
| 279 | |
| 280 | BOOST_AUTO_TEST_SUITE_END() // TestProcedure |
| 281 | BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig |
| 282 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 283 | } // namespace ndn::autoconfig::tests |