blob: 2ec928502daf80e52791080925052c506020d8bd [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi7b6b79d2014-03-26 20:59:35 -07002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 Shi7b6b79d2014-03-26 20:59:35 -070020 */
21
22#include "management/nfd-controller.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070023#include "management/nfd-control-response.hpp"
24
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070025#include <boost/tuple/tuple.hpp>
26
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070027#include "boost-test.hpp"
Junxiao Shi6a90f372014-10-13 20:29:30 -070028#include "../dummy-client-face.hpp"
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070029
30namespace ndn {
31namespace nfd {
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070032namespace tests {
33
34using namespace ::ndn::tests;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070035
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070036BOOST_AUTO_TEST_SUITE(ManagementTestNfdController)
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070037
38class CommandFixture
39{
40protected:
41 CommandFixture()
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070042 : face(makeDummyClientFace())
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070043 , controller(*face)
44 , commandSucceedCallback(bind(&CommandFixture::onCommandSucceed, this, _1))
45 , commandFailCallback(bind(&CommandFixture::onCommandFail, this, _1, _2))
46 {
47 }
48
49private:
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
62protected:
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -070063 shared_ptr<DummyClientFace> face;
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070064 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
76BOOST_FIXTURE_TEST_CASE(CommandSuccess, CommandFixture)
77{
78 ControlParameters parameters;
79 parameters.setUri("tcp://example.com");
80
81 BOOST_CHECK_NO_THROW(controller.start<FaceCreateCommand>(
Junxiao Shi70911652014-08-12 10:14:24 -070082 parameters,
83 commandSucceedCallback,
84 commandFailCallback));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070085 face->processEvents(time::milliseconds(1));
86
87 BOOST_REQUIRE_EQUAL(face->m_sentInterests.size(), 1);
Junxiao Shi6a90f372014-10-13 20:29:30 -070088 const Interest& requestInterest = face->m_sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070089
90 FaceCreateCommand command;
Junxiao Shi6a90f372014-10-13 20:29:30 -070091 BOOST_CHECK(command.getPrefix().isPrefixOf(requestInterest.getName()));
92 // 9 components: ndn:/localhost/nfd/face/create/<parameters>/<signed Interest x4>
93 BOOST_REQUIRE_EQUAL(requestInterest.getName().size(), 9);
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070094 ControlParameters request;
95 // 4th component: <parameters>
Junxiao Shi6a90f372014-10-13 20:29:30 -070096 BOOST_REQUIRE_NO_THROW(request.wireDecode(requestInterest.getName().at(4).blockFromValue()));
Junxiao Shi7b6b79d2014-03-26 20:59:35 -070097 BOOST_CHECK_NO_THROW(command.validateRequest(request));
98 BOOST_CHECK_EQUAL(request.getUri(), parameters.getUri());
Junxiao Shi6a90f372014-10-13 20:29:30 -070099 BOOST_CHECK_EQUAL(requestInterest.getInterestLifetime(), Controller::getDefaultCommandTimeout());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700100
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 Shi6a90f372014-10-13 20:29:30 -0700107 Data responseData(requestInterest.getName());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700108 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
120BOOST_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
133BOOST_FIXTURE_TEST_CASE(CommandErrorCode, CommandFixture)
134{
135 ControlParameters parameters;
136 parameters.setUri("tcp://example.com");
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 Shi6a90f372014-10-13 20:29:30 -0700145 const Interest& requestInterest = face->m_sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700146
147 ControlResponse responsePayload(401, "Not Authenticated");
148
Junxiao Shi6a90f372014-10-13 20:29:30 -0700149 Data responseData(requestInterest.getName());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700150 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
160BOOST_FIXTURE_TEST_CASE(CommandInvalidResponse, CommandFixture)
161{
162 ControlParameters parameters;
163 parameters.setUri("tcp://example.com");
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 Shi6a90f372014-10-13 20:29:30 -0700172 const Interest& requestInterest = face->m_sentInterests[0];
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700173
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 Shi6a90f372014-10-13 20:29:30 -0700181 Data responseData(requestInterest.getName());
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700182 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
191BOOST_AUTO_TEST_SUITE_END()
192
Alexander Afanasyev4abdbf12014-08-11 12:48:54 -0700193} // namespace tests
Junxiao Shi7b6b79d2014-03-26 20:59:35 -0700194} // namespace nfd
195} // namespace ndn