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 | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, 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 | |
| 28 | #include "../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 | |
| 32 | namespace ndn { |
| 33 | namespace tools { |
| 34 | namespace autoconfig { |
| 35 | namespace tests { |
| 36 | |
| 37 | using namespace ::nfd::tests; |
| 38 | using nfd::ControlParameters; |
| 39 | |
| 40 | template<typename ProcedureClass> |
| 41 | class ProcedureFixture : public ::nfd::tools::tests::MockNfdMgmtFixture |
| 42 | { |
| 43 | public: |
| 44 | void |
| 45 | initialize(const Options& options) |
| 46 | { |
| 47 | procedure = make_unique<ProcedureClass>(face, m_keyChain); |
| 48 | procedure->initialize(options); |
| 49 | } |
| 50 | |
| 51 | bool |
| 52 | runOnce() |
| 53 | { |
| 54 | BOOST_ASSERT(procedure != nullptr); |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame^] | 55 | |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 56 | boost::logic::tribool result; |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame^] | 57 | procedure->onComplete.connectSingleShot([&] (bool res) { result = res; }); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 58 | procedure->runOnce(); |
| 59 | face.processEvents(); |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame^] | 60 | |
| 61 | BOOST_REQUIRE_MESSAGE(!boost::logic::indeterminate(result), "onComplete was not invoked"); |
| 62 | return bool(result); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | public: |
| 66 | unique_ptr<ProcedureClass> procedure; |
| 67 | }; |
| 68 | |
| 69 | class DummyStage : public Stage |
| 70 | { |
| 71 | public: |
| 72 | /** \param stageName stage name |
| 73 | * \param nCalls pointer to a variable which is incremented each time doStart is invoked |
| 74 | * \param result expected result, nullopt to cause a failued |
| 75 | * \param io io_service to asynchronously post the result |
| 76 | */ |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame^] | 77 | DummyStage(const std::string& stageName, int* nCalls, |
| 78 | const optional<FaceUri>& result, boost::asio::io_service& io) |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 79 | : m_stageName(stageName) |
| 80 | , m_nCalls(nCalls) |
| 81 | , m_result(result) |
| 82 | , m_io(io) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | const std::string& |
| 87 | getName() const override |
| 88 | { |
| 89 | return m_stageName; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | void |
| 94 | doStart() override |
| 95 | { |
| 96 | if (m_nCalls != nullptr) { |
| 97 | ++(*m_nCalls); |
| 98 | } |
| 99 | m_io.post([this] { |
| 100 | if (m_result) { |
| 101 | this->succeed(*m_result); |
| 102 | } |
| 103 | else { |
| 104 | this->fail("DUMMY-STAGE-FAIL"); |
| 105 | } |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | private: |
| 110 | std::string m_stageName; |
| 111 | int* m_nCalls; |
| 112 | optional<FaceUri> m_result; |
| 113 | boost::asio::io_service& m_io; |
| 114 | }; |
| 115 | |
| 116 | /** \brief two-stage Procedure where the first stage succeeds and the second stage fails |
| 117 | * |
| 118 | * But the second stage shouldn't be invoked after the first stage succeeds. |
| 119 | */ |
| 120 | class ProcedureSuccessFailure : public Procedure |
| 121 | { |
| 122 | public: |
| 123 | ProcedureSuccessFailure(Face& face, KeyChain& keyChain) |
| 124 | : Procedure(face, keyChain) |
| 125 | , m_io(face.getIoService()) |
| 126 | { |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | void |
| 131 | makeStages(const Options& options) override |
| 132 | { |
| 133 | m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, FaceUri("udp://188.7.60.95"), m_io)); |
| 134 | m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, nullopt, m_io)); |
| 135 | } |
| 136 | |
| 137 | public: |
| 138 | int nCalls1 = 0; |
| 139 | int nCalls2 = 0; |
| 140 | |
| 141 | private: |
| 142 | boost::asio::io_service& m_io; |
| 143 | }; |
| 144 | |
| 145 | /** \brief two-stage Procedure where the first stage fails and the second stage succeeds |
| 146 | */ |
| 147 | class ProcedureFailureSuccess : public Procedure |
| 148 | { |
| 149 | public: |
| 150 | ProcedureFailureSuccess(Face& face, KeyChain& keyChain) |
| 151 | : Procedure(face, keyChain) |
| 152 | , m_io(face.getIoService()) |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | void |
| 158 | makeStages(const Options& options) override |
| 159 | { |
| 160 | m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, nullopt, m_io)); |
| 161 | m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, FaceUri("tcp://40.23.174.71"), m_io)); |
| 162 | } |
| 163 | |
| 164 | public: |
| 165 | int nCalls1 = 0; |
| 166 | int nCalls2 = 0; |
| 167 | |
| 168 | private: |
| 169 | boost::asio::io_service& m_io; |
| 170 | }; |
| 171 | |
| 172 | BOOST_AUTO_TEST_SUITE(NdnAutoconfig) |
| 173 | BOOST_AUTO_TEST_SUITE(TestProcedure) |
| 174 | |
| 175 | BOOST_FIXTURE_TEST_CASE(Normal, ProcedureFixture<ProcedureSuccessFailure>) |
| 176 | { |
| 177 | this->initialize(Options{}); |
| 178 | |
| 179 | int nRegisterNdn = 0, nRegisterLocalhopNfd = 0; |
| 180 | this->processInterest = [&] (const Interest& interest) { |
| 181 | optional<ControlParameters> req = parseCommand(interest, "/localhost/nfd/faces/create"); |
| 182 | if (req) { |
| 183 | BOOST_REQUIRE(req->hasUri()); |
| 184 | BOOST_CHECK_EQUAL(req->getUri(), "udp4://188.7.60.95:6363"); |
| 185 | |
| 186 | ControlParameters resp; |
| 187 | resp.setFaceId(1178) |
| 188 | .setUri("udp4://188.7.60.95:6363") |
| 189 | .setLocalUri("udp4://110.69.164.68:23197") |
| 190 | .setFacePersistency(nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT) |
| 191 | .setFlags(0); |
| 192 | this->succeedCommand(interest, resp); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | req = parseCommand(interest, "/localhost/nfd/rib/register"); |
| 197 | if (req) { |
| 198 | BOOST_REQUIRE(req->hasFaceId()); |
| 199 | BOOST_CHECK_EQUAL(req->getFaceId(), 1178); |
| 200 | BOOST_REQUIRE(req->hasOrigin()); |
| 201 | BOOST_CHECK_EQUAL(req->getOrigin(), nfd::ROUTE_ORIGIN_AUTOCONF); |
| 202 | BOOST_REQUIRE(req->hasName()); |
Md Ashiqur Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 203 | if (req->getName() == "/") { |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 204 | ++nRegisterNdn; |
| 205 | } |
| 206 | else if (req->getName() == "/localhop/nfd") { |
| 207 | ++nRegisterLocalhopNfd; |
| 208 | } |
| 209 | else { |
| 210 | BOOST_ERROR("unexpected prefix registration " << req->getName()); |
| 211 | } |
| 212 | |
| 213 | ControlParameters resp; |
| 214 | resp.setName(req->getName()) |
| 215 | .setFaceId(1178) |
| 216 | .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF) |
| 217 | .setCost(1) |
| 218 | .setFlags(0); |
| 219 | this->succeedCommand(interest, resp); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | BOOST_FAIL("unrecognized command Interest " << interest); |
| 224 | }; |
| 225 | |
| 226 | BOOST_CHECK_EQUAL(this->runOnce(), true); |
| 227 | BOOST_CHECK_EQUAL(procedure->nCalls1, 1); |
| 228 | BOOST_CHECK_EQUAL(procedure->nCalls2, 0); |
| 229 | BOOST_CHECK_EQUAL(nRegisterNdn, 1); |
| 230 | BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1); |
| 231 | } |
| 232 | |
| 233 | BOOST_FIXTURE_TEST_CASE(ExistingFace, ProcedureFixture<ProcedureFailureSuccess>) |
| 234 | { |
| 235 | this->initialize(Options{}); |
| 236 | |
Md Ashiqur Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 237 | int nRegisterDefault = 0, nRegisterLocalhopNfd = 0; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 238 | this->processInterest = [&] (const Interest& interest) { |
| 239 | optional<ControlParameters> req = parseCommand(interest, "/localhost/nfd/faces/create"); |
| 240 | if (req) { |
| 241 | ControlParameters resp; |
| 242 | resp.setFaceId(3146) |
| 243 | .setUri("tcp4://40.23.174.71:6363") |
| 244 | .setLocalUri("tcp4://34.213.69.67:14445") |
| 245 | .setFacePersistency(nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT) |
| 246 | .setFlags(0); |
| 247 | this->failCommand(interest, 409, "conflict-409", resp); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | req = parseCommand(interest, "/localhost/nfd/rib/register"); |
| 252 | if (req) { |
| 253 | BOOST_REQUIRE(req->hasName()); |
Md Ashiqur Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 254 | if (req->getName() == "/") { |
| 255 | ++nRegisterDefault; |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 256 | } |
| 257 | else if (req->getName() == "/localhop/nfd") { |
| 258 | ++nRegisterLocalhopNfd; |
| 259 | } |
| 260 | else { |
| 261 | BOOST_ERROR("unexpected prefix registration " << req->getName()); |
| 262 | } |
| 263 | |
| 264 | ControlParameters resp; |
| 265 | resp.setName(req->getName()) |
| 266 | .setFaceId(3146) |
| 267 | .setOrigin(nfd::ROUTE_ORIGIN_AUTOCONF) |
| 268 | .setCost(1) |
| 269 | .setFlags(0); |
| 270 | this->succeedCommand(interest, resp); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | BOOST_FAIL("unrecognized command Interest " << interest); |
| 275 | }; |
| 276 | |
| 277 | BOOST_CHECK_EQUAL(this->runOnce(), true); |
| 278 | BOOST_CHECK_EQUAL(procedure->nCalls1, 1); |
| 279 | BOOST_CHECK_EQUAL(procedure->nCalls2, 1); |
Md Ashiqur Rahman | c8f17e5 | 2017-08-19 19:18:43 +0000 | [diff] [blame] | 280 | BOOST_CHECK_EQUAL(nRegisterDefault, 1); |
Junxiao Shi | cb76686 | 2017-07-07 22:21:04 +0000 | [diff] [blame] | 281 | BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1); |
| 282 | } |
| 283 | |
| 284 | BOOST_AUTO_TEST_SUITE_END() // TestProcedure |
| 285 | BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig |
| 286 | |
| 287 | } // namespace tests |
| 288 | } // namespace autoconfig |
| 289 | } // namespace tools |
| 290 | } // namespace ndn |