Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 2 | /* |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 3 | * Copyright (c) 2016-2019, Regents of the University of California, |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [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 | #ifndef NDN_TOOLS_TESTS_CHUNKS_DISCOVER_VERSION_FIXTURE_HPP |
| 27 | #define NDN_TOOLS_TESTS_CHUNKS_DISCOVER_VERSION_FIXTURE_HPP |
| 28 | |
| 29 | #include "tools/chunks/catchunks/discover-version.hpp" |
| 30 | |
| 31 | #include "tests/test-common.hpp" |
| 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace chunks { |
| 36 | namespace tests { |
| 37 | |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 38 | using namespace ndn::tests; |
| 39 | |
| 40 | class DiscoverVersionFixture : public UnitTestTimeFixture, virtual protected Options |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | DiscoverVersionFixture(const Options& options) |
| 44 | : Options(options) |
| 45 | , face(io) |
| 46 | , name("/ndn/chunks/test") |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 47 | , discoveredVersion(0) |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 48 | , isDiscoveryFinished(false) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | virtual |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 53 | ~DiscoverVersionFixture() = default; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 54 | |
| 55 | protected: |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 56 | void |
| 57 | setDiscover(unique_ptr<DiscoverVersion> disc) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 58 | { |
| 59 | discover = std::move(disc); |
| 60 | discover->onDiscoverySuccess.connect(bind(&DiscoverVersionFixture::onSuccess, this, _1)); |
| 61 | discover->onDiscoveryFailure.connect(bind(&DiscoverVersionFixture::onFailure, this, _1)); |
| 62 | } |
| 63 | |
| 64 | shared_ptr<Data> |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 65 | makeDataWithVersion(uint64_t version) const |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 66 | { |
| 67 | auto data = make_shared<Data>(Name(name).appendVersion(version).appendSegment(0)); |
Davide Pesavento | 969cd5a | 2018-04-20 16:27:47 -0400 | [diff] [blame] | 68 | data->setFinalBlock(name::Component::fromSegment(0)); |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 69 | return signData(data); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static Options |
| 73 | makeOptions() |
| 74 | { |
| 75 | Options options; |
| 76 | options.isVerbose = false; |
| 77 | options.interestLifetime = time::seconds(1); |
| 78 | options.maxRetriesOnTimeoutOrNack = 3; |
| 79 | return options; |
| 80 | } |
| 81 | |
| 82 | virtual void |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 83 | onSuccess(const Name& versionedName) |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 84 | { |
| 85 | isDiscoveryFinished = true; |
| 86 | |
Chavoosh Ghasemi | 5cb6701 | 2019-02-15 09:56:57 -0800 | [diff] [blame] | 87 | BOOST_REQUIRE(!versionedName.empty() && versionedName[-1].isVersion()); |
| 88 | discoveredVersion = versionedName[-1].toVersion(); |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | virtual void |
| 92 | onFailure(const std::string& reason) |
| 93 | { |
| 94 | isDiscoveryFinished = true; |
| 95 | } |
| 96 | |
| 97 | protected: |
| 98 | boost::asio::io_service io; |
| 99 | util::DummyClientFace face; |
| 100 | Name name; |
| 101 | unique_ptr<DiscoverVersion> discover; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 102 | uint64_t discoveredVersion; |
Weiwei Liu | e476501 | 2016-06-01 00:10:29 -0700 | [diff] [blame] | 103 | bool isDiscoveryFinished; |
Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace tests |
| 107 | } // namespace chunks |
| 108 | } // namespace ndn |
| 109 | |
| 110 | #endif // NDN_TOOLS_TESTS_CHUNKS_DISCOVER_VERSION_FIXTURE_HPP |