blob: 67d4aa2b8ea0817d0bebe45e96710aba6cc43973 [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Junxiao Shi32ccfc42022-01-09 21:26:22 +00003 * Copyright (c) 2014-2022, The University of Memphis
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -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 Gawande0b2897e2018-06-20 14:40:47 -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 Gawande0b2897e2018-06-20 14:40:47 -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 Gawande0b2897e2018-06-20 14:40:47 -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/producer-base.hpp"
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050021
Davide Pesavento5b3cf762020-04-03 16:20:04 -040022#include "tests/boost-test.hpp"
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040023#include "tests/key-chain-fixture.hpp"
Davide Pesavento5b3cf762020-04-03 16:20:04 -040024
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050025#include <ndn-cxx/util/dummy-client-face.hpp>
26
27namespace psync {
28
29using namespace ndn;
30
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040031class ProducerBaseFixture : public tests::KeyChainFixture
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050032{
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040033protected:
34 util::DummyClientFace m_face;
35};
36
37BOOST_FIXTURE_TEST_SUITE(TestProducerBase, ProducerBaseFixture)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050038
39BOOST_AUTO_TEST_CASE(Basic)
40{
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050041 Name userNode("/testUser");
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040042 ProducerBase producerBase(m_face, m_keyChain, 40, Name("/psync"), userNode);
43
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050044 // Hash table size should be 40 + 40/2 = 60 (which is perfectly divisible by N_HASH = 3)
45 BOOST_CHECK_EQUAL(producerBase.m_iblt.getHashTable().size(), 60);
46 BOOST_CHECK_EQUAL(producerBase.getSeqNo(userNode).value(), 0);
47
48 producerBase.updateSeqNo(userNode, 1);
Junxiao Shi32ccfc42022-01-09 21:26:22 +000049 BOOST_CHECK(producerBase.getSeqNo(userNode).value() == 1);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050050
Junxiao Shi32ccfc42022-01-09 21:26:22 +000051 auto prefixWithSeq = Name(userNode).appendNumber(1);
Ashlesh Gawande6a5157f2019-12-09 11:49:07 -060052 uint32_t hash = producerBase.m_biMap.right.find(prefixWithSeq)->second;
53 Name prefix(producerBase.m_biMap.left.find(hash)->second);
54 BOOST_CHECK_EQUAL(prefix.getPrefix(-1), userNode);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050055
56 producerBase.removeUserNode(userNode);
Davide Pesaventoc407dee2022-07-21 23:56:05 -040057 BOOST_CHECK(producerBase.getSeqNo(userNode) == std::nullopt);
Ashlesh Gawande6a5157f2019-12-09 11:49:07 -060058 BOOST_CHECK(producerBase.m_biMap.right.find(prefixWithSeq) == producerBase.m_biMap.right.end());
59 BOOST_CHECK(producerBase.m_biMap.left.find(hash) == producerBase.m_biMap.left.end());
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050060
61 Name nonExistentUserNode("/notAUser");
62 producerBase.updateSeqNo(nonExistentUserNode, 1);
Junxiao Shi32ccfc42022-01-09 21:26:22 +000063 BOOST_CHECK(producerBase.m_biMap.right.find(Name(nonExistentUserNode).appendNumber(1)) ==
Ashlesh Gawande6a5157f2019-12-09 11:49:07 -060064 producerBase.m_biMap.right.end());
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050065}
66
67BOOST_AUTO_TEST_CASE(ApplicationNack)
68{
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040069 ProducerBase producerBase(m_face, m_keyChain, 40, Name("/psync"), Name("/testUser"));
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050070
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040071 BOOST_CHECK_EQUAL(m_face.sentData.size(), 0);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050072 producerBase.sendApplicationNack(Name("test"));
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040073 m_face.processEvents(10_ms);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050074
Davide Pesaventoc45a4ea2022-09-19 02:10:53 -040075 BOOST_REQUIRE_EQUAL(m_face.sentData.size(), 1);
76 BOOST_CHECK_EQUAL(m_face.sentData.front().getContentType(), tlv::ContentType_Nack);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050077}
78
79BOOST_AUTO_TEST_SUITE_END()
80
81} // namespace psync