blob: 3f67d50b468a815d01ee57b6781f4ec6d4b5cf99 [file] [log] [blame]
Junxiao Shi02b51252016-09-04 04:30:31 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
Davide Pesaventoe16ac862022-09-25 20:48:22 -04003 * Copyright (c) 2013-2022 Regents of the University of California.
Junxiao Shi02b51252016-09-04 04:30:31 +00004 *
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 Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/encoding/buffer-stream.hpp"
Junxiao Shi02b51252016-09-04 04:30:31 +000023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Junxiao Shi02b51252016-09-04 04:30:31 +000025
26namespace ndn {
27namespace tests {
28
29BOOST_AUTO_TEST_SUITE(Encoding)
30BOOST_AUTO_TEST_SUITE(TestBufferStream)
31
32BOOST_AUTO_TEST_CASE(Empty)
33{
34 OBufferStream os;
35
36 shared_ptr<Buffer> buf = os.buf();
37 BOOST_CHECK_EQUAL(buf->size(), 0);
38}
39
40BOOST_AUTO_TEST_CASE(Put)
41{
42 OBufferStream os;
43 os.put(0x33);
44 os.put(0x44);
45
46 shared_ptr<Buffer> buf = os.buf();
47 BOOST_REQUIRE_EQUAL(buf->size(), 2);
48 BOOST_CHECK_EQUAL(buf->at(0), 0x33);
49 BOOST_CHECK_EQUAL(buf->at(1), 0x44);
50}
51
52BOOST_AUTO_TEST_CASE(Write)
53{
54 OBufferStream os;
55 os.write("\x11\x22", 2);
56
57 shared_ptr<Buffer> buf = os.buf();
58 BOOST_REQUIRE_EQUAL(buf->size(), 2);
59 BOOST_CHECK_EQUAL(buf->at(0), 0x11);
60 BOOST_CHECK_EQUAL(buf->at(1), 0x22);
61}
62
63BOOST_AUTO_TEST_CASE(Destructor) // Bug 3727
64{
65 auto os = make_unique<OBufferStream>();
Davide Pesaventoe16ac862022-09-25 20:48:22 -040066 auto buf = os->buf();
Junxiao Shi02b51252016-09-04 04:30:31 +000067 *os << 'x';
Davide Pesaventoe16ac862022-09-25 20:48:22 -040068 // do NOT flush or call buf() here
Davide Pesaventoeee3e822016-11-26 19:19:34 +010069
Davide Pesaventoe16ac862022-09-25 20:48:22 -040070 os.reset(); // should not cause use-after-free
71 BOOST_CHECK_EQUAL(buf->size(), 1);
72}
73
74BOOST_AUTO_TEST_CASE(Close) // Bug 5240
75{
76 OBufferStream os;
77 os << "foo";
78 os.close();
79
80 auto buf = os.buf(); // should not cause assertion failure
81 BOOST_CHECK_EQUAL(buf->size(), 3);
Junxiao Shi02b51252016-09-04 04:30:31 +000082}
83
84BOOST_AUTO_TEST_SUITE_END() // TestBufferStream
85BOOST_AUTO_TEST_SUITE_END() // Encoding
86
87} // namespace tests
88} // namespace ndn