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