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" |
| 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 | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 56 | setCounters(PacketCounter::rep nInInterests, |
| 57 | PacketCounter::rep nInDatas, |
| 58 | PacketCounter::rep nOutInterests, |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -0700 | [diff] [blame] | 59 | PacketCounter::rep nOutDatas, |
| 60 | ByteCounter::rep nInBytes, |
| 61 | ByteCounter::rep nOutBytes) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 62 | { |
| 63 | FaceCounters& counters = getMutableCounters(); |
Junxiao Shi | 33152f1 | 2014-07-16 19:54:32 -0700 | [diff] [blame] | 64 | counters.getNInInterests().set(nInInterests); |
| 65 | counters.getNInDatas().set(nInDatas); |
| 66 | counters.getNOutInterests().set(nOutInterests); |
| 67 | counters.getNOutDatas().set(nOutDatas); |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -0700 | [diff] [blame] | 68 | counters.getNInBytes().set(nInBytes); |
| 69 | counters.getNOutBytes().set(nOutBytes); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | |
| 73 | }; |
| 74 | |
| 75 | static inline uint64_t |
| 76 | readNonNegativeIntegerType(const Block& block, |
| 77 | uint32_t type) |
| 78 | { |
| 79 | if (block.type() == type) |
| 80 | { |
| 81 | return readNonNegativeInteger(block); |
| 82 | } |
| 83 | std::stringstream error; |
| 84 | error << "expected type " << type << " got " << block.type(); |
Junxiao Shi | 67f11ac | 2014-10-19 09:29:13 -0700 | [diff] [blame] | 85 | throw tlv::Error(error.str()); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static inline uint64_t |
| 89 | checkedReadNonNegativeIntegerType(Block::element_const_iterator& i, |
| 90 | Block::element_const_iterator end, |
| 91 | uint32_t type) |
| 92 | { |
| 93 | if (i != end) |
| 94 | { |
| 95 | const Block& block = *i; |
| 96 | ++i; |
| 97 | return readNonNegativeIntegerType(block, type); |
| 98 | } |
Junxiao Shi | 67f11ac | 2014-10-19 09:29:13 -0700 | [diff] [blame] | 99 | throw tlv::Error("Unexpected end of FaceStatus"); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | class FaceStatusPublisherFixture : public BaseFixture |
| 103 | { |
| 104 | public: |
| 105 | |
| 106 | FaceStatusPublisherFixture() |
| 107 | : m_table(m_forwarder) |
| 108 | , m_face(make_shared<InternalFace>()) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 109 | , m_publisher(m_table, *m_face, "/localhost/nfd/FaceStatusPublisherFixture", m_keyChain) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 110 | , m_finished(false) |
| 111 | { |
| 112 | |
| 113 | } |
| 114 | |
| 115 | virtual |
| 116 | ~FaceStatusPublisherFixture() |
| 117 | { |
| 118 | |
| 119 | } |
| 120 | |
| 121 | void |
| 122 | add(shared_ptr<Face> face) |
| 123 | { |
| 124 | m_table.add(face); |
| 125 | } |
| 126 | |
| 127 | void |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 128 | validateFaceStatus(const Block& statusBlock, const shared_ptr<Face>& reference) |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 129 | { |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 130 | ndn::nfd::FaceStatus status; |
| 131 | BOOST_REQUIRE_NO_THROW(status.wireDecode(statusBlock)); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 132 | const FaceCounters& counters = reference->getCounters(); |
| 133 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 134 | BOOST_CHECK_EQUAL(status.getFaceId(), reference->getId()); |
| 135 | BOOST_CHECK_EQUAL(status.getRemoteUri(), reference->getRemoteUri().toString()); |
| 136 | BOOST_CHECK_EQUAL(status.getLocalUri(), reference->getLocalUri().toString()); |
Chengyu Fan | f9c2bb1 | 2014-10-06 11:52:44 -0600 | [diff] [blame] | 137 | |
| 138 | if (reference->isLocal()) { |
| 139 | BOOST_CHECK_EQUAL(status.getFaceScope(), ndn::nfd::FACE_SCOPE_LOCAL); |
| 140 | } |
| 141 | else { |
| 142 | BOOST_CHECK_EQUAL(status.getFaceScope(), ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 143 | } |
| 144 | |
| 145 | if (reference->isOnDemand()) { |
| 146 | BOOST_CHECK_EQUAL(status.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_ON_DEMAND); |
| 147 | } |
| 148 | else { |
| 149 | BOOST_CHECK_EQUAL(status.getFacePersistency(), ndn::nfd::FACE_PERSISTENCY_PERSISTENT); |
| 150 | } |
| 151 | |
| 152 | if (reference->isMultiAccess()) { |
| 153 | BOOST_CHECK_EQUAL(status.getLinkType(), ndn::nfd::LINK_TYPE_MULTI_ACCESS); |
| 154 | } |
| 155 | else { |
| 156 | BOOST_CHECK_EQUAL(status.getLinkType(), ndn::nfd::LINK_TYPE_POINT_TO_POINT); |
| 157 | } |
| 158 | |
Junxiao Shi | 6e69432 | 2014-04-03 10:27:13 -0700 | [diff] [blame] | 159 | BOOST_CHECK_EQUAL(status.getNInInterests(), counters.getNInInterests()); |
| 160 | BOOST_CHECK_EQUAL(status.getNInDatas(), counters.getNInDatas()); |
| 161 | BOOST_CHECK_EQUAL(status.getNOutInterests(), counters.getNOutInterests()); |
| 162 | BOOST_CHECK_EQUAL(status.getNOutDatas(), counters.getNOutDatas()); |
Junxiao Shi | 632a620 | 2014-07-20 01:14:30 -0700 | [diff] [blame] | 163 | BOOST_CHECK_EQUAL(status.getNInBytes(), counters.getNInBytes()); |
| 164 | BOOST_CHECK_EQUAL(status.getNOutBytes(), counters.getNOutBytes()); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void |
| 168 | decodeFaceStatusBlock(const Data& data) |
| 169 | { |
| 170 | Block payload = data.getContent(); |
| 171 | |
| 172 | m_buffer.appendByteArray(payload.value(), payload.value_size()); |
| 173 | |
Alexander Afanasyev | efea8fe | 2014-03-23 00:00:35 -0700 | [diff] [blame] | 174 | BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment()); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 175 | if (data.getFinalBlockId() != data.getName()[-1]) |
| 176 | { |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // wrap the Face Statuses in a single Content TLV for easy parsing |
| 181 | m_buffer.prependVarNumber(m_buffer.size()); |
Junxiao Shi | 67f11ac | 2014-10-19 09:29:13 -0700 | [diff] [blame] | 182 | m_buffer.prependVarNumber(tlv::Content); |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 183 | |
| 184 | ndn::Block parser(m_buffer.buf(), m_buffer.size()); |
| 185 | parser.parse(); |
| 186 | |
| 187 | BOOST_REQUIRE_EQUAL(parser.elements_size(), m_referenceFaces.size()); |
| 188 | |
| 189 | std::list<shared_ptr<Face> >::const_iterator iReference = m_referenceFaces.begin(); |
| 190 | for (Block::element_const_iterator i = parser.elements_begin(); |
| 191 | i != parser.elements_end(); |
| 192 | ++i) |
| 193 | { |
| 194 | if (i->type() != ndn::tlv::nfd::FaceStatus) |
| 195 | { |
| 196 | BOOST_FAIL("expected face status, got type #" << i->type()); |
| 197 | } |
| 198 | validateFaceStatus(*i, *iReference); |
| 199 | ++iReference; |
| 200 | } |
| 201 | m_finished = true; |
| 202 | } |
| 203 | |
| 204 | protected: |
| 205 | Forwarder m_forwarder; |
| 206 | FaceTable m_table; |
| 207 | shared_ptr<InternalFace> m_face; |
| 208 | FaceStatusPublisher m_publisher; |
| 209 | ndn::EncodingBuffer m_buffer; |
| 210 | std::list<shared_ptr<Face> > m_referenceFaces; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 211 | ndn::KeyChain m_keyChain; |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 212 | |
| 213 | protected: |
| 214 | bool m_finished; |
| 215 | }; |
| 216 | |
| 217 | } // namespace tests |
| 218 | } // namespace nfd |
| 219 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 220 | #endif // NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP |