blob: 83deb234495d1806c9cc7da8913e28e066fe7310 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2bea5c42017-08-14 20:10:32 +00002/*
Davide Pesavento2e481fc2021-07-02 18:20:03 -04003 * Copyright (c) 2013-2021 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/mgmt/nfd/controller.hpp"
23#include "ndn-cxx/mgmt/nfd/control-response.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070024
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050025#include "tests/test-common.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050026#include "tests/unit/mgmt/nfd/controller-fixture.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070027
28namespace ndn {
29namespace nfd {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070030namespace tests {
31
Junxiao Shi85d90832016-08-04 03:19:46 +000032using namespace ndn::tests;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070033
Junxiao Shi7357ef22016-09-07 02:39:37 +000034BOOST_AUTO_TEST_SUITE(Mgmt)
35BOOST_AUTO_TEST_SUITE(Nfd)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070036
Junxiao Shi034c1882016-06-24 18:06:51 +000037class CommandFixture : public ControllerFixture
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070038{
39protected:
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +000040 void
41 respond(const ControlResponse& responsePayload)
42 {
43 auto responseData = makeData(face.sentInterests.at(0).getName());
44 responseData->setContent(responsePayload.wireEncode());
45 face.receive(*responseData);
Davide Pesavento0f830802018-01-16 23:58:58 -050046 this->advanceClocks(1_ms);
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +000047 }
48
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070049protected:
Davide Pesavento2e481fc2021-07-02 18:20:03 -040050 Controller::CommandSucceedCallback succeedCallback = [this] (const auto& params) {
51 succeeds.push_back(params);
52 };
Junxiao Shi034c1882016-06-24 18:06:51 +000053 std::vector<ControlParameters> succeeds;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070054};
55
Junxiao Shi600f7112016-07-16 11:57:18 +000056// This test suite focuses on ControlCommand functionality of Controller.
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050057// Individual commands are tested in control-command.t.cpp
58// StatusDataset functionality is tested in status-dataset.t.cpp
Junxiao Shi7357ef22016-09-07 02:39:37 +000059BOOST_FIXTURE_TEST_SUITE(TestController, CommandFixture)
Junxiao Shib1990df2015-11-05 00:14:44 +000060
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +000061static ControlParameters
62makeFaceCreateResponse()
63{
64 ControlParameters resp;
65 resp.setFaceId(22)
66 .setUri("tcp4://192.0.2.1:6363")
67 .setLocalUri("tcp4://192.0.2.2:10847")
68 .setFacePersistency(ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT)
Eric Newberryd567aab2018-01-27 16:38:24 -070069 .setFlags(0x7);
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +000070 return resp;
71}
72
Junxiao Shi600f7112016-07-16 11:57:18 +000073BOOST_AUTO_TEST_CASE(Success)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070074{
75 ControlParameters parameters;
Junxiao Shi5de006b2014-10-26 20:20:52 -070076 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070077
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +000078 controller.start<FaceCreateCommand>(parameters, succeedCallback, commandFailCallback);
Davide Pesavento0f830802018-01-16 23:58:58 -050079 this->advanceClocks(1_ms);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070080
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -080081 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
82 const Interest& requestInterest = face.sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070083
84 FaceCreateCommand command;
Junxiao Shi5de006b2014-10-26 20:20:52 -070085 BOOST_CHECK(Name("/localhost/nfd/faces/create").isPrefixOf(requestInterest.getName()));
86 // 9 components: ndn:/localhost/nfd/faces/create/<parameters>/<signed Interest x4>
Junxiao Shi6a90f372014-10-13 20:29:30 -070087 BOOST_REQUIRE_EQUAL(requestInterest.getName().size(), 9);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070088 ControlParameters request;
89 // 4th component: <parameters>
Junxiao Shi6a90f372014-10-13 20:29:30 -070090 BOOST_REQUIRE_NO_THROW(request.wireDecode(requestInterest.getName().at(4).blockFromValue()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070091 BOOST_CHECK_NO_THROW(command.validateRequest(request));
92 BOOST_CHECK_EQUAL(request.getUri(), parameters.getUri());
Junxiao Shi415b17c2014-11-12 00:43:25 -070093 BOOST_CHECK_EQUAL(requestInterest.getInterestLifetime(), CommandOptions::DEFAULT_TIMEOUT);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070094
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +000095 ControlParameters responseBody = makeFaceCreateResponse();
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070096 ControlResponse responsePayload(201, "created");
97 responsePayload.setBody(responseBody.wireEncode());
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +000098 this->respond(responsePayload);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070099
Junxiao Shi034c1882016-06-24 18:06:51 +0000100 BOOST_CHECK_EQUAL(failCodes.size(), 0);
101 BOOST_REQUIRE_EQUAL(succeeds.size(), 1);
102 BOOST_CHECK_EQUAL(succeeds.back().getUri(), responseBody.getUri());
103 BOOST_CHECK_EQUAL(succeeds.back().getFaceId(), responseBody.getFaceId());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700104}
105
Junxiao Shi600f7112016-07-16 11:57:18 +0000106BOOST_AUTO_TEST_CASE(SuccessNoCallback)
107{
108 ControlParameters parameters;
109 parameters.setUri("tcp4://192.0.2.1:6363");
110
Junxiao Shie7c7f152016-08-20 22:36:22 +0000111 controller.start<FaceCreateCommand>(parameters, nullptr, commandFailCallback);
Davide Pesavento0f830802018-01-16 23:58:58 -0500112 this->advanceClocks(1_ms);
Junxiao Shi600f7112016-07-16 11:57:18 +0000113
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000114 ControlParameters responseBody = makeFaceCreateResponse();
Junxiao Shi600f7112016-07-16 11:57:18 +0000115 ControlResponse responsePayload(201, "created");
116 responsePayload.setBody(responseBody.wireEncode());
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000117 this->respond(responsePayload);
Junxiao Shi600f7112016-07-16 11:57:18 +0000118
119 BOOST_CHECK_EQUAL(failCodes.size(), 0);
120}
121
122BOOST_AUTO_TEST_CASE(OptionsPrefix)
123{
124 ControlParameters parameters;
125 parameters.setName("/ndn/com/example");
126 parameters.setFaceId(400);
127
128 CommandOptions options;
129 options.setPrefix("/localhop/net/example/router1/nfd");
130
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000131 controller.start<RibRegisterCommand>(parameters, succeedCallback, commandFailCallback, options);
Davide Pesavento0f830802018-01-16 23:58:58 -0500132 this->advanceClocks(1_ms);
Junxiao Shi600f7112016-07-16 11:57:18 +0000133
134 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
135 const Interest& requestInterest = face.sentInterests[0];
136
137 FaceCreateCommand command;
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000138 BOOST_CHECK(Name("/localhop/net/example/router1/nfd/rib/register").isPrefixOf(requestInterest.getName()));
Junxiao Shi600f7112016-07-16 11:57:18 +0000139}
140
141BOOST_AUTO_TEST_CASE(InvalidRequest)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700142{
143 ControlParameters parameters;
144 parameters.setName("ndn:/should-not-have-this-field");
145 // Uri is missing
146
147 BOOST_CHECK_THROW(controller.start<FaceCreateCommand>(
Junxiao Shie7c7f152016-08-20 22:36:22 +0000148 parameters, succeedCallback, commandFailCallback),
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700149 ControlCommand::ArgumentError);
150}
151
Junxiao Shi54f727d2016-08-08 20:29:11 +0000152BOOST_AUTO_TEST_CASE(ValidationFailure)
153{
154 this->setValidationResult(false);
155
156 ControlParameters parameters;
157 parameters.setUri("tcp4://192.0.2.1:6363");
158
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000159 controller.start<FaceCreateCommand>(parameters, succeedCallback, commandFailCallback);
Davide Pesavento0f830802018-01-16 23:58:58 -0500160 this->advanceClocks(1_ms);
Junxiao Shi54f727d2016-08-08 20:29:11 +0000161
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000162 ControlParameters responseBody = makeFaceCreateResponse();
Junxiao Shi54f727d2016-08-08 20:29:11 +0000163 ControlResponse responsePayload(201, "created");
164 responsePayload.setBody(responseBody.wireEncode());
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000165 this->respond(responsePayload);
Junxiao Shi54f727d2016-08-08 20:29:11 +0000166
167 BOOST_CHECK_EQUAL(succeeds.size(), 0);
168 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
169 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_VALIDATION);
170}
171
Junxiao Shi600f7112016-07-16 11:57:18 +0000172BOOST_AUTO_TEST_CASE(ErrorCode)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700173{
174 ControlParameters parameters;
Junxiao Shi5de006b2014-10-26 20:20:52 -0700175 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700176
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000177 controller.start<FaceCreateCommand>(parameters, succeedCallback, commandFailCallback);
Davide Pesavento0f830802018-01-16 23:58:58 -0500178 this->advanceClocks(1_ms);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700179
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700180 ControlResponse responsePayload(401, "Not Authenticated");
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000181 this->respond(responsePayload);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700182
Junxiao Shi034c1882016-06-24 18:06:51 +0000183 BOOST_CHECK_EQUAL(succeeds.size(), 0);
184 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
185 BOOST_CHECK_EQUAL(failCodes.back(), 401);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700186}
187
Junxiao Shi600f7112016-07-16 11:57:18 +0000188BOOST_AUTO_TEST_CASE(InvalidResponse)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700189{
190 ControlParameters parameters;
Junxiao Shi5de006b2014-10-26 20:20:52 -0700191 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700192
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000193 controller.start<FaceCreateCommand>(parameters, succeedCallback, commandFailCallback);
Davide Pesavento0f830802018-01-16 23:58:58 -0500194 this->advanceClocks(1_ms);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700195
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000196 ControlParameters responseBody = makeFaceCreateResponse();
197 responseBody.unsetFaceId() // FaceId is missing
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700198 .setName("ndn:/should-not-have-this-field");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700199 ControlResponse responsePayload(201, "created");
200 responsePayload.setBody(responseBody.wireEncode());
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000201 this->respond(responsePayload);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700202
Junxiao Shi034c1882016-06-24 18:06:51 +0000203 BOOST_CHECK_EQUAL(succeeds.size(), 0);
204 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700205}
206
Junxiao Shi600f7112016-07-16 11:57:18 +0000207BOOST_AUTO_TEST_CASE(Nack)
Junxiao Shib1990df2015-11-05 00:14:44 +0000208{
209 ControlParameters parameters;
210 parameters.setUri("tcp4://192.0.2.1:6363");
211
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000212 controller.start<FaceCreateCommand>(parameters, succeedCallback, commandFailCallback);
Davide Pesavento0f830802018-01-16 23:58:58 -0500213 this->advanceClocks(1_ms);
Junxiao Shib1990df2015-11-05 00:14:44 +0000214
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800215 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
216 const Interest& requestInterest = face.sentInterests[0];
Junxiao Shib1990df2015-11-05 00:14:44 +0000217
Junxiao Shi034c1882016-06-24 18:06:51 +0000218 auto responseNack = makeNack(requestInterest, lp::NackReason::NO_ROUTE);
Alexander Afanasyev9bdbb832015-12-30 12:54:22 -0800219 face.receive(responseNack);
Davide Pesavento0f830802018-01-16 23:58:58 -0500220 this->advanceClocks(1_ms);
Junxiao Shib1990df2015-11-05 00:14:44 +0000221
Junxiao Shi034c1882016-06-24 18:06:51 +0000222 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
223 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_NACK);
Junxiao Shib1990df2015-11-05 00:14:44 +0000224}
225
Junxiao Shi600f7112016-07-16 11:57:18 +0000226BOOST_AUTO_TEST_CASE(Timeout)
Junxiao Shi5de006b2014-10-26 20:20:52 -0700227{
228 ControlParameters parameters;
229 parameters.setUri("tcp4://192.0.2.1:6363");
230
231 CommandOptions options;
Davide Pesavento0f830802018-01-16 23:58:58 -0500232 options.setTimeout(50_ms);
Junxiao Shi5de006b2014-10-26 20:20:52 -0700233
Junxiao Shi8c2ab2e2017-05-05 20:26:34 +0000234 controller.start<FaceCreateCommand>(parameters, succeedCallback, commandFailCallback, options);
Davide Pesavento0f830802018-01-16 23:58:58 -0500235 this->advanceClocks(1_ms); // express Interest
236 this->advanceClocks(51_ms); // timeout
Junxiao Shi5de006b2014-10-26 20:20:52 -0700237
Junxiao Shi034c1882016-06-24 18:06:51 +0000238 BOOST_REQUIRE_EQUAL(failCodes.size(), 1);
239 BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_TIMEOUT);
Junxiao Shi5de006b2014-10-26 20:20:52 -0700240}
241
Junxiao Shi600f7112016-07-16 11:57:18 +0000242BOOST_AUTO_TEST_CASE(FailureNoCallback)
243{
244 ControlParameters parameters;
245 parameters.setUri("tcp4://192.0.2.1:6363");
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700246
Junxiao Shi600f7112016-07-16 11:57:18 +0000247 CommandOptions options;
Davide Pesavento0f830802018-01-16 23:58:58 -0500248 options.setTimeout(50_ms);
Junxiao Shi600f7112016-07-16 11:57:18 +0000249
250 controller.start<FaceCreateCommand>(parameters, succeedCallback, nullptr, options);
Davide Pesavento0f830802018-01-16 23:58:58 -0500251 this->advanceClocks(1_ms); // express Interest
252 this->advanceClocks(51_ms); // timeout
Junxiao Shi600f7112016-07-16 11:57:18 +0000253
254 BOOST_CHECK_EQUAL(succeeds.size(), 0);
255}
256
Junxiao Shi7357ef22016-09-07 02:39:37 +0000257BOOST_AUTO_TEST_SUITE_END() // TestController
258BOOST_AUTO_TEST_SUITE_END() // Nfd
259BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shi034c1882016-06-24 18:06:51 +0000260
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700261} // namespace tests
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700262} // namespace nfd
263} // namespace ndn