blob: 6b4632042cd39f0aee959841d74b022a69a8d21e [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 Pesavento35275582020-07-27 18:02:03 -04003 * Copyright (c) 2016-2020, 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"
29#include "tests/identity-management-fixture.hpp"
30
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -080031#include <ndn-cxx/metadata-object.hpp>
Junxiao Shi20b22972017-05-24 21:04:16 +000032#include <ndn-cxx/security/pib/identity.hpp>
33#include <ndn-cxx/security/pib/key.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010034#include <ndn-cxx/util/dummy-client-face.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010035
Davide Pesaventoc0702702017-08-24 22:04:00 -040036#include <cmath>
37#include <sstream>
Junxiao Shi20b22972017-05-24 21:04:16 +000038
Andrea Tosatto672b9a72016-01-05 16:18:20 +010039namespace ndn {
40namespace chunks {
41namespace tests {
42
43using namespace ndn::tests;
44
Junxiao Shi20b22972017-05-24 21:04:16 +000045class ProducerFixture : public IdentityManagementFixture
46{
47protected:
48 ProducerFixture()
49 : face(io, {true, true})
50 , prefix("/ndn/chunks/test")
Davide Pesavento6f76afc2017-09-19 18:43:51 -040051 , version(1449227841747)
52 , keyLocatorName(m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName())
Junxiao Shi20b22972017-05-24 21:04:16 +000053 , testString(std::string(
54 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
55 "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
56 "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
57 "sem. Nulla consequat massa Donec pede justo,"))
Junxiao Shi20b22972017-05-24 21:04:16 +000058 {
Davide Pesavento6f76afc2017-09-19 18:43:51 -040059 options.maxSegmentSize = 40;
60 options.isQuiet = true;
Junxiao Shi20b22972017-05-24 21:04:16 +000061 }
62
63protected:
64 boost::asio::io_service io;
65 util::DummyClientFace face;
Junxiao Shi20b22972017-05-24 21:04:16 +000066 Name prefix;
Davide Pesavento6f76afc2017-09-19 18:43:51 -040067 Producer::Options options;
68 uint64_t version;
Junxiao Shi20b22972017-05-24 21:04:16 +000069 Name keyLocatorName;
Davide Pesavento6f76afc2017-09-19 18:43:51 -040070 std::istringstream testString;
Junxiao Shi20b22972017-05-24 21:04:16 +000071};
72
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073BOOST_AUTO_TEST_SUITE(Chunks)
Junxiao Shi20b22972017-05-24 21:04:16 +000074BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010075
76BOOST_AUTO_TEST_CASE(InputData)
77{
Davide Pesavento6f76afc2017-09-19 18:43:51 -040078 std::vector<std::string> testStrings{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010079 "",
80
81 "a1b2c3%^&(#$&%^$$/><",
82
83 "123456789123456789123456789123456789123456789123456789123456789"
84 "123456789123456789123456789123456789123456789123456789123456789",
85
86 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
87 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
88 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
89 "consequat massa Donec pede justo,"
90 };
91
Davide Pesavento6f76afc2017-09-19 18:43:51 -040092 for (size_t i = 0; i < testStrings.size(); ++i) {
93 std::istringstream input(testStrings[i]);
94 Producer prod(prefix, face, m_keyChain, input, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010095
Davide Pesavento6f76afc2017-09-19 18:43:51 -040096 size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / options.maxSegmentSize);
97 if (testStrings[i].empty())
Andrea Tosatto672b9a72016-01-05 16:18:20 +010098 expectedSize = 1;
99
100 BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize);
101 }
102}
103
104BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion)
105{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400106 Producer producer(prefix, face, m_keyChain, testString, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100107 io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400108 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100109
110 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600111 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100112 face.processEvents();
113
114 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
115 auto lastData = face.sentData.back();
116 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
117 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400118 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
119 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100120
121 // segment request
122 Name nameWithVersion(prefix);
123 nameWithVersion.append(lastData.getName()[-2]);
124 size_t requestSegmentNo = 1;
125
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600126 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100127 face.processEvents();
128
129 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
130 lastData = face.sentData.back();
131 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
132 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento35275582020-07-27 18:02:03 -0400133 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
134 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100135}
136
137BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
138{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400139 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100140 io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400141 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100142
143 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600144 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100145 face.processEvents();
146
147 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
148 auto lastData = face.sentData.back();
149 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
150 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
151 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400152 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
153 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100154
155 // segment request
156 Name nameWithVersion(prefix);
157 size_t requestSegmentNo = 1;
158
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600159 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100160 face.processEvents();
161
162 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
163 lastData = face.sentData.back();
164 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
165 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
166 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento35275582020-07-27 18:02:03 -0400167 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
168 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100169}
170
171BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
172{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400173 Producer producer(prefix, face, m_keyChain, testString, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100174 io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400175 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100176
177 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600178 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100179 face.processEvents();
180
181 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
182 auto lastData = face.sentData.back();
183 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
184 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento35275582020-07-27 18:02:03 -0400185 BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1);
186 BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100187
188 // segment request
189 Name nameWithVersion(prefix);
190 nameWithVersion.append(lastData.getName()[-2]);
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600191 face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100192 face.processEvents();
193
194 // no new data
195 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
196}
197
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800198BOOST_AUTO_TEST_CASE(RequestMetadata)
199{
200 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
201 io.poll();
202
203 // ask for metadata with a valid discovery interest
204 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1)));
205 face.processEvents();
206
207 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
208 auto lastData = face.sentData.back();
209
210 // check the name of metadata packet
211 BOOST_CHECK(MetadataObject::isValidName(lastData.getName()));
212
213 // make metadata object from metadata packet
214 MetadataObject mobject(lastData);
215 BOOST_CHECK_EQUAL(mobject.getVersionedName(), prefix);
216
217 // ask for metadata with an invalid discovery interest
218 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1))
219 .setCanBePrefix(false));
220 face.processEvents();
221
222 // we expect Nack in response to a discovery interest without CanBePrefix
223 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
224}
225
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100226BOOST_AUTO_TEST_SUITE_END() // TestProducer
227BOOST_AUTO_TEST_SUITE_END() // Chunks
228
229} // namespace tests
230} // namespace chunks
231} // namespace ndn