blob: ef534224d03768ad201265f9dfde39202fc907af [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Klaus Schneider59ec95a2017-09-14 17:50:00 -07002/*
Davide Pesaventoa0e6b602021-01-21 19:47:04 -05003 * Copyright (c) 2016-2021, Regents of the University of California,
Junxiao Shi20b22972017-05-24 21:04:16 +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/putchunks/producer.hpp"
27
Davide Pesaventoc0702702017-08-24 22:04:00 -040028#include "tests/test-common.hpp"
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050029#include "tests/io-fixture.hpp"
Davide Pesavento66777622020-10-09 18:46:03 -040030#include "tests/key-chain-fixture.hpp"
Davide Pesaventoc0702702017-08-24 22:04:00 -040031
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080032#include <ndn-cxx/metadata-object.hpp>
Junxiao Shi20b22972017-05-24 21:04:16 +000033#include <ndn-cxx/security/pib/identity.hpp>
34#include <ndn-cxx/security/pib/key.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035#include <ndn-cxx/util/dummy-client-face.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036
Davide Pesaventoc0702702017-08-24 22:04:00 -040037#include <cmath>
38#include <sstream>
Junxiao Shi20b22972017-05-24 21:04:16 +000039
Andrea Tosatto672b9a72016-01-05 16:18:20 +010040namespace ndn {
41namespace chunks {
42namespace tests {
43
44using namespace ndn::tests;
45
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050046class ProducerFixture : public IoFixture, public KeyChainFixture
Junxiao Shi20b22972017-05-24 21:04:16 +000047{
48protected:
49 ProducerFixture()
Junxiao Shi20b22972017-05-24 21:04:16 +000050 {
Davide Pesavento6f76afc2017-09-19 18:43:51 -040051 options.maxSegmentSize = 40;
52 options.isQuiet = true;
Junxiao Shi20b22972017-05-24 21:04:16 +000053 }
54
55protected:
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050056 util::DummyClientFace face{m_io, {true, true}};
57 Name prefix = "/ndn/chunks/test";
Davide Pesavento6f76afc2017-09-19 18:43:51 -040058 Producer::Options options;
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050059 uint64_t version = 1449227841747;
60 Name keyLocatorName = m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName();
61 std::istringstream testString{
62 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
63 "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
64 "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
65 "sem. Nulla consequat massa Donec pede justo,"s};
Junxiao Shi20b22972017-05-24 21:04:16 +000066};
67
Andrea Tosatto672b9a72016-01-05 16:18:20 +010068BOOST_AUTO_TEST_SUITE(Chunks)
Junxiao Shi20b22972017-05-24 21:04:16 +000069BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010070
71BOOST_AUTO_TEST_CASE(InputData)
72{
Davide Pesavento6f76afc2017-09-19 18:43:51 -040073 std::vector<std::string> testStrings{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010074 "",
75
76 "a1b2c3%^&(#$&%^$$/><",
77
78 "123456789123456789123456789123456789123456789123456789123456789"
79 "123456789123456789123456789123456789123456789123456789123456789",
80
81 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
82 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
83 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
84 "consequat massa Donec pede justo,"
85 };
86
Davide Pesavento6f76afc2017-09-19 18:43:51 -040087 for (size_t i = 0; i < testStrings.size(); ++i) {
88 std::istringstream input(testStrings[i]);
89 Producer prod(prefix, face, m_keyChain, input, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010090
Davide Pesavento6f76afc2017-09-19 18:43:51 -040091 size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / options.maxSegmentSize);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050092 if (testStrings[i].empty()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010093 expectedSize = 1;
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050094 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +010095
96 BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize);
97 }
98}
99
100BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion)
101{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400102 Producer producer(prefix, face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500103 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400104 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100105
106 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600107 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100108 face.processEvents();
109
110 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
111 auto lastData = face.sentData.back();
112 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
113 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400114 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
115 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100116
117 // segment request
118 Name nameWithVersion(prefix);
119 nameWithVersion.append(lastData.getName()[-2]);
120 size_t requestSegmentNo = 1;
121
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600122 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100123 face.processEvents();
124
125 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
126 lastData = face.sentData.back();
127 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
128 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento35275582020-07-27 18:02:03 -0400129 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
130 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100131}
132
133BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
134{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400135 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500136 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400137 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100138
139 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600140 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141 face.processEvents();
142
143 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
144 auto lastData = face.sentData.back();
145 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
146 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
147 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400148 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
149 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100150
151 // segment request
152 Name nameWithVersion(prefix);
153 size_t requestSegmentNo = 1;
154
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600155 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100156 face.processEvents();
157
158 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
159 lastData = face.sentData.back();
160 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
161 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
162 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento35275582020-07-27 18:02:03 -0400163 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
164 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100165}
166
167BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
168{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400169 Producer producer(prefix, face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500170 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400171 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100172
173 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600174 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100175 face.processEvents();
176
177 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
178 auto lastData = face.sentData.back();
179 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
180 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400181 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
182 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100183
184 // segment request
185 Name nameWithVersion(prefix);
186 nameWithVersion.append(lastData.getName()[-2]);
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600187 face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100188 face.processEvents();
189
190 // no new data
191 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
192}
193
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800194BOOST_AUTO_TEST_CASE(RequestMetadata)
195{
196 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500197 m_io.poll();
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800198
199 // ask for metadata with a valid discovery interest
200 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1)));
201 face.processEvents();
202
203 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
204 auto lastData = face.sentData.back();
205
206 // check the name of metadata packet
207 BOOST_CHECK(MetadataObject::isValidName(lastData.getName()));
208
209 // make metadata object from metadata packet
210 MetadataObject mobject(lastData);
211 BOOST_CHECK_EQUAL(mobject.getVersionedName(), prefix);
212
213 // ask for metadata with an invalid discovery interest
214 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1))
215 .setCanBePrefix(false));
216 face.processEvents();
217
218 // we expect Nack in response to a discovery interest without CanBePrefix
219 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
220}
221
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100222BOOST_AUTO_TEST_SUITE_END() // TestProducer
223BOOST_AUTO_TEST_SUITE_END() // Chunks
224
225} // namespace tests
226} // namespace chunks
227} // namespace ndn