blob: 79315cc71ba002047c3147dda47fbdcfcd356e72 [file] [log] [blame]
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento66777622020-10-09 18:46:03 -04003 * Copyright (c) 2016-2020, 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
35namespace ndn {
36namespace chunks {
37namespace tests {
38
39using namespace ndn::tests;
40
Davide Pesavento66777622020-10-09 18:46:03 -040041class DiscoverVersionFixture : public IoFixture, public KeyChainFixture
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070042{
43public:
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070044 void
45 run(const Name& prefix)
46 {
Davide Pesavento97a33b22019-10-17 22:10:47 -040047 discover = make_unique<DiscoverVersion>(face, prefix, opt);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070048 discover->onDiscoverySuccess.connect([this] (const Name& versionedName) {
Davide Pesavento97a33b22019-10-17 22:10:47 -040049 isDiscoveryFinished = true;
Davide Pesavento296b3852019-10-20 16:00:16 -040050 discoveredName = versionedName;
51 if (!versionedName.empty() && versionedName[-1].isVersion())
52 discoveredVersion = versionedName[-1].toVersion();
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070053 });
Davide Pesavento97a33b22019-10-17 22:10:47 -040054 discover->onDiscoveryFailure.connect([this] (const std::string&) {
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070055 isDiscoveryFinished = true;
56 });
57
58 discover->run();
Davide Pesavento66777622020-10-09 18:46:03 -040059 advanceClocks(1_ns);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070060 }
61
62protected:
Davide Pesavento296b3852019-10-20 16:00:16 -040063 const Name name = "/ndn/chunks/test";
64 const uint64_t version = 1449227841747;
Davide Pesavento66777622020-10-09 18:46:03 -040065 util::DummyClientFace face{m_io};
Davide Pesavento97a33b22019-10-17 22:10:47 -040066 Options opt;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070067 unique_ptr<DiscoverVersion> discover;
Davide Pesavento296b3852019-10-20 16:00:16 -040068 optional<Name> discoveredName;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070069 optional<uint64_t> discoveredVersion;
Davide Pesavento97a33b22019-10-17 22:10:47 -040070 bool isDiscoveryFinished = false;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070071};
72
73BOOST_AUTO_TEST_SUITE(Chunks)
74BOOST_FIXTURE_TEST_SUITE(TestDiscoverVersion, DiscoverVersionFixture)
75
Davide Pesavento296b3852019-10-20 16:00:16 -040076BOOST_AUTO_TEST_CASE(Disabled)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070077{
Davide Pesavento296b3852019-10-20 16:00:16 -040078 opt.disableVersionDiscovery = true;
79 run(name);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070080
Davide Pesavento296b3852019-10-20 16:00:16 -040081 // no version discovery Interest is expressed
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070082 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
83
Davide Pesavento296b3852019-10-20 16:00:16 -040084 BOOST_CHECK_EQUAL(discoveredName.value(), name);
85 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
86}
87
88BOOST_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 Ghasemibb2d2802019-03-26 16:07:58 -070098 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
99}
100
Davide Pesavento296b3852019-10-20 16:00:16 -0400101BOOST_AUTO_TEST_CASE(Success)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700102{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700103 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 Pesavento296b3852019-10-20 16:00:16 -0400111 // send back a metadata packet with a valid versioned name
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700112 MetadataObject mobject;
113 mobject.setVersionedName(Name(name).appendVersion(version));
114 face.receive(mobject.makeData(lastInterest.getName(), m_keyChain));
Davide Pesavento66777622020-10-09 18:46:03 -0400115 advanceClocks(1_ns);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700116
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700117 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
118}
119
120BOOST_AUTO_TEST_CASE(InvalidDiscoveredVersionedName)
121{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700122 run(name);
123
124 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
125
Davide Pesavento296b3852019-10-20 16:00:16 -0400126 // send back a metadata packet with an invalid versioned name
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700127 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 Pesavento296b3852019-10-20 16:00:16 -0400132 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
133 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
134 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700135}
136
137BOOST_AUTO_TEST_CASE(InvalidMetadataPacket)
138{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700139 run(name);
140
141 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
142
Davide Pesavento296b3852019-10-20 16:00:16 -0400143 // send back an invalid metadata packet
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700144 Data data(face.sentInterests.back().getName());
Junxiao Shi96192952019-05-22 15:45:12 +0000145 data.setFreshnessPeriod(1_s);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700146 data.setContentType(tlv::ContentType_Key);
147 face.receive(signData(data));
148
149 // finish discovery process without a resolved version number
Davide Pesavento296b3852019-10-20 16:00:16 -0400150 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
151 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
152 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700153}
154
Davide Pesavento296b3852019-10-20 16:00:16 -0400155BOOST_AUTO_TEST_CASE(MaxRetriesExceeded)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700156{
Davide Pesavento296b3852019-10-20 16:00:16 -0400157 opt.maxRetriesOnTimeoutOrNack = 3;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700158 run(name);
159
160 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
161
Davide Pesavento296b3852019-10-20 16:00:16 -0400162 // timeout or nack discovery Interests
163 for (int retries = 0; retries < opt.maxRetriesOnTimeoutOrNack * 2; ++retries) {
164 if (retries % 2 == 0) {
Davide Pesavento66777622020-10-09 18:46:03 -0400165 advanceClocks(opt.interestLifetime);
Davide Pesavento296b3852019-10-20 16:00:16 -0400166 }
167 else {
168 face.receive(makeNack(face.sentInterests.back(), lp::NackReason::DUPLICATE));
Davide Pesavento66777622020-10-09 18:46:03 -0400169 advanceClocks(1_ns);
Davide Pesavento296b3852019-10-20 16:00:16 -0400170 }
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700171
172 BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
173 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), retries + 2);
174 }
175
176 // timeout the last sent Interest
Davide Pesavento66777622020-10-09 18:46:03 -0400177 advanceClocks(opt.interestLifetime);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700178
179 // finish discovery process without a resolved version number
Davide Pesavento296b3852019-10-20 16:00:16 -0400180 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
181 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
182 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700183}
184
Davide Pesavento296b3852019-10-20 16:00:16 -0400185BOOST_AUTO_TEST_CASE(SuccessAfterNackAndTimeout)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700186{
Davide Pesavento296b3852019-10-20 16:00:16 -0400187 opt.maxRetriesOnTimeoutOrNack = 3;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700188 run(name);
189
190 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
191
Davide Pesavento296b3852019-10-20 16:00:16 -0400192 // timeout or nack discovery Interests
193 for (int retries = 0; retries < opt.maxRetriesOnTimeoutOrNack * 2; ++retries) {
194 if (retries % 2 == 0) {
Davide Pesavento66777622020-10-09 18:46:03 -0400195 advanceClocks(opt.interestLifetime);
Davide Pesavento296b3852019-10-20 16:00:16 -0400196 }
197 else {
198 face.receive(makeNack(face.sentInterests.back(), lp::NackReason::DUPLICATE));
Davide Pesavento66777622020-10-09 18:46:03 -0400199 advanceClocks(1_ns);
Davide Pesavento296b3852019-10-20 16:00:16 -0400200 }
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700201
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 Pesavento66777622020-10-09 18:46:03 -0400210 advanceClocks(1_ns);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700211
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700212 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
213}
214
215BOOST_AUTO_TEST_SUITE_END() // TestDiscoverVersion
216BOOST_AUTO_TEST_SUITE_END() // Chunks
217
218} // namespace tests
219} // namespace chunks
220} // namespace ndn