Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 3 | * Copyright (c) 2016-2017, Regents of the University of California, |
| 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") |
| 50 | , testString(std::string( |
| 51 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget " |
| 52 | "dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, " |
| 53 | "nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, " |
| 54 | "sem. Nulla consequat massa Donec pede justo,")) |
| 55 | ,keyLocatorName(m_keyChain.createIdentity("/ProducerFixture").getDefaultKey().getName()) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | boost::asio::io_service io; |
| 61 | util::DummyClientFace face; |
| 62 | security::SigningInfo signingInfo; |
| 63 | Name prefix; |
| 64 | time::milliseconds freshnessPeriod = time::seconds(10); |
| 65 | size_t maxSegmentSize = 40; |
| 66 | std::istringstream testString; |
| 67 | uint64_t version = 1449227841747; |
| 68 | Name keyLocatorName; |
| 69 | }; |
| 70 | |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 71 | BOOST_AUTO_TEST_SUITE(Chunks) |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 72 | BOOST_FIXTURE_TEST_SUITE(TestProducer, ProducerFixture) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 73 | |
| 74 | BOOST_AUTO_TEST_CASE(InputData) |
| 75 | { |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 76 | std::vector<std::string> testStrings { |
| 77 | "", |
| 78 | |
| 79 | "a1b2c3%^&(#$&%^$$/><", |
| 80 | |
| 81 | "123456789123456789123456789123456789123456789123456789123456789" |
| 82 | "123456789123456789123456789123456789123456789123456789123456789", |
| 83 | |
| 84 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 85 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 86 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 87 | "consequat massa Donec pede justo," |
| 88 | }; |
| 89 | |
| 90 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 91 | std::istringstream str(testStrings[i]); |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 92 | Producer prod(prefix, face, m_keyChain, signingInfo, time::seconds(4), maxSegmentSize, false, |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 93 | false, str); |
| 94 | |
| 95 | size_t expectedSize = std::ceil(static_cast<double>(testStrings[i].size()) / maxSegmentSize); |
| 96 | if (testStrings[i].size() == 0) |
| 97 | expectedSize = 1; |
| 98 | |
| 99 | BOOST_CHECK_EQUAL(prod.m_store.size(), expectedSize); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_CASE(RequestSegmentUnspecifiedVersion) |
| 104 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 105 | Producer producer(prefix, face, m_keyChain, signingInfo, freshnessPeriod, maxSegmentSize, |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 106 | false, false, testString); |
| 107 | io.poll(); |
| 108 | |
| 109 | size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize); |
| 110 | |
| 111 | // version request |
| 112 | face.receive(*makeInterest(prefix)); |
| 113 | face.processEvents(); |
| 114 | |
| 115 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 116 | auto lastData = face.sentData.back(); |
| 117 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2); |
| 118 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
| 119 | BOOST_REQUIRE(!lastData.getFinalBlockId().empty()); |
| 120 | BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1); |
| 121 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 122 | |
| 123 | // segment request |
| 124 | Name nameWithVersion(prefix); |
| 125 | nameWithVersion.append(lastData.getName()[-2]); |
| 126 | size_t requestSegmentNo = 1; |
| 127 | |
| 128 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo))); |
| 129 | face.processEvents(); |
| 130 | |
| 131 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
| 132 | lastData = face.sentData.back(); |
| 133 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2); |
| 134 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo); |
| 135 | BOOST_REQUIRE(!lastData.getFinalBlockId().empty()); |
| 136 | BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1); |
| 137 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 138 | } |
| 139 | |
| 140 | BOOST_AUTO_TEST_CASE(RequestSegmentSpecifiedVersion) |
| 141 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 142 | Producer producer(prefix.appendVersion(version), face, m_keyChain, signingInfo, freshnessPeriod, |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 143 | maxSegmentSize, false, false, testString); |
| 144 | io.poll(); |
| 145 | |
| 146 | size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize); |
| 147 | |
| 148 | // version request |
| 149 | face.receive(*makeInterest(prefix)); |
| 150 | face.processEvents(); |
| 151 | |
| 152 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 153 | auto lastData = face.sentData.back(); |
| 154 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1); |
| 155 | BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version); |
| 156 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
| 157 | BOOST_REQUIRE(!lastData.getFinalBlockId().empty()); |
| 158 | BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1); |
| 159 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 160 | |
| 161 | // segment request |
| 162 | Name nameWithVersion(prefix); |
| 163 | size_t requestSegmentNo = 1; |
| 164 | |
| 165 | face.receive(*makeInterest(nameWithVersion.appendSegment(requestSegmentNo))); |
| 166 | face.processEvents(); |
| 167 | |
| 168 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 2); |
| 169 | lastData = face.sentData.back(); |
| 170 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 1); |
| 171 | BOOST_CHECK_EQUAL(lastData.getName()[-2].toVersion(), version); |
| 172 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), requestSegmentNo); |
| 173 | BOOST_REQUIRE(!lastData.getFinalBlockId().empty()); |
| 174 | BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1); |
| 175 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(RequestNotExistingSegment) |
| 179 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 180 | Producer producer(prefix, face, m_keyChain, signingInfo, freshnessPeriod, maxSegmentSize, |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 181 | false, false, testString); |
| 182 | io.poll(); |
| 183 | |
| 184 | size_t nSegments = std::ceil(static_cast<double>(testString.str().size()) / maxSegmentSize); |
| 185 | |
| 186 | // version request |
| 187 | face.receive(*makeInterest(prefix)); |
| 188 | face.processEvents(); |
| 189 | |
| 190 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 191 | auto lastData = face.sentData.back(); |
| 192 | BOOST_REQUIRE_EQUAL(lastData.getName().size(), prefix.size() + 2); |
| 193 | BOOST_CHECK_EQUAL(lastData.getName()[-1].toSegment(), 0); |
| 194 | BOOST_REQUIRE(!lastData.getFinalBlockId().empty()); |
| 195 | BOOST_CHECK_EQUAL(lastData.getFinalBlockId().toSegment(), nSegments - 1); |
| 196 | BOOST_CHECK_EQUAL(lastData.getSignature().getKeyLocator().getName(), keyLocatorName); |
| 197 | |
| 198 | // segment request |
| 199 | Name nameWithVersion(prefix); |
| 200 | nameWithVersion.append(lastData.getName()[-2]); |
| 201 | face.receive(*makeInterest(nameWithVersion.appendSegment(nSegments))); |
| 202 | face.processEvents(); |
| 203 | |
| 204 | // no new data |
| 205 | BOOST_REQUIRE_EQUAL(face.sentData.size(), 1); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_SUITE_END() // TestProducer |
| 209 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 210 | |
| 211 | } // namespace tests |
| 212 | } // namespace chunks |
| 213 | } // namespace ndn |