blob: 15c6eeae14780276854fcfd697117e3edfe05d04 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, The University of Memphis,
4 * 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
28#include <ndn-cxx/management/nfd-control-response.hpp>
29
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
39 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
40 ndn::Block parser(face->sentDatas[0].getContent());
41 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
49 face->sentDatas.clear();
50}
51
52void
53checkErrorResponse(shared_ptr<ndn::util::DummyClientFace> face, uint64_t expectedCode)
54{
55 BOOST_REQUIRE_EQUAL(face->sentDatas.size(), 1);
56
57 ndn::nfd::ControlResponse response(face->sentDatas[0].getContent().blockFromValue());
58 BOOST_CHECK_EQUAL(response.getCode(), expectedCode);
59
60 face->sentDatas.clear();
61}
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");
84 LsdbDatasetInterestHandler publisher(lsdb, *face, thisRouter, keyChain);
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050085 publisher.startListeningOnLocalhost();
Jiewen Tana0497d82015-02-02 21:59:18 -080086
87 face->processEvents(ndn::time::milliseconds(10));
88
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050089 // Localhost prefix
90 ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix();
Jiewen Tana0497d82015-02-02 21:59:18 -080091
92 // Request adjacency LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050093 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("adjacencies")));
Jiewen Tana0497d82015-02-02 21:59:18 -080094 processDatasetInterest(face,
95 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; });
96
97 // Request coordinate LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050098 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("coordinates")));
Jiewen Tana0497d82015-02-02 21:59:18 -080099 processDatasetInterest(face,
100 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; });
101
102 // Request Name LSAs
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500103 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("names")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800104 processDatasetInterest(face,
105 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; });
106
107 // Request LSDB Status
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500108 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("list")));
109 processDatasetInterest(face,
110 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; });
111
112 // Router name prefix
113 ndn::Name routerCommandPrefix = publisher.getLocalhostCommandPrefix();
114
115 // Request adjacency LSAs
116 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("adjacencies")));
117 processDatasetInterest(face,
118 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; });
119
120 // Request coordinate LSAs
121 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("coordinates")));
122 processDatasetInterest(face,
123 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; });
124
125 // Request Name LSAs
126 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("names")));
127 processDatasetInterest(face,
128 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; });
129
130 // Request LSDB Status
131 face->receive(ndn::Interest(ndn::Name(routerCommandPrefix).append("list")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800132 processDatasetInterest(face,
133 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; });
134}
135
136BOOST_AUTO_TEST_CASE(InvalidCommand)
137{
138 ndn::Name thisRouter("/This/Router");
139 LsdbDatasetInterestHandler publisher(lsdb, *face, thisRouter, keyChain);
140
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500141 // Localhost prefix
142 publisher.startListeningOnLocalhost();
Jiewen Tana0497d82015-02-02 21:59:18 -0800143 face->processEvents(ndn::time::milliseconds(10));
144
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500145 ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix();
Jiewen Tana0497d82015-02-02 21:59:18 -0800146
147 // Unsupported command
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500148 face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("unsupported")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800149 face->processEvents(ndn::time::milliseconds(1));
150
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500151 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND);
Jiewen Tana0497d82015-02-02 21:59:18 -0800152
153 // Long malformed command
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500154 face->receive(
155 ndn::Interest(ndn::Name(localhostCommandPrefix).append("extra").append("malformed")));
Jiewen Tana0497d82015-02-02 21:59:18 -0800156 face->processEvents(ndn::time::milliseconds(1));
157
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500158 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND);
159
160 // Router name prefix
161 publisher.startListeningOnRouterPrefix();
162 face->processEvents(ndn::time::milliseconds(10));
163
164 ndn::Name remoteCommandPrefix = publisher.getRouterNameCommandPrefix();
165
166 // Unsupported command
167 face->receive(ndn::Interest(ndn::Name(remoteCommandPrefix).append("unsupported")));
168 face->processEvents(ndn::time::milliseconds(1));
169
170 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND);
171
172 // Long malformed command
173 face->receive(ndn::Interest(ndn::Name(remoteCommandPrefix).append("extra").append("malformed")));
174 face->processEvents(ndn::time::milliseconds(1));
175
176 checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_MALFORMED_COMMAND);
Jiewen Tana0497d82015-02-02 21:59:18 -0800177
178 // Short malformed command
179 face->receive(ndn::Interest(ndn::Name(thisRouter).append("malformed")));
180 face->processEvents(ndn::time::milliseconds(1));
181
182 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
183}
184
185BOOST_AUTO_TEST_SUITE_END()
186
187} // namespace test
188} // namespace nlsr