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 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 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 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 27 | #include "boost-test.hpp" |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 28 | #include "util/dummy-client-face.hpp" |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 29 | #include "../make-interest-data.hpp" |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 30 | #include "../unit-test-time-fixture.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 31 | |
| 32 | namespace ndn { |
| 33 | namespace nfd { |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 34 | namespace tests { |
| 35 | |
Junxiao Shi | a60d936 | 2014-11-12 09:38:21 -0700 | [diff] [blame] | 36 | using ndn::util::DummyClientFace; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 38 | BOOST_AUTO_TEST_SUITE(Management) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 39 | |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 40 | class CommandFixture : public ndn::tests::UnitTestTimeFixture |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 41 | { |
| 42 | protected: |
| 43 | CommandFixture() |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 44 | : face(io) |
| 45 | , controller(face, keyChain) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 46 | , commandSucceedCallback(bind(&CommandFixture::onCommandSucceed, this, _1)) |
| 47 | , commandFailCallback(bind(&CommandFixture::onCommandFail, this, _1, _2)) |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | private: |
| 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 | |
| 64 | protected: |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 65 | DummyClientFace face; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 66 | KeyChain keyChain; |
Junxiao Shi | 415b17c | 2014-11-12 00:43:25 -0700 | [diff] [blame] | 67 | Controller controller; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 68 | |
| 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 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 78 | BOOST_FIXTURE_TEST_SUITE(TestNfdController, CommandFixture) |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(CommandSuccess) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 81 | { |
| 82 | ControlParameters parameters; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 83 | parameters.setUri("tcp4://192.0.2.1:6363"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 84 | |
| 85 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
Junxiao Shi | 7091165 | 2014-08-12 10:14:24 -0700 | [diff] [blame] | 86 | parameters, |
| 87 | commandSucceedCallback, |
| 88 | commandFailCallback)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 89 | |
| 90 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 92 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 93 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 94 | |
| 95 | FaceCreateCommand command; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 96 | BOOST_CHECK(Name("/localhost/nfd/faces/create").isPrefixOf(requestInterest.getName())); |
| 97 | // 9 components: ndn:/localhost/nfd/faces/create/<parameters>/<signed Interest x4> |
Junxiao Shi | 6a90f37 | 2014-10-13 20:29:30 -0700 | [diff] [blame] | 98 | BOOST_REQUIRE_EQUAL(requestInterest.getName().size(), 9); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 99 | ControlParameters request; |
| 100 | // 4th component: <parameters> |
Junxiao Shi | 6a90f37 | 2014-10-13 20:29:30 -0700 | [diff] [blame] | 101 | BOOST_REQUIRE_NO_THROW(request.wireDecode(requestInterest.getName().at(4).blockFromValue())); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 102 | BOOST_CHECK_NO_THROW(command.validateRequest(request)); |
| 103 | BOOST_CHECK_EQUAL(request.getUri(), parameters.getUri()); |
Junxiao Shi | 415b17c | 2014-11-12 00:43:25 -0700 | [diff] [blame] | 104 | BOOST_CHECK_EQUAL(requestInterest.getInterestLifetime(), CommandOptions::DEFAULT_TIMEOUT); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 105 | |
| 106 | ControlParameters responseBody; |
| 107 | responseBody.setUri("tcp4://192.0.2.1:6363") |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 108 | .setFaceId(22) |
| 109 | .setFacePersistency(ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 110 | ControlResponse responsePayload(201, "created"); |
| 111 | responsePayload.setBody(responseBody.wireEncode()); |
| 112 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 113 | auto responseData = util::makeData(requestInterest.getName()); |
| 114 | responseData->setContent(responsePayload.wireEncode()); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 115 | face.receive(*responseData); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 116 | |
| 117 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 118 | |
| 119 | BOOST_CHECK_EQUAL(commandFailHistory.size(), 0); |
| 120 | BOOST_REQUIRE_EQUAL(commandSucceedHistory.size(), 1); |
| 121 | const ControlParameters& response = commandSucceedHistory[0].get<0>(); |
| 122 | BOOST_CHECK_EQUAL(response.getUri(), responseBody.getUri()); |
| 123 | BOOST_CHECK_EQUAL(response.getFaceId(), responseBody.getFaceId()); |
| 124 | } |
| 125 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(CommandInvalidRequest) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 127 | { |
| 128 | ControlParameters parameters; |
| 129 | parameters.setName("ndn:/should-not-have-this-field"); |
| 130 | // Uri is missing |
| 131 | |
| 132 | BOOST_CHECK_THROW(controller.start<FaceCreateCommand>( |
| 133 | parameters, |
| 134 | commandSucceedCallback, |
| 135 | commandFailCallback), |
| 136 | ControlCommand::ArgumentError); |
| 137 | } |
| 138 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 139 | BOOST_AUTO_TEST_CASE(CommandErrorCode) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 140 | { |
| 141 | ControlParameters parameters; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 142 | parameters.setUri("tcp4://192.0.2.1:6363"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 143 | |
| 144 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
| 145 | parameters, |
| 146 | commandSucceedCallback, |
| 147 | commandFailCallback)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 148 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 150 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 151 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 152 | |
| 153 | ControlResponse responsePayload(401, "Not Authenticated"); |
| 154 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 155 | auto responseData = util::makeData(requestInterest.getName()); |
| 156 | responseData->setContent(responsePayload.wireEncode()); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 157 | face.receive(*responseData); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 158 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 159 | |
| 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 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 165 | BOOST_AUTO_TEST_CASE(CommandInvalidResponse) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 166 | { |
| 167 | ControlParameters parameters; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 168 | parameters.setUri("tcp4://192.0.2.1:6363"); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 169 | |
| 170 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
| 171 | parameters, |
| 172 | commandSucceedCallback, |
| 173 | commandFailCallback)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 174 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 176 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 177 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 178 | |
| 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 Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 186 | auto responseData = util::makeData(requestInterest.getName()); |
| 187 | responseData->setContent(responsePayload.wireEncode()); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 188 | face.receive(*responseData); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 189 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 190 | |
| 191 | BOOST_CHECK_EQUAL(commandSucceedHistory.size(), 0); |
| 192 | BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1); |
| 193 | } |
| 194 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 195 | BOOST_AUTO_TEST_CASE(CommandNack) |
| 196 | { |
| 197 | ControlParameters parameters; |
| 198 | parameters.setUri("tcp4://192.0.2.1:6363"); |
| 199 | |
| 200 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
| 201 | parameters, |
| 202 | commandSucceedCallback, |
| 203 | commandFailCallback)); |
| 204 | advanceClocks(time::milliseconds(1)); |
| 205 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 206 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 207 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 208 | |
| 209 | auto responseNack = util::makeNack(requestInterest, lp::NackReason::NO_ROUTE); |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 210 | face.receive(responseNack); |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 211 | advanceClocks(time::milliseconds(1)); |
| 212 | |
| 213 | BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1); |
| 214 | BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), Controller::ERROR_NACK); |
| 215 | } |
| 216 | |
| 217 | BOOST_AUTO_TEST_CASE(OptionsPrefix) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 218 | { |
| 219 | ControlParameters parameters; |
| 220 | parameters.setName("/ndn/com/example"); |
| 221 | parameters.setFaceId(400); |
| 222 | |
| 223 | CommandOptions options; |
| 224 | options.setPrefix("/localhop/net/example/router1/nfd"); |
| 225 | |
| 226 | BOOST_CHECK_NO_THROW(controller.start<RibRegisterCommand>( |
| 227 | parameters, |
| 228 | commandSucceedCallback, |
| 229 | commandFailCallback, |
| 230 | options)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 231 | advanceClocks(time::milliseconds(1)); |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 232 | |
Alexander Afanasyev | 9bdbb83 | 2015-12-30 12:54:22 -0800 | [diff] [blame] | 233 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 234 | const Interest& requestInterest = face.sentInterests[0]; |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 235 | |
| 236 | FaceCreateCommand command; |
| 237 | BOOST_CHECK(Name("/localhop/net/example/router1/nfd/rib/register").isPrefixOf( |
| 238 | requestInterest.getName())); |
| 239 | } |
| 240 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 241 | BOOST_AUTO_TEST_CASE(OptionsTimeout) |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 242 | { |
| 243 | ControlParameters parameters; |
| 244 | parameters.setUri("tcp4://192.0.2.1:6363"); |
| 245 | |
| 246 | CommandOptions options; |
| 247 | options.setTimeout(time::milliseconds(50)); |
| 248 | |
| 249 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
| 250 | parameters, |
| 251 | commandSucceedCallback, |
| 252 | commandFailCallback, |
| 253 | options)); |
Alexander Afanasyev | d3a55b2 | 2014-11-18 19:23:28 -0800 | [diff] [blame] | 254 | advanceClocks(time::milliseconds(1), 101); // Face's PIT granularity is 100ms |
Junxiao Shi | 5de006b | 2014-10-26 20:20:52 -0700 | [diff] [blame] | 255 | |
| 256 | BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1); |
| 257 | BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), Controller::ERROR_TIMEOUT); |
| 258 | } |
| 259 | |
Junxiao Shi | b1990df | 2015-11-05 00:14:44 +0000 | [diff] [blame] | 260 | BOOST_AUTO_TEST_SUITE_END() // TestController |
| 261 | BOOST_AUTO_TEST_SUITE_END() // Management |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 262 | |
Alexander Afanasyev | 4abdbf1 | 2014-08-11 12:48:54 -0700 | [diff] [blame] | 263 | } // namespace tests |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 264 | } // namespace nfd |
| 265 | } // namespace ndn |