blob: e5a584fea172679dc91be890d8767febb16243e6 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento06807dc2017-09-19 15:52:43 -04002/*
Davide Pesavento59984282022-02-16 22:41:03 -05003 * Copyright (c) 2016-2022, Regents of the University of California,
Junxiao Shif8606492017-07-23 03:44:34 +00004 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
Andrea Tosatto672b9a72016-01-05 16:18:20 +01006 *
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 Liu05d92092016-07-19 17:34:33 -070027#include "tools/chunks/catchunks/discover-version.hpp"
28#include "tools/chunks/catchunks/pipeline-interests.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010029
30#include "tests/test-common.hpp"
Davide Pesavento66777622020-10-09 18:46:03 -040031#include "tests/io-fixture.hpp"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010032
Junxiao Shif8606492017-07-23 03:44:34 +000033#include <ndn-cxx/security/validator-null.hpp>
34#include <ndn-cxx/util/dummy-client-face.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035
Davide Pesaventoa8600b02019-12-22 18:52:36 -050036#if BOOST_VERSION >= 105900
37#include <boost/test/tools/output_test_stream.hpp>
38#else
39#include <boost/test/output_test_stream.hpp>
40#endif
41
Davide Pesaventob3570c62022-02-19 19:19:00 -050042namespace ndn::chunks::tests {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010043
44using namespace ndn::tests;
45using boost::test_tools::output_test_stream;
46
47BOOST_AUTO_TEST_SUITE(Chunks)
48BOOST_AUTO_TEST_SUITE(TestConsumer)
49
Davide Pesavento296b3852019-10-20 16:00:16 -040050BOOST_AUTO_TEST_CASE(InOrderData)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010051{
Davide Pesavento296b3852019-10-20 16:00:16 -040052 // Segment order: 0 1 2 3
Andrea Tosatto672b9a72016-01-05 16:18:20 +010053
Davide Pesavento296b3852019-10-20 16:00:16 -040054 const std::string name("/ndn/chunks/test");
55 const std::vector<std::string> testStrings {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010056 "",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057 "a1b2c3%^&(#$&%^$$/><",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058 "123456789123456789123456789123456789123456789123456789123456789"
59 "123456789123456789123456789123456789123456789123456789123456789",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010060 "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 Tosatto672b9a72016-01-05 16:18:20 +010067 output_test_stream output("");
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040068 Consumer cons(security::getAcceptAllValidator(), output);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069
Junxiao Shi20d5a0b2018-08-14 09:26:11 -060070 auto interest = makeInterest(name, true);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071
72 for (size_t i = 0; i < testStrings.size(); ++i) {
73 output.flush();
74
75 auto data = makeData(Name(name).appendVersion(1).appendSegment(i));
Davide Pesavento59984282022-02-16 22:41:03 -050076 data->setContent(make_span(reinterpret_cast<const uint8_t*>(testStrings[i].data()),
77 testStrings[i].size()));
Andrea Tosatto672b9a72016-01-05 16:18:20 +010078
79 cons.m_bufferedData[i] = data;
80 cons.writeInOrderData();
81
82 BOOST_CHECK(output.is_equal(testStrings[i]));
83 }
84}
85
Davide Pesavento296b3852019-10-20 16:00:16 -040086BOOST_AUTO_TEST_CASE(OutOfOrderData)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010087{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088 // Segment order: 1 0 2
89
Davide Pesavento296b3852019-10-20 16:00:16 -040090 const std::string name("/ndn/chunks/test");
91 const std::vector<std::string> testStrings {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010092 "a1b2c3%^&(#$&%^$$/><",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010093 "123456789123456789123456789123456789123456789123456789123456789"
94 "123456789123456789123456789123456789123456789123456789123456789",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010095 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
96 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
97 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
98 "consequat massa Donec pede justo,"
Davide Pesavento296b3852019-10-20 16:00:16 -040099 };
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100100
101 util::DummyClientFace face;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100102 output_test_stream output("");
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400103 Consumer cons(security::getAcceptAllValidator(), output);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100104
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600105 auto interest = makeInterest(name, true);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100106 std::vector<shared_ptr<Data>> dataStore;
107
108 for (size_t i = 0; i < testStrings.size(); ++i) {
109 auto data = makeData(Name(name).appendVersion(1).appendSegment(i));
Davide Pesavento59984282022-02-16 22:41:03 -0500110 data->setContent(make_span(reinterpret_cast<const uint8_t*>(testStrings[i].data()),
111 testStrings[i].size()));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100112
113 dataStore.push_back(data);
114 }
115
116 output.flush();
117 cons.m_bufferedData[1] = dataStore[1];
118 cons.writeInOrderData();
119 BOOST_CHECK(output.is_equal(""));
120
121 output.flush();
122 cons.m_bufferedData[0] = dataStore[0];
123 cons.writeInOrderData();
124 BOOST_CHECK(output.is_equal(testStrings[0] + testStrings[1]));
125
126 output.flush();
127 cons.m_bufferedData[2] = dataStore[2];
128 cons.writeInOrderData();
129 BOOST_CHECK(output.is_equal(testStrings[2]));
130}
131
Weiwei Liu05d92092016-07-19 17:34:33 -0700132class PipelineInterestsDummy : public PipelineInterests
133{
134public:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400135 using PipelineInterests::PipelineInterests;
Weiwei Liu05d92092016-07-19 17:34:33 -0700136
137private:
138 void
139 doRun() final
140 {
141 isPipelineRunning = true;
142 }
143
144 void
145 doCancel() final
146 {
147 }
148
149public:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400150 bool isPipelineRunning = false;
Weiwei Liu05d92092016-07-19 17:34:33 -0700151};
152
Davide Pesavento66777622020-10-09 18:46:03 -0400153BOOST_FIXTURE_TEST_CASE(RunBasic, IoFixture)
Weiwei Liu05d92092016-07-19 17:34:33 -0700154{
Davide Pesavento66777622020-10-09 18:46:03 -0400155 util::DummyClientFace face(m_io);
Davide Pesavento97a33b22019-10-17 22:10:47 -0400156 Options options;
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400157 Consumer consumer(security::getAcceptAllValidator());
Weiwei Liu05d92092016-07-19 17:34:33 -0700158
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700159 Name prefix = Name("/ndn/chunks/test").appendVersion(1);
Davide Pesavento97a33b22019-10-17 22:10:47 -0400160 auto discover = make_unique<DiscoverVersion>(face, prefix, options);
161 auto pipeline = make_unique<PipelineInterestsDummy>(face, options);
Weiwei Liu05d92092016-07-19 17:34:33 -0700162 auto pipelinePtr = pipeline.get();
163
Davide Pesavento296b3852019-10-20 16:00:16 -0400164 BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, false);
165
Weiwei Liu05d92092016-07-19 17:34:33 -0700166 consumer.run(std::move(discover), std::move(pipeline));
Davide Pesavento66777622020-10-09 18:46:03 -0400167 this->advanceClocks(1_ms);
Weiwei Liu05d92092016-07-19 17:34:33 -0700168
Davide Pesavento296b3852019-10-20 16:00:16 -0400169 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); // no discovery Interests are issued
Weiwei Liu05d92092016-07-19 17:34:33 -0700170 BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, true);
171}
172
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100173BOOST_AUTO_TEST_SUITE_END() // TestConsumer
174BOOST_AUTO_TEST_SUITE_END() // Chunks
175
Davide Pesaventob3570c62022-02-19 19:19:00 -0500176} // namespace ndn::chunks::tests