Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 3 | * 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 25 | |
| 26 | #include "mgmt/manager-base.hpp" |
| 27 | #include "mgmt/internal-face.hpp" |
| 28 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 30 | |
| 31 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 32 | namespace tests { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 33 | |
| 34 | NFD_LOG_INIT("ManagerBaseTest"); |
| 35 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 36 | class ManagerBaseTest : public ManagerBase, protected BaseFixture |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 37 | { |
| 38 | |
| 39 | public: |
| 40 | |
| 41 | ManagerBaseTest() |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 42 | : ManagerBase(make_shared<InternalFace>(), "TEST-PRIVILEGE", m_keyChain), |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 43 | m_callbackFired(false) |
| 44 | { |
| 45 | |
| 46 | } |
| 47 | |
| 48 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 49 | testSetResponse(ControlResponse& response, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 50 | 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 59 | 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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 68 | const std::string& text) |
| 69 | { |
| 70 | sendResponse(name, code, text); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | testSendResponse(const Name& name, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 75 | const ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 76 | { |
| 77 | sendResponse(name, response); |
| 78 | } |
| 79 | |
| 80 | shared_ptr<InternalFace> |
| 81 | getInternalFace() |
| 82 | { |
| 83 | return ndn::ptr_lib::static_pointer_cast<InternalFace>(m_face); |
| 84 | } |
| 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 Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 95 | ControlResponse control; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 96 | 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 108 | 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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 136 | bool |
| 137 | didCallbackFire() |
| 138 | { |
| 139 | return m_callbackFired; |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | |
| 144 | bool m_callbackFired; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 145 | ndn::KeyChain m_keyChain; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 146 | |
| 147 | }; |
| 148 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 149 | BOOST_FIXTURE_TEST_SUITE(MgmtManagerBase, ManagerBaseTest) |
| 150 | |
| 151 | BOOST_AUTO_TEST_CASE(SetResponse) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 152 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 153 | ControlResponse response(200, "OK"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 154 | |
| 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 164 | BOOST_AUTO_TEST_CASE(SendResponse4Arg) |
| 165 | { |
| 166 | ndn::nfd::ControlParameters parameters; |
| 167 | parameters.setName("/test/body"); |
| 168 | |
| 169 | getInternalFace()->onReceiveData += |
| 170 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 171 | "/response", 100, "test", parameters.wireEncode()); |
| 172 | |
| 173 | testSendResponse("/response", 100, "test", parameters.wireEncode()); |
| 174 | BOOST_REQUIRE(didCallbackFire()); |
| 175 | } |
| 176 | |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 177 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 178 | BOOST_AUTO_TEST_CASE(SendResponse3Arg) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 179 | { |
| 180 | getInternalFace()->onReceiveData += |
| 181 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 182 | "/response", 100, "test"); |
| 183 | |
| 184 | testSendResponse("/response", 100, "test"); |
| 185 | BOOST_REQUIRE(didCallbackFire()); |
| 186 | } |
| 187 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 188 | BOOST_AUTO_TEST_CASE(SendResponse2Arg) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 189 | { |
| 190 | getInternalFace()->onReceiveData += |
| 191 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 192 | "/response", 100, "test"); |
| 193 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 194 | ControlResponse response(100, "test"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 195 | |
| 196 | testSendResponse("/response", 100, "test"); |
| 197 | BOOST_REQUIRE(didCallbackFire()); |
| 198 | } |
| 199 | |
| 200 | BOOST_AUTO_TEST_SUITE_END() |
| 201 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 202 | } // namespace tests |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 203 | } // namespace nfd |