Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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 | |
| 28 | #include <ndn-cxx/management/nfd-control-response.hpp> |
| 29 | |
| 30 | namespace nlsr { |
| 31 | namespace test { |
| 32 | |
| 33 | void |
| 34 | processDatasetInterest(shared_ptr<ndn::util::DummyClientFace> face, |
| 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 |
| 53 | checkErrorResponse(shared_ptr<ndn::util::DummyClientFace> face, uint64_t expectedCode) |
| 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 | |
| 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 Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame^] | 84 | LsdbDatasetInterestHandler publisher(lsdb, *face, keyChain); |
| 85 | publisher.setRouterNameCommandPrefix(thisRouter); |
| 86 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 87 | publisher.startListeningOnLocalhost(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 88 | |
| 89 | face->processEvents(ndn::time::milliseconds(10)); |
| 90 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 91 | // Localhost prefix |
| 92 | ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 93 | |
| 94 | // Request adjacency LSAs |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 95 | face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("adjacencies"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 96 | processDatasetInterest(face, |
| 97 | [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; }); |
| 98 | |
| 99 | // Request coordinate LSAs |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 100 | face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("coordinates"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 101 | processDatasetInterest(face, |
| 102 | [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; }); |
| 103 | |
| 104 | // Request Name LSAs |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 105 | face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("names"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 106 | processDatasetInterest(face, |
| 107 | [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; }); |
| 108 | |
| 109 | // Request LSDB Status |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 110 | 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 Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 134 | processDatasetInterest(face, |
| 135 | [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; }); |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(InvalidCommand) |
| 139 | { |
| 140 | ndn::Name thisRouter("/This/Router"); |
Muktadir R Chowdhury | 3ac0728 | 2016-06-17 16:30:29 -0500 | [diff] [blame^] | 141 | LsdbDatasetInterestHandler publisher(lsdb, *face, keyChain); |
| 142 | publisher.setRouterNameCommandPrefix(thisRouter); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 143 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 144 | // Localhost prefix |
| 145 | publisher.startListeningOnLocalhost(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 146 | face->processEvents(ndn::time::milliseconds(10)); |
| 147 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 148 | ndn::Name localhostCommandPrefix = publisher.getLocalhostCommandPrefix(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 149 | |
| 150 | // Unsupported command |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 151 | face->receive(ndn::Interest(ndn::Name(localhostCommandPrefix).append("unsupported"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 152 | face->processEvents(ndn::time::milliseconds(1)); |
| 153 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 154 | checkErrorResponse(face, LsdbDatasetInterestHandler::ERROR_CODE_UNSUPPORTED_COMMAND); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 155 | |
| 156 | // Long malformed command |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 157 | face->receive( |
| 158 | ndn::Interest(ndn::Name(localhostCommandPrefix).append("extra").append("malformed"))); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 159 | face->processEvents(ndn::time::milliseconds(1)); |
| 160 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 161 | 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 Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 180 | |
| 181 | // Short malformed command |
| 182 | face->receive(ndn::Interest(ndn::Name(thisRouter).append("malformed"))); |
| 183 | face->processEvents(ndn::time::milliseconds(1)); |
| 184 | |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(face->sentData.size(), 0); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | BOOST_AUTO_TEST_SUITE_END() |
| 189 | |
| 190 | } // namespace test |
| 191 | } // namespace nlsr |