blob: 8f76b65848269c80e8c7834902d730f707bfc698 [file] [log] [blame]
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Ashlesh Gawande0cf4b602019-01-18 15:58:17 -06003 * Copyright (c) 2014-2019, 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
22#include <boost/test/unit_test.hpp>
23#include <ndn-cxx/name.hpp>
24#include <ndn-cxx/data.hpp>
25
26namespace psync {
27
28using namespace ndn;
29
30BOOST_AUTO_TEST_SUITE(TestState)
31
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050032BOOST_AUTO_TEST_CASE(EncodeDecode)
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050033{
34 State state;
35 state.addContent(ndn::Name("test1"));
36 state.addContent(ndn::Name("test2"));
37
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050038 // Simulate getting buffer content from segment fetcher
Ashlesh Gawande40970d62018-11-01 11:24:17 -050039 Data data;
40 data.setContent(state.wireEncode());
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050041 ndn::Buffer buffer(data.getContent().value_size());
42 std::copy(data.getContent().value_begin(),
43 data.getContent().value_end(),
44 buffer.begin());
45
Ashlesh Gawande40970d62018-11-01 11:24:17 -050046 ndn::ConstBufferPtr bufferPtr = make_shared<ndn::Buffer>(buffer);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050047
Ashlesh Gawande40970d62018-11-01 11:24:17 -050048 ndn::Block block(std::move(bufferPtr));
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050049
Ashlesh Gawande40970d62018-11-01 11:24:17 -050050 State rcvdState;
51 rcvdState.wireDecode(block);
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050052
Ashlesh Gawande40970d62018-11-01 11:24:17 -050053 BOOST_CHECK(state.getContent() == rcvdState.getContent());
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050054}
55
56BOOST_AUTO_TEST_CASE(EmptyContent)
57{
Ashlesh Gawande40970d62018-11-01 11:24:17 -050058 State state;
Ashlesh Gawandeec43b362018-08-01 15:15:01 -050059
Ashlesh Gawande40970d62018-11-01 11:24:17 -050060 // Simulate getting buffer content from segment fetcher
61 Data data;
62 data.setContent(state.wireEncode());
63 ndn::Buffer buffer(data.getContent().value_size());
64 std::copy(data.getContent().value_begin(),
65 data.getContent().value_end(),
66 buffer.begin());
67 ndn::ConstBufferPtr bufferPtr = make_shared<ndn::Buffer>(buffer);
68
69 ndn::Block block(std::move(bufferPtr));
70
71 BOOST_CHECK_NO_THROW(State state2(block));
72
73 State state2(block);
74 BOOST_CHECK_EQUAL(state2.getContent().size(), 0);
Ashlesh Gawande0b2897e2018-06-20 14:40:47 -050075}
76
77BOOST_AUTO_TEST_SUITE_END()
78
79} // namespace psync