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