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