blob: e85bc4ae659ff1974adbcbe4af3cb00ed3e1555e [file] [log] [blame]
Steve DiBenedetto3fff5612014-05-30 15:52:56 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehman5144f822014-07-23 15:12:56 -07003 * 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
Steve DiBenedetto3fff5612014-05-30 15:52:56 -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/>.
Vince Lehman5144f822014-07-23 15:12:56 -070024 */
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060025
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>())
Vince Lehman5144f822014-07-23 15:12:56 -070049 , m_publisher(m_strategyChoice, *m_face, "/localhost/nfd/strategy-choice/list", m_keyChain)
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060050 , 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());
Junxiao Shi67f11ac2014-10-19 09:29:13 -070082 m_buffer.prependVarNumber(tlv::Content);
Steve DiBenedetto3fff5612014-05-30 15:52:56 -060083
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;
Vince Lehman5144f822014-07-23 15:12:56 -0700132
133 ndn::KeyChain m_keyChain;
Steve DiBenedetto3fff5612014-05-30 15:52:56 -0600134};
135
136
137
138BOOST_FIXTURE_TEST_SUITE(MgmtStrategyChoicePublisher, StrategyChoicePublisherFixture)
139
140BOOST_AUTO_TEST_CASE(Publish)
141{
142 m_strategyChoice.insert("/test/a", STRATEGY_A->getName());
143 m_strategyChoice.insert("/test/b", STRATEGY_B->getName());
144
145 ndn::nfd::StrategyChoice expectedEntryA;
146 expectedEntryA.setStrategy(STRATEGY_A->getName());
147 expectedEntryA.setName("/test/a");
148
149 ndn::nfd::StrategyChoice expectedEntryB;
150 expectedEntryB.setStrategy(STRATEGY_B->getName());
151 expectedEntryB.setName("/test/b");
152
153 m_expectedEntries["/test/a"] = expectedEntryA;
154 m_expectedEntries["/test/b"] = expectedEntryB;
155
156 m_face->onReceiveData +=
157 bind(&StrategyChoicePublisherFixture::validatePublish, this, _1);
158
159 m_publisher.publish();
160 BOOST_REQUIRE(m_finished);
161}
162
163
164BOOST_AUTO_TEST_SUITE_END()
165
166} // namespace tests
167} // namespace nfd
168
169#endif // NFD_TESTS_NFD_MGMT_STRATEGY_CHOICE_PUBLISHER_HPP