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 | { |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 47 | discover = make_unique<DiscoverVersion>(face, prefix, opt); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 48 | discover->onDiscoverySuccess.connect([this] (const Name& versionedName) { |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 49 | isDiscoveryFinished = true; |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 50 | discoveredName = versionedName; |
| 51 | if (!versionedName.empty() && versionedName[-1].isVersion()) |
| 52 | discoveredVersion = versionedName[-1].toVersion(); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 53 | }); |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 54 | discover->onDiscoveryFailure.connect([this] (const std::string&) { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 55 | isDiscoveryFinished = true; |
| 56 | }); |
| 57 | |
| 58 | discover->run(); |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 59 | advanceClocks(io, 1_ns); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | protected: |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 63 | const Name name = "/ndn/chunks/test"; |
| 64 | const uint64_t version = 1449227841747; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 65 | boost::asio::io_service io; |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 66 | util::DummyClientFace face{io}; |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 67 | Options opt; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 68 | unique_ptr<DiscoverVersion> discover; |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 69 | optional<Name> discoveredName; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 70 | optional<uint64_t> discoveredVersion; |
Davide Pesavento | 97a33b2 | 2019-10-17 22:10:47 -0400 | [diff] [blame] | 71 | bool isDiscoveryFinished = false; |
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 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 77 | BOOST_AUTO_TEST_CASE(Disabled) |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 78 | { |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 79 | opt.disableVersionDiscovery = true; |
| 80 | run(name); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 81 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 82 | // no version discovery Interest is expressed |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); |
| 84 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 85 | BOOST_CHECK_EQUAL(discoveredName.value(), name); |
| 86 | BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false); |
| 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_CASE(NameWithVersion) |
| 90 | { |
| 91 | // start with a name that already contains a version component |
| 92 | Name versionedName = Name(name).appendVersion(version); |
| 93 | run(versionedName); |
| 94 | |
| 95 | // no version discovery Interest is expressed |
| 96 | BOOST_CHECK_EQUAL(face.sentInterests.size(), 0); |
| 97 | |
| 98 | BOOST_CHECK_EQUAL(discoveredName.value(), versionedName); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(discoveredVersion.value(), version); |
| 100 | } |
| 101 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 102 | BOOST_AUTO_TEST_CASE(Success) |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 103 | { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 104 | run(name); |
| 105 | |
| 106 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 107 | |
| 108 | Interest discoveryInterest = MetadataObject::makeDiscoveryInterest(name); |
| 109 | auto lastInterest = face.sentInterests.back(); |
| 110 | BOOST_CHECK_EQUAL(lastInterest.getName(), discoveryInterest.getName()); |
| 111 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 112 | // send back a metadata packet with a valid versioned name |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 113 | MetadataObject mobject; |
| 114 | mobject.setVersionedName(Name(name).appendVersion(version)); |
| 115 | face.receive(mobject.makeData(lastInterest.getName(), m_keyChain)); |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 116 | advanceClocks(io, 1_ns); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 117 | |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(discoveredVersion.value(), version); |
| 119 | } |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(InvalidDiscoveredVersionedName) |
| 122 | { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 123 | run(name); |
| 124 | |
| 125 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 126 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 127 | // send back a metadata packet with an invalid versioned name |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 128 | MetadataObject mobject; |
| 129 | mobject.setVersionedName(name); |
| 130 | face.receive(mobject.makeData(face.sentInterests.back().getName(), m_keyChain)); |
| 131 | |
| 132 | // finish discovery process without a resolved version number |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 133 | BOOST_CHECK_EQUAL(isDiscoveryFinished, true); |
| 134 | BOOST_CHECK_EQUAL(discoveredName.has_value(), false); |
| 135 | BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | BOOST_AUTO_TEST_CASE(InvalidMetadataPacket) |
| 139 | { |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 140 | run(name); |
| 141 | |
| 142 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 143 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 144 | // send back an invalid metadata packet |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 145 | Data data(face.sentInterests.back().getName()); |
Junxiao Shi | 9619295 | 2019-05-22 15:45:12 +0000 | [diff] [blame] | 146 | data.setFreshnessPeriod(1_s); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 147 | data.setContentType(tlv::ContentType_Key); |
| 148 | face.receive(signData(data)); |
| 149 | |
| 150 | // finish discovery process without a resolved version number |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 151 | BOOST_CHECK_EQUAL(isDiscoveryFinished, true); |
| 152 | BOOST_CHECK_EQUAL(discoveredName.has_value(), false); |
| 153 | BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 156 | BOOST_AUTO_TEST_CASE(MaxRetriesExceeded) |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 157 | { |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 158 | opt.maxRetriesOnTimeoutOrNack = 3; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 159 | run(name); |
| 160 | |
| 161 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 162 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 163 | // timeout or nack discovery Interests |
| 164 | for (int retries = 0; retries < opt.maxRetriesOnTimeoutOrNack * 2; ++retries) { |
| 165 | if (retries % 2 == 0) { |
| 166 | advanceClocks(io, opt.interestLifetime); |
| 167 | } |
| 168 | else { |
| 169 | face.receive(makeNack(face.sentInterests.back(), lp::NackReason::DUPLICATE)); |
| 170 | advanceClocks(io, 1_ns); |
| 171 | } |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 172 | |
| 173 | BOOST_CHECK_EQUAL(isDiscoveryFinished, false); |
| 174 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), retries + 2); |
| 175 | } |
| 176 | |
| 177 | // timeout the last sent Interest |
| 178 | advanceClocks(io, opt.interestLifetime); |
| 179 | |
| 180 | // finish discovery process without a resolved version number |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 181 | BOOST_CHECK_EQUAL(isDiscoveryFinished, true); |
| 182 | BOOST_CHECK_EQUAL(discoveredName.has_value(), false); |
| 183 | BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 186 | BOOST_AUTO_TEST_CASE(SuccessAfterNackAndTimeout) |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 187 | { |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 188 | opt.maxRetriesOnTimeoutOrNack = 3; |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 189 | run(name); |
| 190 | |
| 191 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 192 | |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 193 | // timeout or nack discovery Interests |
| 194 | for (int retries = 0; retries < opt.maxRetriesOnTimeoutOrNack * 2; ++retries) { |
| 195 | if (retries % 2 == 0) { |
| 196 | advanceClocks(io, opt.interestLifetime); |
| 197 | } |
| 198 | else { |
| 199 | face.receive(makeNack(face.sentInterests.back(), lp::NackReason::DUPLICATE)); |
| 200 | advanceClocks(io, 1_ns); |
| 201 | } |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 202 | |
| 203 | BOOST_CHECK_EQUAL(isDiscoveryFinished, false); |
| 204 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), retries + 2); |
| 205 | } |
| 206 | |
| 207 | // satisfy the last Interest with a valid metadata packet |
| 208 | MetadataObject mobject; |
| 209 | mobject.setVersionedName(Name(name).appendVersion(version)); |
| 210 | face.receive(mobject.makeData(face.sentInterests.back().getName(), m_keyChain)); |
Davide Pesavento | 296b385 | 2019-10-20 16:00:16 -0400 | [diff] [blame^] | 211 | advanceClocks(io, 1_ns); |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 212 | |
Chavoosh Ghasemi | bb2d280 | 2019-03-26 16:07:58 -0700 | [diff] [blame] | 213 | BOOST_CHECK_EQUAL(discoveredVersion.value(), version); |
| 214 | } |
| 215 | |
| 216 | BOOST_AUTO_TEST_SUITE_END() // TestDiscoverVersion |
| 217 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 218 | |
| 219 | } // namespace tests |
| 220 | } // namespace chunks |
| 221 | } // namespace ndn |