blob: 25db644b9fc0c9627e03b91f906becfeb976238f [file] [log] [blame]
Ashlesh Gawandeec43b362018-08-01 15:15:01 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47eb6d92024-02-12 20:25:51 -05003 * Copyright (c) 2014-2024, The University of Memphis
Ashlesh Gawandeec43b362018-08-01 15:15:01 -05004 *
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 Gawande0cf4b602019-01-18 15:58:17 -06009 * of the GNU Lesser General Public License as published by the Free Software Foundation,
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050010 * 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 Gawande0cf4b602019-01-18 15:58:17 -060014 * PURPOSE. See the GNU Lesser General Public License for more details.
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050015 *
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -060016 * You should have received a copy of the GNU Lesser General Public License along with
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050017 * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
19
Ashlesh Gawande78b94ad2018-12-13 15:29:19 -060020#include "PSync/segment-publisher.hpp"
21#include "PSync/detail/state.hpp"
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050022
Davide Pesavento5b3cf762020-04-03 16:20:04 -040023#include "tests/boost-test.hpp"
Davide Pesaventof91d1df2020-11-25 14:50:41 -050024#include "tests/io-fixture.hpp"
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040025#include "tests/key-chain-fixture.hpp"
Davide Pesavento5b3cf762020-04-03 16:20:04 -040026
Davide Pesaventodb789562020-12-19 23:01:08 -050027#include <ndn-cxx/security/validator-null.hpp>
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050028#include <ndn-cxx/util/dummy-client-face.hpp>
29#include <ndn-cxx/util/segment-fetcher.hpp>
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050030
Davide Pesavento47eb6d92024-02-12 20:25:51 -050031namespace psync::tests {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050032
Junxiao Shic5f5eb12023-08-11 08:05:23 +000033using namespace ndn::time_literals;
34using ndn::Interest;
35using ndn::Name;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050036
Davide Pesavento47eb6d92024-02-12 20:25:51 -050037class SegmentPublisherFixture : public IoFixture, public KeyChainFixture
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050038{
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040039protected:
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050040 SegmentPublisherFixture()
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050041 {
Junxiao Shic5f5eb12023-08-11 08:05:23 +000042 m_face.setInterestFilter(ndn::InterestFilter("/hello/world"),
Davide Pesaventob68f2842022-11-17 19:07:04 -050043 bind(&SegmentPublisherFixture::onInterest, this, _2),
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040044 [] (auto&&...) { BOOST_CHECK(false); });
Davide Pesaventof91d1df2020-11-25 14:50:41 -050045 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050046
47 for (int i = 0; i < 1000; ++i) {
48 state.addContent(Name("/test").appendNumber(i));
49 }
50 }
51
Davide Pesaventof91d1df2020-11-25 14:50:41 -050052 ~SegmentPublisherFixture() override
Davide Pesavento042dfb32020-07-23 21:07:16 -040053 {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050054 fetcher->stop();
55 }
56
57 void
Davide Pesavento042dfb32020-07-23 21:07:16 -040058 expressInterest(const Interest& interest)
59 {
Junxiao Shic5f5eb12023-08-11 08:05:23 +000060 fetcher = ndn::SegmentFetcher::start(m_face, interest, ndn::security::getAcceptAllValidator());
Davide Pesavento042dfb32020-07-23 21:07:16 -040061 fetcher->onComplete.connect([this] (auto&&...) { numComplete++; });
62 fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); });
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050063
Davide Pesaventof91d1df2020-11-25 14:50:41 -050064 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050065 }
66
67 void
Davide Pesaventob68f2842022-11-17 19:07:04 -050068 onInterest(const Interest& interest)
Davide Pesavento042dfb32020-07-23 21:07:16 -040069 {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050070 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 Pesaventoc45a4ea2022-09-19 02:10:53 -040084protected:
Junxiao Shic5f5eb12023-08-11 08:05:23 +000085 ndn::DummyClientFace m_face{m_io, m_keyChain, {true, true}};
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040086 SegmentPublisher publisher{m_face, m_keyChain};
Junxiao Shic5f5eb12023-08-11 08:05:23 +000087 std::shared_ptr<ndn::SegmentFetcher> fetcher;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050088 Name dataName;
Davide Pesaventodb789562020-12-19 23:01:08 -050089 detail::State state;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050090
Davide Pesaventof91d1df2020-11-25 14:50:41 -050091 int numComplete = 0;
92 int numRepliesFromStore = 0;
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040093
Junxiao Shic5f5eb12023-08-11 08:05:23 +000094 static constexpr ndn::time::milliseconds freshness = 1_s;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050095};
96
97BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture)
98
99BOOST_AUTO_TEST_CASE(Basic)
100{
101 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
Davide Pesaventob68f2842022-11-17 19:07:04 -0500102
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500103 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 Pesaventob68f2842022-11-17 19:07:04 -0500110 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 Gawandeec43b362018-08-01 15:15:01 -0500118 numRepliesFromStore = 0;
119 expressInterest(Interest("/hello/world"));
120 BOOST_CHECK_EQUAL(numComplete, 2);
121 BOOST_CHECK_EQUAL(numRepliesFromStore, 3);
122
Davide Pesaventob68f2842022-11-17 19:07:04 -0500123 advanceClocks(freshness);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500124 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 Pesaventob68f2842022-11-17 19:07:04 -0500132 m_face.expressInterest(Interest("/hello/world").setCanBePrefix(true),
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400133 [this] (auto&&...) { this->numComplete++; },
134 [] (auto&&...) { BOOST_CHECK(false); },
135 [] (auto&&...) { BOOST_CHECK(false); });
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500136 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500137 BOOST_CHECK_EQUAL(numComplete, 4);
138 BOOST_CHECK_EQUAL(numRepliesFromStore, 1);
139}
140
Davide Pesaventob68f2842022-11-17 19:07:04 -0500141BOOST_AUTO_TEST_CASE(LongerDataName)
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500142{
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500143 dataName = Name("/hello/world/IBF");
Davide Pesaventob68f2842022-11-17 19:07:04 -0500144 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500145
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 Pesaventob68f2842022-11-17 19:07:04 -0500153 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 Gawandeec43b362018-08-01 15:15:01 -0500162 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
163}
164
165BOOST_AUTO_TEST_SUITE_END()
166
Davide Pesavento47eb6d92024-02-12 20:25:51 -0500167} // namespace psync::tests