blob: 79c6fb7826f9ae9ad005dbb2a86b0753c4c19128 [file] [log] [blame]
Andrea Tosatto672b9a72016-01-05 16:18:20 +01001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2016, 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 Andrea Tosatto
24 */
25
26#include "tools/chunks/catchunks/discover-version-iterative.hpp"
27
28#include "discover-version-fixture.hpp"
29
30namespace ndn {
31namespace chunks {
32namespace tests {
33
Andrea Tosatto672b9a72016-01-05 16:18:20 +010034class DiscoverVersionIterativeFixture : public DiscoverVersionFixture,
35 protected DiscoverVersionIterativeOptions
36{
37public:
38 typedef DiscoverVersionIterativeOptions Options;
39
40public:
41 explicit
42 DiscoverVersionIterativeFixture(const Options& opt = makeOptionsIterative())
43 : chunks::Options(opt)
44 , DiscoverVersionFixture(opt)
45 , Options(opt)
46 {
47 setDiscover(make_unique<DiscoverVersionIterative>(Name(name), face, opt));
48 }
49
50protected:
51 static Options
52 makeOptionsIterative()
53 {
54 Options options;
55 options.isVerbose = false;
56 options.maxRetriesOnTimeoutOrNack = 3;
57 options.maxRetriesAfterVersionFound = 1;
58 return options;
59 }
60};
61
62
63BOOST_AUTO_TEST_SUITE(Chunks)
64BOOST_AUTO_TEST_SUITE(TestDiscoverVersionIterative)
65
66BOOST_FIXTURE_TEST_CASE(SingleVersionAvailable, DiscoverVersionIterativeFixture)
67{
68 discover->run();
69 advanceClocks(io, time::nanoseconds(1), 1);
70
71 uint64_t version = 1449241767037;
72
73 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
74 auto lastInterest = face.sentInterests.back();
75 BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0);
76 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
77 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
78 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
79
80 // Send first segment for the right version
81 face.receive(*makeDataWithVersion(version));
82 advanceClocks(io, time::nanoseconds(1), 1);
83
84 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
85 lastInterest = face.sentInterests.back();
86 Exclude expectedExclude;
87 expectedExclude.excludeBefore(name::Component::fromVersion(version));
88 BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
89 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
90 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
91 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
92
93 // Generate the timeout
94 for (int retries = 0; retries < maxRetriesAfterVersionFound; ++retries) {
95 advanceClocks(io, interestLifetime, 1);
96
97 BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
98
99 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), retries + 3);
100 lastInterest = face.sentInterests.back();
101 Exclude expectedExclude;
102 expectedExclude.excludeBefore(name::Component::fromVersion(version));
103 BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
104 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
105 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
106 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
107 }
108
109 advanceClocks(io, interestLifetime, 1);
110 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
111 BOOST_CHECK_EQUAL(discoveredVersion, version);
112 BOOST_CHECK_EQUAL(face.sentInterests.size(), maxRetriesAfterVersionFound + 2);
113}
114
115
116BOOST_FIXTURE_TEST_CASE(NoVersionsAvailable, DiscoverVersionIterativeFixture)
117{
118 discover->run();
119 advanceClocks(io, time::nanoseconds(1), 1);
120 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
121
122 for (int retries = 0; retries < maxRetriesOnTimeoutOrNack; ++retries) {
123 advanceClocks(io, interestLifetime, 1);
124 BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
125 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2 + retries);
126 }
127
128 for (auto& lastInterest : face.sentInterests) {
129 BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0);
130 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
131 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
132 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
133 }
134
135 advanceClocks(io, interestLifetime, 1);
136 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
137 BOOST_CHECK_EQUAL(face.sentInterests.size(), maxRetriesOnTimeoutOrNack + 1);
138}
139
140BOOST_FIXTURE_TEST_CASE(MultipleVersionsAvailable, DiscoverVersionIterativeFixture)
141{
142 // nVersions must be positive
143 const uint64_t nVersions = 5;
144
145 discover->run();
146 advanceClocks(io, time::nanoseconds(1), 1);
147
148 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
149 auto lastInterest = face.sentInterests.back();
150 BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0);
151 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
152 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
153 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
154
155 for (uint64_t nSentVersions = 0; nSentVersions < nVersions; ++nSentVersions) {
156 face.receive(*makeDataWithVersion(nSentVersions));
157 advanceClocks(io, time::nanoseconds(1), 1);
158
159 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2 + nSentVersions);
160 lastInterest = face.sentInterests.back();
161 Exclude expectedExclude;
162 expectedExclude.excludeBefore(name::Component::fromVersion(nSentVersions));
163 BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
164 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
165 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
166 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
167 }
168
169 for (int retries = 0; retries < maxRetriesAfterVersionFound; ++retries) {
170 advanceClocks(io, interestLifetime, 1);
171
172 BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
173
174 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2 + retries + nVersions);
175 lastInterest = face.sentInterests.back();
176 Exclude expectedExclude;
177 expectedExclude.excludeBefore(name::Component::fromVersion(nVersions - 1));
178 BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
179 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
180 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
181 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
182 }
183
184 advanceClocks(io, interestLifetime, 1);
185 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
186 BOOST_CHECK_EQUAL(discoveredVersion, nVersions - 1);
187 BOOST_CHECK_EQUAL(face.sentInterests.size(), nVersions + maxRetriesAfterVersionFound + 1);
188}
189
190BOOST_FIXTURE_TEST_CASE(MultipleVersionsAvailableDescendent, DiscoverVersionIterativeFixture)
191{
192 // nVersions must be positive
193 const uint64_t nVersions = 5;
194
195 discover->run();
196 advanceClocks(io, time::nanoseconds(1), 1);
197
198 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1);
199 auto lastInterest = face.sentInterests.back();
200 BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
201 BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0);
202 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
203 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
204 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
205
206 for (uint64_t nVersionsToSend = nVersions; nVersionsToSend > 0; --nVersionsToSend) {
207 face.receive(*makeDataWithVersion(nVersionsToSend - 1));
208 advanceClocks(io, time::nanoseconds(1), 1);
209
210 BOOST_CHECK_EQUAL(isDiscoveryFinished, false);
211
212 BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2);
213 lastInterest = face.sentInterests.back();
214 Exclude expectedExclude;
215 expectedExclude.excludeBefore(name::Component::fromVersion(nVersions - 1));
216 BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude);
217 BOOST_CHECK_EQUAL(lastInterest.getChildSelector(), 1);
218 BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh);
219 BOOST_CHECK_EQUAL(lastInterest.getName().equals(name), true);
220 }
221
222 advanceClocks(io, interestLifetime, maxRetriesAfterVersionFound + 1);
223 BOOST_CHECK_EQUAL(isDiscoveryFinished, true);
224 BOOST_CHECK_EQUAL(discoveredVersion, nVersions - 1);
225 BOOST_CHECK_EQUAL(face.sentInterests.size(), maxRetriesAfterVersionFound + 2);
226}
227
228BOOST_AUTO_TEST_SUITE_END() // TestDiscoverVersionIterative
229BOOST_AUTO_TEST_SUITE_END() // Chunks
230
231} // namespace tests
232} // namespace chunks
233} // namespace ndn