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 | #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 | |
| 38 | class DiscoverVersionFixture : public ndn::tests::UnitTestTimeFixture, virtual protected Options |
| 39 | { |
| 40 | public: |
| 41 | DiscoverVersionFixture(const Options& options) |
| 42 | : Options(options) |
| 43 | , face(io) |
| 44 | , name("/ndn/chunks/test") |
| 45 | , isDiscoveryFinished(false) |
| 46 | , discoveredVersion(0) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | protected: |
| 51 | void setDiscover(unique_ptr<DiscoverVersion> disc) |
| 52 | { |
| 53 | discover = std::move(disc); |
| 54 | discover->onDiscoverySuccess.connect(bind(&DiscoverVersionFixture::onSuccess, this, _1)); |
| 55 | discover->onDiscoveryFailure.connect(bind(&DiscoverVersionFixture::onFailure, this, _1)); |
| 56 | } |
| 57 | |
| 58 | shared_ptr<Data> |
| 59 | makeDataWithVersion(uint64_t version) |
| 60 | { |
| 61 | auto data = make_shared<Data>(Name(name).appendVersion(version).appendSegment(0)); |
| 62 | data->setFinalBlockId(name::Component::fromSegment(0)); |
| 63 | return ndn::tests::signData(data); |
| 64 | } |
| 65 | |
| 66 | static Options |
| 67 | makeOptions() |
| 68 | { |
| 69 | Options options; |
| 70 | options.isVerbose = false; |
| 71 | options.interestLifetime = time::seconds(1); |
| 72 | options.maxRetriesOnTimeoutOrNack = 3; |
| 73 | return options; |
| 74 | } |
| 75 | |
| 76 | virtual void |
| 77 | onSuccess(const Data& data) |
| 78 | { |
| 79 | isDiscoveryFinished = true; |
| 80 | |
| 81 | if (data.getName()[name.size()].isVersion()) |
| 82 | discoveredVersion = data.getName()[name.size()].toVersion(); |
| 83 | } |
| 84 | |
| 85 | virtual void |
| 86 | onFailure(const std::string& reason) |
| 87 | { |
| 88 | isDiscoveryFinished = true; |
| 89 | } |
| 90 | |
| 91 | protected: |
| 92 | boost::asio::io_service io; |
| 93 | util::DummyClientFace face; |
| 94 | Name name; |
| 95 | unique_ptr<DiscoverVersion> discover; |
| 96 | bool isDiscoveryFinished; |
| 97 | uint64_t discoveredVersion; |
| 98 | }; |
| 99 | |
| 100 | } // namespace tests |
| 101 | } // namespace chunks |
| 102 | } // namespace ndn |
| 103 | |
| 104 | #endif // NDN_TOOLS_TESTS_CHUNKS_DISCOVER_VERSION_FIXTURE_HPP |