blob: 22b2819f0bb6d010d7fc351edd14cf6e0f104f87 [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);
85
86 face->processEvents(ndn::time::milliseconds(10));
87
88 ndn::Name commandPrefix(thisRouter);
89 commandPrefix.append("lsdb");
90
91 // Request adjacency LSAs
92 face->receive(ndn::Interest(ndn::Name(commandPrefix).append("adjacencies")));
93
94 processDatasetInterest(face,
95 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::AdjacencyLsa; });
96
97 // Request coordinate LSAs
98 face->receive(ndn::Interest(ndn::Name(commandPrefix).append("coordinates")));
99 processDatasetInterest(face,
100 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::CoordinateLsa; });
101
102 // Request Name LSAs
103 face->receive(ndn::Interest(ndn::Name(commandPrefix).append("names")));
104 processDatasetInterest(face,
105 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::NameLsa; });
106
107 // Request LSDB Status
108 face->receive(ndn::Interest(ndn::Name(commandPrefix).append("list")));
109 processDatasetInterest(face,
110 [] (const ndn::Block& block) { return block.type() == ndn::tlv::nlsr::LsdbStatus; });
111}
112
113BOOST_AUTO_TEST_CASE(InvalidCommand)
114{
115 ndn::Name thisRouter("/This/Router");
116 LsdbDatasetInterestHandler publisher(lsdb, *face, thisRouter, keyChain);
117
118 face->processEvents(ndn::time::milliseconds(10));
119
120 ndn::Name commandPrefix(thisRouter);
121 commandPrefix.append("lsdb");
122
123 // Unsupported command
124 face->receive(ndn::Interest(ndn::Name(commandPrefix).append("unsupported")));
125 face->processEvents(ndn::time::milliseconds(1));
126
127 checkErrorResponse(face, 501);
128
129 // Long malformed command
130 face->receive(ndn::Interest(ndn::Name(commandPrefix).append("extra").append("malformed")));
131 face->processEvents(ndn::time::milliseconds(1));
132
133 checkErrorResponse(face, 400);
134
135 // Short malformed command
136 face->receive(ndn::Interest(ndn::Name(thisRouter).append("malformed")));
137 face->processEvents(ndn::time::milliseconds(1));
138
139 BOOST_CHECK_EQUAL(face->sentDatas.size(), 0);
140}
141
142BOOST_AUTO_TEST_SUITE_END()
143
144} // namespace test
145} // namespace nlsr