Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -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 | * |
| 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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 24 | |
| 25 | #include "mgmt/manager-base.hpp" |
| 26 | #include "mgmt/internal-face.hpp" |
| 27 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 29 | |
| 30 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 31 | namespace tests { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 32 | |
| 33 | NFD_LOG_INIT("ManagerBaseTest"); |
| 34 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 35 | class ManagerBaseTest : public ManagerBase, protected BaseFixture |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 36 | { |
| 37 | |
| 38 | public: |
| 39 | |
| 40 | ManagerBaseTest() |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 41 | : ManagerBase(make_shared<InternalFace>(), "TEST-PRIVILEGE"), |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 42 | m_callbackFired(false) |
| 43 | { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 48 | testSetResponse(ControlResponse& response, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 49 | 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 58 | 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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 67 | const std::string& text) |
| 68 | { |
| 69 | sendResponse(name, code, text); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | testSendResponse(const Name& name, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 74 | const ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 75 | { |
| 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 Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 94 | ControlResponse control; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 95 | 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 107 | 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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 135 | bool |
| 136 | didCallbackFire() |
| 137 | { |
| 138 | return m_callbackFired; |
| 139 | } |
| 140 | |
| 141 | private: |
| 142 | |
| 143 | bool m_callbackFired; |
| 144 | |
| 145 | }; |
| 146 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 147 | BOOST_FIXTURE_TEST_SUITE(MgmtManagerBase, ManagerBaseTest) |
| 148 | |
| 149 | BOOST_AUTO_TEST_CASE(SetResponse) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 150 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 151 | ControlResponse response(200, "OK"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 152 | |
| 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 162 | BOOST_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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 175 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 176 | BOOST_AUTO_TEST_CASE(SendResponse3Arg) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 177 | { |
| 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 Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 186 | BOOST_AUTO_TEST_CASE(SendResponse2Arg) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 187 | { |
| 188 | getInternalFace()->onReceiveData += |
| 189 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 190 | "/response", 100, "test"); |
| 191 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 192 | ControlResponse response(100, "test"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 193 | |
| 194 | testSendResponse("/response", 100, "test"); |
| 195 | BOOST_REQUIRE(didCallbackFire()); |
| 196 | } |
| 197 | |
| 198 | BOOST_AUTO_TEST_SUITE_END() |
| 199 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 200 | } // namespace tests |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 201 | } // namespace nfd |