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