Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 1 | /* -*- 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 Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 10 | #include "tests/test-common.hpp" |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 11 | |
| 12 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 13 | namespace tests { |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 14 | |
| 15 | NFD_LOG_INIT("ManagerBaseTest"); |
| 16 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 17 | class ManagerBaseTest : public ManagerBase, protected BaseFixture |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 18 | { |
| 19 | |
| 20 | public: |
| 21 | |
| 22 | ManagerBaseTest() |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 23 | : ManagerBase(make_shared<InternalFace>(), "TEST-PRIVILEGE"), |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 24 | m_callbackFired(false) |
| 25 | { |
| 26 | |
| 27 | } |
| 28 | |
| 29 | void |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 30 | testSetResponse(ControlResponse& response, |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 31 | 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame^] | 40 | 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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 49 | const std::string& text) |
| 50 | { |
| 51 | sendResponse(name, code, text); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | testSendResponse(const Name& name, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 56 | const ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 57 | { |
| 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 Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 76 | ControlResponse control; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 77 | 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame^] | 89 | 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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 117 | bool |
| 118 | didCallbackFire() |
| 119 | { |
| 120 | return m_callbackFired; |
| 121 | } |
| 122 | |
| 123 | private: |
| 124 | |
| 125 | bool m_callbackFired; |
| 126 | |
| 127 | }; |
| 128 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 129 | BOOST_FIXTURE_TEST_SUITE(MgmtManagerBase, ManagerBaseTest) |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(SetResponse) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 132 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 133 | ControlResponse response(200, "OK"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 134 | |
| 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 DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame^] | 144 | BOOST_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 DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 157 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 158 | BOOST_AUTO_TEST_CASE(SendResponse3Arg) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 159 | { |
| 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 Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 168 | BOOST_AUTO_TEST_CASE(SendResponse2Arg) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 169 | { |
| 170 | getInternalFace()->onReceiveData += |
| 171 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 172 | "/response", 100, "test"); |
| 173 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 174 | ControlResponse response(100, "test"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 175 | |
| 176 | testSendResponse("/response", 100, "test"); |
| 177 | BOOST_REQUIRE(didCallbackFire()); |
| 178 | } |
| 179 | |
| 180 | BOOST_AUTO_TEST_SUITE_END() |
| 181 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 182 | } // namespace tests |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 183 | } // namespace nfd |