blob: d26682c0b92a8402be6d5abd4dc6b082e25ca130 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shic778e812016-07-14 15:44:26 +00003 * Copyright (c) 2014-2016, 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
34processDatasetInterest(shared_ptr<ndn::util::DummyClientFace> face,
35 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
53checkErrorResponse(shared_ptr<ndn::util::DummyClientFace> face, uint64_t expectedCode)
54{
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
73 // Install coordinate LSA
74 CoordinateLsa coordinateLsa = createCoordinateLsa("/RouterA", 10.0, 20.0);
75 lsdb.installCoordinateLsa(coordinateLsa);
76
77 // Install Name LSA
78 NameLsa nameLsa;
79 nameLsa.setOrigRouter("/RouterA");
80 nameLsa.addName("/RouterA/name1");
81 lsdb.installNameLsa(nameLsa);
82
83 ndn::Name thisRouter("/This/Router");
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -050084 LsdbDatasetInterestHandler publisher(lsdb, *face, keyChain);
85 publisher.setRouterNameCommandPrefix(thisRouter);
86
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050087 publisher.startListeningOnLocalhost();
Jiewen Tana0497d82015-02-02 21:59:18 -080088
89 face->processEvents(ndn::time::milliseconds(10));
90
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050091 // Localhost prefix
92 ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix();
Jiewen Tana0497d82015-02-02 21:59:18 -080093
94 // Request adjacency LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050095 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("adjacencies")));
Jiewen Tana0497d82015-02-02 21:59:18 -080096 processDatasetInterest(face,
97 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; });
98
99 // Request coordinate LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500100 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("coordinates")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800101 processDatasetInterest(face,
102 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; });
103
104 // Request Name LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500105 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("names")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800106 processDatasetInterest(face,
107 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; });
108
109 // Request LSDB Status
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500110 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("list")));
111 processDatasetInterest(face,
112 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; });
113
114 // Router name prefix
115 ndn::Name routerCommandPrefix = publisher.getLocalhostCommandPrefix();
116
117 // Request adjacency LSAs
118 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("adjacencies")));
119 processDatasetInterest(face,
120 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; });
121
122 // Request coordinate LSAs
123 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("coordinates")));
124 processDatasetInterest(face,
125 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; });
126
127 // Request Name LSAs
128 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("names")));
129 processDatasetInterest(face,
130 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; });
131
132 // Request LSDB Status
133 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("list")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800134 processDatasetInterest(face,
135 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; });
136}
137
138BOOST_AUTO_TEST_CASE(InvalidCommand)
139{
140 ndn::Name thisRouter("/This/Router");
Muktadir R Chowdhury3ac07282016-06-17 16:30:29 -0500141 LsdbDatasetInterestHandler publisher(lsdb, *face, keyChain);
142 publisher.setRouterNameCommandPrefix(thisRouter);
Jiewen Tana0497d82015-02-02 21:59:18 -0800143
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500144 // Localhost prefix
145 publisher.startListeningOnLocalhost();
Jiewen Tana0497d82015-02-02 21:59:18 -0800146 face->processEvents(ndn::time::milliseconds(10));
147
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500148 ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix();
Jiewen Tana0497d82015-02-02 21:59:18 -0800149
150 // Unsupported command
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500151 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("unsupported")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800152 face->processEvents(ndn::time::milliseconds(1));
153
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500154 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND);
Jiewen Tana0497d82015-02-02 21:59:18 -0800155
156 // Long malformed command
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500157 face->receive(
158 ndn::Interest(ndn::Name(localhostCommandPrefix).append("extra").append("malformed")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800159 face->processEvents(ndn::time::milliseconds(1));
160
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500161 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND);
162
163 // Router name prefix
164 publisher.startListeningOnRouterPrefix();
165 face->processEvents(ndn::time::milliseconds(10));
166
167 ndn::Name remoteCommandPrefix = publisher.getRouterNameCommandPrefix();
168
169 // Unsupported command
170 face->receive(ndn::Interest(ndn::Name(remoteCommandPrefix).append("unsupported")));
171 face->processEvents(ndn::time::milliseconds(1));
172
173 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND);
174
175 // Long malformed command
176 face->receive(ndn::Interest(ndn::Name(remoteCommandPrefix).append("extra").append("malformed")));
177 face->processEvents(ndn::time::milliseconds(1));
178
179 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND);
Jiewen Tana0497d82015-02-02 21:59:18 -0800180
181 // Short malformed command
182 face->receive(ndn::Interest(ndn::Name(thisRouter).append("malformed")));
183 face->processEvents(ndn::time::milliseconds(1));
184
Junxiao Shic778e812016-07-14 15:44:26 +0000185 BOOST_CHECK_EQUAL(face->sentData.size(), 0);
Jiewen Tana0497d82015-02-02 21:59:18 -0800186}
187
188BOOST_AUTO_TEST_SUITE_END()
189
190} // namespace test
191} // namespace nlsr