Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 06807dc | 2017-09-19 15:52:43 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2018, Regents of the University of California, |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 6 | * |
| 7 | * This file is part of ndn-tools (Named Data Networking Essential Tools). |
| 8 | * See AUTHORS.md for complete list of ndn-tools authors and contributors. |
| 9 | * |
| 10 | * ndn-tools 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 | * ndn-tools 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 | * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 22 | * |
| 23 | * @author Andrea Tosatto |
| 24 | */ |
| 25 | |
| 26 | #include "tools/chunks/catchunks/consumer.hpp" |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 27 | #include "tools/chunks/catchunks/discover-version.hpp" |
| 28 | #include "tools/chunks/catchunks/pipeline-interests.hpp" |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 29 | |
| 30 | #include "tests/test-common.hpp" |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 31 | |
Junxiao Shi | f860649 | 2017-07-23 03:44:34 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/security/validator-null.hpp> |
| 33 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace chunks { |
| 37 | namespace tests { |
| 38 | |
| 39 | using namespace ndn::tests; |
| 40 | using boost::test_tools::output_test_stream; |
| 41 | |
| 42 | BOOST_AUTO_TEST_SUITE(Chunks) |
| 43 | BOOST_AUTO_TEST_SUITE(TestConsumer) |
| 44 | |
| 45 | BOOST_AUTO_TEST_CASE(OutputDataSequential) |
| 46 | { |
| 47 | // Test sequential segments in the right order |
| 48 | // Segment order: 0 1 2 |
| 49 | |
| 50 | std::string name("/ndn/chunks/test"); |
| 51 | |
| 52 | std::vector<std::string> testStrings { |
| 53 | "", |
| 54 | |
| 55 | "a1b2c3%^&(#$&%^$$/><", |
| 56 | |
| 57 | "123456789123456789123456789123456789123456789123456789123456789" |
| 58 | "123456789123456789123456789123456789123456789123456789123456789", |
| 59 | |
| 60 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 61 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 62 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 63 | "consequat massa Donec pede justo," |
| 64 | }; |
| 65 | |
| 66 | util::DummyClientFace face; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 67 | output_test_stream output(""); |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 68 | Consumer cons(security::v2::getAcceptAllValidator(), output); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 69 | |
| 70 | auto interest = makeInterest(name); |
| 71 | |
| 72 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 73 | output.flush(); |
| 74 | |
| 75 | auto data = makeData(Name(name).appendVersion(1).appendSegment(i)); |
| 76 | data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()), |
| 77 | testStrings[i].size()); |
| 78 | |
| 79 | cons.m_bufferedData[i] = data; |
| 80 | cons.writeInOrderData(); |
| 81 | |
| 82 | BOOST_CHECK(output.is_equal(testStrings[i])); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(OutputDataUnordered) |
| 87 | { |
| 88 | // Test unordered segments |
| 89 | // Segment order: 1 0 2 |
| 90 | |
| 91 | std::string name("/ndn/chunks/test"); |
| 92 | |
| 93 | std::vector<std::string> testStrings { |
| 94 | "a1b2c3%^&(#$&%^$$/><", |
| 95 | |
| 96 | "123456789123456789123456789123456789123456789123456789123456789" |
| 97 | "123456789123456789123456789123456789123456789123456789123456789", |
| 98 | |
| 99 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 100 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 101 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 102 | "consequat massa Donec pede justo," |
| 103 | };; |
| 104 | |
| 105 | util::DummyClientFace face; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 106 | output_test_stream output(""); |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 107 | Consumer cons(security::v2::getAcceptAllValidator(), output); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 108 | |
| 109 | auto interest = makeInterest(name); |
| 110 | std::vector<shared_ptr<Data>> dataStore; |
| 111 | |
| 112 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 113 | auto data = makeData(Name(name).appendVersion(1).appendSegment(i)); |
| 114 | data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()), |
| 115 | testStrings[i].size()); |
| 116 | |
| 117 | dataStore.push_back(data); |
| 118 | } |
| 119 | |
| 120 | output.flush(); |
| 121 | cons.m_bufferedData[1] = dataStore[1]; |
| 122 | cons.writeInOrderData(); |
| 123 | BOOST_CHECK(output.is_equal("")); |
| 124 | |
| 125 | output.flush(); |
| 126 | cons.m_bufferedData[0] = dataStore[0]; |
| 127 | cons.writeInOrderData(); |
| 128 | BOOST_CHECK(output.is_equal(testStrings[0] + testStrings[1])); |
| 129 | |
| 130 | output.flush(); |
| 131 | cons.m_bufferedData[2] = dataStore[2]; |
| 132 | cons.writeInOrderData(); |
| 133 | BOOST_CHECK(output.is_equal(testStrings[2])); |
| 134 | } |
| 135 | |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 136 | class DiscoverVersionDummy : public DiscoverVersion |
| 137 | { |
| 138 | public: |
| 139 | DiscoverVersionDummy(const Name& prefix, Face& face, const Options& options) |
Davide Pesavento | 06807dc | 2017-09-19 15:52:43 -0400 | [diff] [blame] | 140 | : Options(options) |
| 141 | , DiscoverVersion(prefix, face) |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 142 | , isDiscoverRunning(false) |
| 143 | , m_prefix(prefix) |
| 144 | { |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | run() final |
| 149 | { |
| 150 | isDiscoverRunning = true; |
| 151 | |
| 152 | auto interest = makeInterest(m_prefix); |
| 153 | expressInterest(*interest, 1, 1); |
| 154 | } |
| 155 | |
| 156 | private: |
| 157 | void |
| 158 | handleData(const Interest& interest, const Data& data) final |
| 159 | { |
| 160 | this->emitSignal(onDiscoverySuccess, data); |
| 161 | } |
| 162 | |
| 163 | public: |
| 164 | bool isDiscoverRunning; |
| 165 | |
| 166 | private: |
| 167 | Name m_prefix; |
| 168 | }; |
| 169 | |
| 170 | class PipelineInterestsDummy : public PipelineInterests |
| 171 | { |
| 172 | public: |
| 173 | PipelineInterestsDummy(Face& face) |
| 174 | : PipelineInterests(face) |
| 175 | , isPipelineRunning(false) |
| 176 | { |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | void |
| 181 | doRun() final |
| 182 | { |
| 183 | isPipelineRunning = true; |
| 184 | } |
| 185 | |
| 186 | void |
| 187 | doCancel() final |
| 188 | { |
| 189 | } |
| 190 | |
| 191 | public: |
| 192 | bool isPipelineRunning; |
| 193 | }; |
| 194 | |
| 195 | BOOST_FIXTURE_TEST_CASE(RunBasic, UnitTestTimeFixture) |
| 196 | { |
| 197 | boost::asio::io_service io; |
| 198 | util::DummyClientFace face(io); |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 199 | Consumer consumer(security::v2::getAcceptAllValidator()); |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 200 | |
| 201 | Name prefix("/ndn/chunks/test"); |
| 202 | auto discover = make_unique<DiscoverVersionDummy>(prefix, face, Options()); |
| 203 | auto pipeline = make_unique<PipelineInterestsDummy>(face); |
| 204 | auto discoverPtr = discover.get(); |
| 205 | auto pipelinePtr = pipeline.get(); |
| 206 | |
| 207 | consumer.run(std::move(discover), std::move(pipeline)); |
| 208 | BOOST_CHECK_EQUAL(discoverPtr->isDiscoverRunning, true); |
| 209 | |
| 210 | this->advanceClocks(io, time::nanoseconds(1)); |
| 211 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 212 | |
| 213 | auto data = makeData(prefix.appendSegment(0)); |
| 214 | face.receive(*data); |
| 215 | |
| 216 | this->advanceClocks(io, time::nanoseconds(1)); |
| 217 | BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, true); |
| 218 | } |
| 219 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 220 | BOOST_AUTO_TEST_SUITE_END() // TestConsumer |
| 221 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 222 | |
| 223 | } // namespace tests |
| 224 | } // namespace chunks |
| 225 | } // namespace ndn |