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 | 47eb6d9 | 2024-02-12 20:25:51 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2014-2024, 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 | |
Davide Pesavento | 47eb6d9 | 2024-02-12 20:25:51 -0500 | [diff] [blame^] | 31 | namespace psync::tests { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 32 | |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 33 | using namespace ndn::time_literals; |
| 34 | using ndn::Interest; |
| 35 | using ndn::Name; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 36 | |
Davide Pesavento | 47eb6d9 | 2024-02-12 20:25:51 -0500 | [diff] [blame^] | 37 | class SegmentPublisherFixture : public IoFixture, public KeyChainFixture |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 38 | { |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 39 | protected: |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 40 | SegmentPublisherFixture() |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 41 | { |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 42 | m_face.setInterestFilter(ndn::InterestFilter("/hello/world"), |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 43 | bind(&SegmentPublisherFixture::onInterest, this, _2), |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 44 | [] (auto&&...) { BOOST_CHECK(false); }); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 45 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 46 | |
| 47 | for (int i = 0; i < 1000; ++i) { |
| 48 | state.addContent(Name("/test").appendNumber(i)); |
| 49 | } |
| 50 | } |
| 51 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 52 | ~SegmentPublisherFixture() override |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 53 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 54 | fetcher->stop(); |
| 55 | } |
| 56 | |
| 57 | void |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 58 | expressInterest(const Interest& interest) |
| 59 | { |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 60 | fetcher = ndn::SegmentFetcher::start(m_face, interest, ndn::security::getAcceptAllValidator()); |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 61 | fetcher->onComplete.connect([this] (auto&&...) { numComplete++; }); |
| 62 | fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); }); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 63 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 64 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 68 | onInterest(const Interest& interest) |
Davide Pesavento | 042dfb3 | 2020-07-23 21:07:16 -0400 | [diff] [blame] | 69 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 70 | if (publisher.replyFromStore(interest.getName())) { |
| 71 | numRepliesFromStore++; |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | // If dataName is same as interest name |
| 76 | if (dataName.empty()) { |
| 77 | publisher.publish(interest.getName(), interest.getName(), state.wireEncode(), freshness); |
| 78 | } |
| 79 | else { |
| 80 | publisher.publish(interest.getName(), dataName, state.wireEncode(), freshness); |
| 81 | } |
| 82 | } |
| 83 | |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 84 | protected: |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 85 | ndn::DummyClientFace m_face{m_io, m_keyChain, {true, true}}; |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 86 | SegmentPublisher publisher{m_face, m_keyChain}; |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 87 | std::shared_ptr<ndn::SegmentFetcher> fetcher; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 88 | Name dataName; |
Davide Pesavento | db78956 | 2020-12-19 23:01:08 -0500 | [diff] [blame] | 89 | detail::State state; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 90 | |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 91 | int numComplete = 0; |
| 92 | int numRepliesFromStore = 0; |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 93 | |
Junxiao Shi | c5f5eb1 | 2023-08-11 08:05:23 +0000 | [diff] [blame] | 94 | static constexpr ndn::time::milliseconds freshness = 1_s; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture) |
| 98 | |
| 99 | BOOST_AUTO_TEST_CASE(Basic) |
| 100 | { |
| 101 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 102 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 103 | expressInterest(Interest("/hello/world")); |
| 104 | BOOST_CHECK_EQUAL(numComplete, 1); |
| 105 | // First segment is answered directly in publish, |
| 106 | // Rest two are satisfied by the store |
| 107 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 108 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3); |
| 109 | |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 110 | for (const auto& data : publisher.m_ims) { |
| 111 | BOOST_TEST_CONTEXT(data.getName()) { |
| 112 | BOOST_REQUIRE_EQUAL(data.getName().size(), 4); |
| 113 | BOOST_CHECK(data.getName()[-1].isSegment()); |
| 114 | BOOST_CHECK(data.getName()[-2].isVersion()); |
| 115 | } |
| 116 | } |
| 117 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 118 | numRepliesFromStore = 0; |
| 119 | expressInterest(Interest("/hello/world")); |
| 120 | BOOST_CHECK_EQUAL(numComplete, 2); |
| 121 | BOOST_CHECK_EQUAL(numRepliesFromStore, 3); |
| 122 | |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 123 | advanceClocks(freshness); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 125 | |
| 126 | numRepliesFromStore = 0; |
| 127 | expressInterest(Interest("/hello/world")); |
| 128 | BOOST_CHECK_EQUAL(numComplete, 3); |
| 129 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 130 | |
| 131 | numRepliesFromStore = 0; |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 132 | m_face.expressInterest(Interest("/hello/world").setCanBePrefix(true), |
Davide Pesavento | c45a4ea | 2022-09-19 02:10:53 -0400 | [diff] [blame] | 133 | [this] (auto&&...) { this->numComplete++; }, |
| 134 | [] (auto&&...) { BOOST_CHECK(false); }, |
| 135 | [] (auto&&...) { BOOST_CHECK(false); }); |
Davide Pesavento | f91d1df | 2020-11-25 14:50:41 -0500 | [diff] [blame] | 136 | advanceClocks(10_ms); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(numComplete, 4); |
| 138 | BOOST_CHECK_EQUAL(numRepliesFromStore, 1); |
| 139 | } |
| 140 | |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 141 | BOOST_AUTO_TEST_CASE(LongerDataName) |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 142 | { |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 143 | dataName = Name("/hello/world/IBF"); |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 145 | |
| 146 | expressInterest(Interest("/hello/world")); |
| 147 | BOOST_CHECK_EQUAL(numComplete, 1); |
| 148 | // First segment is answered directly in publish, |
| 149 | // Rest two are satisfied by the store |
| 150 | BOOST_CHECK_EQUAL(numRepliesFromStore, 2); |
| 151 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3); |
| 152 | |
Davide Pesavento | b68f284 | 2022-11-17 19:07:04 -0500 | [diff] [blame] | 153 | for (const auto& data : publisher.m_ims) { |
| 154 | BOOST_TEST_CONTEXT(data.getName()) { |
| 155 | BOOST_REQUIRE_EQUAL(data.getName().size(), 5); |
| 156 | BOOST_CHECK(data.getName()[-1].isSegment()); |
| 157 | BOOST_CHECK(data.getName()[-2].isVersion()); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | advanceClocks(freshness); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0); |
| 163 | } |
| 164 | |
| 165 | BOOST_AUTO_TEST_SUITE_END() |
| 166 | |
Davide Pesavento | 47eb6d9 | 2024-02-12 20:25:51 -0500 | [diff] [blame^] | 167 | } // namespace psync::tests |