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