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 | /* |
| 3 | * Copyright (c) 2014-2018, 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: |
| 56 | NdnPeekFixture() |
| 57 | : face(io) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | initialize(const PeekOptions& opts) |
| 63 | { |
| 64 | peek = make_unique<NdnPeek>(face, opts); |
| 65 | } |
| 66 | |
| 67 | protected: |
| 68 | boost::asio::io_service io; |
| 69 | ndn::util::DummyClientFace face; |
| 70 | output_test_stream output; |
| 71 | unique_ptr<NdnPeek> peek; |
| 72 | }; |
| 73 | |
| 74 | static PeekOptions |
| 75 | makeDefaultOptions() |
| 76 | { |
| 77 | PeekOptions opt; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 78 | opt.name = "ndn:/peek/test"; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 79 | opt.interestLifetime = DEFAULT_INTEREST_LIFETIME; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 80 | opt.timeout = 200_ms; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 81 | return opt; |
| 82 | } |
| 83 | |
| 84 | class OutputFull |
| 85 | { |
| 86 | public: |
| 87 | static PeekOptions |
| 88 | makeOptions() |
| 89 | { |
| 90 | return makeDefaultOptions(); |
| 91 | } |
| 92 | |
| 93 | static void |
| 94 | checkOutput(output_test_stream& output, const Data& data) |
| 95 | { |
| 96 | const Block& block = data.wireEncode(); |
| 97 | std::string expected(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 98 | BOOST_CHECK(output.is_equal(expected)); |
| 99 | } |
| 100 | |
| 101 | static void |
| 102 | checkOutput(output_test_stream& output, const lp::Nack& nack) |
| 103 | { |
| 104 | const Block& block = nack.getHeader().wireEncode(); |
| 105 | std::string expected(reinterpret_cast<const char*>(block.wire()), block.size()); |
| 106 | BOOST_CHECK(output.is_equal(expected)); |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | class OutputPayloadOnly |
| 111 | { |
| 112 | public: |
| 113 | static PeekOptions |
| 114 | makeOptions() |
| 115 | { |
| 116 | PeekOptions opt = makeDefaultOptions(); |
| 117 | opt.wantPayloadOnly = true; |
| 118 | return opt; |
| 119 | } |
| 120 | |
| 121 | static void |
| 122 | checkOutput(output_test_stream& output, const Data& data) |
| 123 | { |
| 124 | const Block& block = data.getContent(); |
| 125 | std::string expected(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 126 | BOOST_CHECK(output.is_equal(expected)); |
| 127 | } |
| 128 | |
| 129 | static void |
| 130 | checkOutput(output_test_stream& output, const lp::Nack& nack) |
| 131 | { |
| 132 | std::string expected = boost::lexical_cast<std::string>(nack.getReason()) + '\n'; |
| 133 | BOOST_CHECK(output.is_equal(expected)); |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | BOOST_AUTO_TEST_SUITE(Peek) |
| 138 | BOOST_FIXTURE_TEST_SUITE(TestNdnPeek, NdnPeekFixture) |
| 139 | |
| 140 | using OutputChecks = boost::mpl::vector<OutputFull, OutputPayloadOnly>; |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE_TEMPLATE(Default, OutputCheck, OutputChecks) |
| 143 | { |
| 144 | auto options = OutputCheck::makeOptions(); |
| 145 | initialize(options); |
| 146 | |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 147 | auto data = makeData(options.name); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 148 | std::string payload = "NdnPeekTest"; |
| 149 | data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size()); |
| 150 | |
| 151 | { |
| 152 | CoutRedirector redir(output); |
| 153 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 154 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 155 | face.receive(*data); |
| 156 | } |
| 157 | |
| 158 | OutputCheck::checkOutput(output, *data); |
| 159 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 160 | BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), false); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), false); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 162 | BOOST_CHECK(face.sentInterests.back().getForwardingHint().empty()); |
| 163 | BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), DEFAULT_INTEREST_LIFETIME); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 164 | BOOST_CHECK(peek->getResultCode() == ResultCode::DATA); |
| 165 | } |
| 166 | |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 167 | BOOST_AUTO_TEST_CASE_TEMPLATE(NonDefault, OutputCheck, OutputChecks) |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 168 | { |
| 169 | auto options = OutputCheck::makeOptions(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 170 | options.canBePrefix = true; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 171 | options.mustBeFresh = true; |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 172 | options.interestLifetime = 200_ms; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 173 | initialize(options); |
| 174 | |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 175 | auto data = makeData(Name(options.name).append("suffix")); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 176 | std::string payload = "NdnPeekTest"; |
| 177 | data->setContent(reinterpret_cast<const uint8_t*>(payload.data()), payload.size()); |
| 178 | |
| 179 | { |
| 180 | CoutRedirector redir(output); |
| 181 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 182 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 183 | face.receive(*data); |
| 184 | } |
| 185 | |
| 186 | OutputCheck::checkOutput(output, *data); |
| 187 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 188 | BOOST_CHECK_EQUAL(face.sentInterests.back().getCanBePrefix(), true); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 189 | BOOST_CHECK_EQUAL(face.sentInterests.back().getMustBeFresh(), true); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 190 | BOOST_CHECK(face.sentInterests.back().getForwardingHint().empty()); |
| 191 | BOOST_CHECK_EQUAL(face.sentInterests.back().getInterestLifetime(), 200_ms); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 192 | BOOST_CHECK(peek->getResultCode() == ResultCode::DATA); |
| 193 | } |
| 194 | |
| 195 | BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithReason, OutputCheck, OutputChecks) |
| 196 | { |
| 197 | auto options = OutputCheck::makeOptions(); |
| 198 | initialize(options); |
| 199 | lp::Nack nack; |
| 200 | |
| 201 | { |
| 202 | CoutRedirector redir(output); |
| 203 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 204 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 205 | nack = makeNack(face.sentInterests.at(0), lp::NackReason::NO_ROUTE); |
| 206 | face.receive(nack); |
| 207 | } |
| 208 | |
| 209 | OutputCheck::checkOutput(output, nack); |
| 210 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 211 | BOOST_CHECK(peek->getResultCode() == ResultCode::NACK); |
| 212 | } |
| 213 | |
| 214 | BOOST_AUTO_TEST_CASE_TEMPLATE(ReceiveNackWithoutReason, OutputCheck, OutputChecks) |
| 215 | { |
| 216 | auto options = OutputCheck::makeOptions(); |
| 217 | initialize(options); |
| 218 | lp::Nack nack; |
| 219 | |
| 220 | { |
| 221 | CoutRedirector redir(output); |
| 222 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 223 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 224 | nack = makeNack(face.sentInterests.at(0), lp::NackReason::NONE); |
| 225 | face.receive(nack); |
| 226 | } |
| 227 | |
| 228 | OutputCheck::checkOutput(output, nack); |
| 229 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 230 | BOOST_CHECK_EQUAL(face.sentData.size(), 0); |
| 231 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 0); |
| 232 | BOOST_CHECK(peek->getResultCode() == ResultCode::NACK); |
| 233 | } |
| 234 | |
| 235 | BOOST_AUTO_TEST_CASE(TimeoutDefault) |
| 236 | { |
| 237 | auto options = makeDefaultOptions(); |
| 238 | initialize(options); |
| 239 | |
| 240 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0); |
| 241 | |
| 242 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 243 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 244 | |
| 245 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 246 | BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT); |
| 247 | } |
| 248 | |
| 249 | BOOST_AUTO_TEST_CASE(TimeoutLessThanLifetime) |
| 250 | { |
| 251 | auto options = makeDefaultOptions(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 252 | options.interestLifetime = 200_ms; |
| 253 | options.timeout = 100_ms; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 254 | initialize(options); |
| 255 | |
| 256 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0); |
| 257 | |
| 258 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 259 | this->advanceClocks(io, 25_ms, 8); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 260 | |
| 261 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 262 | BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT); |
| 263 | } |
| 264 | |
| 265 | BOOST_AUTO_TEST_CASE(TimeoutGreaterThanLifetime) |
| 266 | { |
| 267 | auto options = makeDefaultOptions(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 268 | options.interestLifetime = 50_ms; |
| 269 | options.timeout = 200_ms; |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 270 | initialize(options); |
| 271 | |
| 272 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 0); |
| 273 | |
| 274 | peek->start(); |
Junxiao Shi | b54aabd | 2018-04-16 19:36:24 +0000 | [diff] [blame^] | 275 | this->advanceClocks(io, 25_ms, 4); |
Zhuo Li | b355889 | 2016-08-12 15:51:12 -0700 | [diff] [blame] | 276 | |
| 277 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 1); |
| 278 | BOOST_CHECK(peek->getResultCode() == ResultCode::TIMEOUT); |
| 279 | } |
| 280 | |
| 281 | BOOST_AUTO_TEST_SUITE_END() // TestNdnPeek |
| 282 | BOOST_AUTO_TEST_SUITE_END() // Peek |
| 283 | |
| 284 | } // namespace tests |
| 285 | } // namespace peek |
| 286 | } // namespace ndn |