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