Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -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 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -0700 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP |
| 27 | #define NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 28 | |
| 29 | #include "mgmt/face-status-publisher.hpp" |
| 30 | #include "mgmt/app-face.hpp" |
| 31 | #include "mgmt/internal-face.hpp" |
Alexander Afanasyev | e515f0a | 2014-06-30 15:28:10 -0700 | [diff] [blame] | 32 | #include "face/face-flags.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 33 | #include "fw/forwarder.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 34 | |
| 35 | #include "tests/test-common.hpp" |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 36 | #include "tests/daemon/face/dummy-face.hpp" |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 37 | |
Alexander Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 38 | #include <ndn-cxx/management/nfd-face-status.hpp> |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 39 | |
| 40 | namespace nfd { |
| 41 | namespace tests { |
| 42 | |
| 43 | class TestCountersFace : public DummyFace |
| 44 | { |
| 45 | public: |
| 46 | |
| 47 | TestCountersFace() |
| 48 | { |
| 49 | } |
| 50 | |
| 51 | virtual |
| 52 | ~TestCountersFace() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | void |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 57 | setCounters(PacketCounter::rep nInInterests, |
| 58 | PacketCounter::rep nInDatas, |
| 59 | PacketCounter::rep nOutInterests, |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -0700 | [diff] [blame] | 60 | PacketCounter::rep nOutDatas, |
| 61 | ByteCounter::rep nInBytes, |
| 62 | ByteCounter::rep nOutBytes) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 63 | { |
| 64 | FaceCounters& counters = getMutableCounters(); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 65 | counters.getNInInterests().set(nInInterests); |
| 66 | counters.getNInDatas().set(nInDatas); |
| 67 | counters.getNOutInterests().set(nOutInterests); |
| 68 | counters.getNOutDatas().set(nOutDatas); |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -0700 | [diff] [blame] | 69 | counters.getNInBytes().set(nInBytes); |
| 70 | counters.getNOutBytes().set(nOutBytes); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
| 74 | }; |
| 75 | |
| 76 | static inline uint64_t |
| 77 | readNonNegativeIntegerType(const Block& block, |
| 78 | uint32_t type) |
| 79 | { |
| 80 | if (block.type() == type) |
| 81 | { |
| 82 | return readNonNegativeInteger(block); |
| 83 | } |
| 84 | std::stringstream error; |
| 85 | error << "expected type " << type << " got " << block.type(); |
| 86 | throw ndn::Tlv::Error(error.str()); |
| 87 | } |
| 88 | |
| 89 | static inline uint64_t |
| 90 | checkedReadNonNegativeIntegerType(Block::element_const_iterator& i, |
| 91 | Block::element_const_iterator end, |
| 92 | uint32_t type) |
| 93 | { |
| 94 | if (i != end) |
| 95 | { |
| 96 | const Block& block = *i; |
| 97 | ++i; |
| 98 | return readNonNegativeIntegerType(block, type); |
| 99 | } |
| 100 | throw ndn::Tlv::Error("Unexpected end of FaceStatus"); |
| 101 | } |
| 102 | |
| 103 | class FaceStatusPublisherFixture : public BaseFixture |
| 104 | { |
| 105 | public: |
| 106 | |
| 107 | FaceStatusPublisherFixture() |
| 108 | : m_table(m_forwarder) |
| 109 | , m_face(make_shared<InternalFace>()) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame^] | 110 | , m_publisher(m_table, *m_face, "/localhost/nfd/FaceStatusPublisherFixture", m_keyChain) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 111 | , m_finished(false) |
| 112 | { |
| 113 | |
| 114 | } |
| 115 | |
| 116 | virtual |
| 117 | ~FaceStatusPublisherFixture() |
| 118 | { |
| 119 | |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | add(shared_ptr<Face> face) |
| 124 | { |
| 125 | m_table.add(face); |
| 126 | } |
| 127 | |
| 128 | void |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 129 | validateFaceStatus(const Block& statusBlock, const shared_ptr<Face>& reference) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 130 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 131 | ndn::nfd::FaceStatus status; |
| 132 | BOOST_REQUIRE_NO_THROW(status.wireDecode(statusBlock)); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 133 | const FaceCounters& counters = reference->getCounters(); |
| 134 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 135 | BOOST_CHECK_EQUAL(status.getFaceId(), reference->getId()); |
| 136 | BOOST_CHECK_EQUAL(status.getRemoteUri(), reference->getRemoteUri().toString()); |
| 137 | BOOST_CHECK_EQUAL(status.getLocalUri(), reference->getLocalUri().toString()); |
| 138 | BOOST_CHECK_EQUAL(status.getFlags(), getFaceFlags(*reference)); |
| 139 | BOOST_CHECK_EQUAL(status.getNInInterests(), counters.getNInInterests()); |
| 140 | BOOST_CHECK_EQUAL(status.getNInDatas(), counters.getNInDatas()); |
| 141 | BOOST_CHECK_EQUAL(status.getNOutInterests(), counters.getNOutInterests()); |
| 142 | BOOST_CHECK_EQUAL(status.getNOutDatas(), counters.getNOutDatas()); |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -0700 | [diff] [blame] | 143 | BOOST_CHECK_EQUAL(status.getNInBytes(), counters.getNInBytes()); |
| 144 | BOOST_CHECK_EQUAL(status.getNOutBytes(), counters.getNOutBytes()); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void |
| 148 | decodeFaceStatusBlock(const Data& data) |
| 149 | { |
| 150 | Block payload = data.getContent(); |
| 151 | |
| 152 | m_buffer.appendByteArray(payload.value(), payload.value_size()); |
| 153 | |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 154 | BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment()); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 155 | if (data.getFinalBlockId() != data.getName()[-1]) |
| 156 | { |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | // wrap the Face Statuses in a single Content TLV for easy parsing |
| 161 | m_buffer.prependVarNumber(m_buffer.size()); |
| 162 | m_buffer.prependVarNumber(ndn::Tlv::Content); |
| 163 | |
| 164 | ndn::Block parser(m_buffer.buf(), m_buffer.size()); |
| 165 | parser.parse(); |
| 166 | |
| 167 | BOOST_REQUIRE_EQUAL(parser.elements_size(), m_referenceFaces.size()); |
| 168 | |
| 169 | std::list<shared_ptr<Face> >::const_iterator iReference = m_referenceFaces.begin(); |
| 170 | for (Block::element_const_iterator i = parser.elements_begin(); |
| 171 | i != parser.elements_end(); |
| 172 | ++i) |
| 173 | { |
| 174 | if (i->type() != ndn::tlv::nfd::FaceStatus) |
| 175 | { |
| 176 | BOOST_FAIL("expected face status, got type #" << i->type()); |
| 177 | } |
| 178 | validateFaceStatus(*i, *iReference); |
| 179 | ++iReference; |
| 180 | } |
| 181 | m_finished = true; |
| 182 | } |
| 183 | |
| 184 | protected: |
| 185 | Forwarder m_forwarder; |
| 186 | FaceTable m_table; |
| 187 | shared_ptr<InternalFace> m_face; |
| 188 | FaceStatusPublisher m_publisher; |
| 189 | ndn::EncodingBuffer m_buffer; |
| 190 | std::list<shared_ptr<Face> > m_referenceFaces; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame^] | 191 | ndn::KeyChain m_keyChain; |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 192 | |
| 193 | protected: |
| 194 | bool m_finished; |
| 195 | }; |
| 196 | |
| 197 | } // namespace tests |
| 198 | } // namespace nfd |
| 199 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 200 | #endif // NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP |