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 | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2016-2022, 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" |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 29 | #include "tests/io-fixture.hpp" |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 30 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 31 | |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 32 | #include <ndn-cxx/metadata-object.hpp> |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 33 | #include <ndn-cxx/security/pib/identity.hpp> |
| 34 | #include <ndn-cxx/security/pib/key.hpp> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 35 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 36 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 37 | #include <cmath> |
| 38 | #include <sstream> |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 39 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 40 | namespace ndn::chunks::tests { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 41 | |
| 42 | using namespace ndn::tests; |
| 43 | |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 44 | class ProducerFixture : public IoFixture, public KeyChainFixture |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 45 | { |
| 46 | protected: |
| 47 | ProducerFixture() |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 48 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 49 | options.maxSegmentSize = 40; |
| 50 | options.isQuiet = true; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | protected: |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 54 | util::DummyClientFace face{m_io, {true, true}}; |
| 55 | Name prefix = "/ndn/chunks/test"; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 56 | Producer::Options options; |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 57 | uint64_t version = 1449227841747; |
| 58 | Name keyLocatorName = m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName(); |
| 59 | std::istringstream testString{ |
| 60 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget " |
| 61 | "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, " |
| 62 | "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, " |
| 63 | "sem. Nulla consequat massa Donec pede justo,"s}; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 66 | BOOST_AUTO_TEST_SUITE(Chunks) |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 67 | BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 68 | |
| 69 | BOOST_AUTO_TEST_CASE(InputData) |
| 70 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 71 | std::vector<std::string> testStrings{ |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 72 | "", |
| 73 | |
| 74 | "a1b2c3%^&(#$&%^$$/><", |
| 75 | |
| 76 | "123456789123456789123456789123456789123456789123456789123456789" |
| 77 | "123456789123456789123456789123456789123456789123456789123456789", |
| 78 | |
| 79 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 80 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 81 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 82 | "consequat massa Donec pede justo," |
| 83 | }; |
| 84 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 85 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 86 | std::istringstream input(testStrings[i]); |
| 87 | Producer prod(prefix, face, m_keyChain, input, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 88 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 89 | size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / options.maxSegmentSize); |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 90 | if (testStrings[i].empty()) { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 91 | expectedSize = 1; |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 92 | } |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 93 | |
| 94 | BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion) |
| 99 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 100 | Producer producer(prefix, face, m_keyChain, testString, options); |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 101 | m_io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 102 | 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] | 103 | |
| 104 | // version request |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 105 | face.receive(*makeInterest(prefix, true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 106 | face.processEvents(); |
| 107 | |
| 108 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 109 | auto lastData = face.sentData.back(); |
| 110 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2); |
| 111 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame] | 112 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 113 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 114 | |
| 115 | // segment request |
| 116 | Name nameWithVersion(prefix); |
| 117 | nameWithVersion.append(lastData.getName()[-2]); |
| 118 | size_t requestSegmentNo = 1; |
| 119 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 120 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 121 | face.processEvents(); |
| 122 | |
| 123 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
| 124 | lastData = face.sentData.back(); |
| 125 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2); |
| 126 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo); |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 128 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion) |
| 132 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 133 | Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options); |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 134 | m_io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 135 | 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] | 136 | |
| 137 | // version request |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 138 | face.receive(*makeInterest(prefix, true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 139 | face.processEvents(); |
| 140 | |
| 141 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 142 | auto lastData = face.sentData.back(); |
| 143 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1); |
| 144 | BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version); |
| 145 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 147 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 148 | |
| 149 | // segment request |
| 150 | Name nameWithVersion(prefix); |
| 151 | size_t requestSegmentNo = 1; |
| 152 | |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 153 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 154 | face.processEvents(); |
| 155 | |
| 156 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
| 157 | lastData = face.sentData.back(); |
| 158 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1); |
| 159 | BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version); |
| 160 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo); |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame] | 161 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 162 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_CASE(RequestNotExistingSegment) |
| 166 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 167 | Producer producer(prefix, face, m_keyChain, testString, options); |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 168 | m_io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 169 | 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] | 170 | |
| 171 | // version request |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 172 | face.receive(*makeInterest(prefix, true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 173 | face.processEvents(); |
| 174 | |
| 175 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 176 | auto lastData = face.sentData.back(); |
| 177 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2); |
| 178 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
Davide Pesavento | 3527558 | 2020-07-27 18:02:03 -0400 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(lastData.getFinalBlock().value().toSegment(), nSegments - 1); |
| 180 | BOOST_CHECK_EQUAL(lastData.getKeyLocator().value().getName(), keyLocatorName); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 181 | |
| 182 | // segment request |
| 183 | Name nameWithVersion(prefix); |
| 184 | nameWithVersion.append(lastData.getName()[-2]); |
Junxiao Shi | 20d5a0b | 2018-08-14 09:26:11 -0600 | [diff] [blame] | 185 | face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments), true)); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 186 | face.processEvents(); |
| 187 | |
| 188 | // no new data |
| 189 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 190 | } |
| 191 | |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 192 | BOOST_AUTO_TEST_CASE(RequestMetadata) |
| 193 | { |
| 194 | Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options); |
Davide Pesavento | a0e6b60 | 2021-01-21 19:47:04 -0500 | [diff] [blame] | 195 | m_io.poll(); |
Chavoosh Ghasemi | 79991a0 | 2019-01-28 21:22:31 -0800 | [diff] [blame] | 196 | |
| 197 | // ask for metadata with a valid discovery interest |
| 198 | face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1))); |
| 199 | face.processEvents(); |
| 200 | |
| 201 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 202 | auto lastData = face.sentData.back(); |
| 203 | |
| 204 | // check the name of metadata packet |
| 205 | BOOST_CHECK(MetadataObject::isValidName(lastData.getName())); |
| 206 | |
| 207 | // make metadata object from metadata packet |
| 208 | MetadataObject mobject(lastData); |
| 209 | BOOST_CHECK_EQUAL(mobject.getVersionedName(), prefix); |
| 210 | |
| 211 | // ask for metadata with an invalid discovery interest |
| 212 | face.receive(MetadataObject::makeDiscoveryInterest(Name(prefix).getPrefix(-1)) |
| 213 | .setCanBePrefix(false)); |
| 214 | face.processEvents(); |
| 215 | |
| 216 | // we expect Nack in response to a discovery interest without CanBePrefix |
| 217 | BOOST_CHECK_EQUAL(face.sentNacks.size(), 1); |
| 218 | } |
| 219 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 220 | BOOST_AUTO_TEST_SUITE_END() // TestProducer |
| 221 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 222 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 223 | } // namespace ndn::chunks::tests |