Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3de7c52 | 2024-01-25 18:50:12 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/encoding/buffer-stream.hpp" |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 25 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 26 | namespace ndn::tests { |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 27 | |
| 28 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 29 | BOOST_AUTO_TEST_SUITE(TestBufferStream) |
| 30 | |
| 31 | BOOST_AUTO_TEST_CASE(Empty) |
| 32 | { |
| 33 | OBufferStream os; |
| 34 | |
Davide Pesavento | 3de7c52 | 2024-01-25 18:50:12 -0500 | [diff] [blame^] | 35 | std::shared_ptr<Buffer> buf = os.buf(); |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 36 | BOOST_CHECK_EQUAL(buf->size(), 0); |
| 37 | } |
| 38 | |
| 39 | BOOST_AUTO_TEST_CASE(Put) |
| 40 | { |
| 41 | OBufferStream os; |
| 42 | os.put(0x33); |
| 43 | os.put(0x44); |
| 44 | |
Davide Pesavento | 3de7c52 | 2024-01-25 18:50:12 -0500 | [diff] [blame^] | 45 | std::shared_ptr<Buffer> buf = os.buf(); |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 46 | BOOST_REQUIRE_EQUAL(buf->size(), 2); |
| 47 | BOOST_CHECK_EQUAL(buf->at(0), 0x33); |
| 48 | BOOST_CHECK_EQUAL(buf->at(1), 0x44); |
| 49 | } |
| 50 | |
| 51 | BOOST_AUTO_TEST_CASE(Write) |
| 52 | { |
| 53 | OBufferStream os; |
| 54 | os.write("\x11\x22", 2); |
| 55 | |
Davide Pesavento | 3de7c52 | 2024-01-25 18:50:12 -0500 | [diff] [blame^] | 56 | std::shared_ptr<Buffer> buf = os.buf(); |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 57 | BOOST_REQUIRE_EQUAL(buf->size(), 2); |
| 58 | BOOST_CHECK_EQUAL(buf->at(0), 0x11); |
| 59 | BOOST_CHECK_EQUAL(buf->at(1), 0x22); |
| 60 | } |
| 61 | |
| 62 | BOOST_AUTO_TEST_CASE(Destructor) // Bug 3727 |
| 63 | { |
Davide Pesavento | 3de7c52 | 2024-01-25 18:50:12 -0500 | [diff] [blame^] | 64 | auto os = std::make_unique<OBufferStream>(); |
Davide Pesavento | e16ac86 | 2022-09-25 20:48:22 -0400 | [diff] [blame] | 65 | auto buf = os->buf(); |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 66 | *os << 'x'; |
Davide Pesavento | e16ac86 | 2022-09-25 20:48:22 -0400 | [diff] [blame] | 67 | // do NOT flush or call buf() here |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 68 | |
Davide Pesavento | e16ac86 | 2022-09-25 20:48:22 -0400 | [diff] [blame] | 69 | os.reset(); // should not cause use-after-free |
| 70 | BOOST_CHECK_EQUAL(buf->size(), 1); |
| 71 | } |
| 72 | |
| 73 | BOOST_AUTO_TEST_CASE(Close) // Bug 5240 |
| 74 | { |
| 75 | OBufferStream os; |
| 76 | os << "foo"; |
| 77 | os.close(); |
| 78 | |
| 79 | auto buf = os.buf(); // should not cause assertion failure |
| 80 | BOOST_CHECK_EQUAL(buf->size(), 3); |
Junxiao Shi | 02b5125 | 2016-09-04 04:30:31 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | BOOST_AUTO_TEST_SUITE_END() // TestBufferStream |
| 84 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
| 85 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 86 | } // namespace ndn::tests |