blob: 08fbe672027b388fb5e7f85924cc16d580822dd5 [file] [log] [blame]
Steve DiBenedettoef04f272014-06-04 14:28:31 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, 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.
Steve DiBenedettoef04f272014-06-04 14:28:31 -060010 *
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/>.
24 */
25
26#include "mgmt/channel-status-publisher.hpp"
27#include "mgmt/internal-face.hpp"
28
29#include "channel-status-common.hpp"
30
31#include "core/logger.hpp"
32NFD_LOG_INIT("TestChannelStatusPublisher");
33
34namespace nfd {
35namespace tests {
36
37class ChannelStatusPublisherFixture : BaseFixture
38{
39public:
40 ChannelStatusPublisherFixture()
41 : m_face(make_shared<InternalFace>())
Vince Lehman5144f822014-07-23 15:12:56 -070042 , m_publisher(m_factories, *m_face, "/localhost/nfd/faces/channels", m_keyChain)
Steve DiBenedettoef04f272014-06-04 14:28:31 -060043 , m_finished(false)
44 {
45 }
46
47 virtual
48 ~ChannelStatusPublisherFixture()
49 {
50 }
51
52 // virtual shared_ptr<DummyProtocolFactory>
53 // addProtocolFactory(const std::string& protocol)
54 // {
55 // shared_ptr<DummyProtocolFactory> factory(make_shared<DummyProtocolFactory>());
56 // m_factories[protocol] = factory;
57
58 // return factory;
59 // }
60
61 void
62 validatePublish(const Data& data)
63 {
64 Block payload = data.getContent();
65
66 m_buffer.appendByteArray(payload.value(), payload.value_size());
67
68 BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment());
69 if (data.getFinalBlockId() != data.getName()[-1])
70 {
71 return;
72 }
73
74 // wrap the Channel Status entries in a single Content TLV for easy parsing
75 m_buffer.prependVarNumber(m_buffer.size());
Junxiao Shi67f11ac2014-10-19 09:29:13 -070076 m_buffer.prependVarNumber(tlv::Content);
Steve DiBenedettoef04f272014-06-04 14:28:31 -060077
78 ndn::Block parser(m_buffer.buf(), m_buffer.size());
79 parser.parse();
80
81 BOOST_REQUIRE_EQUAL(parser.elements_size(), m_expectedEntries.size());
82
83 for (Block::element_const_iterator i = parser.elements_begin();
84 i != parser.elements_end();
85 ++i)
86 {
87 if (i->type() != ndn::tlv::nfd::ChannelStatus)
88 {
89 BOOST_FAIL("expected ChannelStatus, got type #" << i->type());
90 }
91
92 ndn::nfd::ChannelStatus entry(*i);
93
94 NFD_LOG_DEBUG("looking for channelstatus " << entry.getLocalUri());
95
96 std::map<std::string, ndn::nfd::ChannelStatus>::const_iterator expectedEntryPos =
97 m_expectedEntries.find(entry.getLocalUri());
98
99 BOOST_REQUIRE(expectedEntryPos != m_expectedEntries.end());
100 const ndn::nfd::ChannelStatus& expectedEntry = expectedEntryPos->second;
101
102 BOOST_CHECK_EQUAL(entry.getLocalUri(), expectedEntry.getLocalUri());
103
104 m_matchedEntries.insert(entry.getLocalUri());
105 }
106
107 BOOST_CHECK_EQUAL(m_matchedEntries.size(), m_expectedEntries.size());
108
109 m_finished = true;
110 }
111
112protected:
113 ChannelStatusPublisher::FactoryMap m_factories;
114 shared_ptr<InternalFace> m_face;
115 ChannelStatusPublisher m_publisher;
116
117 ndn::EncodingBuffer m_buffer;
118
119 std::map<std::string, ndn::nfd::ChannelStatus> m_expectedEntries;
120 std::set<std::string> m_matchedEntries;
121
122 bool m_finished;
Vince Lehman5144f822014-07-23 15:12:56 -0700123
124 ndn::KeyChain m_keyChain;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600125};
126
127BOOST_FIXTURE_TEST_SUITE(MgmtChannelStatusPublisher, ChannelStatusPublisherFixture)
128
129BOOST_AUTO_TEST_CASE(Publish)
130{
131 const std::string protocol = "dummy";
132
133 shared_ptr<DummyProtocolFactory> factory(make_shared<DummyProtocolFactory>());
134 m_factories[protocol] = factory;
135
136 for (int i = 0; i < 10; ++i)
137 {
138 const std::string uri = protocol + "://path" + boost::lexical_cast<std::string>(i);
139 factory->addChannel(uri);
140
141 ndn::nfd::ChannelStatus expectedEntry;
142 expectedEntry.setLocalUri(DummyChannel(uri).getUri().toString());
143
144 m_expectedEntries[expectedEntry.getLocalUri()] = expectedEntry;
145 }
146
Junxiao Shic099ddb2014-12-25 20:53:20 -0700147 m_face->onReceiveData.connect(bind(&ChannelStatusPublisherFixture::validatePublish, this, _1));
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600148
149 m_publisher.publish();
150 BOOST_REQUIRE(m_finished);
151}
152
153BOOST_AUTO_TEST_CASE(DuplicateFactories)
154{
155 const std::string protocol1 = "dummy1";
156 const std::string protocol2 = "dummy2";
157
158 shared_ptr<DummyProtocolFactory> factory(make_shared<DummyProtocolFactory>());
159 m_factories[protocol1] = factory;
160 m_factories[protocol2] = factory;
161
162 for (int i = 0; i < 10; ++i)
163 {
164 ndn::nfd::ChannelStatus expectedEntry;
165 const std::string uri = protocol1 + "://path" + boost::lexical_cast<std::string>(i);
166
167 factory->addChannel(uri);
168
169 expectedEntry.setLocalUri(DummyChannel(uri).getUri().toString());
170 m_expectedEntries[expectedEntry.getLocalUri()] = expectedEntry;
171 }
172
Junxiao Shic099ddb2014-12-25 20:53:20 -0700173 m_face->onReceiveData.connect(bind(&ChannelStatusPublisherFixture::validatePublish, this, _1));
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600174
175 m_publisher.publish();
176 BOOST_REQUIRE(m_finished);
177}
178
179BOOST_AUTO_TEST_SUITE_END()
180
181} // namespace tests
182
183
184
185} // namespace nfd