blob: 158148a1258883170df7c695d745a3aac5b7f43b [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2016, Regents of the University of California,
4 * Colorado State University,
5 * University Pierre & Marie Curie, Sorbonne University.
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 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"
31#include <ndn-cxx/util/dummy-client-face.hpp>
32#include <ndn-cxx/security/validator-null.hpp>
33
34#include <boost/test/output_test_stream.hpp>
35
36namespace ndn {
37namespace chunks {
38namespace tests {
39
40using namespace ndn::tests;
41using boost::test_tools::output_test_stream;
42
43BOOST_AUTO_TEST_SUITE(Chunks)
44BOOST_AUTO_TEST_SUITE(TestConsumer)
45
46BOOST_AUTO_TEST_CASE(OutputDataSequential)
47{
48 // Test sequential segments in the right order
49 // Segment order: 0 1 2
50
51 std::string name("/ndn/chunks/test");
52
53 std::vector<std::string> testStrings {
54 "",
55
56 "a1b2c3%^&(#$&%^$$/><",
57
58 "123456789123456789123456789123456789123456789123456789123456789"
59 "123456789123456789123456789123456789123456789123456789123456789",
60
61 "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;
68 ValidatorNull validator;
69 output_test_stream output("");
Weiwei Liu05d92092016-07-19 17:34:33 -070070 Consumer cons(validator, false, output);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071
72 auto interest = makeInterest(name);
73
74 for (size_t i = 0; i < testStrings.size(); ++i) {
75 output.flush();
76
77 auto data = makeData(Name(name).appendVersion(1).appendSegment(i));
78 data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()),
79 testStrings[i].size());
80
81 cons.m_bufferedData[i] = data;
82 cons.writeInOrderData();
83
84 BOOST_CHECK(output.is_equal(testStrings[i]));
85 }
86}
87
88BOOST_AUTO_TEST_CASE(OutputDataUnordered)
89{
90 // Test unordered segments
91 // Segment order: 1 0 2
92
93 std::string name("/ndn/chunks/test");
94
95 std::vector<std::string> testStrings {
96 "a1b2c3%^&(#$&%^$$/><",
97
98 "123456789123456789123456789123456789123456789123456789123456789"
99 "123456789123456789123456789123456789123456789123456789123456789",
100
101 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
102 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
103 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
104 "consequat massa Donec pede justo,"
105 };;
106
107 util::DummyClientFace face;
108 ValidatorNull validator;
109 output_test_stream output("");
Weiwei Liu05d92092016-07-19 17:34:33 -0700110 Consumer cons(validator, false, output);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100111
112 auto interest = makeInterest(name);
113 std::vector<shared_ptr<Data>> dataStore;
114
115 for (size_t i = 0; i < testStrings.size(); ++i) {
116 auto data = makeData(Name(name).appendVersion(1).appendSegment(i));
117 data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()),
118 testStrings[i].size());
119
120 dataStore.push_back(data);
121 }
122
123 output.flush();
124 cons.m_bufferedData[1] = dataStore[1];
125 cons.writeInOrderData();
126 BOOST_CHECK(output.is_equal(""));
127
128 output.flush();
129 cons.m_bufferedData[0] = dataStore[0];
130 cons.writeInOrderData();
131 BOOST_CHECK(output.is_equal(testStrings[0] + testStrings[1]));
132
133 output.flush();
134 cons.m_bufferedData[2] = dataStore[2];
135 cons.writeInOrderData();
136 BOOST_CHECK(output.is_equal(testStrings[2]));
137}
138
Weiwei Liu05d92092016-07-19 17:34:33 -0700139class DiscoverVersionDummy : public DiscoverVersion
140{
141public:
142 DiscoverVersionDummy(const Name& prefix, Face& face, const Options& options)
143 : DiscoverVersion(prefix, face, options)
144 , isDiscoverRunning(false)
145 , m_prefix(prefix)
146 {
147 }
148
149 void
150 run() final
151 {
152 isDiscoverRunning = true;
153
154 auto interest = makeInterest(m_prefix);
155 expressInterest(*interest, 1, 1);
156 }
157
158private:
159 void
160 handleData(const Interest& interest, const Data& data) final
161 {
162 this->emitSignal(onDiscoverySuccess, data);
163 }
164
165public:
166 bool isDiscoverRunning;
167
168private:
169 Name m_prefix;
170};
171
172class PipelineInterestsDummy : public PipelineInterests
173{
174public:
175 PipelineInterestsDummy(Face& face)
176 : PipelineInterests(face)
177 , isPipelineRunning(false)
178 {
179 }
180
181private:
182 void
183 doRun() final
184 {
185 isPipelineRunning = true;
186 }
187
188 void
189 doCancel() final
190 {
191 }
192
193public:
194 bool isPipelineRunning;
195};
196
197BOOST_FIXTURE_TEST_CASE(RunBasic, UnitTestTimeFixture)
198{
199 boost::asio::io_service io;
200 util::DummyClientFace face(io);
201 ValidatorNull validator;
202 Consumer consumer(validator, false);
203
204 Name prefix("/ndn/chunks/test");
205 auto discover = make_unique<DiscoverVersionDummy>(prefix, face, Options());
206 auto pipeline = make_unique<PipelineInterestsDummy>(face);
207 auto discoverPtr = discover.get();
208 auto pipelinePtr = pipeline.get();
209
210 consumer.run(std::move(discover), std::move(pipeline));
211 BOOST_CHECK_EQUAL(discoverPtr->isDiscoverRunning, true);
212
213 this->advanceClocks(io, time::nanoseconds(1));
214 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
215
216 auto data = makeData(prefix.appendSegment(0));
217 face.receive(*data);
218
219 this->advanceClocks(io, time::nanoseconds(1));
220 BOOST_CHECK_EQUAL(pipelinePtr->isPipelineRunning, true);
221}
222
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100223BOOST_AUTO_TEST_SUITE_END() // TestConsumer
224BOOST_AUTO_TEST_SUITE_END() // Chunks
225
226} // namespace tests
227} // namespace chunks
228} // namespace ndn