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