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