Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Klaus Schneider | 59ec95a | 2017-09-14 17:50:00 -0700 | [diff] [blame] | 2 | /* |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 3 | * Copyright (c) 2016-2019, Regents of the University of California, |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 4 | * Colorado State University, |
| 5 | * University Pierre & Marie Curie, Sorbonne University. |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 6 | * |
| 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 Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 28 | #include "tests/test-common.hpp" |
| 29 | #include "tests/identity-management-fixture.hpp" |
| 30 | |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 31 | #include <ndn-cxx/metadata-object.hpp> |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 32 | #include <ndn-cxx/security/pib/identity.hpp> |
| 33 | #include <ndn-cxx/security/pib/key.hpp> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 34 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 35 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 36 | #include <cmath> |
| 37 | #include <sstream> |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 38 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 39 | namespace ndn { |
| 40 | namespace chunks { |
| 41 | namespace tests { |
| 42 | |
| 43 | using namespace ndn::tests; |
| 44 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 45 | class ProducerFixture : public IdentityManagementFixture |
| 46 | { |
| 47 | protected: |
| 48 | ProducerFixture() |
| 49 | : face(io, {true, true}) |
| 50 | , prefix("/ndn/chunks/test") |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 51 | , version(1449227841747) |
| 52 | , keyLocatorName(m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName()) |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 53 | , 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 Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 58 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 59 | options.maxSegmentSize = 40; |
| 60 | options.isQuiet = true; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | protected: |
| 64 | boost::asio::io_service io; |
| 65 | util::DummyClientFace face; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 66 | Name prefix; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 67 | Producer::Options options; |
| 68 | uint64_t version; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 69 | Name keyLocatorName; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 70 | std::istringstream testString; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 73 | BOOST_AUTO_TEST_SUITE(Chunks) |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 74 | BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 75 | |
| 76 | BOOST_AUTO_TEST_CASE(InputData) |
| 77 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 78 | std::vector<std::string> testStrings{ |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 79 | "", |
| 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 Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 92 | 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 Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 95 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 96 | size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / options.maxSegmentSize); |
| 97 | if (testStrings[i].empty()) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 98 | expectedSize = 1; |
| 99 | |
| 100 | BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion) |
| 105 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 106 | Producer producer(prefix, face, m_keyChain, testString, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 107 | io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 108 | size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 109 | |
| 110 | // version request |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 111 | face.receive(*makeInterest(prefix, true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 112 | 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 Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 118 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 119 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 120 | 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 Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 127 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 128 | 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 Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 134 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 135 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 137 | } |
| 138 | |
| 139 | BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion) |
| 140 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 141 | Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 142 | io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 143 | size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 144 | |
| 145 | // version request |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 146 | face.receive(*makeInterest(prefix, true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 147 | 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 Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 154 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 155 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 157 | |
| 158 | // segment request |
| 159 | Name nameWithVersion(prefix); |
| 160 | size_t requestSegmentNo = 1; |
| 161 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 162 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 163 | 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 Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 170 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 171 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 173 | } |
| 174 | |
| 175 | BOOST_AUTO_TEST_CASE(RequestNotExistingSegment) |
| 176 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 177 | Producer producer(prefix, face, m_keyChain, testString, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 178 | io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 179 | size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / options.maxSegmentSize); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 180 | |
| 181 | // version request |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 182 | face.receive(*makeInterest(prefix, true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 183 | 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 Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 189 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 190 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 191 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 192 | |
| 193 | // segment request |
| 194 | Name nameWithVersion(prefix); |
| 195 | nameWithVersion.append(lastData.getName()[-2]); |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 196 | face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 197 | face.processEvents(); |
| 198 | |
| 199 | // no new data |
| 200 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 201 | } |
| 202 | |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 203 | BOOST_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 Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 231 | BOOST_AUTO_TEST_SUITE_END() // TestProducer |
| 232 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 233 | |
| 234 | } // namespace tests |
| 235 | } // namespace chunks |
| 236 | } // namespace ndn |