Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017, Regents of the University of California. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 5 | * This file is part of ChronoShare, a decentralized file sharing application over NDN. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 7 | * ChronoShare is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 11 | * ChronoShare is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 14 | * |
Alexander Afanasyev | fa2f662 | 2016-12-25 12:28:00 -0800 | [diff] [blame] | 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ChronoShare, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ChronoShare authors and contributors. |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 19 | */ |
Alexander Afanasyev | f4cde4e | 2016-12-25 13:42:57 -0800 | [diff] [blame] | 20 | #include "fetch-manager.hpp" |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 21 | |
| 22 | #include "test-common.hpp" |
| 23 | |
| 24 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 25 | |
| 26 | #include <boost/lexical_cast.hpp> |
| 27 | #include <boost/algorithm/string/join.hpp> |
| 28 | #include <boost/range/adaptor/transformed.hpp> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace chronoshare { |
| 32 | namespace tests { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 1cf5c43 | 2017-01-13 23:22:15 -0800 | [diff] [blame] | 34 | _LOG_INIT(Test.FetchManager); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(TestFetchManager) |
| 37 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 38 | class FetcherTestData : public IdentityManagementTimeFixture |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 39 | { |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 40 | public: |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 41 | FetcherTestData() |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 42 | : face(m_io, m_keyChain, {true, true}) |
| 43 | , m_done(false) |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 44 | , m_failed(false) |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 45 | , m_hasMissing(true) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
| 49 | void |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 50 | onData(const ndn::Name& deviceName, const ndn::Name& basename, uint64_t seqno, |
| 51 | ndn::shared_ptr<ndn::Data> data) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 52 | { |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 53 | _LOG_TRACE("onData: " << seqno << data->getName()); |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 54 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 55 | recvData.insert(seqno); |
| 56 | differentNames.insert(basename); |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 57 | Name name = basename; |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 58 | name.appendNumber(seqno); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 59 | segmentNames.insert(name); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 60 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 61 | Block block = data->getContent(); |
| 62 | std::string recvNo(reinterpret_cast<const char*>(block.value()), block.value_size()); |
| 63 | recvContent.insert(boost::lexical_cast<uint32_t>(recvNo)); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 67 | finish(const ndn::Name& deviceName, const ndn::Name& baseName) |
Zhenkai Zhu | 3d1beca | 2013-01-23 14:55:32 -0800 | [diff] [blame] | 68 | { |
| 69 | } |
| 70 | |
| 71 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 72 | onComplete(Fetcher& fetcher) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 73 | { |
| 74 | m_done = true; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 78 | onFail(Fetcher& fetcher) |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 79 | { |
| 80 | m_failed = true; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 81 | } |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 82 | |
| 83 | public: |
| 84 | util::DummyClientFace face; |
| 85 | std::set<uint32_t> recvData; |
| 86 | std::set<uint32_t> recvContent; |
| 87 | |
| 88 | std::set<Name> differentNames; |
| 89 | std::set<Name> segmentNames; |
| 90 | |
| 91 | bool m_done; |
| 92 | bool m_failed; |
| 93 | |
| 94 | bool m_hasMissing; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 95 | }; |
| 96 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 97 | BOOST_FIXTURE_TEST_CASE(TestFetcher, FetcherTestData) |
Yingdi Yu | f0b3de3 | 2013-07-11 12:59:00 -0700 | [diff] [blame] | 98 | { |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 99 | Name baseName("/fetchtest"); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 100 | Name deviceName("/device"); |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 101 | |
| 102 | // publish seqnos: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, <gap 5>, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, <gap 1>, 26 |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 103 | // this will allow us to test our pipeline of 6 |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 104 | std::set<uint32_t> seqs = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, /*<gap 10-14>,*/ |
| 105 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /*<gap 25>,*/ 26}; |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 106 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 107 | face.onSendInterest.connect([&, this] (const Interest& interest) { |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 108 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 109 | uint32_t requestedSeqNo = interest.getName().at(-1).toNumber(); |
| 110 | if (seqs.count(requestedSeqNo) > 0) { |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 111 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 112 | auto data = make_shared<Data>(); |
| 113 | Name name(baseName); |
| 114 | name.appendNumber(requestedSeqNo); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 115 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 116 | data->setName(name); |
| 117 | data->setFreshnessPeriod(time::seconds(300)); |
| 118 | std::string content = to_string(requestedSeqNo); |
| 119 | data->setContent(reinterpret_cast<const uint8_t*>(content.data()), content.size()); |
| 120 | m_keyChain.sign(*data); |
| 121 | m_io.post([data, this] { face.receive(*data); }); |
| 122 | } |
| 123 | }); |
| 124 | |
| 125 | Fetcher fetcher(face, bind(&FetcherTestData::onData, this, _1, _2, _3, _4), |
| 126 | bind(&FetcherTestData::finish, this, _1, _2), |
| 127 | bind(&FetcherTestData::onComplete, this, _1), |
| 128 | bind(&FetcherTestData::onFail, this, _1), deviceName, Name("/fetchtest"), 0, 26, |
| 129 | time::seconds(5), Name()); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 130 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(fetcher.IsActive(), false); |
| 132 | fetcher.RestartPipeline(); |
| 133 | BOOST_CHECK_EQUAL(fetcher.IsActive(), true); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 134 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 135 | this->advanceClocks(time::milliseconds(50), 1000); |
| 136 | BOOST_CHECK_EQUAL(m_failed, true); |
| 137 | BOOST_CHECK_EQUAL(differentNames.size(), 1); |
| 138 | BOOST_CHECK_EQUAL(segmentNames.size(), 20); |
| 139 | BOOST_CHECK_EQUAL(recvData.size(), 20); |
| 140 | BOOST_CHECK_EQUAL(recvContent.size(), 20); |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 141 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL("0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24", |
| 143 | join(recvData | boost::adaptors::transformed([] (int i) { return std::to_string(i); }), ", ")); |
| 144 | BOOST_CHECK_EQUAL_COLLECTIONS(recvData.begin(), recvData.end(), recvContent.begin(), recvContent.end()); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 146 | BOOST_CHECK_EQUAL(fetcher.IsActive(), false); |
| 147 | fetcher.RestartPipeline(); |
| 148 | BOOST_CHECK_EQUAL(fetcher.IsActive(), true); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 149 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 150 | this->advanceClocks(time::milliseconds(100), 100); |
| 151 | BOOST_CHECK_EQUAL(m_done, false); |
| 152 | BOOST_CHECK_EQUAL(m_failed, true); |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 153 | BOOST_CHECK_EQUAL(fetcher.IsActive(), false); |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 154 | |
| 155 | std::set<uint32_t> missing = {10, 11, 12, 13, 14, 25}; |
| 156 | seqs.insert(missing.begin(), missing.end()); |
| 157 | |
| 158 | m_failed = false; |
Alexander Afanasyev | eda3b7a | 2016-12-25 11:26:40 -0800 | [diff] [blame] | 159 | fetcher.RestartPipeline(); |
| 160 | BOOST_CHECK_EQUAL(fetcher.IsActive(), true); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 161 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 162 | this->advanceClocks(time::milliseconds(100), 100); |
| 163 | BOOST_CHECK_EQUAL(m_done, true); |
| 164 | BOOST_CHECK_EQUAL(m_failed, false); |
| 165 | BOOST_CHECK_EQUAL(segmentNames.size(), 27); |
| 166 | BOOST_CHECK_EQUAL(recvData.size(), 27); |
| 167 | BOOST_CHECK_EQUAL(recvContent.size(), 27); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 168 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 169 | BOOST_CHECK_EQUAL("0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26", |
| 170 | join(recvData | boost::adaptors::transformed([] (int i) { return std::to_string(i); }), ", ")); |
| 171 | BOOST_CHECK_EQUAL_COLLECTIONS(recvData.begin(), recvData.end(), recvContent.begin(), recvContent.end()); |
Alexander Afanasyev | 650ba28 | 2013-01-20 20:14:06 -0800 | [diff] [blame] | 172 | |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 173 | // TODO add tests that other callbacks got called |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Alexander Afanasyev | 21a166e | 2013-01-20 16:04:41 -0800 | [diff] [blame] | 176 | BOOST_AUTO_TEST_SUITE_END() |
Yukai Tu | 35963e0 | 2016-10-24 13:48:01 -0700 | [diff] [blame] | 177 | |
| 178 | } // namespace tests |
| 179 | } // namespace chronoshare |
| 180 | } // namespace ndn |