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 | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 3 | * Copyright (c) 2016-2018, 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 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 31 | #include <ndn-cxx/security/pib/identity.hpp> |
| 32 | #include <ndn-cxx/security/pib/key.hpp> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 33 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 34 | |
Davide Pesavento | c070270 | 2017-08-24 22:04:00 -0400 | [diff] [blame] | 35 | #include <cmath> |
| 36 | #include <sstream> |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 37 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 38 | namespace ndn { |
| 39 | namespace chunks { |
| 40 | namespace tests { |
| 41 | |
| 42 | using namespace ndn::tests; |
| 43 | |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 44 | class ProducerFixture : public IdentityManagementFixture |
| 45 | { |
| 46 | protected: |
| 47 | ProducerFixture() |
| 48 | : face(io, {true, true}) |
| 49 | , prefix("/ndn/chunks/test") |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 50 | , version(1449227841747) |
| 51 | , keyLocatorName(m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName()) |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 52 | , testString(std::string( |
| 53 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget " |
| 54 | "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, " |
| 55 | "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, " |
| 56 | "sem. Nulla consequat massa Donec pede justo,")) |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 57 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 58 | options.maxSegmentSize = 40; |
| 59 | options.isQuiet = true; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | protected: |
| 63 | boost::asio::io_service io; |
| 64 | util::DummyClientFace face; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 65 | Name prefix; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 66 | Producer::Options options; |
| 67 | uint64_t version; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 68 | Name keyLocatorName; |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 69 | std::istringstream testString; |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 72 | BOOST_AUTO_TEST_SUITE(Chunks) |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 73 | BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 74 | |
| 75 | BOOST_AUTO_TEST_CASE(InputData) |
| 76 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 77 | std::vector<std::string> testStrings{ |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 78 | "", |
| 79 | |
| 80 | "a1b2c3%^&(#$&%^$$/><", |
| 81 | |
| 82 | "123456789123456789123456789123456789123456789123456789123456789" |
| 83 | "123456789123456789123456789123456789123456789123456789123456789", |
| 84 | |
| 85 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 86 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 87 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 88 | "consequat massa Donec pede justo," |
| 89 | }; |
| 90 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 91 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 92 | std::istringstream input(testStrings[i]); |
| 93 | Producer prod(prefix, face, m_keyChain, input, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 94 | |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 95 | size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / options.maxSegmentSize); |
| 96 | if (testStrings[i].empty()) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 97 | expectedSize = 1; |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion) |
| 104 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 105 | Producer producer(prefix, face, m_keyChain, testString, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 106 | io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 107 | 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] | 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); |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 117 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 118 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 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); |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 133 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 134 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 135 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion) |
| 139 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 140 | Producer producer(prefix.appendVersion(version), face, m_keyChain, testString, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 141 | io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 142 | 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] | 143 | |
| 144 | // version request |
| 145 | face.receive(*makeInterest(prefix)); |
| 146 | face.processEvents(); |
| 147 | |
| 148 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 149 | auto lastData = face.sentData.back(); |
| 150 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1); |
| 151 | BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version); |
| 152 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 153 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 154 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 156 | |
| 157 | // segment request |
| 158 | Name nameWithVersion(prefix); |
| 159 | size_t requestSegmentNo = 1; |
| 160 | |
| 161 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo))); |
| 162 | face.processEvents(); |
| 163 | |
| 164 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
| 165 | lastData = face.sentData.back(); |
| 166 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1); |
| 167 | BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version); |
| 168 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo); |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 169 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 170 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 172 | } |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE(RequestNotExistingSegment) |
| 175 | { |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 176 | Producer producer(prefix, face, m_keyChain, testString, options); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 177 | io.poll(); |
Davide Pesavento | 6f76afc | 2017-09-19 18:43:51 -0400 | [diff] [blame] | 178 | 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] | 179 | |
| 180 | // version request |
| 181 | face.receive(*makeInterest(prefix)); |
| 182 | face.processEvents(); |
| 183 | |
| 184 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 185 | auto lastData = face.sentData.back(); |
| 186 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2); |
| 187 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 188 | BOOST_REQUIRE(lastData.getFinalBlock()); |
| 189 | BOOST_CHECK_EQUAL(lastData.getFinalBlock()->toSegment(), nSegments - 1); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 191 | |
| 192 | // segment request |
| 193 | Name nameWithVersion(prefix); |
| 194 | nameWithVersion.append(lastData.getName()[-2]); |
| 195 | face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments))); |
| 196 | face.processEvents(); |
| 197 | |
| 198 | // no new data |
| 199 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 200 | } |
| 201 | |
| 202 | BOOST_AUTO_TEST_SUITE_END() // TestProducer |
| 203 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 204 | |
| 205 | } // namespace tests |
| 206 | } // namespace chunks |
| 207 | } // namespace ndn |