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