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, |
| 40 | const std::string& text) |
| 41 | { |
| 42 | sendResponse(name, code, text); |
| 43 | } |
| 44 | |
| 45 | void |
| 46 | testSendResponse(const Name& name, |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 47 | const ControlResponse& response) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 48 | { |
| 49 | sendResponse(name, response); |
| 50 | } |
| 51 | |
| 52 | shared_ptr<InternalFace> |
| 53 | getInternalFace() |
| 54 | { |
| 55 | return ndn::ptr_lib::static_pointer_cast<InternalFace>(m_face); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | validateControlResponse(const Data& response, |
| 60 | const Name& expectedName, |
| 61 | uint32_t expectedCode, |
| 62 | const std::string& expectedText) |
| 63 | { |
| 64 | m_callbackFired = true; |
| 65 | Block controlRaw = response.getContent().blockFromValue(); |
| 66 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 67 | ControlResponse control; |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 68 | control.wireDecode(controlRaw); |
| 69 | |
| 70 | NFD_LOG_DEBUG("received control response" |
| 71 | << " name: " << response.getName() |
| 72 | << " code: " << control.getCode() |
| 73 | << " text: " << control.getText()); |
| 74 | |
| 75 | BOOST_REQUIRE(response.getName() == expectedName); |
| 76 | BOOST_REQUIRE(control.getCode() == expectedCode); |
| 77 | BOOST_REQUIRE(control.getText() == expectedText); |
| 78 | } |
| 79 | |
| 80 | bool |
| 81 | didCallbackFire() |
| 82 | { |
| 83 | return m_callbackFired; |
| 84 | } |
| 85 | |
| 86 | private: |
| 87 | |
| 88 | bool m_callbackFired; |
| 89 | |
| 90 | }; |
| 91 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 92 | BOOST_FIXTURE_TEST_SUITE(MgmtManagerBase, ManagerBaseTest) |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(SetResponse) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 95 | { |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 96 | ControlResponse response(200, "OK"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 97 | |
| 98 | BOOST_CHECK_EQUAL(response.getCode(), 200); |
| 99 | BOOST_CHECK_EQUAL(response.getText(), "OK"); |
| 100 | |
| 101 | testSetResponse(response, 100, "test"); |
| 102 | |
| 103 | BOOST_CHECK_EQUAL(response.getCode(), 100); |
| 104 | BOOST_CHECK_EQUAL(response.getText(), "test"); |
| 105 | } |
| 106 | |
| 107 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 108 | BOOST_AUTO_TEST_CASE(SendResponse3Arg) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 109 | { |
| 110 | getInternalFace()->onReceiveData += |
| 111 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 112 | "/response", 100, "test"); |
| 113 | |
| 114 | testSendResponse("/response", 100, "test"); |
| 115 | BOOST_REQUIRE(didCallbackFire()); |
| 116 | } |
| 117 | |
| 118 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 119 | BOOST_AUTO_TEST_CASE(SendResponse2Arg) |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 120 | { |
| 121 | getInternalFace()->onReceiveData += |
| 122 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 123 | "/response", 100, "test"); |
| 124 | |
Alexander Afanasyev | d482fd3 | 2014-02-09 23:40:20 -0800 | [diff] [blame] | 125 | ControlResponse response(100, "test"); |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 126 | |
| 127 | testSendResponse("/response", 100, "test"); |
| 128 | BOOST_REQUIRE(didCallbackFire()); |
| 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_SUITE_END() |
| 132 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 133 | } // namespace tests |
Steve DiBenedetto | 0b73f44 | 2014-02-05 22:02:03 -0700 | [diff] [blame] | 134 | } // namespace nfd |