blob: 74fcb6ef6a3645e6f9ae80b2abb74a8a69b5d5fb [file] [log] [blame]
Steve DiBenedetto0b73f442014-02-05 22:02:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070024
25#include "mgmt/manager-base.hpp"
26#include "mgmt/internal-face.hpp"
27
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028#include "tests/test-common.hpp"
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070029
30namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070031namespace tests {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070032
33NFD_LOG_INIT("ManagerBaseTest");
34
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035class ManagerBaseTest : public ManagerBase, protected BaseFixture
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070036{
37
38public:
39
40 ManagerBaseTest()
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070041 : ManagerBase(make_shared<InternalFace>(), "TEST-PRIVILEGE"),
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070042 m_callbackFired(false)
43 {
44
45 }
46
47 void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080048 testSetResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070049 uint32_t code,
50 const std::string& text)
51 {
52 setResponse(response, code, text);
53 }
54
55 void
56 testSendResponse(const Name& name,
57 uint32_t code,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060058 const std::string& text,
59 const Block& body)
60 {
61 sendResponse(name, code, text, body);
62 }
63
64 void
65 testSendResponse(const Name& name,
66 uint32_t code,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070067 const std::string& text)
68 {
69 sendResponse(name, code, text);
70 }
71
72 void
73 testSendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080074 const ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070075 {
76 sendResponse(name, response);
77 }
78
79 shared_ptr<InternalFace>
80 getInternalFace()
81 {
82 return ndn::ptr_lib::static_pointer_cast<InternalFace>(m_face);
83 }
84
85 void
86 validateControlResponse(const Data& response,
87 const Name& expectedName,
88 uint32_t expectedCode,
89 const std::string& expectedText)
90 {
91 m_callbackFired = true;
92 Block controlRaw = response.getContent().blockFromValue();
93
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080094 ControlResponse control;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070095 control.wireDecode(controlRaw);
96
97 NFD_LOG_DEBUG("received control response"
98 << " name: " << response.getName()
99 << " code: " << control.getCode()
100 << " text: " << control.getText());
101
102 BOOST_REQUIRE(response.getName() == expectedName);
103 BOOST_REQUIRE(control.getCode() == expectedCode);
104 BOOST_REQUIRE(control.getText() == expectedText);
105 }
106
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600107 void
108 validateControlResponse(const Data& response,
109 const Name& expectedName,
110 uint32_t expectedCode,
111 const std::string& expectedText,
112 const Block& expectedBody)
113 {
114 m_callbackFired = true;
115 Block controlRaw = response.getContent().blockFromValue();
116
117 ControlResponse control;
118 control.wireDecode(controlRaw);
119
120 NFD_LOG_DEBUG("received control response"
121 << " name: " << response.getName()
122 << " code: " << control.getCode()
123 << " text: " << control.getText());
124
125 BOOST_REQUIRE(response.getName() == expectedName);
126 BOOST_REQUIRE(control.getCode() == expectedCode);
127 BOOST_REQUIRE(control.getText() == expectedText);
128
129 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
130
131 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
132 expectedBody.value_size()) == 0);
133 }
134
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700135 bool
136 didCallbackFire()
137 {
138 return m_callbackFired;
139 }
140
141private:
142
143 bool m_callbackFired;
144
145};
146
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700147BOOST_FIXTURE_TEST_SUITE(MgmtManagerBase, ManagerBaseTest)
148
149BOOST_AUTO_TEST_CASE(SetResponse)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700150{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800151 ControlResponse response(200, "OK");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700152
153 BOOST_CHECK_EQUAL(response.getCode(), 200);
154 BOOST_CHECK_EQUAL(response.getText(), "OK");
155
156 testSetResponse(response, 100, "test");
157
158 BOOST_CHECK_EQUAL(response.getCode(), 100);
159 BOOST_CHECK_EQUAL(response.getText(), "test");
160}
161
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600162BOOST_AUTO_TEST_CASE(SendResponse4Arg)
163{
164 ndn::nfd::ControlParameters parameters;
165 parameters.setName("/test/body");
166
167 getInternalFace()->onReceiveData +=
168 bind(&ManagerBaseTest::validateControlResponse, this, _1,
169 "/response", 100, "test", parameters.wireEncode());
170
171 testSendResponse("/response", 100, "test", parameters.wireEncode());
172 BOOST_REQUIRE(didCallbackFire());
173}
174
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700175
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700176BOOST_AUTO_TEST_CASE(SendResponse3Arg)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700177{
178 getInternalFace()->onReceiveData +=
179 bind(&ManagerBaseTest::validateControlResponse, this, _1,
180 "/response", 100, "test");
181
182 testSendResponse("/response", 100, "test");
183 BOOST_REQUIRE(didCallbackFire());
184}
185
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700186BOOST_AUTO_TEST_CASE(SendResponse2Arg)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700187{
188 getInternalFace()->onReceiveData +=
189 bind(&ManagerBaseTest::validateControlResponse, this, _1,
190 "/response", 100, "test");
191
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800192 ControlResponse response(100, "test");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700193
194 testSendResponse("/response", 100, "test");
195 BOOST_REQUIRE(didCallbackFire());
196}
197
198BOOST_AUTO_TEST_SUITE_END()
199
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700200} // namespace tests
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700201} // namespace nfd