blob: 6027137617c9ac9841c4aee32b9c9552c5a6a686 [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
Steve DiBenedettoef04f272014-06-04 14:28:31 -060031namespace nfd {
32namespace tests {
33
34class ChannelStatusPublisherFixture : BaseFixture
35{
36public:
37 ChannelStatusPublisherFixture()
38 : m_face(make_shared<InternalFace>())
Vince Lehman5144f822014-07-23 15:12:56 -070039 , m_publisher(m_factories, *m_face, "/localhost/nfd/faces/channels", m_keyChain)
Steve DiBenedettoef04f272014-06-04 14:28:31 -060040 , m_finished(false)
41 {
42 }
43
44 virtual
45 ~ChannelStatusPublisherFixture()
46 {
47 }
48
49 // virtual shared_ptr<DummyProtocolFactory>
50 // addProtocolFactory(const std::string& protocol)
51 // {
52 // shared_ptr<DummyProtocolFactory> factory(make_shared<DummyProtocolFactory>());
53 // m_factories[protocol] = factory;
54
55 // return factory;
56 // }
57
58 void
59 validatePublish(const Data& data)
60 {
61 Block payload = data.getContent();
62
63 m_buffer.appendByteArray(payload.value(), payload.value_size());
64
65 BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment());
66 if (data.getFinalBlockId() != data.getName()[-1])
67 {
68 return;
69 }
70
71 // wrap the Channel Status entries in a single Content TLV for easy parsing
72 m_buffer.prependVarNumber(m_buffer.size());
Junxiao Shi67f11ac2014-10-19 09:29:13 -070073 m_buffer.prependVarNumber(tlv::Content);
Steve DiBenedettoef04f272014-06-04 14:28:31 -060074
75 ndn::Block parser(m_buffer.buf(), m_buffer.size());
76 parser.parse();
77
78 BOOST_REQUIRE_EQUAL(parser.elements_size(), m_expectedEntries.size());
79
80 for (Block::element_const_iterator i = parser.elements_begin();
81 i != parser.elements_end();
82 ++i)
83 {
84 if (i->type() != ndn::tlv::nfd::ChannelStatus)
85 {
86 BOOST_FAIL("expected ChannelStatus, got type #" << i->type());
87 }
88
89 ndn::nfd::ChannelStatus entry(*i);
90
Steve DiBenedettoef04f272014-06-04 14:28:31 -060091 std::map<std::string, ndn::nfd::ChannelStatus>::const_iterator expectedEntryPos =
92 m_expectedEntries.find(entry.getLocalUri());
93
94 BOOST_REQUIRE(expectedEntryPos != m_expectedEntries.end());
95 const ndn::nfd::ChannelStatus& expectedEntry = expectedEntryPos->second;
96
97 BOOST_CHECK_EQUAL(entry.getLocalUri(), expectedEntry.getLocalUri());
98
99 m_matchedEntries.insert(entry.getLocalUri());
100 }
101
102 BOOST_CHECK_EQUAL(m_matchedEntries.size(), m_expectedEntries.size());
103
104 m_finished = true;
105 }
106
107protected:
108 ChannelStatusPublisher::FactoryMap m_factories;
109 shared_ptr<InternalFace> m_face;
110 ChannelStatusPublisher m_publisher;
111
112 ndn::EncodingBuffer m_buffer;
113
114 std::map<std::string, ndn::nfd::ChannelStatus> m_expectedEntries;
115 std::set<std::string> m_matchedEntries;
116
117 bool m_finished;
Vince Lehman5144f822014-07-23 15:12:56 -0700118
119 ndn::KeyChain m_keyChain;
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600120};
121
122BOOST_FIXTURE_TEST_SUITE(MgmtChannelStatusPublisher, ChannelStatusPublisherFixture)
123
124BOOST_AUTO_TEST_CASE(Publish)
125{
126 const std::string protocol = "dummy";
127
128 shared_ptr<DummyProtocolFactory> factory(make_shared<DummyProtocolFactory>());
129 m_factories[protocol] = factory;
130
131 for (int i = 0; i < 10; ++i)
132 {
133 const std::string uri = protocol + "://path" + boost::lexical_cast<std::string>(i);
134 factory->addChannel(uri);
135
136 ndn::nfd::ChannelStatus expectedEntry;
137 expectedEntry.setLocalUri(DummyChannel(uri).getUri().toString());
138
139 m_expectedEntries[expectedEntry.getLocalUri()] = expectedEntry;
140 }
141
Junxiao Shic099ddb2014-12-25 20:53:20 -0700142 m_face->onReceiveData.connect(bind(&ChannelStatusPublisherFixture::validatePublish, this, _1));
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600143
144 m_publisher.publish();
145 BOOST_REQUIRE(m_finished);
146}
147
148BOOST_AUTO_TEST_CASE(DuplicateFactories)
149{
150 const std::string protocol1 = "dummy1";
151 const std::string protocol2 = "dummy2";
152
153 shared_ptr<DummyProtocolFactory> factory(make_shared<DummyProtocolFactory>());
154 m_factories[protocol1] = factory;
155 m_factories[protocol2] = factory;
156
157 for (int i = 0; i < 10; ++i)
158 {
159 ndn::nfd::ChannelStatus expectedEntry;
160 const std::string uri = protocol1 + "://path" + boost::lexical_cast<std::string>(i);
161
162 factory->addChannel(uri);
163
164 expectedEntry.setLocalUri(DummyChannel(uri).getUri().toString());
165 m_expectedEntries[expectedEntry.getLocalUri()] = expectedEntry;
166 }
167
Junxiao Shic099ddb2014-12-25 20:53:20 -0700168 m_face->onReceiveData.connect(bind(&ChannelStatusPublisherFixture::validatePublish, this, _1));
Steve DiBenedettoef04f272014-06-04 14:28:31 -0600169
170 m_publisher.publish();
171 BOOST_REQUIRE(m_finished);
172}
173
174BOOST_AUTO_TEST_SUITE_END()
175
176} // namespace tests
177
178
179
180} // namespace nfd