2005-09-14 21:33:16 +02:00
|
|
|
/*
|
|
|
|
Copyright (c) 2003 - 2005, Arvid Norberg, Daniel Wallin
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
* Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in
|
|
|
|
the documentation and/or other materials provided with the distribution.
|
|
|
|
* Neither the name of Rasterbar Software nor the names of its
|
|
|
|
contributors may be used to endorse or promote products derived
|
|
|
|
from this software without specific prior written permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
2007-09-29 18:14:03 +02:00
|
|
|
#include <set>
|
2005-09-14 21:33:16 +02:00
|
|
|
|
|
|
|
#include "libtorrent/buffer.hpp"
|
2007-09-29 18:14:03 +02:00
|
|
|
#include "libtorrent/chained_buffer.hpp"
|
2008-05-27 18:55:14 +02:00
|
|
|
#include "libtorrent/socket.hpp"
|
2005-09-14 21:33:16 +02:00
|
|
|
|
|
|
|
#include "test.hpp"
|
|
|
|
|
2017-04-12 20:05:53 +02:00
|
|
|
using namespace lt;
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
// -- test buffer --
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
static char const data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TORRENT_TEST(buffer_constructor)
|
|
|
|
{
|
2005-09-14 21:33:16 +02:00
|
|
|
|
|
|
|
{
|
2016-06-27 02:41:03 +02:00
|
|
|
buffer b;
|
|
|
|
TEST_CHECK(b.size() == 0);
|
|
|
|
TEST_CHECK(b.empty());
|
2005-09-14 21:33:16 +02:00
|
|
|
}
|
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
{
|
|
|
|
buffer b(10);
|
|
|
|
TEST_CHECK(b.size() >= 10);
|
|
|
|
}
|
2005-09-14 21:33:16 +02:00
|
|
|
|
|
|
|
{
|
2016-06-27 02:41:03 +02:00
|
|
|
buffer b(50, data);
|
2016-07-24 09:52:20 +02:00
|
|
|
TEST_CHECK(std::memcmp(b.data(), data, 10) == 0);
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b.size() >= 50);
|
|
|
|
}
|
|
|
|
}
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TORRENT_TEST(buffer_swap)
|
|
|
|
{
|
|
|
|
buffer b1;
|
|
|
|
TEST_CHECK(b1.size() == 0);
|
|
|
|
buffer b2(10, data);
|
2018-11-01 23:05:30 +01:00
|
|
|
auto const b2_size = b2.size();
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b2_size >= 10);
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
b1.swap(b2);
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b2.size() == 0);
|
|
|
|
TEST_CHECK(b1.size() == b2_size);
|
2016-07-24 09:52:20 +02:00
|
|
|
TEST_CHECK(std::memcmp(b1.data(), data, 10) == 0);
|
2016-06-27 02:41:03 +02:00
|
|
|
}
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TORRENT_TEST(buffer_subscript)
|
|
|
|
{
|
|
|
|
buffer b(50, data);
|
2016-07-24 09:52:20 +02:00
|
|
|
TEST_CHECK(std::memcmp(b.data(), data, 10) == 0);
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b.size() >= 50);
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
for (int i = 0; i < int(sizeof(data)/sizeof(data[0])); ++i)
|
2018-11-01 23:05:30 +01:00
|
|
|
TEST_CHECK(b[i] == data[i]);
|
2005-09-14 21:33:16 +02:00
|
|
|
}
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TORRENT_TEST(buffer_subscript2)
|
2005-09-14 21:33:16 +02:00
|
|
|
{
|
2016-06-27 02:41:03 +02:00
|
|
|
buffer b(1);
|
|
|
|
TEST_CHECK(b.size() >= 1);
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
for (int i = 0; i < int(b.size()); ++i)
|
2018-11-01 23:05:30 +01:00
|
|
|
b[i] = char(i & 0xff);
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
for (int i = 0; i < int(b.size()); ++i)
|
2018-11-01 23:05:30 +01:00
|
|
|
TEST_CHECK(b[i] == (i & 0xff));
|
2016-06-27 02:41:03 +02:00
|
|
|
}
|
2015-05-30 06:31:23 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TORRENT_TEST(buffer_move_construct)
|
|
|
|
{
|
|
|
|
buffer b1(50, data);
|
2016-07-24 09:52:20 +02:00
|
|
|
TEST_CHECK(std::memcmp(b1.data(), data, 10) == 0);
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b1.size() >= 50);
|
2015-05-30 06:31:23 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
buffer b2(std::move(b1));
|
2015-05-30 06:31:23 +02:00
|
|
|
|
2018-01-29 12:40:44 +01:00
|
|
|
TEST_CHECK(b1.empty());
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-07-24 09:52:20 +02:00
|
|
|
TEST_CHECK(std::memcmp(b2.data(), data, 10) == 0);
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b2.size() >= 50);
|
|
|
|
}
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TORRENT_TEST(buffer_move_assign)
|
|
|
|
{
|
|
|
|
buffer b1(50, data);
|
2016-07-24 09:52:20 +02:00
|
|
|
TEST_CHECK(std::memcmp(b1.data(), data, 10) == 0);
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b1.size() >= 50);
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
buffer b2;
|
|
|
|
TEST_CHECK(b2.size() == 0);
|
2015-05-30 06:31:23 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
b2 = std::move(b1);
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b1.size() == 0);
|
2005-09-14 21:33:16 +02:00
|
|
|
|
2016-07-24 09:52:20 +02:00
|
|
|
TEST_CHECK(std::memcmp(b2.data(), data, 10) == 0);
|
2016-06-27 02:41:03 +02:00
|
|
|
TEST_CHECK(b2.size() >= 50);
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
|
|
|
|
2018-01-28 00:56:21 +01:00
|
|
|
namespace {
|
2007-09-29 18:14:03 +02:00
|
|
|
// -- test chained buffer --
|
|
|
|
|
|
|
|
std::set<char*> buffer_list;
|
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
void free_buffer(char* m)
|
2007-09-29 18:14:03 +02:00
|
|
|
{
|
2018-01-28 00:56:21 +01:00
|
|
|
auto const i = buffer_list.find(m);
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(i != buffer_list.end());
|
|
|
|
|
|
|
|
buffer_list.erase(i);
|
|
|
|
std::free(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* allocate_buffer(int size)
|
|
|
|
{
|
2018-01-29 12:40:44 +01:00
|
|
|
char* mem = static_cast<char*>(std::malloc(std::size_t(size)));
|
2007-09-29 18:14:03 +02:00
|
|
|
buffer_list.insert(mem);
|
|
|
|
return mem;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
int copy_buffers(T const& b, char* target)
|
|
|
|
{
|
|
|
|
int copied = 0;
|
|
|
|
for (typename T::const_iterator i = b.begin()
|
|
|
|
, end(b.end()); i != end; ++i)
|
|
|
|
{
|
2015-06-06 08:10:53 +02:00
|
|
|
memcpy(target, boost::asio::buffer_cast<char const*>(*i), boost::asio::buffer_size(*i));
|
|
|
|
target += boost::asio::buffer_size(*i);
|
2016-04-25 23:22:09 +02:00
|
|
|
copied += int(boost::asio::buffer_size(*i));
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
|
|
|
return copied;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool compare_chained_buffer(chained_buffer& b, char const* mem, int size)
|
|
|
|
{
|
2007-10-07 18:39:45 +02:00
|
|
|
if (size == 0) return true;
|
2018-01-29 12:40:44 +01:00
|
|
|
std::vector<char> flat((std::size_t(size)));
|
2015-06-06 08:10:53 +02:00
|
|
|
std::vector<boost::asio::const_buffer> const& iovec2 = b.build_iovec(size);
|
2007-09-29 18:14:03 +02:00
|
|
|
int copied = copy_buffers(iovec2, &flat[0]);
|
|
|
|
TEST_CHECK(copied == size);
|
2018-01-29 12:40:44 +01:00
|
|
|
return std::memcmp(&flat[0], mem, std::size_t(size)) == 0;
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:47:18 +01:00
|
|
|
struct holder
|
|
|
|
{
|
2017-08-28 11:37:53 +02:00
|
|
|
holder(char* buf, std::size_t size) : m_buf(buf), m_size(size) {}
|
2016-12-29 02:47:18 +01:00
|
|
|
~holder() { if (m_buf) free_buffer(m_buf); }
|
|
|
|
holder(holder const&) = delete;
|
|
|
|
holder& operator=(holder const&) = delete;
|
2017-08-28 11:37:53 +02:00
|
|
|
holder(holder&& rhs) noexcept : m_buf(rhs.m_buf), m_size(rhs.m_size) { rhs.m_buf = nullptr; }
|
2018-02-02 08:26:21 +01:00
|
|
|
holder& operator=(holder&& rhs) = delete;
|
2017-08-28 11:37:53 +02:00
|
|
|
char* data() const { return m_buf; }
|
|
|
|
std::size_t size() const { return m_size; }
|
2016-12-29 02:47:18 +01:00
|
|
|
private:
|
|
|
|
char* m_buf;
|
2017-08-28 11:37:53 +02:00
|
|
|
std::size_t m_size;
|
2016-12-29 02:47:18 +01:00
|
|
|
};
|
|
|
|
|
2018-01-28 00:56:21 +01:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2015-05-30 06:31:23 +02:00
|
|
|
TORRENT_TEST(chained_buffer)
|
2007-09-29 18:14:03 +02:00
|
|
|
{
|
2018-01-30 14:56:07 +01:00
|
|
|
char data_test[] = "foobar";
|
2007-09-29 18:14:03 +02:00
|
|
|
{
|
|
|
|
chained_buffer b;
|
2015-05-30 06:31:23 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(b.empty());
|
2014-05-03 18:51:07 +02:00
|
|
|
TEST_EQUAL(b.capacity(), 0);
|
|
|
|
TEST_EQUAL(b.size(), 0);
|
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 0);
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(buffer_list.empty());
|
|
|
|
|
2014-05-03 18:51:07 +02:00
|
|
|
// there are no buffers, we should not be able to allocate
|
|
|
|
// an appendix in an existing buffer
|
2018-03-22 17:01:38 +01:00
|
|
|
TEST_EQUAL(b.allocate_appendix(1), static_cast<char*>(nullptr));
|
2014-05-03 18:51:07 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
char* b1 = allocate_buffer(512);
|
2018-01-30 14:56:07 +01:00
|
|
|
std::memcpy(b1, data_test, 6);
|
2017-08-28 11:37:53 +02:00
|
|
|
b.append_buffer(holder(b1, 512), 6);
|
2014-05-03 18:51:07 +02:00
|
|
|
TEST_EQUAL(buffer_list.size(), 1);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.capacity(), 512);
|
|
|
|
TEST_EQUAL(b.size(), 6);
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(!b.empty());
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 512 - 6);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
b.pop_front(3);
|
|
|
|
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.capacity(), 512 - 3);
|
|
|
|
TEST_EQUAL(b.size(), 3);
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(!b.empty());
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 512 - 6);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2018-01-30 14:56:07 +01:00
|
|
|
bool ret = b.append({data_test, 6}) != nullptr;
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
TEST_CHECK(ret == true);
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.capacity(), 512 - 3);
|
|
|
|
TEST_EQUAL(b.size(), 9);
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(!b.empty());
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 512 - 12);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2014-01-18 07:54:13 +01:00
|
|
|
char data2[1024];
|
2017-04-23 06:37:29 +02:00
|
|
|
ret = b.append(data2) != nullptr;
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
TEST_CHECK(ret == false);
|
|
|
|
|
|
|
|
char* b2 = allocate_buffer(512);
|
2018-01-30 14:56:07 +01:00
|
|
|
std::memcpy(b2, data_test, 6);
|
2017-08-28 11:37:53 +02:00
|
|
|
b.append_buffer(holder(b2, 512), 6);
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(buffer_list.size(), 2);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
char* b3 = allocate_buffer(512);
|
2018-01-30 14:56:07 +01:00
|
|
|
std::memcpy(b3, data_test, 6);
|
2017-08-28 11:37:53 +02:00
|
|
|
b.append_buffer(holder(b3, 512), 6);
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(buffer_list.size(), 3);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.capacity(), 512 * 3 - 3);
|
|
|
|
TEST_EQUAL(b.size(), 21);
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(!b.empty());
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 512 - 6);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
TEST_CHECK(compare_chained_buffer(b, "barfoobar", 9));
|
|
|
|
|
|
|
|
for (int i = 1; i < 21; ++i)
|
|
|
|
TEST_CHECK(compare_chained_buffer(b, "barfoobarfoobarfoobar", i));
|
|
|
|
|
|
|
|
b.pop_front(5 + 6);
|
|
|
|
|
2017-08-28 11:37:53 +02:00
|
|
|
TEST_EQUAL(buffer_list.size(), 2);
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.capacity(), 512 * 2 - 2);
|
|
|
|
TEST_EQUAL(b.size(), 10);
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(!b.empty());
|
2016-12-29 02:57:46 +01:00
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 512 - 6);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
char const* str = "obarfooba";
|
|
|
|
TEST_CHECK(compare_chained_buffer(b, str, 9));
|
|
|
|
|
|
|
|
for (int i = 0; i < 9; ++i)
|
|
|
|
{
|
|
|
|
b.pop_front(1);
|
|
|
|
++str;
|
|
|
|
TEST_CHECK(compare_chained_buffer(b, str, 8 - i));
|
2017-08-28 11:37:53 +02:00
|
|
|
TEST_EQUAL(b.size(), 9 - i);
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
char* b4 = allocate_buffer(20);
|
2018-01-30 14:56:07 +01:00
|
|
|
std::memcpy(b4, data_test, 6);
|
|
|
|
std::memcpy(b4 + 6, data_test, 6);
|
2017-08-28 11:37:53 +02:00
|
|
|
b.append_buffer(holder(b4, 20), 12);
|
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 8);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
2018-01-30 14:56:07 +01:00
|
|
|
ret = b.append({data_test, 6}) != nullptr;
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(ret == true);
|
2017-08-28 11:37:53 +02:00
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 2);
|
2007-09-29 18:14:03 +02:00
|
|
|
std::cout << b.space_in_last_buffer() << std::endl;
|
2018-01-30 14:56:07 +01:00
|
|
|
ret = b.append({data_test, 2}) != nullptr;
|
2007-09-29 18:14:03 +02:00
|
|
|
TEST_CHECK(ret == true);
|
2017-08-28 11:37:53 +02:00
|
|
|
TEST_EQUAL(b.space_in_last_buffer(), 0);
|
2007-09-29 18:14:03 +02:00
|
|
|
std::cout << b.space_in_last_buffer() << std::endl;
|
2015-05-30 06:31:23 +02:00
|
|
|
|
2007-09-29 18:14:03 +02:00
|
|
|
char* b5 = allocate_buffer(20);
|
2018-01-30 14:56:07 +01:00
|
|
|
std::memcpy(b5, data_test, 6);
|
2017-08-28 11:37:53 +02:00
|
|
|
b.append_buffer(holder(b5, 20), 6);
|
2007-09-29 18:14:03 +02:00
|
|
|
|
|
|
|
b.pop_front(22);
|
2017-08-28 11:37:53 +02:00
|
|
|
TEST_EQUAL(b.size(), 5);
|
2007-09-29 18:14:03 +02:00
|
|
|
}
|
|
|
|
TEST_CHECK(buffer_list.empty());
|
|
|
|
}
|