blob: b3190d4eab0dec23cafe018fa47677123652de93 [file] [log] [blame]
Steve DiBenedetto3fff5612014-05-30 15:52:56 -06001/* -*- 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#ifndef NFD_TESTS_NFD_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP
27#define NFD_TESTS_NFD_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP
28
29#include "mgmt/strategy-choice-publisher.hpp"
30#include "mgmt/app-face.hpp"
31#include "mgmt/internal-face.hpp"
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060032#include "fw/forwarder.hpp"
33#include "../fw/dummy-strategy.hpp"
34
35#include "tests/test-common.hpp"
36
37#include <ndn-cxx/management/nfd-strategy-choice.hpp>
38
39namespace nfd {
40namespace tests {
41
42class StrategyChoicePublisherFixture : BaseFixture
43{
44public:
45
46 StrategyChoicePublisherFixture()
47 : m_strategyChoice(m_forwarder.getStrategyChoice())
48 , m_face(make_shared<InternalFace>())
49 , m_publisher(m_strategyChoice, m_face, "/localhost/nfd/strategy-choice/list")
50 , STRATEGY_A(make_shared<DummyStrategy>(boost::ref(m_forwarder),
51 "/localhost/nfd/strategy/dummy-strategy-a"))
52 , STRATEGY_B(make_shared<DummyStrategy>(boost::ref(m_forwarder),
53 "/localhost/nfd/strategy/dummy-strategy-b"))
54 , m_finished(false)
55 {
56 m_strategyChoice.install(STRATEGY_A);
57 m_strategyChoice.install(STRATEGY_B);
58
59 ndn::nfd::StrategyChoice expectedRootEntry;
60 expectedRootEntry.setStrategy(STRATEGY_A->getName());
61 expectedRootEntry.setName("/");
62
63 m_strategyChoice.insert("/", STRATEGY_A->getName());
64 m_expectedEntries["/"] = expectedRootEntry;
65 }
66
67 void
68 validatePublish(const Data& data)
69 {
70 Block payload = data.getContent();
71
72 m_buffer.appendByteArray(payload.value(), payload.value_size());
73
74 BOOST_CHECK_NO_THROW(data.getName()[-1].toSegment());
75 if (data.getFinalBlockId() != data.getName()[-1])
76 {
77 return;
78 }
79
80 // wrap the Strategy Choice entries in a single Content TLV for easy parsing
81 m_buffer.prependVarNumber(m_buffer.size());
82 m_buffer.prependVarNumber(ndn::Tlv::Content);
83
84 ndn::Block parser(m_buffer.buf(), m_buffer.size());
85 parser.parse();
86
87 BOOST_REQUIRE_EQUAL(parser.elements_size(), m_expectedEntries.size());
88
89 for (Block::element_const_iterator i = parser.elements_begin();
90 i != parser.elements_end();
91 ++i)
92 {
93 if (i->type() != ndn::tlv::nfd::StrategyChoice)
94 {
95 BOOST_FAIL("expected StrategyChoice, got type #" << i->type());
96 }
97
98 ndn::nfd::StrategyChoice entry(*i);
99
100 std::map<std::string, ndn::nfd::StrategyChoice>::const_iterator expectedEntryPos =
101 m_expectedEntries.find(entry.getName().toUri());
102
103 BOOST_REQUIRE(expectedEntryPos != m_expectedEntries.end());
104 const ndn::nfd::StrategyChoice& expectedEntry = expectedEntryPos->second;
105
106 BOOST_CHECK_EQUAL(entry.getStrategy(), expectedEntry.getStrategy());
107 BOOST_CHECK_EQUAL(entry.getName(), expectedEntry.getName());
108
109 m_matchedEntries.insert(entry.getName().toUri());
110 }
111
112 BOOST_CHECK_EQUAL(m_matchedEntries.size(), m_expectedEntries.size());
113
114 m_finished = true;
115 }
116
117protected:
118 Forwarder m_forwarder;
119 StrategyChoice& m_strategyChoice;
120 shared_ptr<InternalFace> m_face;
121 StrategyChoicePublisher m_publisher;
122
123 shared_ptr<DummyStrategy> STRATEGY_A;
124 shared_ptr<DummyStrategy> STRATEGY_B;
125
126 ndn::EncodingBuffer m_buffer;
127
128 std::map<std::string, ndn::nfd::StrategyChoice> m_expectedEntries;
129 std::set<std::string> m_matchedEntries;
130
131 bool m_finished;
132};
133
134
135
136BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoicePublisher, StrategyChoicePublisherFixture)
137
138BOOST_AUTO_TEST_CASE(Publish)
139{
140 m_strategyChoice.insert("/test/a", STRATEGY_A->getName());
141 m_strategyChoice.insert("/test/b", STRATEGY_B->getName());
142
143 ndn::nfd::StrategyChoice expectedEntryA;
144 expectedEntryA.setStrategy(STRATEGY_A->getName());
145 expectedEntryA.setName("/test/a");
146
147 ndn::nfd::StrategyChoice expectedEntryB;
148 expectedEntryB.setStrategy(STRATEGY_B->getName());
149 expectedEntryB.setName("/test/b");
150
151 m_expectedEntries["/test/a"] = expectedEntryA;
152 m_expectedEntries["/test/b"] = expectedEntryB;
153
154 m_face->onReceiveData +=
155 bind(&StrategyChoicePublisherFixture::validatePublish, this, _1);
156
157 m_publisher.publish();
158 BOOST_REQUIRE(m_finished);
159}
160
161
162BOOST_AUTO_TEST_SUITE_END()
163
164} // namespace tests
165} // namespace nfd
166
167#endif // NFD_TESTS_NFD_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP