blob: fec11c6414b978c4b8dffbaec447c0d65a61a2b2 [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;
Davide Pesaventof3637522022-04-20 17:05:06 -040058 Name keyLocatorName = m_keyChain.createIdentity("/putchunks/producer")
59 .getDefaultKey().getDefaultCertificate().getName();
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050060 std::istringstream testString{
61 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
62 "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
63 "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
64 "sem. Nulla consequat massa Donec pede justo,"s};
Junxiao Shi20b22972017-05-24 21:04:16 +000065};
66
Andrea Tosatto672b9a72016-01-05 16:18:20 +010067BOOST_AUTO_TEST_SUITE(Chunks)
Junxiao Shi20b22972017-05-24 21:04:16 +000068BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069
70BOOST_AUTO_TEST_CASE(InputData)
71{
Davide Pesaventof3637522022-04-20 17:05:06 -040072 const std::vector<std::string> testStrings{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073 "",
74
75 "a1b2c3%^&(#$&%^$$/><",
76
77 "123456789123456789123456789123456789123456789123456789123456789"
78 "123456789123456789123456789123456789123456789123456789123456789",
79
80 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
81 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
82 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
83 "consequat massa Donec pede justo,"
84 };
85
Davide Pesaventof3637522022-04-20 17:05:06 -040086 for (const auto& str : testStrings) {
87 std::istringstream input(str);
Davide Pesavento6f76afc2017-09-19 18:43:51 -040088 Producer prod(prefix, face, m_keyChain, input, options);
Davide Pesaventof3637522022-04-20 17:05:06 -040089 size_t expectedSize = str.empty() ? 1 : std::ceil(static_cast<double>(str.size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010090 BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize);
91 }
92}
93
94BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion)
95{
Davide Pesavento6f76afc2017-09-19 18:43:51 -040096 Producer producer(prefix, face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -050097 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -040098 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010099
100 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600101 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100102 face.processEvents();
103
104 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
105 auto lastData = face.sentData.back();
106 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
107 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400108 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
109 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100110
111 // segment request
112 Name nameWithVersion(prefix);
113 nameWithVersion.append(lastData.getName()[-2]);
114 size_t requestSegmentNo = 1;
115
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600116 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100117 face.processEvents();
118
119 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
120 lastData = face.sentData.back();
121 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
122 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento35275582020-07-27 18:02:03 -0400123 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
124 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100125}
126
127BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
128{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400129 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500130 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400131 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100132
133 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600134 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100135 face.processEvents();
136
137 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
138 auto lastData = face.sentData.back();
139 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
140 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
141 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400142 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
143 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100144
145 // segment request
146 Name nameWithVersion(prefix);
147 size_t requestSegmentNo = 1;
148
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600149 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100150 face.processEvents();
151
152 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
153 lastData = face.sentData.back();
154 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
155 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
156 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento35275582020-07-27 18:02:03 -0400157 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
158 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100159}
160
161BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
162{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400163 Producer producer(prefix, face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500164 m_io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400165 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100166
167 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600168 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100169 face.processEvents();
170
171 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
172 auto lastData = face.sentData.back();
173 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
174 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400175 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
176 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100177
178 // segment request
179 Name nameWithVersion(prefix);
180 nameWithVersion.append(lastData.getName()[-2]);
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600181 face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100182 face.processEvents();
183
184 // no new data
185 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
186}
187
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800188BOOST_AUTO_TEST_CASE(RequestMetadata)
189{
190 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Davide Pesaventoa0e6b602021-01-21 19:47:04 -0500191 m_io.poll();
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800192
193 // ask for metadata with a valid discovery interest
194 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1)));
195 face.processEvents();
196
197 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
198 auto lastData = face.sentData.back();
199
200 // check the name of metadata packet
201 BOOST_CHECK(MetadataObject::isValidName(lastData.getName()));
202
203 // make metadata object from metadata packet
204 MetadataObject mobject(lastData);
205 BOOST_CHECK_EQUAL(mobject.getVersionedName(), prefix);
206
207 // ask for metadata with an invalid discovery interest
208 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1))
209 .setCanBePrefix(false));
210 face.processEvents();
211
212 // we expect Nack in response to a discovery interest without CanBePrefix
213 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
214}
215
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100216BOOST_AUTO_TEST_SUITE_END() // TestProducer
217BOOST_AUTO_TEST_SUITE_END() // Chunks
218
Davide Pesaventob3570c62022-02-19 19:19:00 -0500219} // namespace ndn::chunks::tests