blob: cfecc7d217c1c90a6ca55b64ebc4962187d545ea [file] [log] [blame]
Steve DiBenedetto0b73f442014-02-05 22:02:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -07003 * Copyright (c) 2014, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Vince Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070025
26#include "mgmt/manager-base.hpp"
27#include "mgmt/internal-face.hpp"
28
Junxiao Shid9ee45c2014-02-27 15:38:11 -070029#include "tests/test-common.hpp"
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070030
31namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032namespace tests {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070033
34NFD_LOG_INIT("ManagerBaseTest");
35
Junxiao Shid9ee45c2014-02-27 15:38:11 -070036class ManagerBaseTest : public ManagerBase, protected BaseFixture
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070037{
38
39public:
40
41 ManagerBaseTest()
Vince Lehman5144f822014-07-23 15:12:56 -070042 : ManagerBase(make_shared<InternalFace>(), "TEST-PRIVILEGE", m_keyChain),
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070043 m_callbackFired(false)
44 {
45
46 }
47
48 void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080049 testSetResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070050 uint32_t code,
51 const std::string& text)
52 {
53 setResponse(response, code, text);
54 }
55
56 void
57 testSendResponse(const Name& name,
58 uint32_t code,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060059 const std::string& text,
60 const Block& body)
61 {
62 sendResponse(name, code, text, body);
63 }
64
65 void
66 testSendResponse(const Name& name,
67 uint32_t code,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070068 const std::string& text)
69 {
70 sendResponse(name, code, text);
71 }
72
73 void
74 testSendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080075 const ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070076 {
77 sendResponse(name, response);
78 }
79
80 shared_ptr<InternalFace>
81 getInternalFace()
82 {
Davide Pesaventoab1e8f22014-10-21 22:45:33 +020083 return static_pointer_cast<InternalFace>(m_face);
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070084 }
85
86 void
87 validateControlResponse(const Data& response,
88 const Name& expectedName,
89 uint32_t expectedCode,
90 const std::string& expectedText)
91 {
92 m_callbackFired = true;
93 Block controlRaw = response.getContent().blockFromValue();
94
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080095 ControlResponse control;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070096 control.wireDecode(controlRaw);
97
98 NFD_LOG_DEBUG("received control response"
99 << " name: " << response.getName()
100 << " code: " << control.getCode()
101 << " text: " << control.getText());
102
103 BOOST_REQUIRE(response.getName() == expectedName);
104 BOOST_REQUIRE(control.getCode() == expectedCode);
105 BOOST_REQUIRE(control.getText() == expectedText);
106 }
107
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600108 void
109 validateControlResponse(const Data& response,
110 const Name& expectedName,
111 uint32_t expectedCode,
112 const std::string& expectedText,
113 const Block& expectedBody)
114 {
115 m_callbackFired = true;
116 Block controlRaw = response.getContent().blockFromValue();
117
118 ControlResponse control;
119 control.wireDecode(controlRaw);
120
121 NFD_LOG_DEBUG("received control response"
122 << " name: " << response.getName()
123 << " code: " << control.getCode()
124 << " text: " << control.getText());
125
126 BOOST_REQUIRE(response.getName() == expectedName);
127 BOOST_REQUIRE(control.getCode() == expectedCode);
128 BOOST_REQUIRE(control.getText() == expectedText);
129
130 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
131
132 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
133 expectedBody.value_size()) == 0);
134 }
135
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700136 bool
137 didCallbackFire()
138 {
139 return m_callbackFired;
140 }
141
142private:
143
144 bool m_callbackFired;
Vince Lehman5144f822014-07-23 15:12:56 -0700145 ndn::KeyChain m_keyChain;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700146
147};
148
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700149BOOST_FIXTURE_TEST_SUITE(MgmtManagerBase, ManagerBaseTest)
150
151BOOST_AUTO_TEST_CASE(SetResponse)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700152{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800153 ControlResponse response(200, "OK");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700154
155 BOOST_CHECK_EQUAL(response.getCode(), 200);
156 BOOST_CHECK_EQUAL(response.getText(), "OK");
157
158 testSetResponse(response, 100, "test");
159
160 BOOST_CHECK_EQUAL(response.getCode(), 100);
161 BOOST_CHECK_EQUAL(response.getText(), "test");
162}
163
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600164BOOST_AUTO_TEST_CASE(SendResponse4Arg)
165{
166 ndn::nfd::ControlParameters parameters;
167 parameters.setName("/test/body");
168
Junxiao Shic099ddb2014-12-25 20:53:20 -0700169 getInternalFace()->onReceiveData.connect([this, parameters] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700170 this->validateControlResponse(response, "/response", 100, "test", parameters.wireEncode());
Junxiao Shic099ddb2014-12-25 20:53:20 -0700171 });
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600172
173 testSendResponse("/response", 100, "test", parameters.wireEncode());
174 BOOST_REQUIRE(didCallbackFire());
175}
176
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700177
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700178BOOST_AUTO_TEST_CASE(SendResponse3Arg)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700179{
Junxiao Shic099ddb2014-12-25 20:53:20 -0700180 getInternalFace()->onReceiveData.connect([this] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700181 this->validateControlResponse(response, "/response", 100, "test");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700182 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700183
184 testSendResponse("/response", 100, "test");
185 BOOST_REQUIRE(didCallbackFire());
186}
187
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700188BOOST_AUTO_TEST_CASE(SendResponse2Arg)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700189{
Junxiao Shic099ddb2014-12-25 20:53:20 -0700190 getInternalFace()->onReceiveData.connect([this] (const Data& response) {
Junxiao Shicd55cde2014-11-13 16:03:24 -0700191 this->validateControlResponse(response, "/response", 100, "test");
Junxiao Shic099ddb2014-12-25 20:53:20 -0700192 });
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700193
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800194 ControlResponse response(100, "test");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700195
196 testSendResponse("/response", 100, "test");
197 BOOST_REQUIRE(didCallbackFire());
198}
199
200BOOST_AUTO_TEST_SUITE_END()
201
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700202} // namespace tests
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700203} // namespace nfd