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 | /* |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 3 | * Copyright (c) 2016-2019, 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 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 45 | BOOST_AUTO_TEST_CASE(InOrderData) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 46 | { |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 47 | // Segment order: 0 1 2 3 |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 48 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 49 | const std::string name("/ndn/chunks/test"); |
| 50 | const std::vector<std::string> testStrings { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 51 | "", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 52 | "a1b2c3%^&(#$&%^$$/><", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 53 | "123456789123456789123456789123456789123456789123456789123456789" |
| 54 | "123456789123456789123456789123456789123456789123456789123456789", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 55 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 56 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 57 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 58 | "consequat massa Donec pede justo," |
| 59 | }; |
| 60 | |
| 61 | util::DummyClientFace face; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 62 | output_test_stream output(""); |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 63 | Consumer cons(security::v2::getAcceptAllValidator(), output); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 64 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 65 | auto interest = makeInterest(name, true); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 66 | |
| 67 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 68 | output.flush(); |
| 69 | |
| 70 | auto data = makeData(Name(name).appendVersion(1).appendSegment(i)); |
| 71 | data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()), |
| 72 | testStrings[i].size()); |
| 73 | |
| 74 | cons.m_bufferedData[i] = data; |
| 75 | cons.writeInOrderData(); |
| 76 | |
| 77 | BOOST_CHECK(output.is_equal(testStrings[i])); |
| 78 | } |
| 79 | } |
| 80 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 81 | BOOST_AUTO_TEST_CASE(OutOfOrderData) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 82 | { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 83 | // Segment order: 1 0 2 |
| 84 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 85 | const std::string name("/ndn/chunks/test"); |
| 86 | const std::vector<std::string> testStrings { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 87 | "a1b2c3%^&(#$&%^$$/><", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 88 | "123456789123456789123456789123456789123456789123456789123456789" |
| 89 | "123456789123456789123456789123456789123456789123456789123456789", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 90 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 91 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 92 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 93 | "consequat massa Donec pede justo," |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 94 | }; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 95 | |
| 96 | util::DummyClientFace face; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 97 | output_test_stream output(""); |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 98 | Consumer cons(security::v2::getAcceptAllValidator(), output); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 99 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 100 | auto interest = makeInterest(name, true); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 101 | std::vector<shared_ptr<Data>> dataStore; |
| 102 | |
| 103 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 104 | auto data = makeData(Name(name).appendVersion(1).appendSegment(i)); |
| 105 | data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()), |
| 106 | testStrings[i].size()); |
| 107 | |
| 108 | dataStore.push_back(data); |
| 109 | } |
| 110 | |
| 111 | output.flush(); |
| 112 | cons.m_bufferedData[1] = dataStore[1]; |
| 113 | cons.writeInOrderData(); |
| 114 | BOOST_CHECK(output.is_equal("")); |
| 115 | |
| 116 | output.flush(); |
| 117 | cons.m_bufferedData[0] = dataStore[0]; |
| 118 | cons.writeInOrderData(); |
| 119 | BOOST_CHECK(output.is_equal(testStrings[0] + testStrings[1])); |
| 120 | |
| 121 | output.flush(); |
| 122 | cons.m_bufferedData[2] = dataStore[2]; |
| 123 | cons.writeInOrderData(); |
| 124 | BOOST_CHECK(output.is_equal(testStrings[2])); |
| 125 | } |
| 126 | |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 127 | class PipelineInterestsDummy : public PipelineInterests |
| 128 | { |
| 129 | public: |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 130 | using PipelineInterests::PipelineInterests; |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 131 | |
| 132 | private: |
| 133 | void |
| 134 | doRun() final |
| 135 | { |
| 136 | isPipelineRunning = true; |
| 137 | } |
| 138 | |
| 139 | void |
| 140 | doCancel() final |
| 141 | { |
| 142 | } |
| 143 | |
| 144 | public: |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 145 | bool isPipelineRunning = false; |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | BOOST_FIXTURE_TEST_CASE(RunBasic, UnitTestTimeFixture) |
| 149 | { |
| 150 | boost::asio::io_service io; |
| 151 | util::DummyClientFace face(io); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 152 | Options options; |
Davide Pesavento | f6991e1 | 2018-01-08 20:58:50 -0500 | [diff] [blame] | 153 | Consumer consumer(security::v2::getAcceptAllValidator()); |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 154 | |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 155 | Name prefix = Name("/ndn/chunks/test").appendVersion(1); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 156 | auto discover = make_unique<DiscoverVersion>(face, prefix, options); |
| 157 | auto pipeline = make_unique<PipelineInterestsDummy>(face, options); |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 158 | auto pipelinePtr = pipeline.get(); |
| 159 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 160 | BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, false); |
| 161 | |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 162 | consumer.run(std::move(discover), std::move(pipeline)); |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 163 | this->advanceClocks(io, 1_ms); |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 164 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 165 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); // no discovery Interests are issued |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, true); |
| 167 | } |
| 168 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 169 | BOOST_AUTO_TEST_SUITE_END() // TestConsumer |
| 170 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 171 | |
| 172 | } // namespace tests |
| 173 | } // namespace chunks |
| 174 | } // namespace ndn |