blob: c2e2b6c6157fa470ac83ce5b3fb540e0ff8d0f10 [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/**
Junxiao Shi034c1882016-06-24 18:06:51 +00003 * Copyright (c) 2013-2016 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
Junxiao Shi034c1882016-06-24 18:06:51 +000027#include "nfd-controller-fixture.hpp"
Junxiao Shib1990df2015-11-05 00:14:44 +000028#include "../make-interest-data.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070029
30namespace ndn {
31namespace nfd {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070032namespace tests {
33
Junxiao Shi034c1882016-06-24 18:06:51 +000034using ndn::util::makeData;
35using ndn::util::makeNack;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070036
Junxiao Shib1990df2015-11-05 00:14:44 +000037BOOST_AUTO_TEST_SUITE(Management)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070038
Junxiao Shi034c1882016-06-24 18:06:51 +000039class CommandFixture : public ControllerFixture
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070040{
41protected:
42 CommandFixture()
Junxiao Shi034c1882016-06-24 18:06:51 +000043 : succeedCallback(bind(&CommandFixture::succeed, this, _1))
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070044 {
45 }
46
47private:
48 void
Junxiao Shi034c1882016-06-24 18:06:51 +000049 succeed(const ControlParameters& parameters)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070050 {
Junxiao Shi034c1882016-06-24 18:06:51 +000051 succeeds.push_back(parameters);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070052 }
53
54protected:
Junxiao Shi034c1882016-06-24 18:06:51 +000055 Controller::CommandSucceedCallback succeedCallback;
56 std::vector<ControlParameters> succeeds;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070057};
58
Junxiao Shib1990df2015-11-05 00:14:44 +000059BOOST_FIXTURE_TEST_SUITE(TestNfdController, CommandFixture)
60
61BOOST_AUTO_TEST_CASE(CommandSuccess)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070062{
63 ControlParameters parameters;
Junxiao Shi5de006b2014-10-26 20:20:52 -070064 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070065
66 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
Junxiao Shi034c1882016-06-24 18:06:51 +000067 parameters, succeedCallback, failCallback));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080068 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070069
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080070 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
71 const Interest& requestInterest = face.sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070072
73 FaceCreateCommand command;
Junxiao Shi5de006b2014-10-26 20:20:52 -070074 BOOST_CHECK(Name("/localhost/nfd/faces/create").isPrefixOf(requestInterest.getName()));
75 // 9 components: ndn:/localhost/nfd/faces/create/<parameters>/<signed Interest x4>
Junxiao Shi6a90f372014-10-13 20:29:30 -070076 BOOST_REQUIRE_EQUAL(requestInterest.getName().size(), 9);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070077 ControlParameters request;
78 // 4th component: <parameters>
Junxiao Shi6a90f372014-10-13 20:29:30 -070079 BOOST_REQUIRE_NO_THROW(request.wireDecode(requestInterest.getName().at(4).blockFromValue()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070080 BOOST_CHECK_NO_THROW(command.validateRequest(request));
81 BOOST_CHECK_EQUAL(request.getUri(), parameters.getUri());
Junxiao Shi415b17c2014-11-12 00:43:25 -070082 BOOST_CHECK_EQUAL(requestInterest.getInterestLifetime(), CommandOptions::DEFAULT_TIMEOUT);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070083
84 ControlParameters responseBody;
85 responseBody.setUri("tcp4://192.0.2.1:6363")
Yukai Tud93c5fc2015-08-25 11:37:16 +080086 .setFaceId(22)
87 .setFacePersistency(ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070088 ControlResponse responsePayload(201, "created");
89 responsePayload.setBody(responseBody.wireEncode());
90
Junxiao Shi034c1882016-06-24 18:06:51 +000091 auto responseData = makeData(requestInterest.getName());
Junxiao Shib1990df2015-11-05 00:14:44 +000092 responseData->setContent(responsePayload.wireEncode());
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080093 face.receive(*responseData);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -080094
95 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070096
Junxiao Shi034c1882016-06-24 18:06:51 +000097 BOOST_CHECK_EQUAL(failCodes.size(), 0);
98 BOOST_REQUIRE_EQUAL(succeeds.size(), 1);
99 BOOST_CHECK_EQUAL(succeeds.back().getUri(), responseBody.getUri());
100 BOOST_CHECK_EQUAL(succeeds.back().getFaceId(), responseBody.getFaceId());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700101}
102
Junxiao Shib1990df2015-11-05 00:14:44 +0000103BOOST_AUTO_TEST_CASE(CommandInvalidRequest)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700104{
105 ControlParameters parameters;
106 parameters.setName("ndn:/should-not-have-this-field");
107 // Uri is missing
108
109 BOOST_CHECK_THROW(controller.start<FaceCreateCommand>(
Junxiao Shi034c1882016-06-24 18:06:51 +0000110 parameters, succeedCallback, failCallback),
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700111 ControlCommand::ArgumentError);
112}
113
Junxiao Shib1990df2015-11-05 00:14:44 +0000114BOOST_AUTO_TEST_CASE(CommandErrorCode)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700115{
116 ControlParameters parameters;
Junxiao Shi5de006b2014-10-26 20:20:52 -0700117 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700118
119 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
Junxiao Shi034c1882016-06-24 18:06:51 +0000120 parameters, succeedCallback, failCallback));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800121 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700122
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800123 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
124 const Interest& requestInterest = face.sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700125
126 ControlResponse responsePayload(401, "Not Authenticated");
127
Junxiao Shi034c1882016-06-24 18:06:51 +0000128 auto responseData = makeData(requestInterest.getName());
Junxiao Shib1990df2015-11-05 00:14:44 +0000129 responseData->setContent(responsePayload.wireEncode());
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800130 face.receive(*responseData);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800131 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700132
Junxiao Shi034c1882016-06-24 18:06:51 +0000133 BOOST_CHECK_EQUAL(succeeds.size(), 0);
134 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
135 BOOST_CHECK_EQUAL(failCodes.back(), 401);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700136}
137
Junxiao Shib1990df2015-11-05 00:14:44 +0000138BOOST_AUTO_TEST_CASE(CommandInvalidResponse)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700139{
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>(
Junxiao Shi034c1882016-06-24 18:06:51 +0000144 parameters, succeedCallback, failCallback));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800145 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700146
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800147 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
148 const Interest& requestInterest = face.sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700149
150 ControlParameters responseBody;
151 responseBody.setUri("tcp4://192.0.2.1:6363")
152 .setName("ndn:/should-not-have-this-field");
153 // FaceId is missing
154 ControlResponse responsePayload(201, "created");
155 responsePayload.setBody(responseBody.wireEncode());
156
Junxiao Shi034c1882016-06-24 18:06:51 +0000157 auto responseData = makeData(requestInterest.getName());
Junxiao Shib1990df2015-11-05 00:14:44 +0000158 responseData->setContent(responsePayload.wireEncode());
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800159 face.receive(*responseData);
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800160 advanceClocks(time::milliseconds(1));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700161
Junxiao Shi034c1882016-06-24 18:06:51 +0000162 BOOST_CHECK_EQUAL(succeeds.size(), 0);
163 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700164}
165
Junxiao Shib1990df2015-11-05 00:14:44 +0000166BOOST_AUTO_TEST_CASE(CommandNack)
167{
168 ControlParameters parameters;
169 parameters.setUri("tcp4://192.0.2.1:6363");
170
171 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
Junxiao Shi034c1882016-06-24 18:06:51 +0000172 parameters, succeedCallback, failCallback));
Junxiao Shib1990df2015-11-05 00:14:44 +0000173 advanceClocks(time::milliseconds(1));
174
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800175 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
176 const Interest& requestInterest = face.sentInterests[0];
Junxiao Shib1990df2015-11-05 00:14:44 +0000177
Junxiao Shi034c1882016-06-24 18:06:51 +0000178 auto responseNack = makeNack(requestInterest, lp::NackReason::NO_ROUTE);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800179 face.receive(responseNack);
Junxiao Shib1990df2015-11-05 00:14:44 +0000180 advanceClocks(time::milliseconds(1));
181
Junxiao Shi034c1882016-06-24 18:06:51 +0000182 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
183 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_NACK);
Junxiao Shib1990df2015-11-05 00:14:44 +0000184}
185
186BOOST_AUTO_TEST_CASE(OptionsPrefix)
Junxiao Shi5de006b2014-10-26 20:20:52 -0700187{
188 ControlParameters parameters;
189 parameters.setName("/ndn/com/example");
190 parameters.setFaceId(400);
191
192 CommandOptions options;
193 options.setPrefix("/localhop/net/example/router1/nfd");
194
195 BOOST_CHECK_NO_THROW(controller.start<RibRegisterCommand>(
Junxiao Shi034c1882016-06-24 18:06:51 +0000196 parameters, succeedCallback, failCallback, options));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800197 advanceClocks(time::milliseconds(1));
Junxiao Shi5de006b2014-10-26 20:20:52 -0700198
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800199 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
200 const Interest& requestInterest = face.sentInterests[0];
Junxiao Shi5de006b2014-10-26 20:20:52 -0700201
202 FaceCreateCommand command;
203 BOOST_CHECK(Name("/localhop/net/example/router1/nfd/rib/register").isPrefixOf(
204 requestInterest.getName()));
205}
206
Junxiao Shib1990df2015-11-05 00:14:44 +0000207BOOST_AUTO_TEST_CASE(OptionsTimeout)
Junxiao Shi5de006b2014-10-26 20:20:52 -0700208{
209 ControlParameters parameters;
210 parameters.setUri("tcp4://192.0.2.1:6363");
211
212 CommandOptions options;
213 options.setTimeout(time::milliseconds(50));
214
215 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
Junxiao Shi034c1882016-06-24 18:06:51 +0000216 parameters, succeedCallback, failCallback, options));
Alexander Afanasyevd3a55b22014-11-18 19:23:28 -0800217 advanceClocks(time::milliseconds(1), 101); // Face's PIT granularity is 100ms
Junxiao Shi5de006b2014-10-26 20:20:52 -0700218
Junxiao Shi034c1882016-06-24 18:06:51 +0000219 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
220 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_TIMEOUT);
Junxiao Shi5de006b2014-10-26 20:20:52 -0700221}
222
Junxiao Shib1990df2015-11-05 00:14:44 +0000223BOOST_AUTO_TEST_SUITE_END() // TestController
224BOOST_AUTO_TEST_SUITE_END() // Management
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700225
Junxiao Shi034c1882016-06-24 18:06:51 +0000226// Controller::fetch<Dataset> has a separate test suite in nfd-status-dataset.t.cpp
227
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700228} // namespace tests
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700229} // namespace nfd
230} // namespace ndn