blob: 0e31a6781755bb19e3f5ba4b963d67584aba0570 [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -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 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/detail/state.hpp"
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050021
Davide Pesavento5b3cf762020-04-03 16:20:04 -040022#include "tests/boost-test.hpp"
23
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050024#include <ndn-cxx/data.hpp>
25
26namespace psync {
Davide Pesaventodb789562020-12-19 23:01:08 -050027namespace detail {
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050028
29BOOST_AUTO_TEST_SUITE(TestState)
30
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050031BOOST_AUTO_TEST_CASE(EncodeDecode)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050032{
33 State state;
34 state.addContent(ndn::Name("test1"));
35 state.addContent(ndn::Name("test2"));
36
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050037 // Simulate getting buffer content from segment fetcher
Davide Pesaventodb789562020-12-19 23:01:08 -050038 ndn::Data data;
Ashlesh Gawande40970d62018-11-01 11:24:17 -050039 data.setContent(state.wireEncode());
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050040 ndn::Buffer buffer(data.getContent().value_size());
41 std::copy(data.getContent().value_begin(),
42 data.getContent().value_end(),
43 buffer.begin());
44
Davide Pesaventodb789562020-12-19 23:01:08 -050045 ndn::Block block(std::make_shared<ndn::Buffer>(buffer));
Ashlesh Gawande40970d62018-11-01 11:24:17 -050046 State rcvdState;
47 rcvdState.wireDecode(block);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050048
Ashlesh Gawande40970d62018-11-01 11:24:17 -050049 BOOST_CHECK(state.getContent() == rcvdState.getContent());
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050050}
51
52BOOST_AUTO_TEST_CASE(EmptyContent)
53{
Ashlesh Gawande40970d62018-11-01 11:24:17 -050054 State state;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050055
Ashlesh Gawande40970d62018-11-01 11:24:17 -050056 // Simulate getting buffer content from segment fetcher
Davide Pesaventodb789562020-12-19 23:01:08 -050057 ndn::Data data;
Ashlesh Gawande40970d62018-11-01 11:24:17 -050058 data.setContent(state.wireEncode());
59 ndn::Buffer buffer(data.getContent().value_size());
60 std::copy(data.getContent().value_begin(),
61 data.getContent().value_end(),
62 buffer.begin());
Ashlesh Gawande40970d62018-11-01 11:24:17 -050063
Davide Pesaventodb789562020-12-19 23:01:08 -050064 ndn::Block block(std::make_shared<ndn::Buffer>(buffer));
Ashlesh Gawande40970d62018-11-01 11:24:17 -050065 BOOST_CHECK_NO_THROW(State state2(block));
66
67 State state2(block);
68 BOOST_CHECK_EQUAL(state2.getContent().size(), 0);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050069}
70
Ashlesh Gawandee23b53b2020-02-16 13:47:38 -080071BOOST_AUTO_TEST_CASE(ReEncode)
72{
73 State state;
74 state.addContent(ndn::Name("test1"));
75 state.addContent(ndn::Name("test2"));
76
77 state.wireEncode();
78
79 state.addContent(ndn::Name("test3"));
80
81 State state2(state.wireEncode());
82 BOOST_CHECK_EQUAL(state2.getContent().size(), 3);
83}
84
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050085BOOST_AUTO_TEST_SUITE_END()
86
Davide Pesaventodb789562020-12-19 23:01:08 -050087} // namespace detail
Davide Pesavento5b3cf762020-04-03 16:20:04 -040088} // namespace psync