Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Ashlesh Gawande | 5a89547 | 2020-01-25 18:07:32 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, The University of Memphis |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 4 | * |
| 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 Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 9 | * of the GNU Lesser General Public License as published by the Free Software Foundation, |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 10 | * 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 Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 14 | * PURPOSE. See the GNU Lesser General Public License for more details. |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 15 | * |
Ashlesh Gawande | 0cf4b60 | 2019-01-18 15:58:17 -0600 | [diff] [blame] | 16 | * You should have received a copy of the GNU Lesser General Public License along with |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 17 | * PSync, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | **/ |
| 19 | |
Ashlesh Gawande | 78b94ad | 2018-12-13 15:29:19 -0600 | [diff] [blame] | 20 | #include "PSync/detail/state.hpp" |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 21 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 22 | #include "tests/boost-test.hpp" |
| 23 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 24 | #include <ndn-cxx/data.hpp> |
| 25 | |
| 26 | namespace psync { |
| 27 | |
| 28 | using namespace ndn; |
| 29 | |
| 30 | BOOST_AUTO_TEST_SUITE(TestState) |
| 31 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 32 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 33 | { |
| 34 | State state; |
| 35 | state.addContent(ndn::Name("test1")); |
| 36 | state.addContent(ndn::Name("test2")); |
| 37 | |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 38 | // Simulate getting buffer content from segment fetcher |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 39 | Data data; |
| 40 | data.setContent(state.wireEncode()); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 41 | ndn::Buffer buffer(data.getContent().value_size()); |
| 42 | std::copy(data.getContent().value_begin(), |
| 43 | data.getContent().value_end(), |
| 44 | buffer.begin()); |
| 45 | |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 46 | ndn::ConstBufferPtr bufferPtr = make_shared<ndn::Buffer>(buffer); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 47 | |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 48 | ndn::Block block(std::move(bufferPtr)); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 49 | |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 50 | State rcvdState; |
| 51 | rcvdState.wireDecode(block); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 52 | |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 53 | BOOST_CHECK(state.getContent() == rcvdState.getContent()); |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(EmptyContent) |
| 57 | { |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 58 | State state; |
Ashlesh Gawande | ec43b36 | 2018-08-01 15:15:01 -0500 | [diff] [blame] | 59 | |
Ashlesh Gawande | 40970d6 | 2018-11-01 11:24:17 -0500 | [diff] [blame] | 60 | // 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 Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 75 | } |
| 76 | |
Ashlesh Gawande | e23b53b | 2020-02-16 13:47:38 -0800 | [diff] [blame] | 77 | BOOST_AUTO_TEST_CASE(ReEncode) |
| 78 | { |
| 79 | State state; |
| 80 | state.addContent(ndn::Name("test1")); |
| 81 | state.addContent(ndn::Name("test2")); |
| 82 | |
| 83 | state.wireEncode(); |
| 84 | |
| 85 | state.addContent(ndn::Name("test3")); |
| 86 | |
| 87 | State state2(state.wireEncode()); |
| 88 | BOOST_CHECK_EQUAL(state2.getContent().size(), 3); |
| 89 | } |
| 90 | |
Ashlesh Gawande | 0b2897e | 2018-06-20 14:40:47 -0500 | [diff] [blame] | 91 | BOOST_AUTO_TEST_SUITE_END() |
| 92 | |
Davide Pesavento | 5b3cf76 | 2020-04-03 16:20:04 -0400 | [diff] [blame] | 93 | } // namespace psync |