Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 9619295 | 2019-05-22 15:45:12 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, Arizona Board of Regents. |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 6 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 7 | * |
| 8 | * ndn-tools is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "tools/peek/ndnpeek/ndnpeek.hpp" |
| 21 | |
| 22 | #include "tests/test-common.hpp" |
Davide Pesavento | bf28cd7 | 2017-08-13 17:22:47 -0400 | [diff] [blame] | 23 | |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 24 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 25 | |
| 26 | #include <boost/mpl/vector.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace peek { |
| 30 | namespace tests { |
| 31 | |
| 32 | using namespace ndn::tests; |
| 33 | using boost::test_tools::output_test_stream; |
| 34 | |
| 35 | class CoutRedirector : noncopyable |
| 36 | { |
| 37 | public: |
| 38 | explicit |
| 39 | CoutRedirector(std::ostream& destination) |
| 40 | { |
| 41 | m_originalBuf = std::cout.rdbuf(destination.rdbuf()); |
| 42 | } |
| 43 | |
| 44 | ~CoutRedirector() |
| 45 | { |
| 46 | std::cout.rdbuf(m_originalBuf); |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | std::streambuf* m_originalBuf; |
| 51 | }; |
| 52 | |
| 53 | class NdnPeekFixture : public UnitTestTimeFixture |
| 54 | { |
| 55 | protected: |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 56 | void |
| 57 | initialize(const PeekOptions& opts) |
| 58 | { |
| 59 | peek = make_unique<NdnPeek>(face, opts); |
| 60 | } |
| 61 | |
| 62 | protected: |
| 63 | boost::asio::io_service io; |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 64 | ndn::util::DummyClientFace face{io}; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 65 | output_test_stream output; |
| 66 | unique_ptr<NdnPeek> peek; |
| 67 | }; |
| 68 | |
| 69 | static PeekOptions |
| 70 | makeDefaultOptions() |
| 71 | { |
| 72 | PeekOptions opt; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 73 | opt.name = "ndn:/peek/test"; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 74 | opt.timeout = 200_ms; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 75 | return opt; |
| 76 | } |
| 77 | |
| 78 | class OutputFull |
| 79 | { |
| 80 | public: |
| 81 | static PeekOptions |
| 82 | makeOptions() |
| 83 | { |
| 84 | return makeDefaultOptions(); |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | checkOutput(output_test_stream& output, const Data& data) |
| 89 | { |
| 90 | const Block& block = data.wireEncode(); |
| 91 | std::string expected(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 92 | BOOST_CHECK(output.is_equal(expected)); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | checkOutput(output_test_stream& output, const lp::Nack& nack) |
| 97 | { |
| 98 | const Block& block = nack.getHeader().wireEncode(); |
| 99 | std::string expected(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 100 | BOOST_CHECK(output.is_equal(expected)); |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | class OutputPayloadOnly |
| 105 | { |
| 106 | public: |
| 107 | static PeekOptions |
| 108 | makeOptions() |
| 109 | { |
| 110 | PeekOptions opt = makeDefaultOptions(); |
| 111 | opt.wantPayloadOnly = true; |
| 112 | return opt; |
| 113 | } |
| 114 | |
| 115 | static void |
| 116 | checkOutput(output_test_stream& output, const Data& data) |
| 117 | { |
| 118 | const Block& block = data.getContent(); |
| 119 | std::string expected(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 120 | BOOST_CHECK(output.is_equal(expected)); |
| 121 | } |
| 122 | |
| 123 | static void |
| 124 | checkOutput(output_test_stream& output, const lp::Nack& nack) |
| 125 | { |
| 126 | std::string expected = boost::lexical_cast<std::string>(nack.getReason()) + '\n'; |
| 127 | BOOST_CHECK(output.is_equal(expected)); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | BOOST_AUTO_TEST_SUITE(Peek) |
| 132 | BOOST_FIXTURE_TEST_SUITE(TestNdnPeek, NdnPeekFixture) |
| 133 | |
| 134 | using OutputChecks = boost::mpl::vector<OutputFull, OutputPayloadOnly>; |
| 135 | |
| 136 | BOOST_AUTO_TEST_CASE_TEMPLATE(Default, OutputCheck, OutputChecks) |
| 137 | { |
| 138 | auto options = OutputCheck::makeOptions(); |
| 139 | initialize(options); |
| 140 | |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 141 | auto data = makeData(options.name); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 142 | const std::string payload = "NdnPeekTest"; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 143 | data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size()); |
| 144 | |
| 145 | { |
| 146 | CoutRedirector redir(output); |
| 147 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 148 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 149 | face.receive(*data); |
| 150 | } |
| 151 | |
| 152 | OutputCheck::checkOutput(output, *data); |
| 153 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 154 | BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), false); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 156 | BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 158 | BOOST_CHECK_EQUAL(face.sentInterests.back().hasApplicationParameters(), false); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 159 | BOOST_CHECK(peek->getResultCode() == ResultCode::DATA); |
| 160 | } |
| 161 | |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 162 | BOOST_AUTO_TEST_CASE_TEMPLATE(NonDefault, OutputCheck, OutputChecks) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 163 | { |
| 164 | auto options = OutputCheck::makeOptions(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 165 | options.canBePrefix = true; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 166 | options.mustBeFresh = true; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 167 | options.interestLifetime = 200_ms; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 168 | initialize(options); |
| 169 | |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 170 | auto data = makeData(Name(options.name).append("suffix")); |
Junxiao Shi | 9619295 | 2019-05-22 15:45:12 +0000 | [diff] [blame] | 171 | data->setFreshnessPeriod(1_s); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 172 | const std::string payload = "NdnPeekTest"; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 173 | data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size()); |
| 174 | |
| 175 | { |
| 176 | CoutRedirector redir(output); |
| 177 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 178 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 179 | face.receive(*data); |
| 180 | } |
| 181 | |
| 182 | OutputCheck::checkOutput(output, *data); |
| 183 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 184 | BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), true); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), true); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 186 | BOOST_CHECK_EQUAL(face.sentInterests.back().getForwardingHint().empty(), true); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 187 | BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), 200_ms); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 188 | BOOST_CHECK_EQUAL(face.sentInterests.back().hasApplicationParameters(), false); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 189 | BOOST_CHECK(peek->getResultCode() == ResultCode::DATA); |
| 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithReason, OutputCheck, OutputChecks) |
| 193 | { |
| 194 | auto options = OutputCheck::makeOptions(); |
| 195 | initialize(options); |
| 196 | lp::Nack nack; |
| 197 | |
| 198 | { |
| 199 | CoutRedirector redir(output); |
| 200 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 201 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 202 | nack = makeNack(face.sentInterests.at(0), lp::NackReason::NO_ROUTE); |
| 203 | face.receive(nack); |
| 204 | } |
| 205 | |
| 206 | OutputCheck::checkOutput(output, nack); |
| 207 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 208 | BOOST_CHECK(peek->getResultCode() == ResultCode::NACK); |
| 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithoutReason, OutputCheck, OutputChecks) |
| 212 | { |
| 213 | auto options = OutputCheck::makeOptions(); |
| 214 | initialize(options); |
| 215 | lp::Nack nack; |
| 216 | |
| 217 | { |
| 218 | CoutRedirector redir(output); |
| 219 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 220 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 221 | nack = makeNack(face.sentInterests.at(0), lp::NackReason::NONE); |
| 222 | face.receive(nack); |
| 223 | } |
| 224 | |
| 225 | OutputCheck::checkOutput(output, nack); |
| 226 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 227 | BOOST_CHECK(peek->getResultCode() == ResultCode::NACK); |
| 228 | } |
| 229 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 230 | BOOST_AUTO_TEST_CASE(NoTimeout) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 231 | { |
| 232 | auto options = makeDefaultOptions(); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 233 | options.interestLifetime = 1_s; |
| 234 | options.timeout = nullopt; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 235 | initialize(options); |
| 236 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 237 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 238 | |
| 239 | peek->start(); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 240 | this->advanceClocks(io, 100_ms, 9); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 242 | BOOST_CHECK_EQUAL(face.getNPendingInterests(), 1); |
| 243 | BOOST_CHECK(peek->getResultCode() == ResultCode::UNKNOWN); |
| 244 | |
| 245 | this->advanceClocks(io, 100_ms, 2); |
| 246 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 247 | BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 248 | BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT); |
| 249 | } |
| 250 | |
| 251 | BOOST_AUTO_TEST_CASE(TimeoutLessThanLifetime) |
| 252 | { |
| 253 | auto options = makeDefaultOptions(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 254 | options.interestLifetime = 200_ms; |
| 255 | options.timeout = 100_ms; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 256 | initialize(options); |
| 257 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 258 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 259 | |
| 260 | peek->start(); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 261 | this->advanceClocks(io, 25_ms, 6); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 262 | |
| 263 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 264 | BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 265 | BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT); |
| 266 | } |
| 267 | |
| 268 | BOOST_AUTO_TEST_CASE(TimeoutGreaterThanLifetime) |
| 269 | { |
| 270 | auto options = makeDefaultOptions(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 271 | options.interestLifetime = 50_ms; |
| 272 | options.timeout = 200_ms; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 273 | initialize(options); |
| 274 | |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 275 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 276 | |
| 277 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame] | 278 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 279 | |
| 280 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
Davide Pesavento | 65d1155 | 2019-06-09 19:15:50 -0400 | [diff] [blame^] | 281 | BOOST_CHECK_EQUAL(face.getNPendingInterests(), 0); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 282 | BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT); |
| 283 | } |
| 284 | |
| 285 | BOOST_AUTO_TEST_SUITE_END() // TestNdnPeek |
| 286 | BOOST_AUTO_TEST_SUITE_END() // Peek |
| 287 | |
| 288 | } // namespace tests |
| 289 | } // namespace peek |
| 290 | } // namespace ndn |