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 | /* |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2016-2020, 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 | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame^] | 118 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 119 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 120 | |
| 121 | // segment request |
| 122 | Name nameWithVersion(prefix); |
| 123 | nameWithVersion.append(lastData.getName()[-2]); |
| 124 | size_t requestSegmentNo = 1; |
| 125 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 126 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 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 Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame^] | 133 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 134 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion) |
| 138 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 139 | Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 140 | io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 141 | 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] | 142 | |
| 143 | // version request |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 144 | face.receive(*makeInterest(prefix, true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 145 | face.processEvents(); |
| 146 | |
| 147 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 148 | auto lastData = face.sentData.back(); |
| 149 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1); |
| 150 | BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version); |
| 151 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame^] | 152 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 153 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 154 | |
| 155 | // segment request |
| 156 | Name nameWithVersion(prefix); |
| 157 | size_t requestSegmentNo = 1; |
| 158 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 159 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 160 | face.processEvents(); |
| 161 | |
| 162 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
| 163 | lastData = face.sentData.back(); |
| 164 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1); |
| 165 | BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version); |
| 166 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo); |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame^] | 167 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 168 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_CASE(RequestNotExistingSegment) |
| 172 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 173 | Producer producer(prefix, face, m_keyChain, testString, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 174 | io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 175 | 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] | 176 | |
| 177 | // version request |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 178 | face.receive(*makeInterest(prefix, true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 179 | face.processEvents(); |
| 180 | |
| 181 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 182 | auto lastData = face.sentData.back(); |
| 183 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2); |
| 184 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame^] | 185 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 186 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 187 | |
| 188 | // segment request |
| 189 | Name nameWithVersion(prefix); |
| 190 | nameWithVersion.append(lastData.getName()[-2]); |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 191 | face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 192 | face.processEvents(); |
| 193 | |
| 194 | // no new data |
| 195 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 196 | } |
| 197 | |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 198 | BOOST_AUTO_TEST_CASE(RequestMetadata) |
| 199 | { |
| 200 | Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options); |
| 201 | io.poll(); |
| 202 | |
| 203 | // ask for metadata with a valid discovery interest |
| 204 | face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1))); |
| 205 | face.processEvents(); |
| 206 | |
| 207 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 208 | auto lastData = face.sentData.back(); |
| 209 | |
| 210 | // check the name of metadata packet |
| 211 | BOOST_CHECK(MetadataObject::isValidName(lastData.getName())); |
| 212 | |
| 213 | // make metadata object from metadata packet |
| 214 | MetadataObject mobject(lastData); |
| 215 | BOOST_CHECK_EQUAL(mobject.getVersionedName(), prefix); |
| 216 | |
| 217 | // ask for metadata with an invalid discovery interest |
| 218 | face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1)) |
| 219 | .setCanBePrefix(false)); |
| 220 | face.processEvents(); |
| 221 | |
| 222 | // we expect Nack in response to a discovery interest without CanBePrefix |
| 223 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); |
| 224 | } |
| 225 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 226 | BOOST_AUTO_TEST_SUITE_END() // TestProducer |
| 227 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 228 | |
| 229 | } // namespace tests |
| 230 | } // namespace chunks |
| 231 | } // namespace ndn |