blob: e2de21e3334c6bae25d78c27abfa2b4aafe915a7 [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/*
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -08003 * Copyright (c) 2016-2019, 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 Pesavento969cd5a2018-04-20 16:27:47 -0400118 BOOST_REQUIRE(lastData.getFinalBlock());
119 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100120 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
121
122 // segment request
123 Name nameWithVersion(prefix);
124 nameWithVersion.append(lastData.getName()[-2]);
125 size_t requestSegmentNo = 1;
126
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600127 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100128 face.processEvents();
129
130 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
131 lastData = face.sentData.back();
132 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
133 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400134 BOOST_REQUIRE(lastData.getFinalBlock());
135 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100136 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
137}
138
139BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
140{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400141 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100142 io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400143 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100144
145 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600146 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100147 face.processEvents();
148
149 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
150 auto lastData = face.sentData.back();
151 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
152 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
153 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400154 BOOST_REQUIRE(lastData.getFinalBlock());
155 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100156 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
157
158 // segment request
159 Name nameWithVersion(prefix);
160 size_t requestSegmentNo = 1;
161
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600162 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100163 face.processEvents();
164
165 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
166 lastData = face.sentData.back();
167 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
168 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
169 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400170 BOOST_REQUIRE(lastData.getFinalBlock());
171 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100172 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
173}
174
175BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
176{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400177 Producer producer(prefix, face, m_keyChain, testString, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100178 io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400179 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100180
181 // version request
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600182 face.receive(*makeInterest(prefix, true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100183 face.processEvents();
184
185 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
186 auto lastData = face.sentData.back();
187 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
188 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400189 BOOST_REQUIRE(lastData.getFinalBlock());
190 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100191 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
192
193 // segment request
194 Name nameWithVersion(prefix);
195 nameWithVersion.append(lastData.getName()[-2]);
Junxiao Shi20d5a0b2018-08-14 09:26:11 -0600196 face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments), true));
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100197 face.processEvents();
198
199 // no new data
200 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
201}
202
Chavoosh Ghasemi79991a02019-01-28 21:22:31 -0800203BOOST_AUTO_TEST_CASE(RequestMetadata)
204{
205 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
206 io.poll();
207
208 // ask for metadata with a valid discovery interest
209 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1)));
210 face.processEvents();
211
212 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
213 auto lastData = face.sentData.back();
214
215 // check the name of metadata packet
216 BOOST_CHECK(MetadataObject::isValidName(lastData.getName()));
217
218 // make metadata object from metadata packet
219 MetadataObject mobject(lastData);
220 BOOST_CHECK_EQUAL(mobject.getVersionedName(), prefix);
221
222 // ask for metadata with an invalid discovery interest
223 face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1))
224 .setCanBePrefix(false));
225 face.processEvents();
226
227 // we expect Nack in response to a discovery interest without CanBePrefix
228 BOOST_CHECK_EQUAL(face.sentNacks.size(), 1);
229}
230
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100231BOOST_AUTO_TEST_SUITE_END() // TestProducer
232BOOST_AUTO_TEST_SUITE_END() // Chunks
233
234} // namespace tests
235} // namespace chunks
236} // namespace ndn