blob: 07c5894d312193bce1c42c0a0a12713382c06eef [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -07002/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070020 */
21
22#include "management/nfd-controller.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070023#include "management/nfd-control-response.hpp"
24
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070025#include <boost/tuple/tuple.hpp>
26
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027#include "boost-test.hpp"
Junxiao Shia60d9362014-11-12 09:38:21 -070028#include "util/dummy-client-face.hpp"
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080029#include "../unit-test-time-fixture.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070030
31namespace ndn {
32namespace nfd {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070033namespace tests {
34
Junxiao Shia60d9362014-11-12 09:38:21 -070035using ndn::util::DummyClientFace;
36using ndn::util::makeDummyClientFace;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070037
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080038BOOST_AUTO_TEST_SUITE(ManagementNfdController)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070039
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080040class CommandFixture : public ndn::tests::UnitTestTimeFixture
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070041{
42protected:
43 CommandFixture()
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080044 : face(makeDummyClientFace(io))
Junxiao Shi415b17c2014-11-12 00:43:25 -070045 , controller(*face, keyChain)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070046 , commandSucceedCallback(bind(&CommandFixture::onCommandSucceed, this, _1))
47 , commandFailCallback(bind(&CommandFixture::onCommandFail, this, _1, _2))
48 {
49 }
50
51private:
52 void
53 onCommandSucceed(const ControlParameters& parameters)
54 {
55 commandSucceedHistory.push_back(boost::make_tuple(parameters));
56 }
57
58 void
59 onCommandFail(uint32_t code, const std::string& reason)
60 {
61 commandFailHistory.push_back(boost::make_tuple(code, reason));
62 }
63
64protected:
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070065 shared_ptr<DummyClientFace> face;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070066 KeyChain keyChain;
Junxiao Shi415b17c2014-11-12 00:43:25 -070067 Controller controller;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070068
69 Controller::CommandSucceedCallback commandSucceedCallback;
70 typedef boost::tuple<ControlParameters> CommandSucceedArgs;
71 std::vector<CommandSucceedArgs> commandSucceedHistory;
72
73 Controller::CommandFailCallback commandFailCallback;
74 typedef boost::tuple<uint32_t,std::string> CommandFailArgs;
75 std::vector<CommandFailArgs> commandFailHistory;
76};
77
78BOOST_FIXTURE_TEST_CASE(CommandSuccess, CommandFixture)
79{
80 ControlParameters parameters;
Junxiao Shi5de006b2014-10-26 20:20:52 -070081 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070082
83 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
Junxiao Shi70911652014-08-12 10:14:24 -070084 parameters,
85 commandSucceedCallback,
86 commandFailCallback));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080087
88 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070089
Junxiao Shia60d9362014-11-12 09:38:21 -070090 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
91 const Interest& requestInterest = face->sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070092
93 FaceCreateCommand command;
Junxiao Shi5de006b2014-10-26 20:20:52 -070094 BOOST_CHECK(Name("/localhost/nfd/faces/create").isPrefixOf(requestInterest.getName()));
95 // 9 components: ndn:/localhost/nfd/faces/create/<parameters>/<signed Interest x4>
Junxiao Shi6a90f372014-10-13 20:29:30 -070096 BOOST_REQUIRE_EQUAL(requestInterest.getName().size(), 9);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070097 ControlParameters request;
98 // 4th component: <parameters>
Junxiao Shi6a90f372014-10-13 20:29:30 -070099 BOOST_REQUIRE_NO_THROW(request.wireDecode(requestInterest.getName().at(4).blockFromValue()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700100 BOOST_CHECK_NO_THROW(command.validateRequest(request));
101 BOOST_CHECK_EQUAL(request.getUri(), parameters.getUri());
Junxiao Shi415b17c2014-11-12 00:43:25 -0700102 BOOST_CHECK_EQUAL(requestInterest.getInterestLifetime(), CommandOptions::DEFAULT_TIMEOUT);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700103
104 ControlParameters responseBody;
105 responseBody.setUri("tcp4://192.0.2.1:6363")
Yukai Tud93c5fc2015-08-25 11:37:16 +0800106 .setFaceId(22)
107 .setFacePersistency(ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700108 ControlResponse responsePayload(201, "created");
109 responsePayload.setBody(responseBody.wireEncode());
110
Junxiao Shi6a90f372014-10-13 20:29:30 -0700111 Data responseData(requestInterest.getName());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700112 responseData.setContent(responsePayload.wireEncode());
113 keyChain.sign(responseData);
114 face->receive(responseData);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800115
116 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700117
118 BOOST_CHECK_EQUAL(commandFailHistory.size(), 0);
119 BOOST_REQUIRE_EQUAL(commandSucceedHistory.size(), 1);
120 const ControlParameters& response = commandSucceedHistory[0].get<0>();
121 BOOST_CHECK_EQUAL(response.getUri(), responseBody.getUri());
122 BOOST_CHECK_EQUAL(response.getFaceId(), responseBody.getFaceId());
123}
124
125BOOST_FIXTURE_TEST_CASE(CommandInvalidRequest, CommandFixture)
126{
127 ControlParameters parameters;
128 parameters.setName("ndn:/should-not-have-this-field");
129 // Uri is missing
130
131 BOOST_CHECK_THROW(controller.start<FaceCreateCommand>(
132 parameters,
133 commandSucceedCallback,
134 commandFailCallback),
135 ControlCommand::ArgumentError);
136}
137
138BOOST_FIXTURE_TEST_CASE(CommandErrorCode, CommandFixture)
139{
140 ControlParameters parameters;
Junxiao Shi5de006b2014-10-26 20:20:52 -0700141 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700142
143 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
144 parameters,
145 commandSucceedCallback,
146 commandFailCallback));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800147 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700148
Junxiao Shia60d9362014-11-12 09:38:21 -0700149 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
150 const Interest& requestInterest = face->sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700151
152 ControlResponse responsePayload(401, "Not Authenticated");
153
Junxiao Shi6a90f372014-10-13 20:29:30 -0700154 Data responseData(requestInterest.getName());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700155 responseData.setContent(responsePayload.wireEncode());
156 keyChain.sign(responseData);
157 face->receive(responseData);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800158 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700159
160 BOOST_CHECK_EQUAL(commandSucceedHistory.size(), 0);
161 BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1);
162 BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), 401);
163}
164
165BOOST_FIXTURE_TEST_CASE(CommandInvalidResponse, CommandFixture)
166{
167 ControlParameters parameters;
Junxiao Shi5de006b2014-10-26 20:20:52 -0700168 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700169
170 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
171 parameters,
172 commandSucceedCallback,
173 commandFailCallback));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800174 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700175
Junxiao Shia60d9362014-11-12 09:38:21 -0700176 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
177 const Interest& requestInterest = face->sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700178
179 ControlParameters responseBody;
180 responseBody.setUri("tcp4://192.0.2.1:6363")
181 .setName("ndn:/should-not-have-this-field");
182 // FaceId is missing
183 ControlResponse responsePayload(201, "created");
184 responsePayload.setBody(responseBody.wireEncode());
185
Junxiao Shi6a90f372014-10-13 20:29:30 -0700186 Data responseData(requestInterest.getName());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700187 responseData.setContent(responsePayload.wireEncode());
188 keyChain.sign(responseData);
189 face->receive(responseData);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800190 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700191
192 BOOST_CHECK_EQUAL(commandSucceedHistory.size(), 0);
193 BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1);
194}
195
Junxiao Shi5de006b2014-10-26 20:20:52 -0700196BOOST_FIXTURE_TEST_CASE(OptionsPrefix, CommandFixture)
197{
198 ControlParameters parameters;
199 parameters.setName("/ndn/com/example");
200 parameters.setFaceId(400);
201
202 CommandOptions options;
203 options.setPrefix("/localhop/net/example/router1/nfd");
204
205 BOOST_CHECK_NO_THROW(controller.start<RibRegisterCommand>(
206 parameters,
207 commandSucceedCallback,
208 commandFailCallback,
209 options));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800210 advanceClocks(time::milliseconds(1));
Junxiao Shi5de006b2014-10-26 20:20:52 -0700211
Junxiao Shia60d9362014-11-12 09:38:21 -0700212 BOOST_REQUIRE_EQUAL(face->sentInterests.size(), 1);
213 const Interest& requestInterest = face->sentInterests[0];
Junxiao Shi5de006b2014-10-26 20:20:52 -0700214
215 FaceCreateCommand command;
216 BOOST_CHECK(Name("/localhop/net/example/router1/nfd/rib/register").isPrefixOf(
217 requestInterest.getName()));
218}
219
220BOOST_FIXTURE_TEST_CASE(OptionsTimeout, CommandFixture)
221{
222 ControlParameters parameters;
223 parameters.setUri("tcp4://192.0.2.1:6363");
224
225 CommandOptions options;
226 options.setTimeout(time::milliseconds(50));
227
228 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
229 parameters,
230 commandSucceedCallback,
231 commandFailCallback,
232 options));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800233 advanceClocks(time::milliseconds(1), 101); // Face's PIT granularity is 100ms
Junxiao Shi5de006b2014-10-26 20:20:52 -0700234
235 BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1);
236 BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), Controller::ERROR_TIMEOUT);
237}
238
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700239BOOST_AUTO_TEST_SUITE_END()
240
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700241} // namespace tests
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700242} // namespace nfd
243} // namespace ndn