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