blob: 7b7bfc7c61a2f75c4a644e8cb501b479b8a99510 [file] [log] [blame]
Ashlesh Gawandeec43b362018-08-01 15:15:01 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Ashlesh Gawande5a895472020-01-25 18:07:32 -08003 * Copyright (c) 2014-2020, 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"
24#include "tests/unit-test-time-fixture.hpp"
25
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050026#include <ndn-cxx/data.hpp>
27#include <ndn-cxx/interest.hpp>
28#include <ndn-cxx/util/dummy-client-face.hpp>
29#include <ndn-cxx/util/segment-fetcher.hpp>
30#include <ndn-cxx/security/validator-null.hpp>
31
32namespace psync {
33
34using namespace ndn;
35
36class SegmentPublisherFixture : public tests::UnitTestTimeFixture
37{
38public:
39 SegmentPublisherFixture()
Davide Pesavento042dfb32020-07-23 21:07:16 -040040 : face(io, util::DummyClientFace::Options{true, true})
41 , publisher(face, keyChain)
42 , freshness(1000)
43 , numComplete(0)
44 , numRepliesFromStore(0)
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050045 {
46 face.setInterestFilter(InterestFilter("/hello/world"),
47 bind(&SegmentPublisherFixture::onInterest, this, _1, _2),
48 [] (const ndn::Name& prefix, const std::string& msg) {
49 BOOST_CHECK(false);
50 });
51 advanceClocks(ndn::time::milliseconds(10));
52
53 for (int i = 0; i < 1000; ++i) {
54 state.addContent(Name("/test").appendNumber(i));
55 }
56 }
57
Davide Pesavento042dfb32020-07-23 21:07:16 -040058 ~SegmentPublisherFixture()
59 {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050060 fetcher->stop();
61 }
62
63 void
Davide Pesavento042dfb32020-07-23 21:07:16 -040064 expressInterest(const Interest& interest)
65 {
66 fetcher = util::SegmentFetcher::start(face, interest, ndn::security::getAcceptAllValidator());
67 fetcher->onComplete.connect([this] (auto&&...) { numComplete++; });
68 fetcher->onError.connect([] (auto&&...) { BOOST_CHECK(false); });
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050069
70 advanceClocks(ndn::time::milliseconds(10));
71 }
72
73 void
Davide Pesavento042dfb32020-07-23 21:07:16 -040074 onInterest(const Name& prefix, const Interest& interest)
75 {
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050076 if (publisher.replyFromStore(interest.getName())) {
77 numRepliesFromStore++;
78 return;
79 }
80
81 // If dataName is same as interest name
82 if (dataName.empty()) {
83 publisher.publish(interest.getName(), interest.getName(), state.wireEncode(), freshness);
84 }
85 else {
86 publisher.publish(interest.getName(), dataName, state.wireEncode(), freshness);
87 }
88 }
89
90 util::DummyClientFace face;
91 KeyChain keyChain;
92 SegmentPublisher publisher;
93 shared_ptr<util::SegmentFetcher> fetcher;
94 Name dataName;
95 time::milliseconds freshness;
96 State state;
97
98 int numComplete;
99 int numRepliesFromStore;
100};
101
102BOOST_FIXTURE_TEST_SUITE(TestSegmentPublisher, SegmentPublisherFixture)
103
104BOOST_AUTO_TEST_CASE(Basic)
105{
106 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
107 expressInterest(Interest("/hello/world"));
108 BOOST_CHECK_EQUAL(numComplete, 1);
109 // First segment is answered directly in publish,
110 // Rest two are satisfied by the store
111 BOOST_CHECK_EQUAL(numRepliesFromStore, 2);
112 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3);
113
114 numRepliesFromStore = 0;
115 expressInterest(Interest("/hello/world"));
116 BOOST_CHECK_EQUAL(numComplete, 2);
117 BOOST_CHECK_EQUAL(numRepliesFromStore, 3);
118
119 advanceClocks(ndn::time::milliseconds(freshness));
120 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
121
122 numRepliesFromStore = 0;
123 expressInterest(Interest("/hello/world"));
124 BOOST_CHECK_EQUAL(numComplete, 3);
125 BOOST_CHECK_EQUAL(numRepliesFromStore, 2);
126
127 numRepliesFromStore = 0;
Junxiao Shi3426baf2019-01-13 23:19:23 +0000128 face.expressInterest(Interest("/hello/world/").setCanBePrefix(true),
Ashlesh Gawandeec43b362018-08-01 15:15:01 -0500129 [this] (const Interest& interest, const Data& data) {
130 numComplete++;
131 },
132 [] (const Interest& interest, const lp::Nack& nack) {
133 BOOST_CHECK(false);
134 },
135 [] (const Interest& interest) {
136 BOOST_CHECK(false);
137 });
138 advanceClocks(ndn::time::milliseconds(10));
139 BOOST_CHECK_EQUAL(numComplete, 4);
140 BOOST_CHECK_EQUAL(numRepliesFromStore, 1);
141}
142
143BOOST_AUTO_TEST_CASE(LargerDataName)
144{
145 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
146 dataName = Name("/hello/world/IBF");
147
148 expressInterest(Interest("/hello/world"));
149 BOOST_CHECK_EQUAL(numComplete, 1);
150 // First segment is answered directly in publish,
151 // Rest two are satisfied by the store
152 BOOST_CHECK_EQUAL(numRepliesFromStore, 2);
153 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 3);
154
155 advanceClocks(ndn::time::milliseconds(freshness));
156 BOOST_CHECK_EQUAL(publisher.m_ims.size(), 0);
157}
158
159BOOST_AUTO_TEST_SUITE_END()
160
Junxiao Shi3426baf2019-01-13 23:19:23 +0000161} // namespace psync