blob: c508abc6bf2fe21a299b339c350fb314577c20c2 [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 Pesavento969cd5a2018-04-20 16:27:47 -04003 * Copyright (c) 2016-2018, 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
Junxiao Shi20b22972017-05-24 21:04:16 +000031#include <ndn-cxx/security/pib/identity.hpp>
32#include <ndn-cxx/security/pib/key.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010033#include <ndn-cxx/util/dummy-client-face.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010034
Davide Pesaventoc0702702017-08-24 22:04:00 -040035#include <cmath>
36#include <sstream>
Junxiao Shi20b22972017-05-24 21:04:16 +000037
Andrea Tosatto672b9a72016-01-05 16:18:20 +010038namespace ndn {
39namespace chunks {
40namespace tests {
41
42using namespace ndn::tests;
43
Junxiao Shi20b22972017-05-24 21:04:16 +000044class ProducerFixture : public IdentityManagementFixture
45{
46protected:
47 ProducerFixture()
48 : face(io, {true, true})
49 , prefix("/ndn/chunks/test")
Davide Pesavento6f76afc2017-09-19 18:43:51 -040050 , version(1449227841747)
51 , keyLocatorName(m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName())
Junxiao Shi20b22972017-05-24 21:04:16 +000052 , testString(std::string(
53 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
54 "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
55 "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
56 "sem. Nulla consequat massa Donec pede justo,"))
Junxiao Shi20b22972017-05-24 21:04:16 +000057 {
Davide Pesavento6f76afc2017-09-19 18:43:51 -040058 options.maxSegmentSize = 40;
59 options.isQuiet = true;
Junxiao Shi20b22972017-05-24 21:04:16 +000060 }
61
62protected:
63 boost::asio::io_service io;
64 util::DummyClientFace face;
Junxiao Shi20b22972017-05-24 21:04:16 +000065 Name prefix;
Davide Pesavento6f76afc2017-09-19 18:43:51 -040066 Producer::Options options;
67 uint64_t version;
Junxiao Shi20b22972017-05-24 21:04:16 +000068 Name keyLocatorName;
Davide Pesavento6f76afc2017-09-19 18:43:51 -040069 std::istringstream testString;
Junxiao Shi20b22972017-05-24 21:04:16 +000070};
71
Andrea Tosatto672b9a72016-01-05 16:18:20 +010072BOOST_AUTO_TEST_SUITE(Chunks)
Junxiao Shi20b22972017-05-24 21:04:16 +000073BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010074
75BOOST_AUTO_TEST_CASE(InputData)
76{
Davide Pesavento6f76afc2017-09-19 18:43:51 -040077 std::vector<std::string> testStrings{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010078 "",
79
80 "a1b2c3%^&(#$&%^$$/><",
81
82 "123456789123456789123456789123456789123456789123456789123456789"
83 "123456789123456789123456789123456789123456789123456789123456789",
84
85 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
86 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
87 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
88 "consequat massa Donec pede justo,"
89 };
90
Davide Pesavento6f76afc2017-09-19 18:43:51 -040091 for (size_t i = 0; i < testStrings.size(); ++i) {
92 std::istringstream input(testStrings[i]);
93 Producer prod(prefix, face, m_keyChain, input, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +010094
Davide Pesavento6f76afc2017-09-19 18:43:51 -040095 size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / options.maxSegmentSize);
96 if (testStrings[i].empty())
Andrea Tosatto672b9a72016-01-05 16:18:20 +010097 expectedSize = 1;
98
99 BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize);
100 }
101}
102
103BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion)
104{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400105 Producer producer(prefix, face, m_keyChain, testString, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100106 io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400107 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100108
109 // version request
110 face.receive(*makeInterest(prefix));
111 face.processEvents();
112
113 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
114 auto lastData = face.sentData.back();
115 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
116 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400117 BOOST_REQUIRE(lastData.getFinalBlock());
118 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100119 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
120
121 // segment request
122 Name nameWithVersion(prefix);
123 nameWithVersion.append(lastData.getName()[-2]);
124 size_t requestSegmentNo = 1;
125
126 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo)));
127 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 Pesavento969cd5a2018-04-20 16:27:47 -0400133 BOOST_REQUIRE(lastData.getFinalBlock());
134 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100135 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
136}
137
138BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
139{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400140 Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141 io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400142 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100143
144 // version request
145 face.receive(*makeInterest(prefix));
146 face.processEvents();
147
148 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
149 auto lastData = face.sentData.back();
150 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
151 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
152 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400153 BOOST_REQUIRE(lastData.getFinalBlock());
154 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100155 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
156
157 // segment request
158 Name nameWithVersion(prefix);
159 size_t requestSegmentNo = 1;
160
161 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo)));
162 face.processEvents();
163
164 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
165 lastData = face.sentData.back();
166 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
167 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
168 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400169 BOOST_REQUIRE(lastData.getFinalBlock());
170 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100171 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
172}
173
174BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
175{
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400176 Producer producer(prefix, face, m_keyChain, testString, options);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100177 io.poll();
Davide Pesavento6f76afc2017-09-19 18:43:51 -0400178 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100179
180 // version request
181 face.receive(*makeInterest(prefix));
182 face.processEvents();
183
184 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
185 auto lastData = face.sentData.back();
186 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
187 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
Davide Pesavento969cd5a2018-04-20 16:27:47 -0400188 BOOST_REQUIRE(lastData.getFinalBlock());
189 BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1);
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100190 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
191
192 // segment request
193 Name nameWithVersion(prefix);
194 nameWithVersion.append(lastData.getName()[-2]);
195 face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments)));
196 face.processEvents();
197
198 // no new data
199 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
200}
201
202BOOST_AUTO_TEST_SUITE_END() // TestProducer
203BOOST_AUTO_TEST_SUITE_END() // Chunks
204
205} // namespace tests
206} // namespace chunks
207} // namespace ndn