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