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 | |
| 10 | #include <ndn-cpp-dev/management/control-response.hpp> |
| 11 | |
| 12 | #include <boost/test/unit_test.hpp> |
| 13 | |
| 14 | namespace nfd { |
| 15 | |
| 16 | NFD_LOG_INIT("ManagerBaseTest"); |
| 17 | |
| 18 | BOOST_AUTO_TEST_SUITE(MgmtManagerBase) |
| 19 | |
| 20 | class ManagerBaseTest : public ManagerBase |
| 21 | { |
| 22 | |
| 23 | public: |
| 24 | |
| 25 | ManagerBaseTest() |
| 26 | : ManagerBase(make_shared<InternalFace>()), |
| 27 | m_callbackFired(false) |
| 28 | { |
| 29 | |
| 30 | } |
| 31 | |
| 32 | void |
| 33 | testSetResponse(ndn::ControlResponse& response, |
| 34 | uint32_t code, |
| 35 | const std::string& text) |
| 36 | { |
| 37 | setResponse(response, code, text); |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | testSendResponse(const Name& name, |
| 42 | uint32_t code, |
| 43 | const std::string& text) |
| 44 | { |
| 45 | sendResponse(name, code, text); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | testSendResponse(const Name& name, |
| 50 | const ndn::ControlResponse& response) |
| 51 | { |
| 52 | sendResponse(name, response); |
| 53 | } |
| 54 | |
| 55 | shared_ptr<InternalFace> |
| 56 | getInternalFace() |
| 57 | { |
| 58 | return ndn::ptr_lib::static_pointer_cast<InternalFace>(m_face); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | validateControlResponse(const Data& response, |
| 63 | const Name& expectedName, |
| 64 | uint32_t expectedCode, |
| 65 | const std::string& expectedText) |
| 66 | { |
| 67 | m_callbackFired = true; |
| 68 | Block controlRaw = response.getContent().blockFromValue(); |
| 69 | |
| 70 | ndn::ControlResponse control; |
| 71 | control.wireDecode(controlRaw); |
| 72 | |
| 73 | NFD_LOG_DEBUG("received control response" |
| 74 | << " name: " << response.getName() |
| 75 | << " code: " << control.getCode() |
| 76 | << " text: " << control.getText()); |
| 77 | |
| 78 | BOOST_REQUIRE(response.getName() == expectedName); |
| 79 | BOOST_REQUIRE(control.getCode() == expectedCode); |
| 80 | BOOST_REQUIRE(control.getText() == expectedText); |
| 81 | } |
| 82 | |
| 83 | bool |
| 84 | didCallbackFire() |
| 85 | { |
| 86 | return m_callbackFired; |
| 87 | } |
| 88 | |
| 89 | private: |
| 90 | |
| 91 | bool m_callbackFired; |
| 92 | |
| 93 | }; |
| 94 | |
| 95 | BOOST_FIXTURE_TEST_CASE(SetResponse, ManagerBaseTest) |
| 96 | { |
| 97 | ndn::ControlResponse response(200, "OK"); |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(response.getCode(), 200); |
| 100 | BOOST_CHECK_EQUAL(response.getText(), "OK"); |
| 101 | |
| 102 | testSetResponse(response, 100, "test"); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(response.getCode(), 100); |
| 105 | BOOST_CHECK_EQUAL(response.getText(), "test"); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | BOOST_FIXTURE_TEST_CASE(SendResponse3Arg, ManagerBaseTest) |
| 110 | { |
| 111 | getInternalFace()->onReceiveData += |
| 112 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 113 | "/response", 100, "test"); |
| 114 | |
| 115 | testSendResponse("/response", 100, "test"); |
| 116 | BOOST_REQUIRE(didCallbackFire()); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | BOOST_FIXTURE_TEST_CASE(SendResponse2Arg, ManagerBaseTest) |
| 121 | { |
| 122 | getInternalFace()->onReceiveData += |
| 123 | bind(&ManagerBaseTest::validateControlResponse, this, _1, |
| 124 | "/response", 100, "test"); |
| 125 | |
| 126 | ndn::ControlResponse response(100, "test"); |
| 127 | |
| 128 | testSendResponse("/response", 100, "test"); |
| 129 | BOOST_REQUIRE(didCallbackFire()); |
| 130 | } |
| 131 | |
| 132 | BOOST_AUTO_TEST_SUITE_END() |
| 133 | |
| 134 | } // namespace nfd |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |