blob: a4a280b75e6b1e5c59bab624434a156665f72f72 [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 Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, 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
Davide Pesaventob3570c62022-02-19 19:19:00 -050040namespace ndn::chunks::tests {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010041
42using namespace ndn::tests;
43
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050044class ProducerFixture : public IoFixture, public KeyChainFixture
Junxiao Shi20b22972017-05-24 21:04:16 +000045{
46protected:
47 ProducerFixture()
Junxiao Shi20b22972017-05-24 21:04:16 +000048 {
Davide Pesavento6f76afc2017-09-19 18:43:51 -040049 options.maxSegmentSize = 40;
50 options.isQuiet = true;
Junxiao Shi20b22972017-05-24 21:04:16 +000051 }
52
53protected:
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050054 util::DummyClientFace face{m_io, {true, true}};
55 Name prefix = "/ndn/chunks/test";
Davide Pesavento6f76afc2017-09-19 18:43:51 -040056 Producer::Options options;
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050057 uint64_t version = 1449227841747;
58 Name keyLocatorName = m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName();
59 std::istringstream testString{
60 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
61 "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
62 "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
63 "sem. Nulla consequat massa Donec pede justo,"s};
Junxiao Shi20b22972017-05-24 21:04:16 +000064};
65
Andrea Tosatto672b9a72016-01-05 16:18:20 +010066BOOST_AUTO_TEST_SUITE(Chunks)
Junxiao Shi20b22972017-05-24 21:04:16 +000067BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010068
69BOOST_AUTO_TEST_CASE(InputData)
70{
Davide Pesavento6f76afc2017-09-19 18:43:51 -040071 std::vector<std::string> testStrings{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072 "",
73
74 "a1b2c3%^&(#$&%^$$/><",
75
76 "123456789123456789123456789123456789123456789123456789123456789"
77 "123456789123456789123456789123456789123456789123456789123456789",
78
79 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
80 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
81 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
82 "consequat massa Donec pede justo,"
83 };
84
Davide Pesavento6f76afc2017-09-19 18:43:51 -040085 for (size_t i = 0; i < testStrings.size(); ++i) {
86 std::istringstream input(testStrings[i]);
87 Producer prod(prefix, face, m_keyChain, input, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010088
Davide Pesavento6f76afc2017-09-19 18:43:51 -040089 size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / options.maxSegmentSize);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050090 if (testStrings[i].empty()) {
Andrea Tosatto672b9a72016-01-05 16:18:20 +010091 expectedSize = 1;
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050092 }
Andrea Tosatto672b9a72016-01-05 16:18:20 +010093
94 BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize);
95 }
96}
97
98BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion)
99{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400100 Producer producer(prefix, face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500101 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400102 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100103
104 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600105 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100106 face.processEvents();
107
108 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
109 auto lastData = face.sentData.back();
110 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
111 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400112 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
113 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100114
115 // segment request
116 Name nameWithVersion(prefix);
117 nameWithVersion.append(lastData.getName()[-2]);
118 size_t requestSegmentNo = 1;
119
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600120 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100121 face.processEvents();
122
123 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
124 lastData = face.sentData.back();
125 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
126 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento35275582020-07-27 18:02:03 -0400127 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
128 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100129}
130
131BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
132{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400133 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500134 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400135 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100136
137 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600138 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100139 face.processEvents();
140
141 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
142 auto lastData = face.sentData.back();
143 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
144 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
145 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400146 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
147 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100148
149 // segment request
150 Name nameWithVersion(prefix);
151 size_t requestSegmentNo = 1;
152
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600153 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100154 face.processEvents();
155
156 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
157 lastData = face.sentData.back();
158 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
159 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
160 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento35275582020-07-27 18:02:03 -0400161 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
162 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100163}
164
165BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
166{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400167 Producer producer(prefix, face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500168 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400169 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100170
171 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600172 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100173 face.processEvents();
174
175 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
176 auto lastData = face.sentData.back();
177 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
178 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400179 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
180 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100181
182 // segment request
183 Name nameWithVersion(prefix);
184 nameWithVersion.append(lastData.getName()[-2]);
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600185 face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100186 face.processEvents();
187
188 // no new data
189 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
190}
191
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800192BOOST_AUTO_TEST_CASE(RequestMetadata)
193{
194 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500195 m_io.poll();
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800196
197 // ask for metadata with a valid discovery interest
198 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1)));
199 face.processEvents();
200
201 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
202 auto lastData = face.sentData.back();
203
204 // check the name of metadata packet
205 BOOST_CHECK(MetadataObject::isValidName(lastData.getName()));
206
207 // make metadata object from metadata packet
208 MetadataObject mobject(lastData);
209 BOOST_CHECK_EQUAL(mobject.getVersionedName(), prefix);
210
211 // ask for metadata with an invalid discovery interest
212 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1))
213 .setCanBePrefix(false));
214 face.processEvents();
215
216 // we expect Nack in response to a discovery interest without CanBePrefix
217 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
218}
219
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100220BOOST_AUTO_TEST_SUITE_END() // TestProducer
221BOOST_AUTO_TEST_SUITE_END() // Chunks
222
Davide Pesaventob3570c62022-02-19 19:19:00 -0500223} // namespace ndn::chunks::tests