Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 29 | |
| 30 | namespace nlsr { |
| 31 | namespace test { |
| 32 | |
| 33 | void |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 34 | processDatasetInterest(std::shared_ptr<ndn::util::DummyClientFace> face, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 35 | std::function<bool(const ndn::Block&)> isSameType) |
| 36 | { |
| 37 | face->processEvents(ndn::time::milliseconds(1)); |
| 38 | |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 39 | BOOST_REQUIRE_EQUAL(face->sentData.size(), 1); |
| 40 | ndn::Block parser(face->sentData[0].getContent()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 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 | |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 49 | face->sentData.clear(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 53 | checkErrorResponse(std::shared_ptr<ndn::util::DummyClientFace> face, uint64_t expectedCode) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 54 | { |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 55 | BOOST_REQUIRE_EQUAL(face->sentData.size(), 1); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 56 | |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 57 | ndn::nfd::ControlResponse response(face->sentData[0].getContent().blockFromValue()); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(response.getCode(), expectedCode); |
| 59 | |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 60 | face->sentData.clear(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | BOOST_FIXTURE_TEST_SUITE(PublisherTestLsdbDatasetInterestHandler, PublisherFixture) |
| 64 | |
| 65 | BOOST_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 Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 73 | std::vector<double> angles = {20.00, 30.00}; |
| 74 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 75 | // Install coordinate LSA |
Muktadir R Chowdhury | b00dc2a | 2016-11-05 10:48:58 -0600 | [diff] [blame^] | 76 | CoordinateLsa coordinateLsa = createCoordinateLsa("/RouterA", 10.0, angles); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 77 | 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 Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame] | 86 | LsdbDatasetInterestHandler publisher(lsdb, *face, keyChain); |
| 87 | publisher.setRouterNameCommandPrefix(thisRouter); |
| 88 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 89 | publisher.startListeningOnLocalhost(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 90 | |
| 91 | face->processEvents(ndn::time::milliseconds(10)); |
| 92 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 93 | // Localhost prefix |
| 94 | ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 95 | |
| 96 | // Request adjacency LSAs |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 97 | face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("adjacencies"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 98 | processDatasetInterest(face, |
| 99 | [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; }); |
| 100 | |
| 101 | // Request coordinate LSAs |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 102 | face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("coordinates"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 103 | processDatasetInterest(face, |
| 104 | [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; }); |
| 105 | |
| 106 | // Request Name LSAs |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 107 | face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("names"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 108 | processDatasetInterest(face, |
| 109 | [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; }); |
| 110 | |
| 111 | // Request LSDB Status |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 112 | 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 Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 136 | processDatasetInterest(face, |
| 137 | [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; }); |
| 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_CASE(InvalidCommand) |
| 141 | { |
| 142 | ndn::Name thisRouter("/This/Router"); |
Muktadir R Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame] | 143 | LsdbDatasetInterestHandler publisher(lsdb, *face, keyChain); |
| 144 | publisher.setRouterNameCommandPrefix(thisRouter); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 145 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 146 | // Localhost prefix |
| 147 | publisher.startListeningOnLocalhost(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 148 | face->processEvents(ndn::time::milliseconds(10)); |
| 149 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 150 | ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 151 | |
| 152 | // Unsupported command |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 153 | face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("unsupported"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 154 | face->processEvents(ndn::time::milliseconds(1)); |
| 155 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 156 | checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 157 | |
| 158 | // Long malformed command |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 159 | face->receive( |
| 160 | ndn::Interest(ndn::Name(localhostCommandPrefix).append("extra").append("malformed"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 161 | face->processEvents(ndn::time::milliseconds(1)); |
| 162 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 163 | 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 Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 182 | |
| 183 | // Short malformed command |
| 184 | face->receive(ndn::Interest(ndn::Name(thisRouter).append("malformed"))); |
| 185 | face->processEvents(ndn::time::milliseconds(1)); |
| 186 | |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 187 | BOOST_CHECK_EQUAL(face->sentData.size(), 0); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_SUITE_END() |
| 191 | |
| 192 | } // namespace test |
| 193 | } // namespace nlsr |