Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, The University of Memphis |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of PSync. |
| 6 | * See AUTHORS.md for complete list of PSync authors and contributors. |
| 7 | * |
| 8 | * PSync is free software: you can redistribute it and/or modify it under the terms |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 9 | * of the GNU Lesser General Public License as published by the Free Software Foundation, |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * PSync is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 14 | * PURPOSE. See the GNU Lesser General Public License for more details. |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 15 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public License along with |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 17 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
Ashlesh Gawande | 78b94ad | 2018-12-13 15:29:19 -0600 | [diff] [blame] | 20 | #include "PSync/segment-publisher.hpp" |
| 21 | #include "PSync/detail/state.hpp" |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 22 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 23 | #include "tests/boost-test.hpp" |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 24 | #include "tests/io-fixture.hpp" |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 25 | #include "tests/key-chain-fixture.hpp" |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 26 | |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 27 | #include <ndn-cxx/security/validator-null.hpp> |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 28 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 29 | #include <ndn-cxx/util/segment-fetcher.hpp> |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 30 | |
| 31 | namespace psync { |
| 32 | |
| 33 | using namespace ndn; |
| 34 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 35 | class SegmentPublisherFixture : public tests::IoFixture, public tests::KeyChainFixture |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 36 | { |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 37 | protected: |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 38 | SegmentPublisherFixture() |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 39 | { |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 40 | m_face.setInterestFilter(InterestFilter("/hello/world"), |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 41 | bind(&SegmentPublisherFixture::onInterest, this, _2), |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 42 | [] (auto&&...) { BOOST_CHECK(false); }); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 43 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 44 | |
| 45 | for (int i = 0; i < 1000; ++i) { |
| 46 | state.addContent(Name("/test").appendNumber(i)); |
| 47 | } |
| 48 | } |
| 49 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 50 | ~SegmentPublisherFixture() override |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 51 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 52 | fetcher->stop(); |
| 53 | } |
| 54 | |
| 55 | void |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 56 | expressInterest(const Interest& interest) |
| 57 | { |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 58 | fetcher = util::SegmentFetcher::start(m_face, interest, security::getAcceptAllValidator()); |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 59 | fetcher->onComplete.connect([this] (auto&&...) { numComplete++; }); |
| 60 | fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); }); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 61 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 62 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 66 | onInterest(const Interest& interest) |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 67 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 68 | if (publisher.replyFromStore(interest.getName())) { |
| 69 | numRepliesFromStore++; |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | // If dataName is same as interest name |
| 74 | if (dataName.empty()) { |
| 75 | publisher.publish(interest.getName(), interest.getName(), state.wireEncode(), freshness); |
| 76 | } |
| 77 | else { |
| 78 | publisher.publish(interest.getName(), dataName, state.wireEncode(), freshness); |
| 79 | } |
| 80 | } |
| 81 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 82 | protected: |
| 83 | util::DummyClientFace m_face{m_io, m_keyChain, {true, true}}; |
| 84 | SegmentPublisher publisher{m_face, m_keyChain}; |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 85 | std::shared_ptr<util::SegmentFetcher> fetcher; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 86 | Name dataName; |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 87 | detail::State state; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 88 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 89 | int numComplete = 0; |
| 90 | int numRepliesFromStore = 0; |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 91 | |
| 92 | static constexpr time::milliseconds freshness = 1_s; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture) |
| 96 | |
| 97 | BOOST_AUTO_TEST_CASE(Basic) |
| 98 | { |
| 99 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 100 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 101 | expressInterest(Interest("/hello/world")); |
| 102 | BOOST_CHECK_EQUAL(numComplete, 1); |
| 103 | // First segment is answered directly in publish, |
| 104 | // Rest two are satisfied by the store |
| 105 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 106 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3); |
| 107 | |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 108 | for (const auto& data : publisher.m_ims) { |
| 109 | BOOST_TEST_CONTEXT(data.getName()) { |
| 110 | BOOST_REQUIRE_EQUAL(data.getName().size(), 4); |
| 111 | BOOST_CHECK(data.getName()[-1].isSegment()); |
| 112 | BOOST_CHECK(data.getName()[-2].isVersion()); |
| 113 | } |
| 114 | } |
| 115 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 116 | numRepliesFromStore = 0; |
| 117 | expressInterest(Interest("/hello/world")); |
| 118 | BOOST_CHECK_EQUAL(numComplete, 2); |
| 119 | BOOST_CHECK_EQUAL(numRepliesFromStore, 3); |
| 120 | |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 121 | advanceClocks(freshness); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 123 | |
| 124 | numRepliesFromStore = 0; |
| 125 | expressInterest(Interest("/hello/world")); |
| 126 | BOOST_CHECK_EQUAL(numComplete, 3); |
| 127 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 128 | |
| 129 | numRepliesFromStore = 0; |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 130 | m_face.expressInterest(Interest("/hello/world").setCanBePrefix(true), |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 131 | [this] (auto&&...) { this->numComplete++; }, |
| 132 | [] (auto&&...) { BOOST_CHECK(false); }, |
| 133 | [] (auto&&...) { BOOST_CHECK(false); }); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 134 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 135 | BOOST_CHECK_EQUAL(numComplete, 4); |
| 136 | BOOST_CHECK_EQUAL(numRepliesFromStore, 1); |
| 137 | } |
| 138 | |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 139 | BOOST_AUTO_TEST_CASE(LongerDataName) |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 140 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 141 | dataName = Name("/hello/world/IBF"); |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 142 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 143 | |
| 144 | expressInterest(Interest("/hello/world")); |
| 145 | BOOST_CHECK_EQUAL(numComplete, 1); |
| 146 | // First segment is answered directly in publish, |
| 147 | // Rest two are satisfied by the store |
| 148 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 149 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3); |
| 150 | |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame^] | 151 | for (const auto& data : publisher.m_ims) { |
| 152 | BOOST_TEST_CONTEXT(data.getName()) { |
| 153 | BOOST_REQUIRE_EQUAL(data.getName().size(), 5); |
| 154 | BOOST_CHECK(data.getName()[-1].isSegment()); |
| 155 | BOOST_CHECK(data.getName()[-2].isVersion()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | advanceClocks(freshness); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 161 | } |
| 162 | |
| 163 | BOOST_AUTO_TEST_SUITE_END() |
| 164 | |
Junxiao Shi | 3426baf | 2019-01-13 23:19:23 +0000 | [diff] [blame] | 165 | } // namespace psync |