blob: 9c3a8444430fd26c20d7f9ca4e88c26645c4df3d [file] [log] [blame]
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -07001/* -*- 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
34namespace ndn {
35namespace chunks {
36namespace tests {
37
38using namespace ndn::tests;
39
40class DiscoverVersionFixture : public UnitTestTimeFixture,
41 public IdentityManagementFixture
42{
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 Pesavento296b3852019-10-20 16:00:16 -040059 advanceClocks(io, 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;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070065 boost::asio::io_service io;
Davide Pesavento97a33b22019-10-17 22:10:47 -040066 util::DummyClientFace face{io};
Davide Pesavento97a33b22019-10-17 22:10:47 -040067 Options opt;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070068 unique_ptr<DiscoverVersion> discover;
Davide Pesavento296b3852019-10-20 16:00:16 -040069 optional<Name> discoveredName;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070070 optional<uint64_t> discoveredVersion;
Davide Pesavento97a33b22019-10-17 22:10:47 -040071 bool isDiscoveryFinished = false;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070072};
73
74BOOST_AUTO_TEST_SUITE(Chunks)
75BOOST_FIXTURE_TEST_SUITE(TestDiscoverVersion, DiscoverVersionFixture)
76
Davide Pesavento296b3852019-10-20 16:00:16 -040077BOOST_AUTO_TEST_CASE(Disabled)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070078{
Davide Pesavento296b3852019-10-20 16:00:16 -040079 opt.disableVersionDiscovery = true;
80 run(name);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070081
Davide Pesavento296b3852019-10-20 16:00:16 -040082 // no version discovery Interest is expressed
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -070083 BOOST_CHECK_EQUAL(face.sentInterests.size(), 0);
84
Davide Pesavento296b3852019-10-20 16:00:16 -040085 BOOST_CHECK_EQUAL(discoveredName.value(), name);
86 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
87}
88
89BOOST_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 Ghasemibb2d2802019-03-26 16:07:58 -070099 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
100}
101
Davide Pesavento296b3852019-10-20 16:00:16 -0400102BOOST_AUTO_TEST_CASE(Success)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700103{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700104 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 Pesavento296b3852019-10-20 16:00:16 -0400112 // send back a metadata packet with a valid versioned name
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700113 MetadataObject mobject;
114 mobject.setVersionedName(Name(name).appendVersion(version));
115 face.receive(mobject.makeData(lastInterest.getName(), m_keyChain));
Davide Pesavento296b3852019-10-20 16:00:16 -0400116 advanceClocks(io, 1_ns);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700117
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700118 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
119}
120
121BOOST_AUTO_TEST_CASE(InvalidDiscoveredVersionedName)
122{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700123 run(name);
124
125 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
126
Davide Pesavento296b3852019-10-20 16:00:16 -0400127 // send back a metadata packet with an invalid versioned name
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700128 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 Pesavento296b3852019-10-20 16:00:16 -0400133 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
134 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
135 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700136}
137
138BOOST_AUTO_TEST_CASE(InvalidMetadataPacket)
139{
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700140 run(name);
141
142 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
143
Davide Pesavento296b3852019-10-20 16:00:16 -0400144 // send back an invalid metadata packet
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700145 Data data(face.sentInterests.back().getName());
Junxiao Shi96192952019-05-22 15:45:12 +0000146 data.setFreshnessPeriod(1_s);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700147 data.setContentType(tlv::ContentType_Key);
148 face.receive(signData(data));
149
150 // finish discovery process without a resolved version number
Davide Pesavento296b3852019-10-20 16:00:16 -0400151 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
152 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
153 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700154}
155
Davide Pesavento296b3852019-10-20 16:00:16 -0400156BOOST_AUTO_TEST_CASE(MaxRetriesExceeded)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700157{
Davide Pesavento296b3852019-10-20 16:00:16 -0400158 opt.maxRetriesOnTimeoutOrNack = 3;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700159 run(name);
160
161 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
162
Davide Pesavento296b3852019-10-20 16:00:16 -0400163 // 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 Ghasemibb2d2802019-03-26 16:07:58 -0700172
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 Pesavento296b3852019-10-20 16:00:16 -0400181 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
182 BOOST_CHECK_EQUAL(discoveredName.has_value(), false);
183 BOOST_CHECK_EQUAL(discoveredVersion.has_value(), false);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700184}
185
Davide Pesavento296b3852019-10-20 16:00:16 -0400186BOOST_AUTO_TEST_CASE(SuccessAfterNackAndTimeout)
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700187{
Davide Pesavento296b3852019-10-20 16:00:16 -0400188 opt.maxRetriesOnTimeoutOrNack = 3;
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700189 run(name);
190
191 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
192
Davide Pesavento296b3852019-10-20 16:00:16 -0400193 // 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 Ghasemibb2d2802019-03-26 16:07:58 -0700202
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 Pesavento296b3852019-10-20 16:00:16 -0400211 advanceClocks(io, 1_ns);
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700212
Chavoosh Ghasemibb2d2802019-03-26 16:07:58 -0700213 BOOST_CHECK_EQUAL(discoveredVersion.value(), version);
214}
215
216BOOST_AUTO_TEST_SUITE_END() // TestDiscoverVersion
217BOOST_AUTO_TEST_SUITE_END() // Chunks
218
219} // namespace tests
220} // namespace chunks
221} // namespace ndn