blob: e08d1f24f4c5f45910bc884278abff5ea5057980 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Jiewen Tana0497d82015-02-02 21:59:18 -08004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
22#include "publisher/lsdb-dataset-interest-handler.hpp"
23#include "tlv/tlv-nlsr.hpp"
24
25#include "publisher-fixture.hpp"
26#include "../boost-test.hpp"
27
Junxiao Shi3e5120c2016-09-10 16:58:34 +000028#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Jiewen Tana0497d82015-02-02 21:59:18 -080029
30namespace nlsr {
31namespace test {
32
33void
dmcoomes9f936662017-03-02 10:33:09 -060034processDatasetInterest(std::shared_ptr<ndn::util::DummyClientFace> face,
Jiewen Tana0497d82015-02-02 21:59:18 -080035 std::function<bool(const ndn::Block&)> isSameType)
36{
37 face->processEvents(ndn::time::milliseconds(1));
38
Junxiao Shic778e812016-07-14 15:44:26 +000039 BOOST_REQUIRE_EQUAL(face->sentData.size(), 1);
40 ndn::Block parser(face->sentData[0].getContent());
Jiewen Tana0497d82015-02-02 21:59:18 -080041 parser.parse();
42
43 ndn::Block::element_const_iterator it = parser.elements_begin();
44 BOOST_CHECK_EQUAL(isSameType(*it), true);
45 ++it;
46
47 BOOST_CHECK(it == parser.elements_end());
48
Junxiao Shic778e812016-07-14 15:44:26 +000049 face->sentData.clear();
Jiewen Tana0497d82015-02-02 21:59:18 -080050}
51
52void
dmcoomes9f936662017-03-02 10:33:09 -060053checkErrorResponse(std::shared_ptr<ndn::util::DummyClientFace> face, uint64_t expectedCode)
Jiewen Tana0497d82015-02-02 21:59:18 -080054{
Junxiao Shic778e812016-07-14 15:44:26 +000055 BOOST_REQUIRE_EQUAL(face->sentData.size(), 1);
Jiewen Tana0497d82015-02-02 21:59:18 -080056
Junxiao Shic778e812016-07-14 15:44:26 +000057 ndn::nfd::ControlResponse response(face->sentData[0].getContent().blockFromValue());
Jiewen Tana0497d82015-02-02 21:59:18 -080058 BOOST_CHECK_EQUAL(response.getCode(), expectedCode);
59
Junxiao Shic778e812016-07-14 15:44:26 +000060 face->sentData.clear();
Jiewen Tana0497d82015-02-02 21:59:18 -080061}
62
63BOOST_FIXTURE_TEST_SUITE(PublisherTestLsdbDatasetInterestHandler, PublisherFixture)
64
65BOOST_AUTO_TEST_CASE(Basic)
66{
67 // Install adjacency LSA
68 AdjLsa adjLsa;
69 adjLsa.setOrigRouter("/RouterA");
70 addAdjacency(adjLsa, "/RouterA/adjacency1", "udp://face-1", 10);
71 lsdb.installAdjLsa(adjLsa);
72
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060073 std::vector<double> angles = {20.00, 30.00};
74
Jiewen Tana0497d82015-02-02 21:59:18 -080075 // Install coordinate LSA
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -060076 CoordinateLsa coordinateLsa = createCoordinateLsa("/RouterA", 10.0, angles);
Jiewen Tana0497d82015-02-02 21:59:18 -080077 lsdb.installCoordinateLsa(coordinateLsa);
78
79 // Install Name LSA
80 NameLsa nameLsa;
81 nameLsa.setOrigRouter("/RouterA");
82 nameLsa.addName("/RouterA/name1");
83 lsdb.installNameLsa(nameLsa);
84
85 ndn::Name thisRouter("/This/Router");
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -050086 LsdbDatasetInterestHandler publisher(lsdb, *face, keyChain);
87 publisher.setRouterNameCommandPrefix(thisRouter);
88
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050089 publisher.startListeningOnLocalhost();
Jiewen Tana0497d82015-02-02 21:59:18 -080090
91 face->processEvents(ndn::time::milliseconds(10));
92
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050093 // Localhost prefix
94 ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix();
Jiewen Tana0497d82015-02-02 21:59:18 -080095
96 // Request adjacency LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050097 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("adjacencies")));
Jiewen Tana0497d82015-02-02 21:59:18 -080098 processDatasetInterest(face,
99 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; });
100
101 // Request coordinate LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500102 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("coordinates")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800103 processDatasetInterest(face,
104 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; });
105
106 // Request Name LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500107 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("names")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800108 processDatasetInterest(face,
109 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; });
110
111 // Request LSDB Status
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500112 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("list")));
113 processDatasetInterest(face,
114 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; });
115
116 // Router name prefix
117 ndn::Name routerCommandPrefix = publisher.getLocalhostCommandPrefix();
118
119 // Request adjacency LSAs
120 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("adjacencies")));
121 processDatasetInterest(face,
122 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; });
123
124 // Request coordinate LSAs
125 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("coordinates")));
126 processDatasetInterest(face,
127 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; });
128
129 // Request Name LSAs
130 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("names")));
131 processDatasetInterest(face,
132 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; });
133
134 // Request LSDB Status
135 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("list")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800136 processDatasetInterest(face,
137 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; });
138}
139
140BOOST_AUTO_TEST_CASE(InvalidCommand)
141{
142 ndn::Name thisRouter("/This/Router");
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500143 LsdbDatasetInterestHandler publisher(lsdb, *face, keyChain);
144 publisher.setRouterNameCommandPrefix(thisRouter);
Jiewen Tana0497d82015-02-02 21:59:18 -0800145
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500146 // Localhost prefix
147 publisher.startListeningOnLocalhost();
Jiewen Tana0497d82015-02-02 21:59:18 -0800148 face->processEvents(ndn::time::milliseconds(10));
149
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500150 ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix();
Jiewen Tana0497d82015-02-02 21:59:18 -0800151
152 // Unsupported command
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500153 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("unsupported")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800154 face->processEvents(ndn::time::milliseconds(1));
155
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500156 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND);
Jiewen Tana0497d82015-02-02 21:59:18 -0800157
158 // Long malformed command
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500159 face->receive(
160 ndn::Interest(ndn::Name(localhostCommandPrefix).append("extra").append("malformed")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800161 face->processEvents(ndn::time::milliseconds(1));
162
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500163 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND);
164
165 // Router name prefix
166 publisher.startListeningOnRouterPrefix();
167 face->processEvents(ndn::time::milliseconds(10));
168
169 ndn::Name remoteCommandPrefix = publisher.getRouterNameCommandPrefix();
170
171 // Unsupported command
172 face->receive(ndn::Interest(ndn::Name(remoteCommandPrefix).append("unsupported")));
173 face->processEvents(ndn::time::milliseconds(1));
174
175 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND);
176
177 // Long malformed command
178 face->receive(ndn::Interest(ndn::Name(remoteCommandPrefix).append("extra").append("malformed")));
179 face->processEvents(ndn::time::milliseconds(1));
180
181 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND);
Jiewen Tana0497d82015-02-02 21:59:18 -0800182
183 // Short malformed command
184 face->receive(ndn::Interest(ndn::Name(thisRouter).append("malformed")));
185 face->processEvents(ndn::time::milliseconds(1));
186
Junxiao Shic778e812016-07-14 15:44:26 +0000187 BOOST_CHECK_EQUAL(face->sentData.size(), 0);
Jiewen Tana0497d82015-02-02 21:59:18 -0800188}
189
190BOOST_AUTO_TEST_SUITE_END()
191
192} // namespace test
193} // namespace nlsr