blob: c19920b573e96aba8874dfff3bfc23d666fa16e7 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi20b22972017-05-24 21:04:16 +00003 * Copyright (c) 2016-2017, Regents of the University of California,
4 * 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")
50 , testString(std::string(
51 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
52 "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
53 "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
54 "sem. Nulla consequat massa Donec pede justo,"))
55 ,keyLocatorName(m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName())
56 {
57 }
58
59protected:
60 boost::asio::io_service io;
61 util::DummyClientFace face;
62 security::SigningInfo signingInfo;
63 Name prefix;
64 time::milliseconds freshnessPeriod = time::seconds(10);
65 size_t maxSegmentSize = 40;
66 std::istringstream testString;
67 uint64_t version = 1449227841747;
68 Name keyLocatorName;
69};
70
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071BOOST_AUTO_TEST_SUITE(Chunks)
Junxiao Shi20b22972017-05-24 21:04:16 +000072BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010073
74BOOST_AUTO_TEST_CASE(InputData)
75{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010076 std::vector<std::string> testStrings {
77 "",
78
79 "a1b2c3%^&(#$&%^$$/><",
80
81 "123456789123456789123456789123456789123456789123456789123456789"
82 "123456789123456789123456789123456789123456789123456789123456789",
83
84 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
85 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
86 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
87 "consequat massa Donec pede justo,"
88 };
89
90 for (size_t i = 0; i < testStrings.size(); ++i) {
91 std::istringstream str(testStrings[i]);
Junxiao Shi20b22972017-05-24 21:04:16 +000092 Producer prod(prefix, face, m_keyChain, signingInfo, time::seconds(4), maxSegmentSize, false,
Andrea Tosatto672b9a72016-01-05 16:18:20 +010093 false, str);
94
95 size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / maxSegmentSize);
96 if (testStrings[i].size() == 0)
97 expectedSize = 1;
98
99 BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize);
100 }
101}
102
103BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion)
104{
Junxiao Shi20b22972017-05-24 21:04:16 +0000105 Producer producer(prefix, face, m_keyChain, signingInfo, freshnessPeriod, maxSegmentSize,
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100106 false, false, testString);
107 io.poll();
108
109 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize);
110
111 // version request
112 face.receive(*makeInterest(prefix));
113 face.processEvents();
114
115 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
116 auto lastData = face.sentData.back();
117 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
118 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
119 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
120 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
121 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
122
123 // segment request
124 Name nameWithVersion(prefix);
125 nameWithVersion.append(lastData.getName()[-2]);
126 size_t requestSegmentNo = 1;
127
128 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo)));
129 face.processEvents();
130
131 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
132 lastData = face.sentData.back();
133 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
134 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
135 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
136 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
137 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
138}
139
140BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
141{
Junxiao Shi20b22972017-05-24 21:04:16 +0000142 Producer producer(prefix.appendVersion(version), face, m_keyChain, signingInfo, freshnessPeriod,
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100143 maxSegmentSize, false, false, testString);
144 io.poll();
145
146 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize);
147
148 // version request
149 face.receive(*makeInterest(prefix));
150 face.processEvents();
151
152 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
153 auto 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(), 0);
157 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
158 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
159 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
160
161 // segment request
162 Name nameWithVersion(prefix);
163 size_t requestSegmentNo = 1;
164
165 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo)));
166 face.processEvents();
167
168 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
169 lastData = face.sentData.back();
170 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
171 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
172 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
173 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
174 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
175 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
176}
177
178BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
179{
Junxiao Shi20b22972017-05-24 21:04:16 +0000180 Producer producer(prefix, face, m_keyChain, signingInfo, freshnessPeriod, maxSegmentSize,
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100181 false, false, testString);
182 io.poll();
183
184 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize);
185
186 // version request
187 face.receive(*makeInterest(prefix));
188 face.processEvents();
189
190 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
191 auto lastData = face.sentData.back();
192 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
193 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
194 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
195 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
196 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
197
198 // segment request
199 Name nameWithVersion(prefix);
200 nameWithVersion.append(lastData.getName()[-2]);
201 face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments)));
202 face.processEvents();
203
204 // no new data
205 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
206}
207
208BOOST_AUTO_TEST_SUITE_END() // TestProducer
209BOOST_AUTO_TEST_SUITE_END() // Chunks
210
211} // namespace tests
212} // namespace chunks
213} // namespace ndn