Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2016-2019, 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 Chavoosh Ghasemi |
| 24 | */ |
| 25 | |
| 26 | #include "tools/chunks/catchunks/discover-version.hpp" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | #include "tests/identity-management-fixture.hpp" |
| 30 | |
| 31 | #include <ndn-cxx/metadata-object.hpp> |
| 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 33 | |
| 34 | namespace ndn { |
| 35 | namespace chunks { |
| 36 | namespace tests { |
| 37 | |
| 38 | using namespace ndn::tests; |
| 39 | |
| 40 | class DiscoverVersionFixture : public UnitTestTimeFixture, |
| 41 | public IdentityManagementFixture |
| 42 | { |
| 43 | public: |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 44 | void |
| 45 | run(const Name& prefix) |
| 46 | { |
| 47 | BOOST_REQUIRE(!prefix.empty()); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame^] | 48 | |
| 49 | discover = make_unique<DiscoverVersion>(face, prefix, opt); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 50 | discover->onDiscoverySuccess.connect([this] (const Name& versionedName) { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 51 | BOOST_REQUIRE(!versionedName.empty() && versionedName[-1].isVersion()); |
| 52 | discoveredVersion = versionedName[-1].toVersion(); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame^] | 53 | isDiscoveryFinished = true; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 54 | }); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame^] | 55 | discover->onDiscoveryFailure.connect([this] (const std::string&) { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 56 | isDiscoveryFinished = true; |
| 57 | }); |
| 58 | |
| 59 | discover->run(); |
| 60 | advanceClocks(io, time::nanoseconds(1)); |
| 61 | } |
| 62 | |
| 63 | protected: |
| 64 | boost::asio::io_service io; |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame^] | 65 | util::DummyClientFace face{io}; |
| 66 | Name name = "/ndn/chunks/test"; |
| 67 | Options opt; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 68 | unique_ptr<DiscoverVersion> discover; |
| 69 | optional<uint64_t> discoveredVersion; |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame^] | 70 | bool isDiscoveryFinished = false; |
| 71 | uint64_t version = 1449227841747; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | BOOST_AUTO_TEST_SUITE(Chunks) |
| 75 | BOOST_FIXTURE_TEST_SUITE(TestDiscoverVersion, DiscoverVersionFixture) |
| 76 | |
| 77 | BOOST_AUTO_TEST_CASE(VersionNumberIsProvided) |
| 78 | { |
| 79 | run(Name(name).appendVersion(version)); |
| 80 | |
| 81 | // no version discovery interest is expressed |
| 82 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); |
| 83 | |
| 84 | BOOST_CHECK_EQUAL(isDiscoveryFinished, true); |
| 85 | BOOST_CHECK_EQUAL(discoveredVersion.value(), version); |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE(DiscoverySuccess) |
| 89 | { |
| 90 | // express a discovery Interest to learn Data version |
| 91 | run(name); |
| 92 | |
| 93 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 94 | |
| 95 | Interest discoveryInterest = MetadataObject::makeDiscoveryInterest(name); |
| 96 | auto lastInterest = face.sentInterests.back(); |
| 97 | BOOST_CHECK_EQUAL(lastInterest.getName(), discoveryInterest.getName()); |
| 98 | |
| 99 | // Send back a metadata packet with a valid versioned name |
| 100 | MetadataObject mobject; |
| 101 | mobject.setVersionedName(Name(name).appendVersion(version)); |
| 102 | face.receive(mobject.makeData(lastInterest.getName(), m_keyChain)); |
| 103 | advanceClocks(io, time::nanoseconds(1)); |
| 104 | |
| 105 | BOOST_CHECK(isDiscoveryFinished); |
| 106 | BOOST_CHECK_EQUAL(discoveredVersion.value(), version); |
| 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_CASE(InvalidDiscoveredVersionedName) |
| 110 | { |
| 111 | // issue a discovery Interest to learn Data version |
| 112 | run(name); |
| 113 | |
| 114 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 115 | |
| 116 | // Send back a metadata packet with an invalid versioned name |
| 117 | MetadataObject mobject; |
| 118 | mobject.setVersionedName(name); |
| 119 | face.receive(mobject.makeData(face.sentInterests.back().getName(), m_keyChain)); |
| 120 | |
| 121 | // finish discovery process without a resolved version number |
| 122 | BOOST_CHECK(isDiscoveryFinished); |
| 123 | BOOST_CHECK(!discoveredVersion.has_value()); |
| 124 | } |
| 125 | |
| 126 | BOOST_AUTO_TEST_CASE(InvalidMetadataPacket) |
| 127 | { |
| 128 | // issue a discovery Interest to learn Data version |
| 129 | run(name); |
| 130 | |
| 131 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 132 | |
| 133 | // Send back an invalid metadata packet |
| 134 | Data data(face.sentInterests.back().getName()); |
Junxiao Shi | 9619295 | 2019-05-22 15:45:12 +0000 | [diff] [blame] | 135 | data.setFreshnessPeriod(1_s); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 136 | data.setContentType(tlv::ContentType_Key); |
| 137 | face.receive(signData(data)); |
| 138 | |
| 139 | // finish discovery process without a resolved version number |
| 140 | BOOST_CHECK(isDiscoveryFinished); |
| 141 | BOOST_CHECK(!discoveredVersion.has_value()); |
| 142 | } |
| 143 | |
| 144 | BOOST_AUTO_TEST_CASE(Timeout1) |
| 145 | { |
| 146 | // issue a discovery Interest to learn Data version |
| 147 | run(name); |
| 148 | |
| 149 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 150 | |
| 151 | // timeout discovery Interests |
| 152 | for (int retries = 0; retries < opt.maxRetriesOnTimeoutOrNack; ++retries) { |
| 153 | advanceClocks(io, opt.interestLifetime); |
| 154 | |
| 155 | BOOST_CHECK_EQUAL(isDiscoveryFinished, false); |
| 156 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), retries + 2); |
| 157 | } |
| 158 | |
| 159 | // timeout the last sent Interest |
| 160 | advanceClocks(io, opt.interestLifetime); |
| 161 | |
| 162 | // finish discovery process without a resolved version number |
| 163 | BOOST_CHECK(isDiscoveryFinished); |
| 164 | BOOST_CHECK(!discoveredVersion.has_value()); |
| 165 | } |
| 166 | |
| 167 | BOOST_AUTO_TEST_CASE(Timeout2) |
| 168 | { |
| 169 | // issue a discovery Interest to learn Data version |
| 170 | run(name); |
| 171 | |
| 172 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 173 | |
| 174 | // timeout discovery Interests |
| 175 | for (int retries = 0; retries < opt.maxRetriesOnTimeoutOrNack; ++retries) { |
| 176 | advanceClocks(io, opt.interestLifetime); |
| 177 | |
| 178 | BOOST_CHECK_EQUAL(isDiscoveryFinished, false); |
| 179 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), retries + 2); |
| 180 | } |
| 181 | |
| 182 | // satisfy the last Interest with a valid metadata packet |
| 183 | MetadataObject mobject; |
| 184 | mobject.setVersionedName(Name(name).appendVersion(version)); |
| 185 | face.receive(mobject.makeData(face.sentInterests.back().getName(), m_keyChain)); |
| 186 | advanceClocks(io, time::nanoseconds(1)); |
| 187 | |
| 188 | BOOST_CHECK(isDiscoveryFinished); |
| 189 | BOOST_CHECK_EQUAL(discoveredVersion.value(), version); |
| 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_SUITE_END() // TestDiscoverVersion |
| 193 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 194 | |
| 195 | } // namespace tests |
| 196 | } // namespace chunks |
| 197 | } // namespace ndn |