blob: bccaee125901ea3aa8427fc78ca6a96b1a0f6d01 [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"),
41 bind(&SegmentPublisherFixture::onInterest, this, _1, _2),
42 [] (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 Pesavento042dfb32020-07-23 21:07:16 -040066 onInterest(const Name& prefix, const Interest& interest)
67 {
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};
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050085 shared_ptr<util::SegmentFetcher> fetcher;
86 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);
100 expressInterest(Interest("/hello/world"));
101 BOOST_CHECK_EQUAL(numComplete, 1);
102 // First segment is answered directly in publish,
103 // Rest two are satisfied by the store
104 BOOST_CHECK_EQUAL(numRepliesFromStore, 2);
105 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3);
106
107 numRepliesFromStore = 0;
108 expressInterest(Interest("/hello/world"));
109 BOOST_CHECK_EQUAL(numComplete, 2);
110 BOOST_CHECK_EQUAL(numRepliesFromStore, 3);
111
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500112 advanceClocks(time::milliseconds(freshness));
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500113 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
114
115 numRepliesFromStore = 0;
116 expressInterest(Interest("/hello/world"));
117 BOOST_CHECK_EQUAL(numComplete, 3);
118 BOOST_CHECK_EQUAL(numRepliesFromStore, 2);
119
120 numRepliesFromStore = 0;
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -0400121 m_face.expressInterest(Interest("/hello/world/").setCanBePrefix(true),
122 [this] (auto&&...) { this->numComplete++; },
123 [] (auto&&...) { BOOST_CHECK(false); },
124 [] (auto&&...) { BOOST_CHECK(false); });
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500125 advanceClocks(10_ms);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500126 BOOST_CHECK_EQUAL(numComplete, 4);
127 BOOST_CHECK_EQUAL(numRepliesFromStore, 1);
128}
129
130BOOST_AUTO_TEST_CASE(LargerDataName)
131{
132 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
133 dataName = Name("/hello/world/IBF");
134
135 expressInterest(Interest("/hello/world"));
136 BOOST_CHECK_EQUAL(numComplete, 1);
137 // First segment is answered directly in publish,
138 // Rest two are satisfied by the store
139 BOOST_CHECK_EQUAL(numRepliesFromStore, 2);
140 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3);
141
Davide Pesaventof91d1df2020-11-25 14:50:41 -0500142 advanceClocks(time::milliseconds(freshness));
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500143 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
144}
145
146BOOST_AUTO_TEST_SUITE_END()
147
Junxiao Shi3426baf2019-01-13 23:19:23 +0000148} // namespace psync