blob: 2e7ec55138d7d4995eae02403b658c2ea3135ee9 [file] [log] [blame]
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001/* -*- 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 Shi6e694322014-04-03 10:27:13 -070013#include "mgmt/face-flags.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060014#include "fw/forwarder.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060015
16#include "tests/test-common.hpp"
Junxiao Shi6e694322014-04-03 10:27:13 -070017#include "tests/face/dummy-face.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060018
Junxiao Shi6e694322014-04-03 10:27:13 -070019#include <ndn-cpp-dev/management/nfd-face-status.hpp>
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060020
21namespace nfd {
22namespace tests {
23
24class TestCountersFace : public DummyFace
25{
26public:
27
28 TestCountersFace()
29 {
30 }
31
32 virtual
33 ~TestCountersFace()
34 {
35 }
36
37 void
Junxiao Shi6e694322014-04-03 10:27:13 -070038 setCounters(FaceCounter nInInterests,
39 FaceCounter nInDatas,
40 FaceCounter nOutInterests,
41 FaceCounter nOutDatas)
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060042 {
43 FaceCounters& counters = getMutableCounters();
Junxiao Shi6e694322014-04-03 10:27:13 -070044 counters.getNInInterests() = nInInterests;
45 counters.getNInDatas() = nInDatas;
46 counters.getNOutInterests() = nOutInterests;
47 counters.getNOutDatas() = nOutDatas;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060048 }
49
50
51};
52
53static inline uint64_t
54readNonNegativeIntegerType(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
66static inline uint64_t
67checkedReadNonNegativeIntegerType(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
80class FaceStatusPublisherFixture : public BaseFixture
81{
82public:
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 Shi6e694322014-04-03 10:27:13 -0700106 validateFaceStatus(const Block& statusBlock, const shared_ptr<Face>& reference)
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600107 {
Junxiao Shi6e694322014-04-03 10:27:13 -0700108 ndn::nfd::FaceStatus status;
109 BOOST_REQUIRE_NO_THROW(status.wireDecode(statusBlock));
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600110 const FaceCounters& counters = reference->getCounters();
111
Junxiao Shi6e694322014-04-03 10:27:13 -0700112 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 DiBenedetto9f6c3642014-03-10 17:02:27 -0600120 }
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 Afanasyevefea8fe2014-03-23 00:00:35 -0700129 BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment());
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600130 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
159protected:
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
167protected:
168 bool m_finished;
169};
170
171} // namespace tests
172} // namespace nfd
173
174#endif // NFD_TESTS_MGMT_FACE_STATUS_PUBLISHER_COMMON_HPP