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/catchunks/consumer.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 <boost/test/output_test_stream.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace chunks { |
| 36 | namespace tests { |
| 37 | |
| 38 | using namespace ndn::tests; |
| 39 | using boost::test_tools::output_test_stream; |
| 40 | |
| 41 | BOOST_AUTO_TEST_SUITE(Chunks) |
| 42 | BOOST_AUTO_TEST_SUITE(TestConsumer) |
| 43 | |
| 44 | BOOST_AUTO_TEST_CASE(OutputDataSequential) |
| 45 | { |
| 46 | // Test sequential segments in the right order |
| 47 | // Segment order: 0 1 2 |
| 48 | |
| 49 | std::string name("/ndn/chunks/test"); |
| 50 | |
| 51 | std::vector<std::string> testStrings { |
| 52 | "", |
| 53 | |
| 54 | "a1b2c3%^&(#$&%^$$/><", |
| 55 | |
| 56 | "123456789123456789123456789123456789123456789123456789123456789" |
| 57 | "123456789123456789123456789123456789123456789123456789123456789", |
| 58 | |
| 59 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 60 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 61 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 62 | "consequat massa Donec pede justo," |
| 63 | }; |
| 64 | |
| 65 | util::DummyClientFace face; |
| 66 | ValidatorNull validator; |
| 67 | output_test_stream output(""); |
| 68 | Consumer cons(face, validator, false, output); |
| 69 | |
| 70 | auto interest = makeInterest(name); |
| 71 | |
| 72 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 73 | output.flush(); |
| 74 | |
| 75 | auto data = makeData(Name(name).appendVersion(1).appendSegment(i)); |
| 76 | data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()), |
| 77 | testStrings[i].size()); |
| 78 | |
| 79 | cons.m_bufferedData[i] = data; |
| 80 | cons.writeInOrderData(); |
| 81 | |
| 82 | BOOST_CHECK(output.is_equal(testStrings[i])); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_CASE(OutputDataUnordered) |
| 87 | { |
| 88 | // Test unordered segments |
| 89 | // Segment order: 1 0 2 |
| 90 | |
| 91 | std::string name("/ndn/chunks/test"); |
| 92 | |
| 93 | std::vector<std::string> testStrings { |
| 94 | "a1b2c3%^&(#$&%^$$/><", |
| 95 | |
| 96 | "123456789123456789123456789123456789123456789123456789123456789" |
| 97 | "123456789123456789123456789123456789123456789123456789123456789", |
| 98 | |
| 99 | "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. " |
| 100 | "Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur " |
| 101 | "ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla " |
| 102 | "consequat massa Donec pede justo," |
| 103 | };; |
| 104 | |
| 105 | util::DummyClientFace face; |
| 106 | ValidatorNull validator; |
| 107 | output_test_stream output(""); |
| 108 | Consumer cons(face, validator, false, output); |
| 109 | |
| 110 | auto interest = makeInterest(name); |
| 111 | std::vector<shared_ptr<Data>> dataStore; |
| 112 | |
| 113 | for (size_t i = 0; i < testStrings.size(); ++i) { |
| 114 | auto data = makeData(Name(name).appendVersion(1).appendSegment(i)); |
| 115 | data->setContent(reinterpret_cast<const uint8_t*>(testStrings[i].data()), |
| 116 | testStrings[i].size()); |
| 117 | |
| 118 | dataStore.push_back(data); |
| 119 | } |
| 120 | |
| 121 | output.flush(); |
| 122 | cons.m_bufferedData[1] = dataStore[1]; |
| 123 | cons.writeInOrderData(); |
| 124 | BOOST_CHECK(output.is_equal("")); |
| 125 | |
| 126 | output.flush(); |
| 127 | cons.m_bufferedData[0] = dataStore[0]; |
| 128 | cons.writeInOrderData(); |
| 129 | BOOST_CHECK(output.is_equal(testStrings[0] + testStrings[1])); |
| 130 | |
| 131 | output.flush(); |
| 132 | cons.m_bufferedData[2] = dataStore[2]; |
| 133 | cons.writeInOrderData(); |
| 134 | BOOST_CHECK(output.is_equal(testStrings[2])); |
| 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_SUITE_END() // TestConsumer |
| 138 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 139 | |
| 140 | } // namespace tests |
| 141 | } // namespace chunks |
| 142 | } // namespace ndn |