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