Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 2 | /** |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "management/nfd-controller.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 23 | #include "management/nfd-control-response.hpp" |
| 24 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 25 | #include <boost/tuple/tuple.hpp> |
| 26 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 27 | #include "nfd-controller-fixture.hpp" |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 28 | #include "../make-interest-data.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace nfd { |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 32 | namespace tests { |
| 33 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 34 | using ndn::util::makeData; |
| 35 | using ndn::util::makeNack; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 37 | BOOST_AUTO_TEST_SUITE(Management) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 38 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 39 | class CommandFixture : public ControllerFixture |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 40 | { |
| 41 | protected: |
| 42 | CommandFixture() |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 43 | : succeedCallback(bind(&CommandFixture::succeed, this, _1)) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
| 47 | private: |
| 48 | void |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 49 | succeed(const ControlParameters& parameters) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 50 | { |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 51 | succeeds.push_back(parameters); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | protected: |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 55 | Controller::CommandSucceedCallback succeedCallback; |
| 56 | std::vector<ControlParameters> succeeds; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 57 | }; |
| 58 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 59 | BOOST_FIXTURE_TEST_SUITE(TestNfdController, CommandFixture) |
| 60 | |
| 61 | BOOST_AUTO_TEST_CASE(CommandSuccess) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 62 | { |
| 63 | ControlParameters parameters; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 64 | parameters.setUri("tcp4://192.0.2.1:6363"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 65 | |
| 66 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 67 | parameters, succeedCallback, failCallback)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 68 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 70 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 71 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 72 | |
| 73 | FaceCreateCommand command; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 74 | BOOST_CHECK(Name("/localhost/nfd/faces/create").isPrefixOf(requestInterest.getName())); |
| 75 | // 9 components: ndn:/localhost/nfd/faces/create/<parameters>/<signed Interest x4> |
Junxiao Shi | 6a90f37 | 2014-10-13 20:29:30 -0700 | [diff] [blame] | 76 | BOOST_REQUIRE_EQUAL(requestInterest.getName().size(), 9); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 77 | ControlParameters request; |
| 78 | // 4th component: <parameters> |
Junxiao Shi | 6a90f37 | 2014-10-13 20:29:30 -0700 | [diff] [blame] | 79 | BOOST_REQUIRE_NO_THROW(request.wireDecode(requestInterest.getName().at(4).blockFromValue())); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 80 | BOOST_CHECK_NO_THROW(command.validateRequest(request)); |
| 81 | BOOST_CHECK_EQUAL(request.getUri(), parameters.getUri()); |
Junxiao Shi | 415b17c | 2014-11-12 00:43:25 -0700 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(requestInterest.getInterestLifetime(), CommandOptions::DEFAULT_TIMEOUT); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 83 | |
| 84 | ControlParameters responseBody; |
| 85 | responseBody.setUri("tcp4://192.0.2.1:6363") |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 86 | .setFaceId(22) |
| 87 | .setFacePersistency(ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 88 | ControlResponse responsePayload(201, "created"); |
| 89 | responsePayload.setBody(responseBody.wireEncode()); |
| 90 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 91 | auto responseData = makeData(requestInterest.getName()); |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 92 | responseData->setContent(responsePayload.wireEncode()); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 93 | face.receive(*responseData); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 94 | |
| 95 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 96 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 97 | 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 Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 103 | BOOST_AUTO_TEST_CASE(CommandInvalidRequest) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 104 | { |
| 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 Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 110 | parameters, succeedCallback, failCallback), |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 111 | ControlCommand::ArgumentError); |
| 112 | } |
| 113 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 114 | BOOST_AUTO_TEST_CASE(CommandErrorCode) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 115 | { |
| 116 | ControlParameters parameters; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 117 | parameters.setUri("tcp4://192.0.2.1:6363"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 118 | |
| 119 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 120 | parameters, succeedCallback, failCallback)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 121 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 123 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 124 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 125 | |
| 126 | ControlResponse responsePayload(401, "Not Authenticated"); |
| 127 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 128 | auto responseData = makeData(requestInterest.getName()); |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 129 | responseData->setContent(responsePayload.wireEncode()); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 130 | face.receive(*responseData); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 131 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 132 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 133 | BOOST_CHECK_EQUAL(succeeds.size(), 0); |
| 134 | BOOST_REQUIRE_EQUAL(failCodes.size(), 1); |
| 135 | BOOST_CHECK_EQUAL(failCodes.back(), 401); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 138 | BOOST_AUTO_TEST_CASE(CommandInvalidResponse) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 139 | { |
| 140 | ControlParameters parameters; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 141 | parameters.setUri("tcp4://192.0.2.1:6363"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 142 | |
| 143 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 144 | parameters, succeedCallback, failCallback)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 145 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 146 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 147 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 148 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 149 | |
| 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 Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 157 | auto responseData = makeData(requestInterest.getName()); |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 158 | responseData->setContent(responsePayload.wireEncode()); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 159 | face.receive(*responseData); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 160 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 161 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(succeeds.size(), 0); |
| 163 | BOOST_REQUIRE_EQUAL(failCodes.size(), 1); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 166 | BOOST_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 Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 172 | parameters, succeedCallback, failCallback)); |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 173 | advanceClocks(time::milliseconds(1)); |
| 174 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 175 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 176 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 177 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 178 | auto responseNack = makeNack(requestInterest, lp::NackReason::NO_ROUTE); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 179 | face.receive(responseNack); |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 180 | advanceClocks(time::milliseconds(1)); |
| 181 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 182 | BOOST_REQUIRE_EQUAL(failCodes.size(), 1); |
| 183 | BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_NACK); |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | BOOST_AUTO_TEST_CASE(OptionsPrefix) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 187 | { |
| 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 Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 196 | parameters, succeedCallback, failCallback, options)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 197 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 199 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 200 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 201 | |
| 202 | FaceCreateCommand command; |
| 203 | BOOST_CHECK(Name("/localhop/net/example/router1/nfd/rib/register").isPrefixOf( |
| 204 | requestInterest.getName())); |
| 205 | } |
| 206 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 207 | BOOST_AUTO_TEST_CASE(OptionsTimeout) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 208 | { |
| 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 Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 216 | parameters, succeedCallback, failCallback, options)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 217 | advanceClocks(time::milliseconds(1), 101); // Face's PIT granularity is 100ms |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 218 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 219 | BOOST_REQUIRE_EQUAL(failCodes.size(), 1); |
| 220 | BOOST_CHECK_EQUAL(failCodes.back(), Controller::ERROR_TIMEOUT); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 223 | BOOST_AUTO_TEST_SUITE_END() // TestController |
| 224 | BOOST_AUTO_TEST_SUITE_END() // Management |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 225 | |
Junxiao Shi | 034c188 | 2016-06-24 18:06:51 +0000 | [diff] [blame] | 226 | // Controller::fetch<Dataset> has a separate test suite in nfd-status-dataset.t.cpp |
| 227 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 228 | } // namespace tests |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 229 | } // namespace nfd |
| 230 | } // namespace ndn |