blob: 671f1cee5ff753387ac6dd0cbf2e10e7d832348b [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/*
Chavoosh Ghasemi5cb67012019-02-15 09:56:57 -08003 * Copyright (c) 2016-2019, 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
35namespace ndn {
36namespace chunks {
37namespace tests {
38
39using namespace ndn::tests;
40using boost::test_tools::output_test_stream;
41
42BOOST_AUTO_TEST_SUITE(Chunks)
43BOOST_AUTO_TEST_SUITE(TestConsumer)
44
Davide Pesavento296b3852019-10-20 16:00:16 -040045BOOST_AUTO_TEST_CASE(InOrderData)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010046{
Davide Pesavento296b3852019-10-20 16:00:16 -040047 // Segment order: 0 1 2 3
Andrea Tosatto672b9a72016-01-05 16:18:20 +010048
Davide Pesavento296b3852019-10-20 16:00:16 -040049 const std::string name("/ndn/chunks/test");
50 const std::vector<std::string> testStrings {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010051 "",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010052 "a1b2c3%^&(#$&%^$$/><",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010053 "123456789123456789123456789123456789123456789123456789123456789"
54 "123456789123456789123456789123456789123456789123456789123456789",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010055 "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 Tosatto672b9a72016-01-05 16:18:20 +010062 output_test_stream output("");
Davide Pesaventof6991e12018-01-08 20:58:50 -050063 Consumer cons(security::v2::getAcceptAllValidator(), output);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010064
Junxiao Shi20d5a0b2018-08-14 09:26:11 -060065 auto interest = makeInterest(name, true);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010066
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 Pesavento296b3852019-10-20 16:00:16 -040081BOOST_AUTO_TEST_CASE(OutOfOrderData)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010082{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010083 // Segment order: 1 0 2
84
Davide Pesavento296b3852019-10-20 16:00:16 -040085 const std::string name("/ndn/chunks/test");
86 const std::vector<std::string> testStrings {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010087 "a1b2c3%^&(#$&%^$$/><",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088 "123456789123456789123456789123456789123456789123456789123456789"
89 "123456789123456789123456789123456789123456789123456789123456789",
Andrea Tosatto672b9a72016-01-05 16:18:20 +010090 "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 Pesavento296b3852019-10-20 16:00:16 -040094 };
Andrea Tosatto672b9a72016-01-05 16:18:20 +010095
96 util::DummyClientFace face;
Andrea Tosatto672b9a72016-01-05 16:18:20 +010097 output_test_stream output("");
Davide Pesaventof6991e12018-01-08 20:58:50 -050098 Consumer cons(security::v2::getAcceptAllValidator(), output);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010099
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600100 auto interest = makeInterest(name, true);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100101 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 Liu05d92092016-07-19 17:34:33 -0700127class PipelineInterestsDummy : public PipelineInterests
128{
129public:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400130 using PipelineInterests::PipelineInterests;
Weiwei Liu05d92092016-07-19 17:34:33 -0700131
132private:
133 void
134 doRun() final
135 {
136 isPipelineRunning = true;
137 }
138
139 void
140 doCancel() final
141 {
142 }
143
144public:
Davide Pesavento97a33b22019-10-17 22:10:47 -0400145 bool isPipelineRunning = false;
Weiwei Liu05d92092016-07-19 17:34:33 -0700146};
147
148BOOST_FIXTURE_TEST_CASE(RunBasic, UnitTestTimeFixture)
149{
150 boost::asio::io_service io;
151 util::DummyClientFace face(io);
Davide Pesavento97a33b22019-10-17 22:10:47 -0400152 Options options;
Davide Pesaventof6991e12018-01-08 20:58:50 -0500153 Consumer consumer(security::v2::getAcceptAllValidator());
Weiwei Liu05d92092016-07-19 17:34:33 -0700154
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700155 Name prefix = Name("/ndn/chunks/test").appendVersion(1);
Davide Pesavento97a33b22019-10-17 22:10:47 -0400156 auto discover = make_unique<DiscoverVersion>(face, prefix, options);
157 auto pipeline = make_unique<PipelineInterestsDummy>(face, options);
Weiwei Liu05d92092016-07-19 17:34:33 -0700158 auto pipelinePtr = pipeline.get();
159
Davide Pesavento296b3852019-10-20 16:00:16 -0400160 BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, false);
161
Weiwei Liu05d92092016-07-19 17:34:33 -0700162 consumer.run(std::move(discover), std::move(pipeline));
Davide Pesavento296b3852019-10-20 16:00:16 -0400163 this->advanceClocks(io, 1_ms);
Weiwei Liu05d92092016-07-19 17:34:33 -0700164
Davide Pesavento296b3852019-10-20 16:00:16 -0400165 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); // no discovery Interests are issued
Weiwei Liu05d92092016-07-19 17:34:33 -0700166 BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, true);
167}
168
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100169BOOST_AUTO_TEST_SUITE_END() // TestConsumer
170BOOST_AUTO_TEST_SUITE_END() // Chunks
171
172} // namespace tests
173} // namespace chunks
174} // namespace ndn