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 | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 3 | * Copyright (c) 2016-2024, 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" |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 31 | #include "tests/io-fixture.hpp" |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 32 | |
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 | |
Davide Pesavento | a8600b0 | 2019-12-22 18:52:36 -0500 | [diff] [blame] | 36 | #include <boost/test/tools/output_test_stream.hpp> |
Davide Pesavento | a8600b0 | 2019-12-22 18:52:36 -0500 | [diff] [blame] | 37 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 38 | namespace ndn::chunks::tests { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 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 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 46 | BOOST_AUTO_TEST_CASE(InOrderData) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 47 | { |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 48 | // Segment order: 0 1 2 3 |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 49 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 50 | const std::string name("/ndn/chunks/test"); |
| 51 | const std::vector<std::string> testStrings { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 52 | "", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 53 | "a1b2c3%^&(#$&%^$$/><", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 54 | "123456789123456789123456789123456789123456789123456789123456789" |
| 55 | "123456789123456789123456789123456789123456789123456789123456789", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 56 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 57 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 58 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 59 | "consequat massa Donec pede justo," |
| 60 | }; |
| 61 | |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 62 | DummyClientFace face; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 63 | output_test_stream output(""); |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame] | 64 | Consumer cons(security::getAcceptAllValidator(), output); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 65 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 66 | auto interest = makeInterest(name, true); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 67 | |
| 68 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 69 | output.flush(); |
| 70 | |
| 71 | auto data = makeData(Name(name).appendVersion(1).appendSegment(i)); |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame] | 72 | data->setContent(make_span(reinterpret_cast<const uint8_t*>(testStrings[i].data()), |
| 73 | testStrings[i].size())); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 74 | |
| 75 | cons.m_bufferedData[i] = data; |
| 76 | cons.writeInOrderData(); |
| 77 | |
| 78 | BOOST_CHECK(output.is_equal(testStrings[i])); |
| 79 | } |
| 80 | } |
| 81 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 82 | BOOST_AUTO_TEST_CASE(OutOfOrderData) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 83 | { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 84 | // Segment order: 1 0 2 |
| 85 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 86 | const std::string name("/ndn/chunks/test"); |
| 87 | const std::vector<std::string> testStrings { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 88 | "a1b2c3%^&(#$&%^$$/><", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 89 | "123456789123456789123456789123456789123456789123456789123456789" |
| 90 | "123456789123456789123456789123456789123456789123456789123456789", |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 91 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 92 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 93 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 94 | "consequat massa Donec pede justo," |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame] | 95 | }; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 96 | |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 97 | DummyClientFace face; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 98 | output_test_stream output(""); |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame] | 99 | Consumer cons(security::getAcceptAllValidator(), output); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 100 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 101 | auto interest = makeInterest(name, true); |
Davide Pesavento | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 102 | std::vector<std::shared_ptr<Data>> dataStore; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 103 | |
| 104 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 105 | auto data = makeData(Name(name).appendVersion(1).appendSegment(i)); |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame] | 106 | data->setContent(make_span(reinterpret_cast<const uint8_t*>(testStrings[i].data()), |
| 107 | testStrings[i].size())); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 108 | |
| 109 | dataStore.push_back(data); |
| 110 | } |
| 111 | |
| 112 | output.flush(); |
| 113 | cons.m_bufferedData[1] = dataStore[1]; |
| 114 | cons.writeInOrderData(); |
| 115 | BOOST_CHECK(output.is_equal("")); |
| 116 | |
| 117 | output.flush(); |
| 118 | cons.m_bufferedData[0] = dataStore[0]; |
| 119 | cons.writeInOrderData(); |
| 120 | BOOST_CHECK(output.is_equal(testStrings[0] + testStrings[1])); |
| 121 | |
| 122 | output.flush(); |
| 123 | cons.m_bufferedData[2] = dataStore[2]; |
| 124 | cons.writeInOrderData(); |
| 125 | BOOST_CHECK(output.is_equal(testStrings[2])); |
| 126 | } |
| 127 | |
Davide Pesavento | 816767b | 2023-10-17 15:40:22 -0400 | [diff] [blame] | 128 | class PipelineInterestsDummy final : public PipelineInterests |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 129 | { |
| 130 | public: |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 131 | using PipelineInterests::PipelineInterests; |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 132 | |
| 133 | private: |
| 134 | void |
| 135 | doRun() final |
| 136 | { |
| 137 | isPipelineRunning = true; |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | doCancel() final |
| 142 | { |
| 143 | } |
| 144 | |
| 145 | public: |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 146 | bool isPipelineRunning = false; |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 149 | BOOST_FIXTURE_TEST_CASE(RunBasic, IoFixture) |
Weiwei Liu | 05d9209 | 2016-07-19 17:34:33 -0700 | [diff] [blame] | 150 | { |
Junxiao Shi | 869d73e | 2023-08-10 22:52:26 +0000 | [diff] [blame] | 151 | DummyClientFace face(m_io); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 152 | Options options; |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame] | 153 | Consumer consumer(security::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 | 5748e82 | 2024-01-26 18:40:22 -0500 | [diff] [blame] | 156 | auto discover = std::make_unique<DiscoverVersion>(face, prefix, options); |
| 157 | auto pipeline = std::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 | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 163 | this->advanceClocks(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 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame] | 172 | } // namespace ndn::chunks::tests |