blob: 7d18886237b3f4630a2343b2a63785fa0c304fcc [file] [log] [blame]
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventob3570c62022-02-19 19:19:00 -05003 * Copyright (c) 2016-2022, Regents of the University of California,
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -07004 * 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 Pesavento66777622020-10-09 18:46:03 -040029#include "tests/io-fixture.hpp"
30#include "tests/key-chain-fixture.hpp"
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070031
32#include <ndn-cxx/metadata-object.hpp>
33#include <ndn-cxx/util/dummy-client-face.hpp>
34
Davide Pesaventob3570c62022-02-19 19:19:00 -050035namespace ndn::chunks::tests {
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070036
37using namespace ndn::tests;
38
Davide Pesavento66777622020-10-09 18:46:03 -040039class DiscoverVersionFixture : public IoFixture, public KeyChainFixture
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070040{
41public:
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070042 void
43 run(const Name& prefix)
44 {
Davide Pesavento97a33b22019-10-17 22:10:47 -040045 discover = make_unique<DiscoverVersion>(face, prefix, opt);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070046 discover->onDiscoverySuccess.connect([this] (const Name& versionedName) {
Davide Pesavento97a33b22019-10-17 22:10:47 -040047 isDiscoveryFinished = true;
Davide Pesavento296b3852019-10-20 16:00:16 -040048 discoveredName = versionedName;
49 if (!versionedName.empty() && versionedName[-1].isVersion())
50 discoveredVersion = versionedName[-1].toVersion();
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070051 });
Davide Pesavento97a33b22019-10-17 22:10:47 -040052 discover->onDiscoveryFailure.connect([this] (const std::string&) {
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070053 isDiscoveryFinished = true;
54 });
55
56 discover->run();
Davide Pesavento66777622020-10-09 18:46:03 -040057 advanceClocks(1_ns);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070058 }
59
60protected:
Davide Pesavento296b3852019-10-20 16:00:16 -040061 const Name name = "/ndn/chunks/test";
62 const uint64_t version = 1449227841747;
Davide Pesavento66777622020-10-09 18:46:03 -040063 util::DummyClientFace face{m_io};
Davide Pesavento97a33b22019-10-17 22:10:47 -040064 Options opt;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070065 unique_ptr<DiscoverVersion> discover;
Davide Pesavento296b3852019-10-20 16:00:16 -040066 optional<Name> discoveredName;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070067 optional<uint64_t> discoveredVersion;
Davide Pesavento97a33b22019-10-17 22:10:47 -040068 bool isDiscoveryFinished = false;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070069};
70
71BOOST_AUTO_TEST_SUITE(Chunks)
72BOOST_FIXTURE_TEST_SUITE(TestDiscoverVersion, DiscoverVersionFixture)
73
Davide Pesavento296b3852019-10-20 16:00:16 -040074BOOST_AUTO_TEST_CASE(Disabled)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070075{
Davide Pesavento296b3852019-10-20 16:00:16 -040076 opt.disableVersionDiscovery = true;
77 run(name);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070078
Davide Pesavento296b3852019-10-20 16:00:16 -040079 // no version discovery Interest is expressed
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070080 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
81
Davide Pesavento296b3852019-10-20 16:00:16 -040082 BOOST_CHECK_EQUAL(discoveredName.value(), name);
83 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
84}
85
86BOOST_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 Ghasemibb2d2802019-03-26 16:07:58 -070096 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
97}
98
Davide Pesavento296b3852019-10-20 16:00:16 -040099BOOST_AUTO_TEST_CASE(Success)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700100{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700101 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 Pesavento296b3852019-10-20 16:00:16 -0400109 // send back a metadata packet with a valid versioned name
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700110 MetadataObject mobject;
111 mobject.setVersionedName(Name(name).appendVersion(version));
112 face.receive(mobject.makeData(lastInterest.getName(), m_keyChain));
Davide Pesavento66777622020-10-09 18:46:03 -0400113 advanceClocks(1_ns);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700114
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700115 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
116}
117
118BOOST_AUTO_TEST_CASE(InvalidDiscoveredVersionedName)
119{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700120 run(name);
121
122 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
123
Davide Pesavento296b3852019-10-20 16:00:16 -0400124 // send back a metadata packet with an invalid versioned name
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700125 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 Pesavento296b3852019-10-20 16:00:16 -0400130 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
131 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
132 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700133}
134
135BOOST_AUTO_TEST_CASE(InvalidMetadataPacket)
136{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700137 run(name);
138
139 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
140
Davide Pesavento296b3852019-10-20 16:00:16 -0400141 // send back an invalid metadata packet
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700142 Data data(face.sentInterests.back().getName());
Junxiao Shi96192952019-05-22 15:45:12 +0000143 data.setFreshnessPeriod(1_s);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700144 data.setContentType(tlv::ContentType_Key);
145 face.receive(signData(data));
146
147 // finish discovery process without a resolved version number
Davide Pesavento296b3852019-10-20 16:00:16 -0400148 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
149 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
150 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700151}
152
Davide Pesavento296b3852019-10-20 16:00:16 -0400153BOOST_AUTO_TEST_CASE(MaxRetriesExceeded)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700154{
Davide Pesavento296b3852019-10-20 16:00:16 -0400155 opt.maxRetriesOnTimeoutOrNack = 3;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700156 run(name);
157
158 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
159
Davide Pesavento296b3852019-10-20 16:00:16 -0400160 // timeout or nack discovery Interests
161 for (int retries = 0; retries < opt.maxRetriesOnTimeoutOrNack * 2; ++retries) {
162 if (retries % 2 == 0) {
Davide Pesavento66777622020-10-09 18:46:03 -0400163 advanceClocks(opt.interestLifetime);
Davide Pesavento296b3852019-10-20 16:00:16 -0400164 }
165 else {
166 face.receive(makeNack(face.sentInterests.back(), lp::NackReason::DUPLICATE));
Davide Pesavento66777622020-10-09 18:46:03 -0400167 advanceClocks(1_ns);
Davide Pesavento296b3852019-10-20 16:00:16 -0400168 }
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700169
170 BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
171 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), retries + 2);
172 }
173
174 // timeout the last sent Interest
Davide Pesavento66777622020-10-09 18:46:03 -0400175 advanceClocks(opt.interestLifetime);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700176
177 // finish discovery process without a resolved version number
Davide Pesavento296b3852019-10-20 16:00:16 -0400178 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
179 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
180 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700181}
182
Davide Pesavento296b3852019-10-20 16:00:16 -0400183BOOST_AUTO_TEST_CASE(SuccessAfterNackAndTimeout)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700184{
Davide Pesavento296b3852019-10-20 16:00:16 -0400185 opt.maxRetriesOnTimeoutOrNack = 3;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700186 run(name);
187
188 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
189
Davide Pesavento296b3852019-10-20 16:00:16 -0400190 // timeout or nack discovery Interests
191 for (int retries = 0; retries < opt.maxRetriesOnTimeoutOrNack * 2; ++retries) {
192 if (retries % 2 == 0) {
Davide Pesavento66777622020-10-09 18:46:03 -0400193 advanceClocks(opt.interestLifetime);
Davide Pesavento296b3852019-10-20 16:00:16 -0400194 }
195 else {
196 face.receive(makeNack(face.sentInterests.back(), lp::NackReason::DUPLICATE));
Davide Pesavento66777622020-10-09 18:46:03 -0400197 advanceClocks(1_ns);
Davide Pesavento296b3852019-10-20 16:00:16 -0400198 }
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700199
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 Pesavento66777622020-10-09 18:46:03 -0400208 advanceClocks(1_ns);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700209
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700210 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
211}
212
213BOOST_AUTO_TEST_SUITE_END() // TestDiscoverVersion
214BOOST_AUTO_TEST_SUITE_END() // Chunks
215
Davide Pesaventob3570c62022-02-19 19:19:00 -0500216} // namespace ndn::chunks::tests