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