Andrea Tosatto | 672b9a7 | 2016-01-05 16:18:20 +0100 | [diff] [blame^] | 1 | /* -*- 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-fixed.hpp" |
| 27 | |
| 28 | #include "discover-version-fixture.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace chunks { |
| 32 | namespace tests { |
| 33 | |
| 34 | using namespace ndn::tests; |
| 35 | |
| 36 | class DiscoverVersionFixedFixture : public DiscoverVersionFixture |
| 37 | { |
| 38 | public: |
| 39 | DiscoverVersionFixedFixture() |
| 40 | : DiscoverVersionFixture(makeOptions()) |
| 41 | , version(1449227841747) |
| 42 | { |
| 43 | setDiscover(make_unique<DiscoverVersionFixed>(Name(name).appendVersion(version), |
| 44 | face, makeOptions())); |
| 45 | } |
| 46 | |
| 47 | protected: |
| 48 | uint64_t version; //Version to find |
| 49 | }; |
| 50 | |
| 51 | BOOST_AUTO_TEST_SUITE(Chunks) |
| 52 | BOOST_AUTO_TEST_SUITE(TestDiscoverVersionFixed) |
| 53 | |
| 54 | BOOST_FIXTURE_TEST_CASE(RequestedVersionAvailable, DiscoverVersionFixedFixture) |
| 55 | { |
| 56 | discover->run(); |
| 57 | advanceClocks(io, time::nanoseconds(1), 1); |
| 58 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 59 | |
| 60 | face.receive(*makeDataWithVersion(version)); |
| 61 | |
| 62 | advanceClocks(io, time::nanoseconds(1), 1); |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(isDiscoveryFinished, true); |
| 65 | BOOST_CHECK_EQUAL(discoveredVersion, version); |
| 66 | |
| 67 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 68 | auto lastInterest = face.sentInterests.back(); |
| 69 | BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0); |
| 70 | BOOST_CHECK_EQUAL(lastInterest.getMaxSuffixComponents(), 2); |
| 71 | BOOST_CHECK_EQUAL(lastInterest.getMinSuffixComponents(), 2); |
| 72 | BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh); |
| 73 | } |
| 74 | |
| 75 | BOOST_FIXTURE_TEST_CASE(NoVersionsAvailable, DiscoverVersionFixedFixture) |
| 76 | { |
| 77 | discover->run(); |
| 78 | advanceClocks(io, time::nanoseconds(1), 1); |
| 79 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 80 | |
| 81 | for (int retries = 0; retries < maxRetriesOnTimeoutOrNack; ++retries) { |
| 82 | advanceClocks(io, interestLifetime, 1); |
| 83 | BOOST_CHECK_EQUAL(isDiscoveryFinished, false); |
| 84 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 2 + retries); |
| 85 | } |
| 86 | |
| 87 | for (const auto& lastInterest : face.sentInterests) { |
| 88 | BOOST_CHECK_EQUAL(lastInterest.getExclude().size(), 0); |
| 89 | BOOST_CHECK_EQUAL(lastInterest.getMaxSuffixComponents(), 2); |
| 90 | BOOST_CHECK_EQUAL(lastInterest.getMinSuffixComponents(), 2); |
| 91 | BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh); |
| 92 | BOOST_CHECK_EQUAL(lastInterest.getName().equals(Name(name).appendVersion(version)), true); |
| 93 | } |
| 94 | |
| 95 | advanceClocks(io, interestLifetime, 1); |
| 96 | BOOST_CHECK_EQUAL(isDiscoveryFinished, true); |
| 97 | // check if discovered version is the default value |
| 98 | BOOST_CHECK_EQUAL(discoveredVersion, 0); |
| 99 | BOOST_CHECK_EQUAL(face.sentInterests.size(), maxRetriesOnTimeoutOrNack + 1); |
| 100 | } |
| 101 | |
| 102 | BOOST_FIXTURE_TEST_CASE(DataNotSegment, DiscoverVersionFixedFixture) |
| 103 | { |
| 104 | discover->run(); |
| 105 | advanceClocks(io, time::nanoseconds(1), 1); |
| 106 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1); |
| 107 | |
| 108 | std::vector<std::string> randomStrings { |
| 109 | "", |
| 110 | "abcdefg", |
| 111 | "12345", |
| 112 | "qr%67a3%4e" |
| 113 | }; |
| 114 | |
| 115 | Exclude expectedExclude; |
| 116 | for (size_t retries = 0; retries < randomStrings.size(); ++retries) { |
| 117 | auto data = make_shared<Data>(Name(name).appendVersion(version).append(randomStrings[retries])); |
| 118 | data->setFinalBlockId(name::Component::fromSegment(0)); |
| 119 | data = signData(data); |
| 120 | |
| 121 | face.receive(*data); |
| 122 | advanceClocks(io, time::nanoseconds(1), 1); |
| 123 | |
| 124 | BOOST_CHECK_EQUAL(isDiscoveryFinished, false); |
| 125 | |
| 126 | BOOST_REQUIRE_EQUAL(face.sentInterests.size(), 1 + retries); |
| 127 | auto lastInterest = face.sentInterests.back(); |
| 128 | if (randomStrings[retries] != "") |
| 129 | expectedExclude.excludeOne(name::Component::fromEscapedString(randomStrings[retries])); |
| 130 | BOOST_CHECK_EQUAL(lastInterest.getExclude(), expectedExclude); |
| 131 | BOOST_CHECK_EQUAL(lastInterest.getMaxSuffixComponents(), 2); |
| 132 | BOOST_CHECK_EQUAL(lastInterest.getMinSuffixComponents(), 2); |
| 133 | BOOST_CHECK_EQUAL(lastInterest.getMustBeFresh(), mustBeFresh); |
| 134 | } |
| 135 | |
| 136 | advanceClocks(io, interestLifetime, maxRetriesOnTimeoutOrNack + 1); |
| 137 | BOOST_CHECK_EQUAL(isDiscoveryFinished, true); |
| 138 | // check if discovered version is the default value |
| 139 | BOOST_CHECK_EQUAL(discoveredVersion, 0); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_SUITE_END() // TestDiscoverVersionFixed |
| 143 | BOOST_AUTO_TEST_SUITE_END() // Chunks |
| 144 | |
| 145 | } // namespace tests |
| 146 | } // namespace chunks |
| 147 | } // namespace ndn |