blob: ec7cf319f8833d6e1c33bbcd6d60e1f870df0af5 [file] [log] [blame]
Junxiao Shicb766862017-07-07 22:21:04 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventofa2aa502019-03-22 13:30:02 -04003 * Copyright (c) 2014-2019, 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
28#include "../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
32namespace ndn {
33namespace tools {
34namespace autoconfig {
35namespace tests {
36
37using namespace ::nfd::tests;
38using nfd::ControlParameters;
39
40template<typename ProcedureClass>
41class ProcedureFixture : public ::nfd::tools::tests::MockNfdMgmtFixture
42{
43public:
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 Pesaventofa2aa502019-03-22 13:30:02 -040055
Junxiao Shicb766862017-07-07 22:21:04 +000056 boost::logic::tribool result;
Davide Pesaventofa2aa502019-03-22 13:30:02 -040057 procedure->onComplete.connectSingleShot([&] (bool res) { result = res; });
Junxiao Shicb766862017-07-07 22:21:04 +000058 procedure->runOnce();
59 face.processEvents();
Davide Pesaventofa2aa502019-03-22 13:30:02 -040060
61 BOOST_REQUIRE_MESSAGE(!boost::logic::indeterminate(result), "onComplete was not invoked");
62 return bool(result);
Junxiao Shicb766862017-07-07 22:21:04 +000063 }
64
65public:
66 unique_ptr<ProcedureClass> procedure;
67};
68
69class DummyStage : public Stage
70{
71public:
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 Pesaventofa2aa502019-03-22 13:30:02 -040077 DummyStage(const std::string& stageName, int* nCalls,
78 const optional<FaceUri>& result, boost::asio::io_service& io)
Junxiao Shicb766862017-07-07 22:21:04 +000079 : 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
92private:
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
109private:
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 */
120class ProcedureSuccessFailure : public Procedure
121{
122public:
123 ProcedureSuccessFailure(Face& face, KeyChain& keyChain)
124 : Procedure(face, keyChain)
125 , m_io(face.getIoService())
126 {
127 }
128
129private:
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
137public:
138 int nCalls1 = 0;
139 int nCalls2 = 0;
140
141private:
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 */
147class ProcedureFailureSuccess : public Procedure
148{
149public:
150 ProcedureFailureSuccess(Face& face, KeyChain& keyChain)
151 : Procedure(face, keyChain)
152 , m_io(face.getIoService())
153 {
154 }
155
156private:
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
164public:
165 int nCalls1 = 0;
166 int nCalls2 = 0;
167
168private:
169 boost::asio::io_service& m_io;
170};
171
172BOOST_AUTO_TEST_SUITE(NdnAutoconfig)
173BOOST_AUTO_TEST_SUITE(TestProcedure)
174
175BOOST_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 Rahmanc8f17e52017-08-19 19:18:43 +0000203 if (req->getName() == "/") {
Junxiao Shicb766862017-07-07 22:21:04 +0000204 ++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
233BOOST_FIXTURE_TEST_CASE(ExistingFace, ProcedureFixture<ProcedureFailureSuccess>)
234{
235 this->initialize(Options{});
236
Md Ashiqur Rahmanc8f17e52017-08-19 19:18:43 +0000237 int nRegisterDefault = 0, nRegisterLocalhopNfd = 0;
Junxiao Shicb766862017-07-07 22:21:04 +0000238 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 Rahmanc8f17e52017-08-19 19:18:43 +0000254 if (req->getName() == "/") {
255 ++nRegisterDefault;
Junxiao Shicb766862017-07-07 22:21:04 +0000256 }
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 Rahmanc8f17e52017-08-19 19:18:43 +0000280 BOOST_CHECK_EQUAL(nRegisterDefault, 1);
Junxiao Shicb766862017-07-07 22:21:04 +0000281 BOOST_CHECK_EQUAL(nRegisterLocalhopNfd, 1);
282}
283
284BOOST_AUTO_TEST_SUITE_END() // TestProcedure
285BOOST_AUTO_TEST_SUITE_END() // NdnAutoconfig
286
287} // namespace tests
288} // namespace autoconfig
289} // namespace tools
290} // namespace ndn