Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [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 | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 24 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 25 | #ifndef NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP |
| 26 | #define NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 27 | |
| 28 | #include "mgmt/face-status-publisher.hpp" |
| 29 | #include "mgmt/app-face.hpp" |
| 30 | #include "mgmt/internal-face.hpp" |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 31 | #include "face/face-flags.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 32 | #include "fw/forwarder.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 33 | |
| 34 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 35 | #include "tests/daemon/face/dummy-face.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 36 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 37 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 38 | |
| 39 | namespace nfd { |
| 40 | namespace tests { |
| 41 | |
| 42 | class TestCountersFace : public DummyFace |
| 43 | { |
| 44 | public: |
| 45 | |
| 46 | TestCountersFace() |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | virtual |
| 51 | ~TestCountersFace() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | void |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 56 | setCounters(FaceCounter nInInterests, |
| 57 | FaceCounter nInDatas, |
| 58 | FaceCounter nOutInterests, |
| 59 | FaceCounter nOutDatas) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 60 | { |
| 61 | FaceCounters& counters = getMutableCounters(); |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 62 | counters.getNInInterests() = nInInterests; |
| 63 | counters.getNInDatas() = nInDatas; |
| 64 | counters.getNOutInterests() = nOutInterests; |
| 65 | counters.getNOutDatas() = nOutDatas; |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
| 69 | }; |
| 70 | |
| 71 | static inline uint64_t |
| 72 | readNonNegativeIntegerType(const Block& block, |
| 73 | uint32_t type) |
| 74 | { |
| 75 | if (block.type() == type) |
| 76 | { |
| 77 | return readNonNegativeInteger(block); |
| 78 | } |
| 79 | std::stringstream error; |
| 80 | error << "expected type " << type << " got " << block.type(); |
| 81 | throw ndn::Tlv::Error(error.str()); |
| 82 | } |
| 83 | |
| 84 | static inline uint64_t |
| 85 | checkedReadNonNegativeIntegerType(Block::element_const_iterator& i, |
| 86 | Block::element_const_iterator end, |
| 87 | uint32_t type) |
| 88 | { |
| 89 | if (i != end) |
| 90 | { |
| 91 | const Block& block = *i; |
| 92 | ++i; |
| 93 | return readNonNegativeIntegerType(block, type); |
| 94 | } |
| 95 | throw ndn::Tlv::Error("Unexpected end of FaceStatus"); |
| 96 | } |
| 97 | |
| 98 | class FaceStatusPublisherFixture : public BaseFixture |
| 99 | { |
| 100 | public: |
| 101 | |
| 102 | FaceStatusPublisherFixture() |
| 103 | : m_table(m_forwarder) |
| 104 | , m_face(make_shared<InternalFace>()) |
| 105 | , m_publisher(m_table, m_face, "/localhost/nfd/FaceStatusPublisherFixture") |
| 106 | , m_finished(false) |
| 107 | { |
| 108 | |
| 109 | } |
| 110 | |
| 111 | virtual |
| 112 | ~FaceStatusPublisherFixture() |
| 113 | { |
| 114 | |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | add(shared_ptr<Face> face) |
| 119 | { |
| 120 | m_table.add(face); |
| 121 | } |
| 122 | |
| 123 | void |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 124 | validateFaceStatus(const Block& statusBlock, const shared_ptr<Face>& reference) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 125 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 126 | ndn::nfd::FaceStatus status; |
| 127 | BOOST_REQUIRE_NO_THROW(status.wireDecode(statusBlock)); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 128 | const FaceCounters& counters = reference->getCounters(); |
| 129 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(status.getFaceId(), reference->getId()); |
| 131 | BOOST_CHECK_EQUAL(status.getRemoteUri(), reference->getRemoteUri().toString()); |
| 132 | BOOST_CHECK_EQUAL(status.getLocalUri(), reference->getLocalUri().toString()); |
| 133 | BOOST_CHECK_EQUAL(status.getFlags(), getFaceFlags(*reference)); |
| 134 | BOOST_CHECK_EQUAL(status.getNInInterests(), counters.getNInInterests()); |
| 135 | BOOST_CHECK_EQUAL(status.getNInDatas(), counters.getNInDatas()); |
| 136 | BOOST_CHECK_EQUAL(status.getNOutInterests(), counters.getNOutInterests()); |
| 137 | BOOST_CHECK_EQUAL(status.getNOutDatas(), counters.getNOutDatas()); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void |
| 141 | decodeFaceStatusBlock(const Data& data) |
| 142 | { |
| 143 | Block payload = data.getContent(); |
| 144 | |
| 145 | m_buffer.appendByteArray(payload.value(), payload.value_size()); |
| 146 | |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 147 | BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment()); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 148 | if (data.getFinalBlockId() != data.getName()[-1]) |
| 149 | { |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | // wrap the Face Statuses in a single Content TLV for easy parsing |
| 154 | m_buffer.prependVarNumber(m_buffer.size()); |
| 155 | m_buffer.prependVarNumber(ndn::Tlv::Content); |
| 156 | |
| 157 | ndn::Block parser(m_buffer.buf(), m_buffer.size()); |
| 158 | parser.parse(); |
| 159 | |
| 160 | BOOST_REQUIRE_EQUAL(parser.elements_size(), m_referenceFaces.size()); |
| 161 | |
| 162 | std::list<shared_ptr<Face> >::const_iterator iReference = m_referenceFaces.begin(); |
| 163 | for (Block::element_const_iterator i = parser.elements_begin(); |
| 164 | i != parser.elements_end(); |
| 165 | ++i) |
| 166 | { |
| 167 | if (i->type() != ndn::tlv::nfd::FaceStatus) |
| 168 | { |
| 169 | BOOST_FAIL("expected face status, got type #" << i->type()); |
| 170 | } |
| 171 | validateFaceStatus(*i, *iReference); |
| 172 | ++iReference; |
| 173 | } |
| 174 | m_finished = true; |
| 175 | } |
| 176 | |
| 177 | protected: |
| 178 | Forwarder m_forwarder; |
| 179 | FaceTable m_table; |
| 180 | shared_ptr<InternalFace> m_face; |
| 181 | FaceStatusPublisher m_publisher; |
| 182 | ndn::EncodingBuffer m_buffer; |
| 183 | std::list<shared_ptr<Face> > m_referenceFaces; |
| 184 | |
| 185 | protected: |
| 186 | bool m_finished; |
| 187 | }; |
| 188 | |
| 189 | } // namespace tests |
| 190 | } // namespace nfd |
| 191 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 192 | #endif // NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP |