blob: 210555dca4a6ed7c0ffb6925ae0fbc760f37327f [file] [log] [blame]
Ashlesh Gawandeec43b362018-08-01 15:15:01 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -04003 * Copyright (c) 2014-2022, 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
31namespace psync {
32
33using namespace ndn;
34
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040035class SegmentPublisherFixture : public tests::IoFixture, public tests::KeyChainFixture
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050036{
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040037protected:
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050038 SegmentPublisherFixture()
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050039 {
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040040 m_face.setInterestFilter(InterestFilter("/hello/world"),
Davide Pesaventob68f2842022-11-17 19:07:04 -050041 bind(&SegmentPublisherFixture::onInterest, this, _2),
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040042 [] (auto&&...) { BOOST_CHECK(false); });
Davide Pesaventof91d1df2020-11-25 14:50:41 -050043 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050044
45 for (int i = 0; i < 1000; ++i) {
46 state.addContent(Name("/test").appendNumber(i));
47 }
48 }
49
Davide Pesaventof91d1df2020-11-25 14:50:41 -050050 ~SegmentPublisherFixture() override
Davide Pesavento042dfb32020-07-23 21:07:16 -040051 {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050052 fetcher->stop();
53 }
54
55 void
Davide Pesavento042dfb32020-07-23 21:07:16 -040056 expressInterest(const Interest& interest)
57 {
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040058 fetcher = util::SegmentFetcher::start(m_face, interest, security::getAcceptAllValidator());
Davide Pesavento042dfb32020-07-23 21:07:16 -040059 fetcher->onComplete.connect([this] (auto&&...) { numComplete++; });
60 fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); });
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050061
Davide Pesaventof91d1df2020-11-25 14:50:41 -050062 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050063 }
64
65 void
Davide Pesaventob68f2842022-11-17 19:07:04 -050066 onInterest(const Interest& interest)
Davide Pesavento042dfb32020-07-23 21:07:16 -040067 {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050068 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 Pesaventoc45a4ea2022-09-19 02:10:53 -040082protected:
83 util::DummyClientFace m_face{m_io, m_keyChain, {true, true}};
84 SegmentPublisher publisher{m_face, m_keyChain};
Davide Pesaventob68f2842022-11-17 19:07:04 -050085 std::shared_ptr<util::SegmentFetcher> fetcher;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050086 Name dataName;
Davide Pesaventodb789562020-12-19 23:01:08 -050087 detail::State state;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050088
Davide Pesaventof91d1df2020-11-25 14:50:41 -050089 int numComplete = 0;
90 int numRepliesFromStore = 0;
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040091
92 static constexpr time::milliseconds freshness = 1_s;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050093};
94
95BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture)
96
97BOOST_AUTO_TEST_CASE(Basic)
98{
99 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
Davide Pesaventob68f2842022-11-17 19:07:04 -0500100
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500101 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 Pesaventob68f2842022-11-17 19:07:04 -0500108 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 Gawandeec43b362018-08-01 15:15:01 -0500116 numRepliesFromStore = 0;
117 expressInterest(Interest("/hello/world"));
118 BOOST_CHECK_EQUAL(numComplete, 2);
119 BOOST_CHECK_EQUAL(numRepliesFromStore, 3);
120
Davide Pesaventob68f2842022-11-17 19:07:04 -0500121 advanceClocks(freshness);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500122 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 Pesaventob68f2842022-11-17 19:07:04 -0500130 m_face.expressInterest(Interest("/hello/world").setCanBePrefix(true),
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400131 [this] (auto&&...) { this->numComplete++; },
132 [] (auto&&...) { BOOST_CHECK(false); },
133 [] (auto&&...) { BOOST_CHECK(false); });
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500134 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500135 BOOST_CHECK_EQUAL(numComplete, 4);
136 BOOST_CHECK_EQUAL(numRepliesFromStore, 1);
137}
138
Davide Pesaventob68f2842022-11-17 19:07:04 -0500139BOOST_AUTO_TEST_CASE(LongerDataName)
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500140{
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500141 dataName = Name("/hello/world/IBF");
Davide Pesaventob68f2842022-11-17 19:07:04 -0500142 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500143
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 Pesaventob68f2842022-11-17 19:07:04 -0500151 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 Gawandeec43b362018-08-01 15:15:01 -0500160 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
161}
162
163BOOST_AUTO_TEST_SUITE_END()
164
Junxiao Shi3426baf2019-01-13 23:19:23 +0000165} // namespace psync