blob: 11ed191bfe74a7bf62b9e211cf6568268d62c25d [file] [log] [blame]
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 DiBenedetto9f6c3642014-03-10 17:02:27 -060024
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070025#ifndef NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP
26#define NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060027
28#include "mgmt/face-status-publisher.hpp"
29#include "mgmt/app-face.hpp"
30#include "mgmt/internal-face.hpp"
Junxiao Shi6e694322014-04-03 10:27:13 -070031#include "mgmt/face-flags.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060032#include "fw/forwarder.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060033
34#include "tests/test-common.hpp"
Alexander Afanasyev613e2a92014-04-15 13:36:58 -070035#include "tests/daemon/face/dummy-face.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060036
Junxiao Shi6e694322014-04-03 10:27:13 -070037#include <ndn-cpp-dev/management/nfd-face-status.hpp>
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060038
39namespace nfd {
40namespace tests {
41
42class TestCountersFace : public DummyFace
43{
44public:
45
46 TestCountersFace()
47 {
48 }
49
50 virtual
51 ~TestCountersFace()
52 {
53 }
54
55 void
Junxiao Shi6e694322014-04-03 10:27:13 -070056 setCounters(FaceCounter nInInterests,
57 FaceCounter nInDatas,
58 FaceCounter nOutInterests,
59 FaceCounter nOutDatas)
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060060 {
61 FaceCounters& counters = getMutableCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -070062 counters.getNInInterests() = nInInterests;
63 counters.getNInDatas() = nInDatas;
64 counters.getNOutInterests() = nOutInterests;
65 counters.getNOutDatas() = nOutDatas;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060066 }
67
68
69};
70
71static inline uint64_t
72readNonNegativeIntegerType(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
84static inline uint64_t
85checkedReadNonNegativeIntegerType(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
98class FaceStatusPublisherFixture : public BaseFixture
99{
100public:
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 Shi6e694322014-04-03 10:27:13 -0700124 validateFaceStatus(const Block& statusBlock, const shared_ptr<Face>& reference)
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600125 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700126 ndn::nfd::FaceStatus status;
127 BOOST_REQUIRE_NO_THROW(status.wireDecode(statusBlock));
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600128 const FaceCounters& counters = reference->getCounters();
129
Junxiao Shi6e694322014-04-03 10:27:13 -0700130 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 DiBenedetto9f6c3642014-03-10 17:02:27 -0600138 }
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 Afanasyevefea8fe2014-03-23 00:00:35 -0700147 BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment());
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600148 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
177protected:
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
185protected:
186 bool m_finished;
187};
188
189} // namespace tests
190} // namespace nfd
191
Alexander Afanasyev613e2a92014-04-15 13:36:58 -0700192#endif // NFD_TESTS_NFD_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP