blob: a2bd683ac272b23eabbd25fe0d1ce07ea5f88b99 [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/*
Alexander Afanasyev28181ee2020-06-03 13:58:47 -04003 * Copyright (c) 2016-2020, 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"
Andrea Tosatto672b9a72016-01-05 16:18:20 +010031
Junxiao Shif8606492017-07-23 03:44:34 +000032#include <ndn-cxx/security/validator-null.hpp>
33#include <ndn-cxx/util/dummy-client-face.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010034
Davide Pesaventoa8600b02019-12-22 18:52:36 -050035#if BOOST_VERSION >= 105900
36#include <boost/test/tools/output_test_stream.hpp>
37#else
38#include <boost/test/output_test_stream.hpp>
39#endif
40
Andrea Tosatto672b9a72016-01-05 16:18:20 +010041namespace ndn {
42namespace chunks {
43namespace tests {
44
45using namespace ndn::tests;
46using boost::test_tools::output_test_stream;
47
48BOOST_AUTO_TEST_SUITE(Chunks)
49BOOST_AUTO_TEST_SUITE(TestConsumer)
50
Davide Pesavento296b3852019-10-20 16:00:16 -040051BOOST_AUTO_TEST_CASE(InOrderData)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052{
Davide Pesavento296b3852019-10-20 16:00:16 -040053 // Segment order: 0 1 2 3
Andrea Tosatto672b9a72016-01-05 16:18:20 +010054
Davide Pesavento296b3852019-10-20 16:00:16 -040055 const std::string name("/ndn/chunks/test");
56 const std::vector<std::string> testStrings {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010057 "",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010058 "a1b2c3%^&(#$&%^$$/><",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010059 "123456789123456789123456789123456789123456789123456789123456789"
60 "123456789123456789123456789123456789123456789123456789123456789",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010061 "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 Tosatto672b9a72016-01-05 16:18:20 +010068 output_test_stream output("");
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040069 Consumer cons(security::getAcceptAllValidator(), output);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070
Junxiao Shi20d5a0b2018-08-14 09:26:11 -060071 auto interest = makeInterest(name, true);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072
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
Davide Pesavento296b3852019-10-20 16:00:16 -040087BOOST_AUTO_TEST_CASE(OutOfOrderData)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010089 // Segment order: 1 0 2
90
Davide Pesavento296b3852019-10-20 16:00:16 -040091 const std::string name("/ndn/chunks/test");
92 const std::vector<std::string> testStrings {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010093 "a1b2c3%^&(#$&%^$$/><",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010094 "123456789123456789123456789123456789123456789123456789123456789"
95 "123456789123456789123456789123456789123456789123456789123456789",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010096 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
97 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
98 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
99 "consequat massa Donec pede justo,"
Davide Pesavento296b3852019-10-20 16:00:16 -0400100 };
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100101
102 util::DummyClientFace face;
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100103 output_test_stream output("");
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400104 Consumer cons(security::getAcceptAllValidator(), output);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100105
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600106 auto interest = makeInterest(name, true);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100107 std::vector<shared_ptr<Data>> dataStore;
108
109 for (size_t i = 0; i < testStrings.size(); ++i) {
110 auto data = makeData(Name(name).appendVersion(1).appendSegment(i));
111 data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()),
112 testStrings[i].size());
113
114 dataStore.push_back(data);
115 }
116
117 output.flush();
118 cons.m_bufferedData[1] = dataStore[1];
119 cons.writeInOrderData();
120 BOOST_CHECK(output.is_equal(""));
121
122 output.flush();
123 cons.m_bufferedData[0] = dataStore[0];
124 cons.writeInOrderData();
125 BOOST_CHECK(output.is_equal(testStrings[0] + testStrings[1]));
126
127 output.flush();
128 cons.m_bufferedData[2] = dataStore[2];
129 cons.writeInOrderData();
130 BOOST_CHECK(output.is_equal(testStrings[2]));
131}
132
Weiwei Liu05d92092016-07-19 17:34:33 -0700133class PipelineInterestsDummy : public PipelineInterests
134{
135public:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400136 using PipelineInterests::PipelineInterests;
Weiwei Liu05d92092016-07-19 17:34:33 -0700137
138private:
139 void
140 doRun() final
141 {
142 isPipelineRunning = true;
143 }
144
145 void
146 doCancel() final
147 {
148 }
149
150public:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400151 bool isPipelineRunning = false;
Weiwei Liu05d92092016-07-19 17:34:33 -0700152};
153
154BOOST_FIXTURE_TEST_CASE(RunBasic, UnitTestTimeFixture)
155{
156 boost::asio::io_service io;
157 util::DummyClientFace face(io);
Davide Pesavento97a33b22019-10-17 22:10:47 -0400158 Options options;
Alexander Afanasyev28181ee2020-06-03 13:58:47 -0400159 Consumer consumer(security::getAcceptAllValidator());
Weiwei Liu05d92092016-07-19 17:34:33 -0700160
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700161 Name prefix = Name("/ndn/chunks/test").appendVersion(1);
Davide Pesavento97a33b22019-10-17 22:10:47 -0400162 auto discover = make_unique<DiscoverVersion>(face, prefix, options);
163 auto pipeline = make_unique<PipelineInterestsDummy>(face, options);
Weiwei Liu05d92092016-07-19 17:34:33 -0700164 auto pipelinePtr = pipeline.get();
165
Davide Pesavento296b3852019-10-20 16:00:16 -0400166 BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, false);
167
Weiwei Liu05d92092016-07-19 17:34:33 -0700168 consumer.run(std::move(discover), std::move(pipeline));
Davide Pesavento296b3852019-10-20 16:00:16 -0400169 this->advanceClocks(io, 1_ms);
Weiwei Liu05d92092016-07-19 17:34:33 -0700170
Davide Pesavento296b3852019-10-20 16:00:16 -0400171 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); // no discovery Interests are issued
Weiwei Liu05d92092016-07-19 17:34:33 -0700172 BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, true);
173}
174
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100175BOOST_AUTO_TEST_SUITE_END() // TestConsumer
176BOOST_AUTO_TEST_SUITE_END() // Chunks
177
178} // namespace tests
179} // namespace chunks
180} // namespace ndn