Steve DiBenedetto | ef04f27 | 2014-06-04 14:28:31 -0600 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * 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 | * The University of Memphis |
| 10 | * |
| 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" |
| 32 | NFD_LOG_INIT("TestChannelStatusPublisher"); |
| 33 | |
| 34 | namespace nfd { |
| 35 | namespace tests { |
| 36 | |
| 37 | class ChannelStatusPublisherFixture : BaseFixture |
| 38 | { |
| 39 | public: |
| 40 | ChannelStatusPublisherFixture() |
| 41 | : m_face(make_shared<InternalFace>()) |
| 42 | , m_publisher(m_factories, m_face, "/localhost/nfd/faces/channels") |
| 43 | , 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()); |
| 76 | m_buffer.prependVarNumber(ndn::Tlv::Content); |
| 77 | |
| 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 | |
| 112 | protected: |
| 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; |
| 123 | }; |
| 124 | |
| 125 | BOOST_FIXTURE_TEST_SUITE(MgmtChannelStatusPublisher, ChannelStatusPublisherFixture) |
| 126 | |
| 127 | BOOST_AUTO_TEST_CASE(Publish) |
| 128 | { |
| 129 | const std::string protocol = "dummy"; |
| 130 | |
| 131 | shared_ptr<DummyProtocolFactory> factory(make_shared<DummyProtocolFactory>()); |
| 132 | m_factories[protocol] = factory; |
| 133 | |
| 134 | for (int i = 0; i < 10; ++i) |
| 135 | { |
| 136 | const std::string uri = protocol + "://path" + boost::lexical_cast<std::string>(i); |
| 137 | factory->addChannel(uri); |
| 138 | |
| 139 | ndn::nfd::ChannelStatus expectedEntry; |
| 140 | expectedEntry.setLocalUri(DummyChannel(uri).getUri().toString()); |
| 141 | |
| 142 | m_expectedEntries[expectedEntry.getLocalUri()] = expectedEntry; |
| 143 | } |
| 144 | |
| 145 | m_face->onReceiveData += |
| 146 | bind(&ChannelStatusPublisherFixture::validatePublish, this, _1); |
| 147 | |
| 148 | m_publisher.publish(); |
| 149 | BOOST_REQUIRE(m_finished); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_CASE(DuplicateFactories) |
| 153 | { |
| 154 | const std::string protocol1 = "dummy1"; |
| 155 | const std::string protocol2 = "dummy2"; |
| 156 | |
| 157 | shared_ptr<DummyProtocolFactory> factory(make_shared<DummyProtocolFactory>()); |
| 158 | m_factories[protocol1] = factory; |
| 159 | m_factories[protocol2] = factory; |
| 160 | |
| 161 | for (int i = 0; i < 10; ++i) |
| 162 | { |
| 163 | ndn::nfd::ChannelStatus expectedEntry; |
| 164 | const std::string uri = protocol1 + "://path" + boost::lexical_cast<std::string>(i); |
| 165 | |
| 166 | factory->addChannel(uri); |
| 167 | |
| 168 | expectedEntry.setLocalUri(DummyChannel(uri).getUri().toString()); |
| 169 | m_expectedEntries[expectedEntry.getLocalUri()] = expectedEntry; |
| 170 | } |
| 171 | |
| 172 | m_face->onReceiveData += |
| 173 | bind(&ChannelStatusPublisherFixture::validatePublish, this, _1); |
| 174 | |
| 175 | m_publisher.publish(); |
| 176 | BOOST_REQUIRE(m_finished); |
| 177 | } |
| 178 | |
| 179 | BOOST_AUTO_TEST_SUITE_END() |
| 180 | |
| 181 | } // namespace tests |
| 182 | |
| 183 | |
| 184 | |
| 185 | } // namespace nfd |