blob: 86dc4843f15ebc52ce54615c97d762181d0a1ac0 [file] [log] [blame]
Junxiao Shicb766862017-07-07 22:21:04 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2017, Regents of the University of California,
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"
29#include <boost/logic/tribool.hpp>
30
31namespace ndn {
32namespace tools {
33namespace autoconfig {
34namespace tests {
35
36using namespace ::nfd::tests;
37using nfd::ControlParameters;
38
39template<typename ProcedureClass>
40class ProcedureFixture : public ::nfd::tools::tests::MockNfdMgmtFixture
41{
42public:
43 void
44 initialize(const Options& options)
45 {
46 procedure = make_unique<ProcedureClass>(face, m_keyChain);
47 procedure->initialize(options);
48 }
49
50 bool
51 runOnce()
52 {
53 BOOST_ASSERT(procedure != nullptr);
54 boost::logic::tribool result;
55 procedure->onComplete.connectSingleShot([&] (bool result1) { result = result1; });
56 procedure->runOnce();
57 face.processEvents();
58 BOOST_CHECK_MESSAGE(!boost::logic::indeterminate(result), "onComplete is not invoked");
59 return result;
60 }
61
62public:
63 unique_ptr<ProcedureClass> procedure;
64};
65
66class DummyStage : public Stage
67{
68public:
69 /** \param stageName stage name
70 * \param nCalls pointer to a variable which is incremented each time doStart is invoked
71 * \param result expected result, nullopt to cause a failued
72 * \param io io_service to asynchronously post the result
73 */
74 DummyStage(const std::string& stageName, int* nCalls, const optional<FaceUri>& result, boost::asio::io_service& io)
75 : m_stageName(stageName)
76 , m_nCalls(nCalls)
77 , m_result(result)
78 , m_io(io)
79 {
80 }
81
82 const std::string&
83 getName() const override
84 {
85 return m_stageName;
86 }
87
88private:
89 void
90 doStart() override
91 {
92 if (m_nCalls != nullptr) {
93 ++(*m_nCalls);
94 }
95 m_io.post([this] {
96 if (m_result) {
97 this->succeed(*m_result);
98 }
99 else {
100 this->fail("DUMMY-STAGE-FAIL");
101 }
102 });
103 }
104
105private:
106 std::string m_stageName;
107 int* m_nCalls;
108 optional<FaceUri> m_result;
109 boost::asio::io_service& m_io;
110};
111
112/** \brief two-stage Procedure where the first stage succeeds and the second stage fails
113 *
114 * But the second stage shouldn't be invoked after the first stage succeeds.
115 */
116class ProcedureSuccessFailure : public Procedure
117{
118public:
119 ProcedureSuccessFailure(Face& face, KeyChain& keyChain)
120 : Procedure(face, keyChain)
121 , m_io(face.getIoService())
122 {
123 }
124
125private:
126 void
127 makeStages(const Options& options) override
128 {
129 m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, FaceUri("udp://188.7.60.95"), m_io));
130 m_stages.push_back(make_unique<DummyStage>("second", &nCalls2, nullopt, m_io));
131 }
132
133public:
134 int nCalls1 = 0;
135 int nCalls2 = 0;
136
137private:
138 boost::asio::io_service& m_io;
139};
140
141/** \brief two-stage Procedure where the first stage fails and the second stage succeeds
142 */
143class ProcedureFailureSuccess : public Procedure
144{
145public:
146 ProcedureFailureSuccess(Face& face, KeyChain& keyChain)
147 : Procedure(face, keyChain)
148 , m_io(face.getIoService())
149 {
150 }
151
152private:
153 void
154 makeStages(const Options& options) override
155 {
156 m_stages.push_back(make_unique<DummyStage>("first", &nCalls1, nullopt, m_io));
157 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:
165 boost::asio::io_service& m_io;
166};
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) {
177 optional<ControlParameters> req = parseCommand(interest, "/localhost/nfd/faces/create");
178 if (req) {
179 BOOST_REQUIRE(req->hasUri());
180 BOOST_CHECK_EQUAL(req->getUri(), "udp4://188.7.60.95:6363");
181
182 ControlParameters resp;
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 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
209 ControlParameters resp;
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
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) {
235 optional<ControlParameters> req = parseCommand(interest, "/localhost/nfd/faces/create");
236 if (req) {
237 ControlParameters resp;
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 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
260 ControlParameters resp;
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 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
283} // namespace tests
284} // namespace autoconfig
285} // namespace tools
286} // namespace ndn