Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "management/nfd-controller.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 8 | #include "management/nfd-control-response.hpp" |
| 9 | |
| 10 | #include "../transport/dummy-face.hpp" |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 11 | |
| 12 | #include "boost-test.hpp" |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
| 15 | namespace nfd { |
| 16 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 17 | BOOST_AUTO_TEST_SUITE(ManagementTestNfdController) |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 18 | |
| 19 | class CommandFixture |
| 20 | { |
| 21 | protected: |
| 22 | CommandFixture() |
| 23 | : face(makeDummyFace()) |
| 24 | , controller(*face) |
| 25 | , commandSucceedCallback(bind(&CommandFixture::onCommandSucceed, this, _1)) |
| 26 | , commandFailCallback(bind(&CommandFixture::onCommandFail, this, _1, _2)) |
| 27 | { |
| 28 | } |
| 29 | |
| 30 | private: |
| 31 | void |
| 32 | onCommandSucceed(const ControlParameters& parameters) |
| 33 | { |
| 34 | commandSucceedHistory.push_back(boost::make_tuple(parameters)); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | onCommandFail(uint32_t code, const std::string& reason) |
| 39 | { |
| 40 | commandFailHistory.push_back(boost::make_tuple(code, reason)); |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | shared_ptr<DummyFace> face; |
| 45 | Controller controller; |
| 46 | KeyChain keyChain; |
| 47 | |
| 48 | Controller::CommandSucceedCallback commandSucceedCallback; |
| 49 | typedef boost::tuple<ControlParameters> CommandSucceedArgs; |
| 50 | std::vector<CommandSucceedArgs> commandSucceedHistory; |
| 51 | |
| 52 | Controller::CommandFailCallback commandFailCallback; |
| 53 | typedef boost::tuple<uint32_t,std::string> CommandFailArgs; |
| 54 | std::vector<CommandFailArgs> commandFailHistory; |
| 55 | }; |
| 56 | |
| 57 | BOOST_FIXTURE_TEST_CASE(CommandSuccess, CommandFixture) |
| 58 | { |
| 59 | ControlParameters parameters; |
| 60 | parameters.setUri("tcp://example.com"); |
| 61 | |
| 62 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
| 63 | parameters, |
| 64 | commandSucceedCallback, |
| 65 | commandFailCallback)); |
| 66 | face->processEvents(time::milliseconds(1)); |
| 67 | |
| 68 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1); |
| 69 | const Interest& commandInterest = face->m_sentInterests[0]; |
| 70 | |
| 71 | FaceCreateCommand command; |
| 72 | BOOST_CHECK(command.getPrefix().isPrefixOf(commandInterest.getName())); |
| 73 | // 9 components: ndn:/localhost/nfd/face/create/<parameters>/<command Interest signature x4> |
| 74 | BOOST_REQUIRE_EQUAL(commandInterest.getName().size(), 9); |
| 75 | ControlParameters request; |
| 76 | // 4th component: <parameters> |
| 77 | BOOST_REQUIRE_NO_THROW(request.wireDecode(commandInterest.getName().at(4).blockFromValue())); |
| 78 | BOOST_CHECK_NO_THROW(command.validateRequest(request)); |
| 79 | BOOST_CHECK_EQUAL(request.getUri(), parameters.getUri()); |
Junxiao Shi | 5c785d6 | 2014-04-20 18:10:20 -0700 | [diff] [blame^] | 80 | BOOST_CHECK_EQUAL(commandInterest.getInterestLifetime(), Controller::getDefaultCommandTimeout()); |
Junxiao Shi | 7b6b79d | 2014-03-26 20:59:35 -0700 | [diff] [blame] | 81 | |
| 82 | ControlParameters responseBody; |
| 83 | responseBody.setUri("tcp4://192.0.2.1:6363") |
| 84 | .setFaceId(22); |
| 85 | ControlResponse responsePayload(201, "created"); |
| 86 | responsePayload.setBody(responseBody.wireEncode()); |
| 87 | |
| 88 | Data responseData(commandInterest.getName()); |
| 89 | responseData.setContent(responsePayload.wireEncode()); |
| 90 | keyChain.sign(responseData); |
| 91 | face->receive(responseData); |
| 92 | face->processEvents(time::milliseconds(1)); |
| 93 | |
| 94 | BOOST_CHECK_EQUAL(commandFailHistory.size(), 0); |
| 95 | BOOST_REQUIRE_EQUAL(commandSucceedHistory.size(), 1); |
| 96 | const ControlParameters& response = commandSucceedHistory[0].get<0>(); |
| 97 | BOOST_CHECK_EQUAL(response.getUri(), responseBody.getUri()); |
| 98 | BOOST_CHECK_EQUAL(response.getFaceId(), responseBody.getFaceId()); |
| 99 | } |
| 100 | |
| 101 | BOOST_FIXTURE_TEST_CASE(CommandInvalidRequest, CommandFixture) |
| 102 | { |
| 103 | ControlParameters parameters; |
| 104 | parameters.setName("ndn:/should-not-have-this-field"); |
| 105 | // Uri is missing |
| 106 | |
| 107 | BOOST_CHECK_THROW(controller.start<FaceCreateCommand>( |
| 108 | parameters, |
| 109 | commandSucceedCallback, |
| 110 | commandFailCallback), |
| 111 | ControlCommand::ArgumentError); |
| 112 | } |
| 113 | |
| 114 | BOOST_FIXTURE_TEST_CASE(CommandErrorCode, CommandFixture) |
| 115 | { |
| 116 | ControlParameters parameters; |
| 117 | parameters.setUri("tcp://example.com"); |
| 118 | |
| 119 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
| 120 | parameters, |
| 121 | commandSucceedCallback, |
| 122 | commandFailCallback)); |
| 123 | face->processEvents(time::milliseconds(1)); |
| 124 | |
| 125 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1); |
| 126 | const Interest& commandInterest = face->m_sentInterests[0]; |
| 127 | |
| 128 | ControlResponse responsePayload(401, "Not Authenticated"); |
| 129 | |
| 130 | Data responseData(commandInterest.getName()); |
| 131 | responseData.setContent(responsePayload.wireEncode()); |
| 132 | keyChain.sign(responseData); |
| 133 | face->receive(responseData); |
| 134 | face->processEvents(time::milliseconds(1)); |
| 135 | |
| 136 | BOOST_CHECK_EQUAL(commandSucceedHistory.size(), 0); |
| 137 | BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1); |
| 138 | BOOST_CHECK_EQUAL(commandFailHistory[0].get<0>(), 401); |
| 139 | } |
| 140 | |
| 141 | BOOST_FIXTURE_TEST_CASE(CommandInvalidResponse, CommandFixture) |
| 142 | { |
| 143 | ControlParameters parameters; |
| 144 | parameters.setUri("tcp://example.com"); |
| 145 | |
| 146 | BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>( |
| 147 | parameters, |
| 148 | commandSucceedCallback, |
| 149 | commandFailCallback)); |
| 150 | face->processEvents(time::milliseconds(1)); |
| 151 | |
| 152 | BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1); |
| 153 | const Interest& commandInterest = face->m_sentInterests[0]; |
| 154 | |
| 155 | ControlParameters responseBody; |
| 156 | responseBody.setUri("tcp4://192.0.2.1:6363") |
| 157 | .setName("ndn:/should-not-have-this-field"); |
| 158 | // FaceId is missing |
| 159 | ControlResponse responsePayload(201, "created"); |
| 160 | responsePayload.setBody(responseBody.wireEncode()); |
| 161 | |
| 162 | Data responseData(commandInterest.getName()); |
| 163 | responseData.setContent(responsePayload.wireEncode()); |
| 164 | keyChain.sign(responseData); |
| 165 | face->receive(responseData); |
| 166 | face->processEvents(time::milliseconds(1)); |
| 167 | |
| 168 | BOOST_CHECK_EQUAL(commandSucceedHistory.size(), 0); |
| 169 | BOOST_REQUIRE_EQUAL(commandFailHistory.size(), 1); |
| 170 | } |
| 171 | |
| 172 | BOOST_AUTO_TEST_SUITE_END() |
| 173 | |
| 174 | } // namespace nfd |
| 175 | } // namespace ndn |