blob: 4e690a91ed835a45a8c792470483cdf8b5183487 [file] [log] [blame]
Steve DiBenedetto0b73f442014-02-05 22:02:03 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "mgmt/manager-base.hpp"
8#include "mgmt/internal-face.hpp"
9
Junxiao Shid9ee45c2014-02-27 15:38:11 -070010#include "tests/test-common.hpp"
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070011
12namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070013namespace tests {
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070014
15NFD_LOG_INIT("ManagerBaseTest");
16
Junxiao Shid9ee45c2014-02-27 15:38:11 -070017class ManagerBaseTest : public ManagerBase, protected BaseFixture
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070018{
19
20public:
21
22 ManagerBaseTest()
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070023 : ManagerBase(make_shared<InternalFace>(), "TEST-PRIVILEGE"),
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070024 m_callbackFired(false)
25 {
26
27 }
28
29 void
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080030 testSetResponse(ControlResponse& response,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070031 uint32_t code,
32 const std::string& text)
33 {
34 setResponse(response, code, text);
35 }
36
37 void
38 testSendResponse(const Name& name,
39 uint32_t code,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060040 const std::string& text,
41 const Block& body)
42 {
43 sendResponse(name, code, text, body);
44 }
45
46 void
47 testSendResponse(const Name& name,
48 uint32_t code,
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070049 const std::string& text)
50 {
51 sendResponse(name, code, text);
52 }
53
54 void
55 testSendResponse(const Name& name,
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080056 const ControlResponse& response)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070057 {
58 sendResponse(name, response);
59 }
60
61 shared_ptr<InternalFace>
62 getInternalFace()
63 {
64 return ndn::ptr_lib::static_pointer_cast<InternalFace>(m_face);
65 }
66
67 void
68 validateControlResponse(const Data& response,
69 const Name& expectedName,
70 uint32_t expectedCode,
71 const std::string& expectedText)
72 {
73 m_callbackFired = true;
74 Block controlRaw = response.getContent().blockFromValue();
75
Alexander Afanasyevd482fd32014-02-09 23:40:20 -080076 ControlResponse control;
Steve DiBenedetto0b73f442014-02-05 22:02:03 -070077 control.wireDecode(controlRaw);
78
79 NFD_LOG_DEBUG("received control response"
80 << " name: " << response.getName()
81 << " code: " << control.getCode()
82 << " text: " << control.getText());
83
84 BOOST_REQUIRE(response.getName() == expectedName);
85 BOOST_REQUIRE(control.getCode() == expectedCode);
86 BOOST_REQUIRE(control.getText() == expectedText);
87 }
88
Steve DiBenedetto7564d972014-03-24 14:28:46 -060089 void
90 validateControlResponse(const Data& response,
91 const Name& expectedName,
92 uint32_t expectedCode,
93 const std::string& expectedText,
94 const Block& expectedBody)
95 {
96 m_callbackFired = true;
97 Block controlRaw = response.getContent().blockFromValue();
98
99 ControlResponse control;
100 control.wireDecode(controlRaw);
101
102 NFD_LOG_DEBUG("received control response"
103 << " name: " << response.getName()
104 << " code: " << control.getCode()
105 << " text: " << control.getText());
106
107 BOOST_REQUIRE(response.getName() == expectedName);
108 BOOST_REQUIRE(control.getCode() == expectedCode);
109 BOOST_REQUIRE(control.getText() == expectedText);
110
111 BOOST_REQUIRE(control.getBody().value_size() == expectedBody.value_size());
112
113 BOOST_CHECK(memcmp(control.getBody().value(), expectedBody.value(),
114 expectedBody.value_size()) == 0);
115 }
116
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700117 bool
118 didCallbackFire()
119 {
120 return m_callbackFired;
121 }
122
123private:
124
125 bool m_callbackFired;
126
127};
128
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700129BOOST_FIXTURE_TEST_SUITE(MgmtManagerBase, ManagerBaseTest)
130
131BOOST_AUTO_TEST_CASE(SetResponse)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700132{
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800133 ControlResponse response(200, "OK");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700134
135 BOOST_CHECK_EQUAL(response.getCode(), 200);
136 BOOST_CHECK_EQUAL(response.getText(), "OK");
137
138 testSetResponse(response, 100, "test");
139
140 BOOST_CHECK_EQUAL(response.getCode(), 100);
141 BOOST_CHECK_EQUAL(response.getText(), "test");
142}
143
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600144BOOST_AUTO_TEST_CASE(SendResponse4Arg)
145{
146 ndn::nfd::ControlParameters parameters;
147 parameters.setName("/test/body");
148
149 getInternalFace()->onReceiveData +=
150 bind(&ManagerBaseTest::validateControlResponse, this, _1,
151 "/response", 100, "test", parameters.wireEncode());
152
153 testSendResponse("/response", 100, "test", parameters.wireEncode());
154 BOOST_REQUIRE(didCallbackFire());
155}
156
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700157
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700158BOOST_AUTO_TEST_CASE(SendResponse3Arg)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700159{
160 getInternalFace()->onReceiveData +=
161 bind(&ManagerBaseTest::validateControlResponse, this, _1,
162 "/response", 100, "test");
163
164 testSendResponse("/response", 100, "test");
165 BOOST_REQUIRE(didCallbackFire());
166}
167
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700168BOOST_AUTO_TEST_CASE(SendResponse2Arg)
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700169{
170 getInternalFace()->onReceiveData +=
171 bind(&ManagerBaseTest::validateControlResponse, this, _1,
172 "/response", 100, "test");
173
Alexander Afanasyevd482fd32014-02-09 23:40:20 -0800174 ControlResponse response(100, "test");
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700175
176 testSendResponse("/response", 100, "test");
177 BOOST_REQUIRE(didCallbackFire());
178}
179
180BOOST_AUTO_TEST_SUITE_END()
181
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700182} // namespace tests
Steve DiBenedetto0b73f442014-02-05 22:02:03 -0700183} // namespace nfd