blob: d3c339f4f5ca67bf15827090579214062b25acba [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
Junxiao Shi20b22972017-05-24 21:04:16 +000028#include <ndn-cxx/security/pib/identity.hpp>
29#include <ndn-cxx/security/pib/key.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010030#include <ndn-cxx/util/dummy-client-face.hpp>
Andrea Tosatto672b9a72016-01-05 16:18:20 +010031#include <cmath>
32
Junxiao Shi20b22972017-05-24 21:04:16 +000033#include "tests/test-common.hpp"
34#include "tests/identity-management-fixture.hpp"
35
Andrea Tosatto672b9a72016-01-05 16:18:20 +010036namespace ndn {
37namespace chunks {
38namespace tests {
39
40using namespace ndn::tests;
41
Junxiao Shi20b22972017-05-24 21:04:16 +000042class ProducerFixture : public IdentityManagementFixture
43{
44protected:
45 ProducerFixture()
46 : face(io, {true, true})
47 , prefix("/ndn/chunks/test")
48 , testString(std::string(
49 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget "
50 "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, "
51 "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, "
52 "sem. Nulla consequat massa Donec pede justo,"))
53 ,keyLocatorName(m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName())
54 {
55 }
56
57protected:
58 boost::asio::io_service io;
59 util::DummyClientFace face;
60 security::SigningInfo signingInfo;
61 Name prefix;
62 time::milliseconds freshnessPeriod = time::seconds(10);
63 size_t maxSegmentSize = 40;
64 std::istringstream testString;
65 uint64_t version = 1449227841747;
66 Name keyLocatorName;
67};
68
Andrea Tosatto672b9a72016-01-05 16:18:20 +010069BOOST_AUTO_TEST_SUITE(Chunks)
Junxiao Shi20b22972017-05-24 21:04:16 +000070BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture)
Andrea Tosatto672b9a72016-01-05 16:18:20 +010071
72BOOST_AUTO_TEST_CASE(InputData)
73{
Andrea Tosatto672b9a72016-01-05 16:18:20 +010074 std::vector<std::string> testStrings {
75 "",
76
77 "a1b2c3%^&(#$&%^$$/><",
78
79 "123456789123456789123456789123456789123456789123456789123456789"
80 "123456789123456789123456789123456789123456789123456789123456789",
81
82 "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. "
83 "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur "
84 "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla "
85 "consequat massa Donec pede justo,"
86 };
87
88 for (size_t i = 0; i < testStrings.size(); ++i) {
89 std::istringstream str(testStrings[i]);
Junxiao Shi20b22972017-05-24 21:04:16 +000090 Producer prod(prefix, face, m_keyChain, signingInfo, time::seconds(4), maxSegmentSize, false,
Andrea Tosatto672b9a72016-01-05 16:18:20 +010091 false, str);
92
93 size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / maxSegmentSize);
94 if (testStrings[i].size() == 0)
95 expectedSize = 1;
96
97 BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize);
98 }
99}
100
101BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion)
102{
Junxiao Shi20b22972017-05-24 21:04:16 +0000103 Producer producer(prefix, face, m_keyChain, signingInfo, freshnessPeriod, maxSegmentSize,
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100104 false, false, testString);
105 io.poll();
106
107 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize);
108
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);
117 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
118 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
119 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);
133 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
134 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
135 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
136}
137
138BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion)
139{
Junxiao Shi20b22972017-05-24 21:04:16 +0000140 Producer producer(prefix.appendVersion(version), face, m_keyChain, signingInfo, freshnessPeriod,
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100141 maxSegmentSize, false, false, testString);
142 io.poll();
143
144 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize);
145
146 // version request
147 face.receive(*makeInterest(prefix));
148 face.processEvents();
149
150 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
151 auto lastData = face.sentData.back();
152 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
153 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
154 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
155 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
156 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
157 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
158
159 // segment request
160 Name nameWithVersion(prefix);
161 size_t requestSegmentNo = 1;
162
163 face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo)));
164 face.processEvents();
165
166 BOOST_REQUIRE_EQUAL(face.sentData.size(), 2);
167 lastData = face.sentData.back();
168 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1);
169 BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version);
170 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo);
171 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
172 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
173 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
174}
175
176BOOST_AUTO_TEST_CASE(RequestNotExistingSegment)
177{
Junxiao Shi20b22972017-05-24 21:04:16 +0000178 Producer producer(prefix, face, m_keyChain, signingInfo, freshnessPeriod, maxSegmentSize,
Andrea Tosatto672b9a72016-01-05 16:18:20 +0100179 false, false, testString);
180 io.poll();
181
182 size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize);
183
184 // version request
185 face.receive(*makeInterest(prefix));
186 face.processEvents();
187
188 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
189 auto lastData = face.sentData.back();
190 BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2);
191 BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0);
192 BOOST_REQUIRE(!lastData.getFinalBlockId().empty());
193 BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1);
194 BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName);
195
196 // segment request
197 Name nameWithVersion(prefix);
198 nameWithVersion.append(lastData.getName()[-2]);
199 face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments)));
200 face.processEvents();
201
202 // no new data
203 BOOST_REQUIRE_EQUAL(face.sentData.size(), 1);
204}
205
206BOOST_AUTO_TEST_SUITE_END() // TestProducer
207BOOST_AUTO_TEST_SUITE_END() // Chunks
208
209} // namespace tests
210} // namespace chunks
211} // namespace ndn